Exemplo n.º 1
0
 def __init__(self, *args, **kwargs):
     self.fmt = StatusMessageFormatFactory()
     self.struct = self.fmt.get_struct()
     self.store = dict()
     self.update(dict(*args, **kwargs))
     self.lock = threading.Lock()
     self.is_valid = False
Exemplo n.º 2
0
 def __init__(self, *args, **kwargs):
     self.fmt = StatusMessageFormatFactory()
     self.struct = self.fmt.get_struct()
     self.store = dict()
     self.update(dict(*args, **kwargs))
     self.lock = threading.Lock()
     self.is_valid = False
Exemplo n.º 3
0
class Status(ElixysObject, collections.MutableMapping):
    """ The Status object has a dictionary interface,
    it is capable of properly parsing the binary packets from
    the client hardware an converting them into python data types """
    def __init__(self, *args, **kwargs):
        self.fmt = StatusMessageFormatFactory()
        self.struct = self.fmt.get_struct()
        self.store = dict()
        self.update(dict(*args, **kwargs))
        self.lock = threading.Lock()
        self.is_valid = False

    def __getitem__(self, key):
        if self.is_valid is False:
            return None
        self.lock.acquire()
        val = self.store[self.__keytransform__(key)]
        self.lock.release()
        return val

    def __setitem__(self, key, value):
        #raise ElixysReadOnlyError("Status Packet only updated"
        #                          "by new packet from hardware")
        self.store[self.__keytransform__(key)] = value

    def __delitem__(self, key):
        self.lock.acquire()
        del self.store[self.__keytransform__(key)]
        self.lock.release()

    def __iter__(self):
        return iter(self.store)

    def __len__(self):
        return len(self.store)

    def __keytransform__(self, key):
        return key

    def __getattr__(self, name):
        if self.is_valid:
            super(Status, self).__getattr__(self, name)
        else:
            raise ElixysCommError(
                "The status packet is invalid, is a client connected?")

    def parse_packet(self, pkt):
        """ Maybe the most complicates function, read the config and
        properly parses the binary packet from the hardware accordingly """
        #Todo: rework this, too complicated!
        subsystems = self.fmt.subsystems
        data = self.struct.unpack(pkt)
        data_dict = dict()
        data_idx = 0
        for subsystem, count, messagefmt in subsystems:
            #print subsystem
            sub_dict = dict()

            for key, value in messagefmt.items():
                if isinstance(value, str):
                    #print key, data_idx
                    sub_dict[key] = data[data_idx]
                    data_idx += 1

                elif key == "Repeat":
                    rptmessagefmt = messagefmt['Repeat']
                    units = []
                    for i in range(count):
                        unit_dict = dict()
                        for rkey, rval in rptmessagefmt.items():
                            unit_dict[rkey] = data[data_idx]
                            #print rkey, data_idx
                            data_idx += 1
                        units.append(unit_dict)
                        sub_dict[i] = unit_dict
                    sub_dict['Subs'] = units
                    sub_dict['count'] = count
            data_dict[subsystem] = sub_dict
        self.lock.acquire()
        self.store = data_dict
        for key, value in data_dict.items():
            setattr(self, key, value)
        self.lock.release()
        self.is_valid = True
        return data_dict

    def update_from_queue(self, queue):
        self.thread = StatusThread(self, queue)
        self.thread.start()

    def stop_update(self):
        log.debug("Starting update thread")
        self.thread.stop()

    def as_json(self):
        return json.dumps(self.store, indent=2)

    def as_dict(self):
        return copy.deepcopy(self.store)
Exemplo n.º 4
0
class Status(ElixysObject, collections.MutableMapping):
    """ The Status object has a dictionary interface,
    it is capable of properly parsing the binary packets from
    the client hardware an converting them into python data types """
    def __init__(self, *args, **kwargs):
        self.fmt = StatusMessageFormatFactory()
        self.struct = self.fmt.get_struct()
        self.store = dict()
        self.update(dict(*args, **kwargs))
        self.lock = threading.Lock()
        self.is_valid = False

    def __getitem__(self, key):
        if self.is_valid is False:
            return None
        self.lock.acquire()
        val = self.store[self.__keytransform__(key)]
        self.lock.release()
        return val

    def __setitem__(self, key, value):
        #raise ElixysReadOnlyError("Status Packet only updated"
        #                          "by new packet from hardware")
        self.store[self.__keytransform__(key)] = value

    def __delitem__(self, key):
        self.lock.acquire()
        del self.store[self.__keytransform__(key)]
        self.lock.release()

    def __iter__(self):
        return iter(self.store)

    def __len__(self):
        return len(self.store)

    def __keytransform__(self, key):
        return key

    def __getattr__(self, name):
        if self.is_valid:
            super(Status, self).__getattr__(self, name)
        else:
            raise ElixysCommError("The status packet is invalid, is a client connected?")

    def parse_packet(self, pkt):
        """ Maybe the most complicates function, read the config and
        properly parses the binary packet from the hardware accordingly """
        #Todo: rework this, too complicated!
        subsystems = self.fmt.subsystems
        data = self.struct.unpack(pkt)
        data_dict = dict()
        data_idx = 0
        for subsystem, count, messagefmt in subsystems:
            #print subsystem
            sub_dict = dict()

            for key, value in messagefmt.items():
                if isinstance(value, str):
                    #print key, data_idx
                    sub_dict[key] = data[data_idx]
                    data_idx += 1

                elif key == "Repeat":
                    rptmessagefmt = messagefmt['Repeat']
                    units = []
                    for i in range(count):
                        unit_dict = dict()
                        for rkey, rval in rptmessagefmt.items():
                            unit_dict[rkey] = data[data_idx]
                            #print rkey, data_idx
                            data_idx += 1
                        units.append(unit_dict)
                        sub_dict[i] = unit_dict
                    sub_dict['Subs'] = units
                    sub_dict['count'] = count
            data_dict[subsystem] = sub_dict
        self.lock.acquire()
        self.store = data_dict
        for key, value in data_dict.items():
            setattr(self,key,value)
        self.lock.release()
        self.is_valid = True
        return data_dict

    def update_from_queue(self, queue):
        self.thread = StatusThread(self, queue)
        self.thread.start()

    def stop_update(self):
        log.debug("Starting update thread")
        self.thread.stop()

    def as_json(self):
        return json.dumps(self.store, indent=2)

    def as_dict(self):
        return copy.deepcopy(self.store)