def storage(topic, item): """ Storage plugin for m2s. The function signature MUST match the above. `topic' contains the message topic (e.g. "mqttitude/jpm/nexus") and `item' is a dict which contains the rest of the data (including weather and reverse geo-coding information if requested) This function need not return anything. """ logging.debug("---- in storage: %s" % topic) item['tst'] = item['date_string'] # replace for database try: loca = Location(**item) loca.save() except Exception, e: logging.info("Cannot store in DB: %s" % (str(e)))
# Handle _type location/waypoint specifically if '_type' in item: if item['_type'] == 'waypoint': # Upsert try: mysql_db.execute_sql( """ REPLACE INTO waypoint (topic, username, device, lat, lon, tst, rad, waypoint) VALUES (%s, %s, %s, %s, %s, %s, %s, %s) """, ( item['topic'], item['username'], item['device'], item['lat'], item['lon'], item['tst'], item['rad'], item['desc'], )) except Exception, e: logging.info("Cannot upsert waypoint in DB: %s" % (str(e))) else: try: loca = Location(**item) loca.save() except Exception, e: logging.info("Cannot store location in DB: %s" % (str(e)))
item['tst'] = item['date_string'] # replace for database # Attempt to connect if not already connected. Takes care of MySQL 2006 try: database.connect() except Exception, e: logging.info("Cannot connect to database: %s" % (str(e))) # Handle _type location/waypoint specifically if '_type' in item: if item['_type'] == 'waypoint': # Upsert try: try: Waypoint.get(Waypoint.tst == item['tst']).delete_instance() except peewee.WaypointDoesNotExist: pass waypoint = Waypoint(**item) waypoint.save() except Exception, e: logging.info("Cannot upsert waypoint in DB: %s" % (str(e))) else: try: loca = Location(**item) loca.save() except Exception, e: logging.info("Cannot store location in DB: %s" % (str(e)))
def main(argv): from_date = '2013-11-24' to_date = '2013-11-28' username = None device = None title = None xcode = False try: opts, args = getopt.getopt(argv, "f:t:u:d:T:X", ["from", "to", "username", "device", 'Title', 'Xcode' ]) except getopt.GetoptError as e: print_usage() sys.exit(2) for opt, arg in opts: if opt in ('-d', '--device'): device = arg if opt in ('-u', '--username'): username = arg if opt in ('-f', '--from'): from_date = arg if opt in ('-t', '--to'): to_date = arg if opt in ('-T', '--title'): title = arg if opt in ('-X', '--Xcode'): xcode = True if username is None: print "You must provide a username" sys.exit(2) if device is None: print "You must provide a device name" sys.exit(2) if title is None: title = "Trip %s to %s" % (from_date, to_date) root = ET.Element('gpx') root.set('version', '1.0') root.set('creator', 'OwnTracks GPX Exporter') root.set('xmlns', "http://www.topografix.com/GPX/1/0") root.append(Comment('Hi JP')) if not xcode: track = Element('trk') track_name = SubElement(track, 'name') track_name.text = title track_desc = SubElement(track, 'desc') track_desc.text = "Length: xxx km or so" segment = Element('trkseg') track.append(segment) trackpoints = [] waypoints = [] lat1 = None lon1 = None lat2 = None lon2 = None query = Location.select().where( (Location.username == username) & (Location.device == device) & (Location.tst.between(from_date, to_date)) ) query = query.order_by(Location.tst.asc()) for l in query: dbid = l.id topic = l.topic lat = l.lat lon = l.lon dt = l.tst weather = l.weather revgeo = l.revgeo desc = l.waypoint # First point if lat1 is None: lat1 = lat lon1 = lon lat2 = lat lon2 = lon tp = Element('trkpt') tp.set('lat', lat) tp.set('lon', lon) tp_time = SubElement(tp, 'time') tp_time.text = dt.isoformat()[:19]+'Z' tp.append(Comment(u'#%s %s' % (dbid, topic))) trackpoints.append(tp) if xcode: wpt = Element('wpt') wpt.set('lat', lat) wpt.set('lon', lon) waypoints.append(wpt) else: if (weather is not None and revgeo is not None) or (desc is not None): wpt = Element('wpt') wpt.set('lat', lat) wpt.set('lon', lon) wpt_name = SubElement(wpt, 'name') wpt_name.text = u'%s' % (dt.isoformat()[:19]+'Z') wpt_desc = SubElement(wpt, 'desc') if desc is not None: wpt_desc.text = u'%s' % (desc) else: wpt_desc.text = u'(%s) %s' % (weather, revgeo) waypoints.append(wpt) for waypoint in waypoints: root.append(waypoint) if not xcode: root.append(track) for trackpoint in trackpoints: segment.append(trackpoint) try: distance = haversine(lat1, lon1, lat2, lon2) track_desc.text = "Distance: %.2f" % distance except: track_desc.text = "Distance unknown" print prettify(root)
def main(argv): from_date = '2013-11-24' to_date = '2013-11-28' username = None device = None title = None xcode = False try: opts, args = getopt.getopt(argv, "f:t:u:d:T:X", ["from", "to", "username", "device", 'Title', 'Xcode' ]) except getopt.GetoptError as e: print_usage() sys.exit(2) for opt, arg in opts: if opt in ('-d', '--device'): device = arg if opt in ('-u', '--username'): username = arg if opt in ('-f', '--from'): from_date = arg if opt in ('-t', '--to'): to_date = arg if opt in ('-T', '--title'): title = arg if opt in ('-X', '--Xcode'): xcode = True if username is None: print "You must provide a username" sys.exit(2) if device is None: print "You must provide a device name" sys.exit(2) if title is None: title = "Trip %s to %s" % (from_date, to_date) root = ET.Element('gpx') root.set('version', '1.0') root.set('creator', 'OwnTracks GPX Exporter') root.set('xmlns', "http://www.topografix.com/GPX/1/0") root.append(Comment('Hi JP')) if not xcode: track = Element('trk') track_name = SubElement(track, 'name') track_name.text = title track_desc = SubElement(track, 'desc') track_desc.text = "Length: xxx km or so" segment = Element('trkseg') track.append(segment) trackpoints = [] waypoints = [] lat1 = None lon1 = None lat2 = None lon2 = None query = Location.select().where( (Location.username == username) & (Location.device == device) & (Location.tst.between(from_date, to_date)) ) query = query.order_by(Location.tst.asc()) for l in query: dbid = l.id topic = l.topic lat = l.lat lon = l.lon dt = l.tst weather = l.weather revgeo = l.revgeo desc = l.waypoint # First point if lat1 is None: lat1 = lat lon1 = lon lat2 = lat lon2 = lon tp = Element('trkpt') tp.set('lat', lat) tp.set('lon', lon) tp_time = SubElement(tp, 'time') tp_time.text = dt.isoformat()[:19]+'Z' tp.append(Comment(u'#%s %s' % (dbid, topic))) trackpoints.append(tp) if xcode: wpt = Element('wpt') wpt.set('lat', lat) wpt.set('lon', lon) waypoints.append(wpt) else: if (weather is not None and revgeo is not None) or (desc is not None): wpt = Element('wpt') wpt.set('lat', lat) wpt.set('lon', lon) wpt_name = SubElement(wpt, 'name') wpt_name.text = u'%s' % (dt.isoformat()[:19]+'Z') wpt_desc = SubElement(wpt, 'desc') if desc is not None: wpt_desc.text = u'%s' % (desc) else: wpt_desc.text = u'(%s) %s' % (weather, revgeo) waypoints.append(wpt) root.extend(waypoints) if not xcode: root.append(track) segment.extend(trackpoints) try: distance = haversine(lat1, lon1, lat2, lon2) track_desc.text = "Distance: %.2f" % distance except: track_desc.text = "Distance unknown" print prettify(root)
if topic not in store_only: return logging.debug("---- in storage: %s" % topic) item['tst'] = item['date_string'] # replace for database # Attempt to connect if not already connected. Takes care of MySQL 2006 try: store_db.connect() except Exception, e: logging.info("Cannot connect to database: %s" % (str(e))) # Just make sure the database already exists ... try: Location.create_table(fail_silently=True) except Exception, e: logging.info ("Database Error: %s" % str(e)) # Handle _type location/waypoint specifically if '_type' in item: if item['_type'] == 'waypoint': # Upsert try: store_db.execute_sql(""" REPLACE INTO waypoint (topic, username, device, lat, lon, tst, rad, waypoint) VALUES (%s, %s, %s, %s, %s, %s, %s, %s) """, ( item['topic'], item['username'], item['device'], item['lat'],