def LoadPersistentData(persistent_path, server=True): try: file = open(persistent_path) persistent_data = kjson.loads(file.readline()) 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()) + '\n') file.close() try: file = open(persistent_path + '.bak') persistent_data = kjson.loads(file.readline()) 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 __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() if 'host' in config and not host: host = config['host'] elif config['host'] != host: self.write_config = config self.write_config['host'] = host except Exception as e: print 'failed to read config file:', self.configfilename, e 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 = server.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)
def LoadPersistentValues(self): try: file = open(self.persistent_path) self.persistent_data = kjson.loads(file.readline()) file.close() except: print 'failed to load', self.persistent_path self.persistent_data = {}
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: unknown value: ' + name + '\n') else: self.HandleNamedRequest(socket, data)
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 ConnectionLost: self.disconnected() dt = time.time() - t return self.receive_line(timeout - dt)