Esempio n. 1
0
def main():
    if '-h' in sys.argv:
        print 'usage', sys.argv[0], '[host] -i -c -h [NAME]...'
        print '-i', 'print info about each value type'
        print '-c', 'continuous watch'
        print '-h', 'show this message'
        exit(0)

    continuous = '-c' in sys.argv
    if continuous:
        sys.argv.remove('-c')

    info = '-i' in sys.argv
    if info:
        sys.argv.remove('-i')

    client = SignalKClientFromArgs(sys.argv, continuous)
    if not client.have_watches:
        while True:
            if not client.print_values(10, info):
                print 'timed out'
                exit(1)
            if not continuous:
                break
        exit()

    while True:
        msg = client.receive_single(.1)
        if not msg:
            continue

        if not continuous:
            # split on separate lines if not continuous
            name, value = msg
            if info:
                print kjson.dumps({name: value})
            else:
                print name, '=', nice_str(value['value'])
            return
        if info:
            print kjson.dumps(msg)
        else:
            first = True
            name, value = msg
            if first:
                first = False
            else:
                sys.stdout.write(', ')
            sys.stdout.write(name + ' = ' + nice_str(value['value']))
            print ''
Esempio n. 2
0
def main():
    if '-h' in sys.argv:
        print 'usage', sys.argv[0], '[host] -i -c -h [NAME]...'
        print '-i', 'print info about each value type'
        print '-c', 'continuous watch'
        print '-h', 'show this message'
        exit(0)

    continuous = '-c' in sys.argv
    if continuous:
        sys.argv.remove('-c')

    info = '-i' in sys.argv
    if info:
        sys.argv.remove('-i')

    client = SignalKClientFromArgs(sys.argv, continuous)
    if not client.have_watches:
        while True:
            if not client.print_values(10, info):
                print 'timed out'
                exit(1)
            if not continuous:
                break
        exit()

    while True:
        result = client.receive(1000)
        if result:
            if not continuous:
                # split on separate lines if not continuous
                for r in result:
                    if info:
                        print kjson.dumps({r: result[r]})
                    else:
                        print r, '=', nice_str(result[r]['value'])
                return
            if info:
                print kjson.dumps(result)
            else:
                first = True
                for r in result:
                    if first:
                        first = False
                    else:
                        sys.stdout.write(', ')
                    sys.stdout.write(r + ' = ' + nice_str(result[r]['value']))
                print ''
Esempio n. 3
0
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
Esempio n. 4
0
    def ListValues(self, socket):
        msg = {}
        for value in self.values:
            t = self.values[value].type()
            if type(t) == type(''):
                t = {'type' : t}
            msg[value] = t

        socket.send(kjson.dumps(msg) + '\n')
Esempio n. 5
0
    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'
        configfilename = configfilepath + 'signalk.conf'

        try:
            file = open(configfilename)
            config = kjson.loads(file.readline())

            if 'host' in config and not host:
                host = config['host']
        except:
            print 'failed to read config file:', configfilename

        if not host:
            host = 'pypilot'
            print 'host not specified using host', host

        try:
            config['host'] = host
            file = open(configfilename, 'w')
            file.write(kjson.dumps(config) + '\n')
        except IOError:
            print 'failed to write config file:', configfilename

        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)
Esempio n. 6
0
    def StorePersistentValues(self):
        self.persistent_timeout = time.time() + 600 # 10 minutes
        need_store = False
        for name in self.values:
            value = self.values[name]
            if not value.persistent:
                continue
            if not name in self.persistent_data or value.value != self.persistent_data[name]:
                self.persistent_data[name] = value.value
                need_store = True

        if not need_store:
            return
                
        try:
            file = open(self.persistent_path, 'w')
            file.write(kjson.dumps(self.persistent_data)+'\n')
            file.close()
        except Exception as e:
            print 'failed to write', self.persistent_path, e
Esempio n. 7
0
    def onconnected(self, connection):
        if self.write_config:
            try:
                file = open(self.configfilename, 'w')
                file.write(kjson.dumps(self.write_config) + '\n')
                file.close()
                self.write_config = False
            except IOError:
                print 'failed to write config file:', self.configfilename
            except Exception as e:
                print 'Exception writing config file:', self.configfilename, e

        self.socket = LineBufferedNonBlockingSocket(connection)
        self.values = []
        self.msg_queue = []
        self.poller = select.poll()
        if self.socket:
            fd = self.socket.socket.fileno()
        else:
            fd = self.serial.fileno()
        self.poller.register(fd, select.POLLIN)
        self.f_on_connected(self)
Esempio n. 8
0
 def send(self, request):
     self.socket.send(kjson.dumps(request) + '\n')
Esempio n. 9
0
 def get_signalk(self):
     return '{"' + self.name + '": {"value": ' + kjson.dumps(
         self.value) + '}}'