nightui/src/modules/navigationd/navigationd.py

118 lines
3.1 KiB
Python

import gpsd
import time
import requests
import geopandas as gpd
import osmnx as ox
import networkx as nx
import os
urlBase = 'http://localhost:5000'
urlLocation = urlBase + '/api/navigation/position'
urlImage = urlBase + '/api/navigation/image'
urlAddCyberware = urlBase + '/api/cyberware/add'
urlRemoveCyberware = urlBase + '/api/cyberware/remove'
uuid = ""
def connectCyberware():
global uuid
# Add to Cyberware and get UUID
try:
uuidRequest = requests.post(urlAddCyberware, json={ 'name': 'GPS', 'hotpluggable': True, 'canSet': [ '/api/navigation/position', '/api/navigation/image' ] })
uuid = uuidRequest.json()[0]['uuid']
except:
print("Cannot connect to NightCall. Stopping.")
exit()
def disconnectCyberware():
try:
disconnectRequest = requests.post(urlRemoveCyberware, json={ 'uuid': uuid })
except:
print("Cannot disconnect Cyberware.")
def getMapImage(distance, gpsdData):
ox.settings.use_cache = True
# We don't want to send any data if we don't have a fix.
if(gpsdData.lat == 0 and gpsdData.lon == 0):
return
#point = (42.352593, -83.2640164)
point = (gpsdData.lat, gpsdData.lon)
try:
G = ox.graph_from_point(point, dist=distance, dist_type="bbox", network_type="drive")
# impute edge (driving) speeds and calculate edge travel times
G = ox.routing.add_edge_speeds(G)
G = ox.routing.add_edge_travel_times(G)
# I'm not sure how to handle this short of saving it and then reading it. :(
filepath = uuid+".png"
fig, ax = ox.plot_graph(G, show=True, node_size=0, edge_linewidth=2, bgcolor="#0B1629", edge_color="#5FE0E9", save=True, filepath=filepath, close=True)
#fig.close()
except ox._errors.InsufficientResponseError:
print("Insufficient response. Skipping.")
#def getMapImage(area):
# #lat, lon = gpsdData.position()
# lat, lon = 41.7573113, -93.8128377
# bbox = lat-(area/2.0), lat+(area/2.0), lon+(area/2.0), lon-(area/2.0)
#
# G = ox.graph_from_bbox(bbox=bbox, network_type="drive_service")
def sendLocation(gpsdData):
# We don't want to hit the API if we don't have a fix.
if(gpsdData.lat != 0 and gpsdData.lon !=0):
try:
request = requests.post(urlLocation, json={ 'x': gpsdData.lat, 'y': gpsdData.lon, 'z': gpsdData.alt, 'o': None, 'uuid': uuid })
except:
print('Could not contact NightCall')
def sendMapImage():
try:
files = { 'image': open(uuid+".png", 'rb') }
request = requests.post(urlImage, files=files)
except FileNotFoundError: # Occurs if we don't have a fix yet.
return
except:
print("Could not contact NightCall")
print("Connecting to gpsd")
gpsd.connect() # Connect to gpsd
continueFlag = True
print("Adding Cyberware")
connectCyberware()
while continueFlag:
try:
time.sleep(1)
try:
gpsdData = gpsd.get_current()
except gpsd.NoFixError:
print("No GPS fix!")
getMapImage(1000, gpsdData)
sendLocation(gpsdData)
sendMapImage()
#print(packet.position())
except KeyboardInterrupt:
print("Exiting.")
continueFlag = False;
print("Removing Cyberware")
disconnectCyberware()
print("Cleaning up")
try:
os.remove(uuid+".png")
except:
print("No minimap image was ever made. GPS probably never had a fix.")