def from_bytes(cls, data): if len(data) < 3: raise Exception('invalid data (size)') if data[0] != 0x40 and data[0] != 0x4f: raise Exception('invalid start byte') tableid = struct.unpack('>H', data[1:3])[0] chksum = data[-1] if data[0] == 0x40: table_data = data[5:-1] offset = None elif data[0] == 0x4f: table_data = data[8:-1] offset = struct.unpack('>I', b'\x00' + data[3:6])[0] if check_data_checksum(table_data, chksum): raise Exception('invalid check sum') request = cls(tableid, table_data, offset=offset) request.write = data[0] return request
def parse(data): if len(data) < 3: raise Exception('invalid data (size)') if data[0] != b'\x40' and data[0] != b'\x4f': raise Exception('invalid start byte') tableid = struct.unpack('>H', data[1:3])[0] chksum = data[-1] if data[0] == b'\x40': table_data = data[5:-1] offset = None elif data[0] == b'\x4f': table_data = data[8:-1] offset = struct.unpack('>I', b'\x00' + data[3:6])[0] if check_data_checksum(table_data, chksum): raise Exception('invalid check sum') request = C1218WriteRequest(tableid, table_data, offset=offset) request.write = data[0] return request
def get_table_data(self, tableid, octetcount=None, offset=None): #print("\n\n\n***connection.py-564 : Inside get table data****\n\n\n") """ Read data from a table. If successful, all of the data from the requested table will be returned. :param int tableid: The table number to read from (0x0000 <= tableid <= 0xffff) :param int octetcount: Limit the amount of data read, only works if the meter supports this type of reading. :param int offset: The offset at which to start to read the data from. """ #print("\n\n\n***Table ID: {}****\n\n\n".format(tableid)) if self.caching_enabled and tableid in self._cacheable_tables and tableid in self._table_cache.keys(): self.logger.info('returning cached table #' + str(tableid)) return self._table_cache[tableid] self.send(C1218ReadRequest(tableid, offset, octetcount)) data = self.recv() status = data[0] if status != 0x00: status = status details = (C1218_RESPONSE_CODES.get(status) or 'unknown response code') self.logger.error('could not read table id: ' + str(tableid) + ', error: ' + details) raise C1218ReadTableError('could not read table id: ' + str(tableid) + ', error: ' + details, status) if len(data) < 4: if len(data) == 0: self.logger.error('could not read table id: ' + str(tableid) + ', error: no data was returned') raise C1218ReadTableError('could not read table id: ' + str(tableid) + ', error: no data was returned') self.logger.error('could not read table id: ' + str(tableid) + ', error: data read was corrupt, invalid length (less than 4)') raise C1218ReadTableError('could not read table id: ' + str(tableid) + ', error: data read was corrupt, invalid length (less than 4)') length = struct.unpack('>H', data[1:3])[0] chksum = data[-1] data = data[3:-1] if len(data) != length: self.logger.error('could not read table id: ' + str(tableid) + ', error: data read was corrupt, invalid length') raise C1218ReadTableError('could not read table id: ' + str(tableid) + ', error: data read was corrupt, invalid length') if not check_data_checksum(data, chksum): self.logger.error('could not read table id: ' + str(tableid) + ', error: data read was corrupt, invalid check sum') raise C1218ReadTableError('could not read table id: ' + str(tableid) + ', error: data read was corrupt, invalid checksum') if self.caching_enabled and tableid in self._cacheable_tables and not tableid in self._table_cache.keys(): self.logger.info('caching table #' + str(tableid)) self._table_cache[tableid] = data #print("\n\n\n***End of get_table_data****\n\n\n") return data
def get_table_data(self, tableid, octetcount=None, offset=None): """ Read data from a table. If successful, all of the data from the requested table will be returned. :param int tableid: The table number to read from (0x0000 <= tableid <= 0xffff) :param int octetcount: Limit the amount of data read, only works if the meter supports this type of reading. :param int offset: The offset at which to start to read the data from. """ if self.caching_enabled and tableid in self.__cacheable_tbls__ and tableid in self.__tbl_cache__.keys(): self.logger.info('returning cached table #' + str(tableid)) return self.__tbl_cache__[tableid] self.send(C1218ReadRequest(tableid, offset, octetcount)) data = self.recv() status = data[0] if status != 0x00: status = status details = (C1218_RESPONSE_CODES.get(status) or 'unknown response code') self.logger.error('could not read table id: ' + str(tableid) + ', error: ' + details) raise C1218ReadTableError('could not read table id: ' + str(tableid) + ', error: ' + details, status) if len(data) < 4: if len(data) == 0: self.logger.error('could not read table id: ' + str(tableid) + ', error: no data was returned') raise C1218ReadTableError('could not read table id: ' + str(tableid) + ', error: no data was returned') self.logger.error('could not read table id: ' + str(tableid) + ', error: data read was corrupt, invalid length (less than 4)') raise C1218ReadTableError('could not read table id: ' + str(tableid) + ', error: data read was corrupt, invalid length (less than 4)') length = struct.unpack('>H', data[1:3])[0] chksum = data[-1] data = data[3:-1] if len(data) != length: self.logger.error('could not read table id: ' + str(tableid) + ', error: data read was corrupt, invalid length') raise C1218ReadTableError('could not read table id: ' + str(tableid) + ', error: data read was corrupt, invalid length') if not check_data_checksum(data, chksum): self.logger.error('could not read table id: ' + str(tableid) + ', error: data read was corrupt, invalid check sum') raise C1218ReadTableError('could not read table id: ' + str(tableid) + ', error: data read was corrupt, invalid checksum') if self.caching_enabled and tableid in self.__cacheable_tbls__ and not tableid in self.__tbl_cache__.keys(): self.logger.info('caching table #' + str(tableid)) self.__tbl_cache__[tableid] = data return data