def LoadPersistentData(persistent_path, server=True): try: file = open(persistent_path) persistent_data = kjson.loads(file.read()) file.close() except Exception as e: print('failed to load', persistent_path, e) print('WARNING Alignment and other data lost!!!!!!!!!') if server: # log failing to load persistent data persist_fail = os.getenv('HOME') + '/.pypilot/persist_fail' file = open(persist_fail, 'a') file.write(str(time.time()) + ' ' + str(e) + '\n') file.close() try: file = open(persistent_path + '.bak') persistent_data = kjson.loads(file.read()) file.close() return persistent_data except Exception as e: print('backup data failed as well', e) return {} if server: # backup persistent_data if it loaded with success file = open(persistent_path + '.bak', 'w') file.write(kjson.dumps(persistent_data) + '\n') file.close() return persistent_data
def GsKVProc(tlm,skval): while True: print('\nAttempting Connection!\n') sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: connection = socket.create_connection((SIGKHOST,SIGKPORT)) # CONNECTION HANDLING is WEAK ????????????? if connection: print('Connection Established from GskVProc to SK_Server!\n') while True: #time.sleep(2) #FOR TEST PURPOSE DELAY ONLY if skval.poll(): wsk = skval.recv() request = {'method' : 'get', 'name' : wsk} connection.send(kjson.dumps(request)+'\n') line=connection.recv(1024) msg = kjson.loads(line.rstrip()) value = msg[wsk]["value"] #Return telemetry data skval.send(str(value)) #tlm.send(str(value)) #if not connection: #DispNCON() #print('No Server Connection Established!') #time.sleep(2) except: DispNCON() print('\nNo Server Connection Established!') time.sleep(2)
def HandleRequest(self, socket, request): data = kjson.loads(request) if data['method'] == 'list': self.ListValues(socket) else: name = data['name'] if not name in self.values: socket.send('invalid request: ' + data['method'] + ' unknown value: ' + name + '\n') else: self.HandleNamedRequest(socket, data)
def GetSignalkValue(name): connection = socket.create_connection((HOST, PORT)) request = {'method': 'get', 'name': name} connection.send(kjson.dumps(request) + '\n') line = connection.recv(1024) try: msg = kjson.loads(line.rstrip()) value = msg[name]["value"] except: value = "" connection.close() return value
def receive_line(self, timeout=0): line = self.socket.readline() if line: try: msg = kjson.loads(line.rstrip()) except: raise Exception('invalid message from server:', line) return msg if timeout < 0: return False t = time.time() try: if not self.poll(timeout): return False except: self.disconnected() dt = time.time() - t return self.receive_line(timeout - dt)
def __init__(self, f_on_connected, host=False, port=False, autoreconnect=False, have_watches=False): self.autoreconnect = autoreconnect config = {} configfilepath = os.getenv('HOME') + '/.pypilot/' if not os.path.exists(configfilepath): os.makedirs(configfilepath) if not os.path.isdir(configfilepath): raise configfilepath + 'should be a directory' self.configfilename = configfilepath + 'signalk.conf' self.write_config = False try: file = open(self.configfilename) config = kjson.loads(file.readline()) file.close() except Exception as e: print('failed to read config file:', self.configfilename, e) config = {} if not 'host' in config: config['host'] = '127.0.0.1' if not host: host = config['host'] if config['host'] != host: self.write_config = config self.write_config['host'] = host if not host: host = 'pypilot' print('host not specified using host', host) if '/dev' in host: # serial port device, baud = host, port if not baud or baud == 21311: baud = 9600 connection = serial.Serial(device, baud) cmd = 'stty -F ' + device + ' icanon iexten' print('running', cmd) os.system(cmd) else: if not port: if ':' in host: i = host.index(':') host = host[:i] port = host[i + 1:] else: port = DEFAULT_PORT try: connection = socket.create_connection((host, port), 1) except: print('connect failed to %s:%d' % (host, port)) raise self.host_port = host, port self.f_on_connected = f_on_connected self.have_watches = have_watches self.onconnected(connection)