class ConnectionDialog(simpledialog.Dialog): def body(self, master): self.parent = master self.title('Device Connection') self.frame = tk.Frame(self.parent) self.resizable(width=False, height=False) self.init_classes() self.init_variables() self.init_ui() self.frame.pack(expand=1, fill=BOTH) def init_classes(self): self._settings = Settings() def init_variables(self): self.port_label = StringVar() self.port = StringVar() self.baud = StringVar() self.baud_rates = [ 1200, 2400, 4800, 9600, 14400, 19200, 38400, 57600, 115200, 128000, 256000 ] self.ports = list(serial.tools.list_ports.comports()) self.port_label.set(self._settings.GetSetting('port_label')) self.port.set(self._settings.GetSetting('port')) self.baud.set(self._settings.GetSetting('baud')) self.OK = False, self.port.get() pass def init_ui(self): self.dir_name = os.path.dirname(os.path.realpath(__file__)) self.img_path = os.path.join(self.dir_name, "images", "serial48.png") logo = PhotoImage(file=self.img_path) self.label_logo = Label(self.frame, image=logo) self.label_logo.image = logo self.label_logo.grid(row=0, column=0, rowspan=2, sticky='W', padx=1, pady=4) self.lbl_WFM = tk.Label(self.frame, text="Device port settings", font='Tahoma 8 bold', fg='gray') self.lbl_WFM.grid(row=0, column=1, sticky='W', padx=1, pady=4) self.lbl_Note = tk.Label( self.frame, wraplength=200, text= "Please select port where your GPS device was connected and baud rate", fg='darkgray') self.lbl_Note.grid(row=1, column=1, sticky='W', padx=1, pady=1) self.lbl_ports = tk.Label(self.frame, text="Serial Port:") self.lbl_ports.grid(row=2, column=0, sticky='W', padx=10, pady=2) self.cbox_ports = ttk.Combobox(self.frame, textvariable=self.port_label, state="readonly", width=50) self.cbox_ports.bind('<Return>') self.cbox_ports['values'] = self.ports self.cbox_ports.grid(row=2, column=1, sticky="W", pady=3) self.lbl_baud = tk.Label(self.frame, text="Baudrate:") self.lbl_baud.grid(row=3, column=0, sticky='W', padx=10, pady=2) self.cbox_baud = ttk.Combobox(self.frame, textvariable=self.baud, state="readonly", width=30) self.cbox_baud.bind('<Return>') self.cbox_baud['values'] = self.baud_rates #self.cbox_baud.current(2) # select index 2 (48000) by default self.cbox_baud.grid(row=3, column=1, sticky="W", pady=3) pass def ok(self): sel_port = self.port_label.get().split(' ')[0].strip() print(sel_port) self._settings.SetSettings('port', sel_port) self._settings.SetSettings('port_label', self.port_label.get()) self._settings.SetSettings('baud', self.baud.get()) self.OK = True, self.port.get() self.destroy()
class Application(Frame): def __init__(self, parent): Frame.__init__(self, parent) self.parent = parent self.parent.protocol("WM_DELETE_WINDOW", self.onExit) self.parent.bind("<Destroy>", lambda e: self.onExit) self.init_settings() self.init_classes() self.init_variables() self.init_images() self.init_menubar() self.init_toolbar() self.init_tabs() self.init_statusbar() self.init_connections() self.pack() def init_settings(self): pass def init_classes(self): self.config_menus = configparser.ConfigParser() self.config_tools = configparser.ConfigParser() self.config_tabs = configparser.ConfigParser() self.config_map = configparser.ConfigParser() self._serial = serial.Serial() self._parser = parsenmea.ParseNmea() self._storage = StorageDB('gps.sqlite') self._settings = Settings() self._gpgga = nmea.GPGGA() self._gprmc = nmea.GPRMC() pass def init_variables(self): self.images_ui = {} self.toolbars = {} self.menus = {} self.lbl_status = StringVar() self.connection_status = StringVar() self.port_label = StringVar() self.port = StringVar() self.baud = StringVar() self.line_raw = IntVar() self.progress = IntVar() self.progress_maximum = IntVar() self.lastLat = 0.0 self.lastLon = 0.0 self.is_zoomIn = True # get current project root path self.dir_name = os.path.dirname(os.path.realpath(__file__)) self.menu_file = os.path.join(self.dir_name, "configs/menus.ini") self.tool_file = os.path.join(self.dir_name, "configs/toolbars.ini") self.tabs_file = os.path.join(self.dir_name, "configs/tabs.ini") self.config_file = os.path.join(self.dir_name, "configs/global.ini") self.img_path = os.path.join(self.dir_name, "images") self.output_path = os.path.join(self.dir_name, "outputs") pass def init_images(self): for file in os.listdir(self.img_path): if file.endswith(".png"): self.images_ui[file.replace(".png", "")] = PhotoImage( file=str(os.path.join(self.img_path, file))) pass def init_menubar(self): # Load the menus configuration file self.config_menus.read(self.menu_file) # Initialize menus from configuration file self.menubar = Menu(self.parent) for section in self.config_menus.sections(): topMenu = Menu(self.menubar, tearoff=0) for option in self.config_menus.options(section): if option.strip() == "-": topMenu.add_separator() else: topMenu.add_command( label=self.config_menus.get(section, option), compound=LEFT, image=self.images_ui[option], command=lambda tag=option: self.doClickEvent(0, tag)) self.menus[option] = topMenu self.menubar.add_cascade(label=section, menu=topMenu) self.parent.config(menu=self.menubar) #for entry in self.menus: #self.menus[entry].config(fg= 'green', image= self.images_ui['connect']) pass def init_toolbar(self): # Load the toolbars configuration file self.config_tools.read(self.tool_file) # Initialize toolbars from configuration file self.toolbar = Frame(self.parent, bd=1, relief=RAISED) for section in self.config_tools.sections(): for option in self.config_tools.options(section): self.btn_tool = Button( self.toolbar, image=self.images_ui[option], text=option, relief=FLAT, justify=CENTER, command=lambda tag=option: self.doClickEvent(0, tag)) self.btn_tool.image = self.images_ui[option] self.btn_tool.pack(side=LEFT, padx=2, pady=2) self.toolbars[option] = self.btn_tool self.toolbar.pack(side=TOP, fill=X) #for entry in self.toolbars: # self.toolbars[entry]['state'] = DISABLED pass def init_tabs(self): # Load the tabs configuration file self.config_tabs.read(self.tabs_file) # Initialize tabs from configuration file self.tabs = ttk.Notebook(self.parent) for section in self.config_tabs.sections(): for tab in self.config_tabs.options(section): tabPage = ttk.Frame( self.tabs ) # first page, which would get widgets gridded into it self.tabs.add(tabPage, text=self.config_tabs.get(section, tab), image=self.images_ui[tab], compound=LEFT) self.create_tabPages(tab, tabPage) pass self.tabs.pack(expand=1, fill="both") # Pack to make visible def init_statusbar(self): self.status_frame = Frame(self.parent, bd=1, relief=GROOVE) self.status_frame.pack(fill=X) label_Status = Label(self.status_frame, textvariable=self.lbl_status, anchor=W) label_Status.pack(side=LEFT) label_Dummy = Label(self.status_frame, text='| GPS Device:', anchor=W) label_Dummy.pack(side=LEFT) lbl_Connection = Label(self.status_frame, textvariable=self.connection_status, anchor=E, fg='blue') lbl_Connection.pack(side=LEFT) self.connection_status.set("Disconnected") label_lineDummy = Label(self.status_frame, text='| Line:', anchor=W) label_lineDummy.pack(side=LEFT) lbl_lineRaw = Label(self.status_frame, textvariable=self.line_raw, anchor=E, fg='green') lbl_lineRaw.pack(side=LEFT) self.line_raw.set(0) def create_tabPages(self, tag, tabPage): if tag == 'parse': self.tree_parseDate = ttk.Treeview(tabPage, selectmode='browse') verticalScroll = ttk.Scrollbar(tabPage, orient='vertical', command=self.tree_parseDate.yview) verticalScroll.pack(side='right', fill='y') horScroll = ttk.Scrollbar(tabPage, orient='horizontal', command=self.tree_parseDate.xview) horScroll.pack(side='bottom', fill='x') self.tree_parseDate.configure(yscrollcommand=verticalScroll.set) self.tree_parseDate.configure(xscrollcommand=horScroll.set) self.tree_parseDate['columns'] = ('latitude', 'longhitude', 'altitude', 'distance', 'satellites', 'quality', 'speed', 'course', 'date') #self.tree_parseDate['show'] = 'headings' self.tree_parseDate.heading("#0", text='Timestamp', anchor='w') self.tree_parseDate.column("#0", anchor="w", width=40) self.tree_parseDate.heading('latitude', text='Latitude') self.tree_parseDate.column('latitude', stretch='yes', anchor='w', width=14) self.tree_parseDate.heading('longhitude', text='Longhitude') self.tree_parseDate.column('longhitude', anchor='w', width=20) self.tree_parseDate.heading('altitude', text='Altitude') self.tree_parseDate.column('altitude', anchor='w', width=20) self.tree_parseDate.heading('distance', text='Distance') self.tree_parseDate.column('distance', anchor='w', width=20) self.tree_parseDate.heading('satellites', text='Satellites') self.tree_parseDate.column('satellites', anchor='w', width=10) self.tree_parseDate.heading('quality', text='Quality') self.tree_parseDate.column('quality', anchor='w', width=20) self.tree_parseDate.heading('speed', text='Speed') self.tree_parseDate.column('speed', anchor='w', width=20) self.tree_parseDate.heading('course', text='Course') self.tree_parseDate.column('course', anchor='w', width=20) self.tree_parseDate.heading('date', text='Date') self.tree_parseDate.column('date', anchor='w', width=20) #self.init_workspaces() self.tree_parseDate.pack(expand=1, fill='both') elif tag == 'raw': self.txt_rawData = Text(tabPage) self.txt_rawData.pack(expand=1, fill='both') self.txt_rawData.insert(END, 'GPS Logger started...') pass elif tag == 'summary': pass else: self.canvas = Canvas(tabPage) self.canvas.pack(expand=1, fill='both') self.label = Label(self.canvas) self.label.bind('<B1-Motion>', self.goompy_drag) self.label.bind('<Button-1>', self.goompy_click) self.label.bind("<Double-1>", lambda e: self.goompy_zoom(+1)) self.label.bind("<Double-3>", lambda e: self.goompy_zoom(-1)) self.label.bind_all("<MouseWheel>", self.goompy_mousewheel) self.zoomlevel = ZOOM self.goompy = GooMPy(WIDTH, HEIGHT, LATITUDE, LONGITUDE, ZOOM, MAPTYPE) self.goompy_restart() pass pass def goompy_reload(self): self.coords = None self.goompy_redraw() self.parent['cursor'] = '' pass def goompy_restart(self): # A little trick to get a watch cursor along with loading self.parent['cursor'] = 'watch' self.after(1, self.goompy_reload) def goompy_redraw(self): self.image = self.goompy.getImage() self.image_tk = ImageTk.PhotoImage(self.image) self.label['image'] = self.image_tk self.label.pack(expand=1, fill='both') def goompy_zoom(self, sign): newlevel = self.zoomlevel + sign if newlevel > 0 and newlevel < 22: self.zoomlevel = newlevel self.goompy.useZoom(newlevel) self.goompy_restart() def goompy_mousewheel(self, event): if event.num == 5 or event.delta < 0: self.goompy_zoom(-1) else: self.goompy_zoom(+1) pass def goompy_drag(self, event): try: # Ignore NONE value self.goompy.move(self.coords[0] - event.x, self.coords[1] - event.y) except: pass self.image = self.goompy.getImage() self.goompy_redraw() self.coords = event.x, event.y def goompy_click(self, event): self.coords = event.x, event.y def init_connections(self): self.port_label.set(self._settings.GetSetting('port_label')) self.port.set(self._settings.GetSetting('port')) self.baud.set(self._settings.GetSetting('baud')) self.lbl_status.set( ('%s | %s' % (self.port_label.get(), self.baud.get()))) self.port_old = self._settings.GetSetting('port_old') if self.port.get() != self.port_old: if self._serial.is_open == True: self._serial.close() self.connection_status.set("Disconnected") self.toolbars['connect']['image'] = self.images_ui['connect'] self.menus['connect'].entryconfig( 1, image=self.images_ui['connect'], label='Connect') self._serial.baudrate = self.baud.get() self._serial.port = self.port.get() def connect_device(self): if self._serial.is_open == False: try: self._serial.open() # Open serial port except: pass else: self._serial.close() if self._serial.is_open == True: self.connection_status.set("Connected") self.toolbars['connect']['image'] = self.images_ui['disconnect'] self.menus['connect'].entryconfig( 1, image=self.images_ui['disconnect'], label='Disconnect') self.thread = Thread(target=self.read_serial, name=str(datetime.datetime.now())) self.thread.start() else: self.connection_status.set("Disconnected") self.toolbars['connect']['image'] = self.images_ui['connect'] self.menus['connect'].entryconfig(1, image=self.images_ui['connect'], label='Connect') def read_serial(self): self.txt_rawData.insert(END, '\n') # add first line as line space self.line_raw.set(0) i = 0 while self._serial.is_open: reading = self._serial.read(self._serial.in_waiting) self.txt_rawData.insert(END, reading) self.txt_rawData.see("end") self.line_raw.set(i) self.parse_data(reading) i += 1 def read_data(self, filename): if filename.strip() == '': return self._parser.ParseGpsNmeaFile(filename) # If the gpsData is length zero the file was not in the # GPGGA, GPRMC pair format. Try the just GPRMC format if len(self._parser.gpsData) == 0: self._parser.ParseGpsNmeaGprmcFile(filename) if len(self._parser.gpsData) == 0: print("Error parsing data. Fix input file?") exit output_file = ''.join(ch for ch in str(datetime.datetime.now()) if ch.isalnum()) self._parser.SaveReducedGpsData( '%s.txt' % os.path.join(self.output_path, output_file)) self.txt_rawData.delete('1.0', END) self.txt_rawData.insert(END, '\n') # add first line as line space with open(filename) as fp: self.line_raw.set(0) i = 0 for cnt, line in enumerate(fp): #print("Line {}: {}".format(cnt, line)) if line.strip() is not '': self.txt_rawData.insert(END, line) self.txt_rawData.see("end") self.line_raw.set(i) self.parse_data(line) i += 1 def parse_data(self, line): # Skip any sentence other than GPGGA if line.startswith('$GPGGA'): self._gpgga.parse(line) #if self._parser.DoNotHaveFix(self._gpgga.latitude): # continue [lat, lon] = self._parser.ConvertLatLonToDecimalDegrees( self._gpgga.latitude, self._gpgga.lat_direction, self._gpgga.longitude, self._gpgga.lon_direction) distance = self._parser.HaversineDistance(lat, self.lastLat, lon, self.lastLon) self.lastLat = lat self.lastLon = lon self.tree_parseDate.insert( '', 'end', self._gpgga.timestamp, values=(lat, lon, self._gpgga.antenna_altitude, distance, self._gpgga.num_sats, self._gpgga.gps_qual)) ''' This region starts with control events ''' def doClickEvent(self, index, tag): if tag == 'zoom_out': self.is_zoomIn = False self.goompy_zoom(-1) elif tag == 'zoom_in': self.is_zoomIn = True self.goompy_zoom(+1) elif tag == 'port': self._settings.SetSettings('port_old', self.port.get()) cd = ConnectionDialog(self.parent) if cd.OK[0] == True: self.init_connections() elif tag == 'connect': self.connect_device() pass elif tag == 'open': self.file_name = filedialog.askopenfilename() self.lbl_status.set(self.file_name) self.read_data(self.file_name) #self.thread = Thread(target=self.read_data, name = str(datetime.datetime.now())) #self.thread.start() elif tag == 'exit': self.onExit() pass else: print((index, tag)) pass pass def onExit(self): try: if messagebox.askokcancel('Quit', 'Do you want to quit?'): self.parent.destroy() except: pass