def lockAction(self,lockAction): self._makeBLEConnection() keyturnerUSDIOHandle = self.device.get_handle("a92ee202-5501-11e4-916c-0800200c9a66") self.device.subscribe('a92ee202-5501-11e4-916c-0800200c9a66', self._handleCharWriteResponse, indication=True)) challengeReq = nuki_messages.Nuki_REQ('0004') challengeReqEncrypted = nuki_messages.Nuki_EncryptedCommand(authID=self.config.get(self.macAddress, 'authorizationID'), nukiCommand=challengeReq, publicKey=self.config.get(self.macAddress, 'publicKeyNuki'), privateKey=self.config.get(self.macAddress, 'privateKeyHex')) challengeReqEncryptedCommand = challengeReqEncrypted.generate() self._charWriteResponse = "" self.device.char_write_handle(keyturnerUSDIOHandle,challengeReqEncryptedCommand,True,4) print "Nuki CHALLENGE Request sent: %s" % challengeReq.show() time.sleep(2) commandParsed = self.parser.decrypt(self._charWriteResponse,self.config.get(self.macAddress, 'publicKeyNuki'),self.config.get(self.macAddress, 'privateKeyHex'))[8:] if self.parser.isNukiCommand(commandParsed) == False: sys.exit("Error while requesting Nuki CHALLENGE: %s" % commandParsed) commandParsed = self.parser.parse(commandParsed) if commandParsed.command != '0004': sys.exit("Nuki returned unexpected response (expecting Nuki CHALLENGE): %s" % commandParsed.show()) print "Challenge received: %s" % commandParsed.nonce lockActionReq = nuki_messages.Nuki_LOCK_ACTION() lockActionReq.createPayload(self.config.getint(self.macAddress, 'ID'), lockAction, commandParsed.nonce) lockActionReqEncrypted = nuki_messages.Nuki_EncryptedCommand(authID=self.config.get(self.macAddress, 'authorizationID'), nukiCommand=lockActionReq, publicKey=self.config.get(self.macAddress, 'publicKeyNuki'), privateKey=self.config.get(self.macAddress, 'privateKeyHex')) lockActionReqEncryptedCommand = lockActionReqEncrypted.generate() self._charWriteResponse = "" self.device.char_write_handle(keyturnerUSDIOHandle,lockActionReqEncryptedCommand,True,4) print "Nuki Lock Action Request sent: %s" % lockActionReq.show() time.sleep(2) commandParsed = self.parser.decrypt(self._charWriteResponse,self.config.get(self.macAddress, 'publicKeyNuki'),self.config.get(self.macAddress, 'privateKeyHex'))[8:] if self.parser.isNukiCommand(commandParsed) == False: sys.exit("Error while requesting Nuki Lock Action: %s" % commandParsed) commandParsed = self.parser.parse(commandParsed) if commandParsed.command != '000C' and commandParsed.command != '000E': sys.exit("Nuki returned unexpected response (expecting Nuki STATUS/STATES): %s" % commandParsed.show()) print "%s" % commandParsed.show()
def executeLockAction(self, keyturnerUSDIOHandle, lockAction, commandParsed): lockActionReq = nuki_messages.Nuki_LOCK_ACTION() lockActionReq.createPayload(self.config.getint(self.macAddress, 'ID'), lockAction, commandParsed.nonce) lockActionReqEncrypted = nuki_messages.Nuki_EncryptedCommand( authID=self.config.get(self.macAddress, 'authorizationID'), nukiCommand=lockActionReq, publicKey=self.config.get(self.macAddress, 'publicKeyNuki'), privateKey=self.config.get(self.macAddress, 'privateKeyHex')) lockActionReqEncryptedCommand = lockActionReqEncrypted.generate() self._charWriteResponse = "" self.device.char_write_handle(keyturnerUSDIOHandle, lockActionReqEncryptedCommand, True, 4) print("Nuki Lock Action Request sent: %s" % lockActionReq.show())