def addTransaction(self, devPublicKey, encryptedObj): global gwPvt global gwPub t1 = time.time() blk = chainFunctions.findBlock(devPublicKey) if (blk != False and blk.index > 0): devAESKey = findAESKey(devPublicKey) if (devAESKey != False): # plainObject contains [Signature + Time + Data] plainObject = criptoFunctions.decryptAES( encryptedObj, devAESKey) signature = plainObject[:-20] # remove the last 20 chars devTime = plainObject[-20: -4] # remove the 16 char of timestamp deviceData = plainObject[ -4:] # retrieve the las 4 chars which are the data d = devTime + deviceData isSigned = criptoFunctions.signVerify(d, signature, devPublicKey) if isSigned: deviceInfo = DeviceInfo.DeviceInfo(signature, devTime, deviceData) nextInt = blk.transactions[len(blk.transactions) - 1].index + 1 signData = criptoFunctions.signInfo(gwPvt, str(deviceInfo)) gwTime = "{:.0f}".format(((time.time() * 1000) * 1000)) # code responsible to create the hash between Info nodes. prevInfoHash = criptoFunctions.calculateTransactionHash( chainFunctions.getLatestBlockTransaction(blk)) transaction = Transaction.Transaction( nextInt, prevInfoHash, gwTime, deviceInfo, signData) # send to consensus #if not consensus(newBlockLedger, gwPub, devPublicKey): # return "Not Approved" chainFunctions.addBlockTransaction(blk, transaction) logger.debug( "block added locally... now sending to peers..") t2 = time.time() logger.debug( "=====2=====>time to add transaction in a block: " + '{0:.12f}'.format((t2 - t1) * 1000)) sendTransactionToPeers( devPublicKey, transaction ) # --->> this function should be run in a different thread. #print("all done") return "ok!" else: return "Invalid Signature" return "key not found"
def parse(self, stream): self.stream = stream in_bytes = bytearray(stream) index = 0 # append packet to buffer for idx in range(len(in_bytes)): self.bytes.append(in_bytes[idx]) if len(self.bytes) < PACKET_LEN: continue # check packet validity at this index if ( index + PACKET_LEN <= len(self.bytes) and self.bytes[index] == IN_START1 and self.bytes[index + 1] == IN_START2 and self.bytes[index + PACKET_LEN - 1] == IN_END ): # start parsing # index+2 : category1, index+3 : category2, index+4 : ID, index+5 : command # index+6~index+7 : Data1, index+8~index+9 : Data2, ..., index+12~index+13 : Data4 # index+14 : End byte device_info = DeviceInfo() device_info.itemtype = 1 # 0: queue item type device_info.cat1 = int(self.bytes[index + 2]) # 1: cat1 device_info.cat2 = int(self.bytes[index + 3]) # 2: cat2 device_info.devid = int(self.bytes[index + 4]) # 3: device ID device_info.cmd = int(self.bytes[index + 5]) # 4: command # ping response if device_info.cmd == CMD_PING: # print ' Command : Ping response' logging.warning("Command received : Ping response") device_info.name = "User device" # name device_info.loc = "NA" # location device_info.time = int(time.time()) # timestamp # pass to callback function self.cmd_callback(device_info) # empty buffer self.bytes = list() # register device elif device_info.cmd == CMD_REGISTER: # print ' Command : Register device' logging.warning("Command received : Register device") device_info.cmd1 = int(self.bytes[index + 6]) # 5: available cmd 1 device_info.cmd1dtype = int(self.bytes[index + 7]) # 6: available cmd 1 - data type device_info.cmd2 = int(self.bytes[index + 8]) # 7: device_info.cmd2dtype = int(self.bytes[index + 9]) # 8: device_info.cmd3 = int(self.bytes[index + 10]) # 9: device_info.cmd3dtype = int(self.bytes[index + 11]) # 10: device_info.cmd4 = int(self.bytes[index + 12]) # 11: available cmd 4 device_info.cmd4dtype = int(self.bytes[index + 13]) # 12: available cmd 4 - data type device_info.name = "User device" # 13: name device_info.loc = "NA" # 14: location device_info.time = int(time.time()) # 15: timestamp # put in queue self.recv_queue.put(device_info) # empty buffer self.bytes = list() # update sensor value elif device_info.cmd == CMD_UPDATE: data1, data2, data3, data4 = 0x0000, 0x0000, 0x0000, 0x0000 # you have to check negative value if self.bytes[index + 6] & 0x80 == 0x80: data1 = int(self.bytes[index + 6] ^ 0xFF) << 8 | int(self.bytes[index + 7] ^ 0xFF) data1 *= -1 else: data1 = (int(self.bytes[index + 6]) << 8) | int(self.bytes[index + 7]) if self.bytes[index + 8] & 0x80 == 0x80: data2 = int(self.bytes[index + 8] ^ 0xFF) << 8 | int(self.bytes[index + 9] ^ 0xFF) data2 *= -1 else: data2 = (int(self.bytes[index + 8]) << 8) | int(self.bytes[index + 9]) if self.bytes[index + 10] & 0x80 == 0x80: data3 = int(self.bytes[index + 10] ^ 0xFF) << 8 | int(self.bytes[index + 11] ^ 0xFF) data3 *= -1 else: data3 = (int(self.bytes[index + 10]) << 8) | int(self.bytes[index + 11]) if self.bytes[index + 12] & 0x80 == 0x80: data4 = int(self.bytes[index + 12] ^ 0xFF) << 8 | int(self.bytes[index + 13] ^ 0xFF) data4 *= -1 else: data4 = (int(self.bytes[index + 12]) << 8) | int(self.bytes[index + 13]) # print ' Command : Update sensor value (%d, %d, %d, %d)' % (data1, data2, data3, data4) logging.warning( "Command received : Update sensor value (%d, %d, %d, %d)" % (data1, data2, data3, data4) ) device_info.data1 = data1 # 5: int data 1 device_info.data2 = data2 # 6: int data 2 device_info.data3 = data3 # 7: int data 3 device_info.data4 = data4 # 8: int data 4 device_info.name = "User device" # 13: name device_info.loc = "NA" # 14: location device_info.time = int(time.time()) # 15: timestamp # put in queue self.recv_queue.put(device_info) # empty buffer self.bytes = list() # control signal response elif device_info.cmd == CMD_CONTROL: data1, data2, data3, data4 = 0x0000, 0x0000, 0x0000, 0x0000 # you have to check negative value if self.bytes[index + 6] & 0x80 == 0x80: data1 = int(self.bytes[index + 6] ^ 0xFF) << 8 | int(self.bytes[index + 7] ^ 0xFF) data1 *= -1 else: data1 = (int(self.bytes[index + 6]) << 8) | int(self.bytes[index + 7]) if self.bytes[index + 8] & 0x80 == 0x80: data2 = int(self.bytes[index + 8] ^ 0xFF) << 8 | int(self.bytes[index + 9] ^ 0xFF) data2 *= -1 else: data2 = (int(self.bytes[index + 8]) << 8) | int(self.bytes[index + 9]) if self.bytes[index + 10] & 0x80 == 0x80: data3 = int(self.bytes[index + 10] ^ 0xFF) << 8 | int(self.bytes[index + 11] ^ 0xFF) data3 *= -1 else: data3 = (int(self.bytes[index + 10]) << 8) | int(self.bytes[index + 11]) if self.bytes[index + 12] & 0x80 == 0x80: data4 = int(self.bytes[index + 12] ^ 0xFF) << 8 | int(self.bytes[index + 13] ^ 0xFF) data4 *= -1 else: data4 = (int(self.bytes[index + 12]) << 8) | int(self.bytes[index + 13]) print " Command : result of control request (%d, %d, %d, %d)" % (data1, data2, data3, data4) logging.warning("Command received : control result (%d, %d, %d, %d)" % (data1, data2, data3, data4)) device_info.data1 = data1 # int data 1 device_info.data2 = data2 # int data 2 device_info.data3 = data3 # int data 3 device_info.data4 = data4 # int data 4 device_info.name = "User device" # name device_info.loc = "NA" # location device_info.time = int(time.time()) # timestamp # pass to callback function self.cmd_callback(device_info) # empty buffer self.bytes = list() else: # print ' Undefined command !!' logging.warning("Undefined command found!!") # resize buffer if len(self.bytes) > 0: del self.bytes[0] # End of parse function return
def parse(self, stream): self.stream = stream in_bytes = bytearray(stream) index = 0 # append packet to buffer for idx in range(len(in_bytes)): self.bytes.append(in_bytes[idx]) if len(self.bytes) < PACKET_LEN: continue # check packet validity at this index if (index + PACKET_LEN <= len(self.bytes) and self.bytes[index] == IN_START1 and self.bytes[index + 1] == IN_START2 and self.bytes[index + PACKET_LEN - 1] == IN_END): # start parsing # index+2 : category1, index+3 : category2, index+4 : ID, index+5 : command # index+6~index+7 : Data1, index+8~index+9 : Data2, ..., index+12~index+13 : Data4 # index+14 : End byte device_info = DeviceInfo() device_info.itemtype = 1 # 0: queue item type device_info.cat1 = int(self.bytes[index + 2]) # 1: cat1 device_info.cat2 = int(self.bytes[index + 3]) # 2: cat2 device_info.devid = int(self.bytes[index + 4]) # 3: device ID device_info.cmd = int(self.bytes[index + 5]) # 4: command # ping response if device_info.cmd == CMD_PING: #print ' Command : Ping response' logging.warning('Command received : Ping response') device_info.name = 'User device' # name device_info.loc = 'NA' # location device_info.time = int(time.time()) # timestamp # pass to callback function self.cmd_callback(device_info) # empty buffer self.bytes = list() # register device elif device_info.cmd == CMD_REGISTER: #print ' Command : Register device' logging.warning('Command received : Register device') device_info.cmd1 = int(self.bytes[index + 6]) # 5: available cmd 1 device_info.cmd1dtype = int( self.bytes[index + 7]) # 6: available cmd 1 - data type device_info.cmd2 = int(self.bytes[index + 8]) # 7: device_info.cmd2dtype = int(self.bytes[index + 9]) # 8: device_info.cmd3 = int(self.bytes[index + 10]) # 9: device_info.cmd3dtype = int(self.bytes[index + 11]) # 10: device_info.cmd4 = int( self.bytes[index + 12]) # 11: available cmd 4 device_info.cmd4dtype = int( self.bytes[index + 13]) # 12: available cmd 4 - data type device_info.name = 'User device' # 13: name device_info.loc = 'NA' # 14: location device_info.time = int(time.time()) # 15: timestamp # put in queue self.recv_queue.put(device_info) # empty buffer self.bytes = list() # update sensor value elif device_info.cmd == CMD_UPDATE: data1, data2, data3, data4 = 0x0000, 0x0000, 0x0000, 0x0000 # you have to check negative value if self.bytes[index + 6] & 0x80 == 0x80: data1 = int(self.bytes[index + 6] ^ 0xff) << 8 | int( self.bytes[index + 7] ^ 0xff) data1 *= -1 else: data1 = (int(self.bytes[index + 6]) << 8) | int( self.bytes[index + 7]) if self.bytes[index + 8] & 0x80 == 0x80: data2 = int(self.bytes[index + 8] ^ 0xff) << 8 | int( self.bytes[index + 9] ^ 0xff) data2 *= -1 else: data2 = (int(self.bytes[index + 8]) << 8) | int( self.bytes[index + 9]) if self.bytes[index + 10] & 0x80 == 0x80: data3 = int(self.bytes[index + 10] ^ 0xff) << 8 | int( self.bytes[index + 11] ^ 0xff) data3 *= -1 else: data3 = (int(self.bytes[index + 10]) << 8) | int( self.bytes[index + 11]) if self.bytes[index + 12] & 0x80 == 0x80: data4 = int(self.bytes[index + 12] ^ 0xff) << 8 | int( self.bytes[index + 13] ^ 0xff) data4 *= -1 else: data4 = (int(self.bytes[index + 12]) << 8) | int( self.bytes[index + 13]) #print ' Command : Update sensor value (%d, %d, %d, %d)' % (data1, data2, data3, data4) logging.warning( 'Command received : Update sensor value (%d, %d, %d, %d)' % (data1, data2, data3, data4)) device_info.data1 = data1 # 5: int data 1 device_info.data2 = data2 # 6: int data 2 device_info.data3 = data3 # 7: int data 3 device_info.data4 = data4 # 8: int data 4 device_info.name = 'User device' # 13: name device_info.loc = 'NA' # 14: location device_info.time = int(time.time()) # 15: timestamp # put in queue self.recv_queue.put(device_info) # empty buffer self.bytes = list() # control signal response elif device_info.cmd == CMD_CONTROL: data1, data2, data3, data4 = 0x0000, 0x0000, 0x0000, 0x0000 # you have to check negative value if self.bytes[index + 6] & 0x80 == 0x80: data1 = int(self.bytes[index + 6] ^ 0xff) << 8 | int( self.bytes[index + 7] ^ 0xff) data1 *= -1 else: data1 = (int(self.bytes[index + 6]) << 8) | int( self.bytes[index + 7]) if self.bytes[index + 8] & 0x80 == 0x80: data2 = int(self.bytes[index + 8] ^ 0xff) << 8 | int( self.bytes[index + 9] ^ 0xff) data2 *= -1 else: data2 = (int(self.bytes[index + 8]) << 8) | int( self.bytes[index + 9]) if self.bytes[index + 10] & 0x80 == 0x80: data3 = int(self.bytes[index + 10] ^ 0xff) << 8 | int( self.bytes[index + 11] ^ 0xff) data3 *= -1 else: data3 = (int(self.bytes[index + 10]) << 8) | int( self.bytes[index + 11]) if self.bytes[index + 12] & 0x80 == 0x80: data4 = int(self.bytes[index + 12] ^ 0xff) << 8 | int( self.bytes[index + 13] ^ 0xff) data4 *= -1 else: data4 = (int(self.bytes[index + 12]) << 8) | int( self.bytes[index + 13]) print ' Command : result of control request (%d, %d, %d, %d)' % ( data1, data2, data3, data4) logging.warning( 'Command received : control result (%d, %d, %d, %d)' % (data1, data2, data3, data4)) device_info.data1 = data1 # int data 1 device_info.data2 = data2 # int data 2 device_info.data3 = data3 # int data 3 device_info.data4 = data4 # int data 4 device_info.name = 'User device' # name device_info.loc = 'NA' # location device_info.time = int(time.time()) #timestamp # pass to callback function self.cmd_callback(device_info) # empty buffer self.bytes = list() else: #print ' Undefined command !!' logging.warning('Undefined command found!!') # resize buffer if len(self.bytes) > 0: del self.bytes[0] # End of parse function return
def load_devices(self): results = self.db.select_all_device() logging.warning('Load device info from DB: count = '+str(len(results))) for row in results: device_info = DeviceInfo() device_info.objtype = 1 # queue item type device_info.cat1 = int(row[1]) # cat1 device_info.cat2 = int(row[2]) # cat2 device_info.devid = int(row[3]) # device ID device_info.cmd = 0x11 # command (set dummy value) device_info.cmd1 = int(row[7]) # available cmd 1 device_info.cmd1dtype = int(row[11]) # available cmd 1 - data type device_info.cmd2 = int(row[8]) device_info.cmd2dtype = int(row[12]) device_info.cmd3 = int(row[9]) device_info.cmd3dtype = int(row[13]) device_info.cmd4 = int(row[10]) # available cmd 4 device_info.cmd4dtype = int(row[14]) # available cmd 4 - data type device_info.name = row[4] # name device_info.loc = row[5] # location device_info.time = int(row[6]) # timestamp self.add_device_to_list(device_info) return
def load_devices(self): results = self.db.select_all_device() logging.warning('Load device info from DB: count = ' + str(len(results))) for row in results: device_info = DeviceInfo() device_info.objtype = 1 # queue item type device_info.cat1 = int(row[1]) # cat1 device_info.cat2 = int(row[2]) # cat2 device_info.devid = int(row[3]) # device ID device_info.cmd = 0x11 # command (set dummy value) device_info.cmd1 = int(row[7]) # available cmd 1 device_info.cmd1dtype = int(row[11]) # available cmd 1 - data type device_info.cmd2 = int(row[8]) device_info.cmd2dtype = int(row[12]) device_info.cmd3 = int(row[9]) device_info.cmd3dtype = int(row[13]) device_info.cmd4 = int(row[10]) # available cmd 4 device_info.cmd4dtype = int(row[14]) # available cmd 4 - data type device_info.name = row[4] # name device_info.loc = row[5] # location device_info.time = int(row[6]) # timestamp self.add_device_to_list(device_info) return