Example #1
0
	def do_build(self):
		packet = self.write
		packet += self.__tableid__
		packet += self.__offset__
		packet += self.__datalen__
		packet += self.__data__
		packet += data_chksum_str(self.__data__)
		return packet
Example #2
0
 def do_build(self):
     packet = self.write
     packet += self.__tableid__
     packet += self.__offset__
     packet += self.__datalen__
     packet += self.__data__
     packet += data_chksum_str(self.__data__)
     return packet
	def getTableData(self, tableid, octetcount = None, offset = None):
		"""
		Read data from a table. If successful, all of the data from the 
		requested table will be returned.
		
		@type tableid: Integer (0x0000 <= tableid <= 0xffff)
		@param tableid: The table number to read from
		
		@type octetcount: Integer (0x0000 <= tableid <= 0xffff)
		@param octetcount: Limit the amount of data read, only works if 
		the meter supports this type of reading.
		
		@type offset: Integer (0x000000 <= octetcount <= 0xffffff)
		@param 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 != '\x00':
			status = ord(status)
			details = (ERROR_CODE_DICT.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 = 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 data_chksum_str(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('cacheing table #' + str(tableid))
			self.__tbl_cache__[tableid] = data
		return data
Example #4
0
	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.

		@type tableid: Integer (0x0000 <= tableid <= 0xffff)
		@param tableid: The table number to read from

		@type octetcount: Integer (0x0000 <= tableid <= 0xffff)
		@param octetcount: Limit the amount of data read, only works if
		the meter supports this type of reading.

		@type offset: Integer (0x000000 <= octetcount <= 0xffffff)
		@param 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 != '\x00':
			status = ord(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 = 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 data_chksum_str(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('cacheing table #' + str(tableid))
			self.__tbl_cache__[tableid] = data
		return data
Example #5
0
	def parse(data):
		if len(data) < 3:
			raise Exception('invalid data (size)')
		if data[0] != '\x40' and data[0] != '\x4f':
			raise Exception('invalid start byte')
		tableid = unpack('>H', data[1:3])[0]
		chksum = data[-1]
		if data[0] == '\x40':
			table_data = data[5:-1]
			offset = None
		elif data[0] == '\x4f':
			table_data = data[8:-1]
			offset = unpack('>I', '\x00' + data[3:6])[0]
		if data_chksum_str(table_data) != chksum:
			raise Exception('invalid check sum')
		request = C1218WriteRequest(tableid, table_data, offset = offset)
		request.write = data[0]
		return request
Example #6
0
 def parse(data):
     if len(data) < 3:
         raise Exception('invalid data (size)')
     if data[0] != '\x40' and data[0] != '\x4f':
         raise Exception('invalid start byte')
     tableid = struct.unpack('>H', data[1:3])[0]
     chksum = data[-1]
     if data[0] == '\x40':
         table_data = data[5:-1]
         offset = None
     elif data[0] == '\x4f':
         table_data = data[8:-1]
         offset = struct.unpack('>I', '\x00' + data[3:6])[0]
     if data_chksum_str(table_data) != chksum:
         raise Exception('invalid check sum')
     request = C1218WriteRequest(tableid, table_data, offset=offset)
     request.write = data[0]
     return request