def changeLocation(self, device: Device, locationId: int): # check location is good loc = self.LocationManager.getLocation(locId=locationId) if not loc: raise Exception("Location not found") # check location but not global self.assertDeviceTypeAllowedAtLocation(typeId=device.getDeviceType().id, locationId=locationId, moveDevice=True) # update device and trigger device type dependent Updates # might raise exception and cancle DB update device.changeLocation(locationId=locationId) # update DB self.DatabaseManager.update(tableName=self.DB_DEVICE, callerName=self.name, values={'locationID': locationId}, row=('id', device.id))
def startTasmotaFlashingProcess(self, device: Device, replyOnSiteId: str, session: DialogSession) -> bool: replyOnSiteId = self.MqttManager.getDefaultSiteId(replyOnSiteId) if session: self.ThreadManager.doLater( interval=0.5, func=self.MqttManager.endDialog, args=[ session.sessionId, self.randomTalk('connectESPForFlashing') ]) elif replyOnSiteId: self.ThreadManager.doLater( interval=0.5, func=self.MqttManager.say, args=[self.randomTalk('connectESPForFlashing')]) self._broadcastFlag.set() binFile = Path('tasmota.bin') if binFile.exists(): binFile.unlink() try: tasmotaConfigs = TasmotaConfigs( deviceType=device.getDeviceType().ESPTYPE, uid='dummy') req = requests.get(tasmotaConfigs.getTasmotaDownloadLink()) with binFile.open('wb') as file: file.write(req.content) self.logInfo('Downloaded tasmota.bin') except Exception as e: self.logError(f'Something went wrong downloading tasmota.bin: {e}') self._broadcastFlag.clear() return False self.ThreadManager.newThread(name='flashThread', target=self.doFlashTasmota, args=[device, replyOnSiteId]) return True
def doFlashTasmota(self, device: Device, replyOnSiteId: str): port = self.DeviceManager.findUSBPort(timeout=60) if not port: if replyOnSiteId: self.MqttManager.say(text=self.TalkManager.randomTalk( 'noESPFound', skill='Tasmota'), client=replyOnSiteId) self._broadcastFlag.clear() return if replyOnSiteId: self.MqttManager.say(text=self.TalkManager.randomTalk( 'usbDeviceFound', skill='AliceCore'), client=replyOnSiteId) try: mac = ESPLoader.detect_chip(port=port, baud=115200).read_mac() mac = ':'.join([f'{x:02x}' for x in mac]) cmd = [ '--port', port, '--baud', '115200', '--after', 'no_reset', 'write_flash', '--flash_mode', 'dout', '0x00000', 'tasmota.bin', '--erase-all' ] esptool.main(cmd) except Exception as e: self.logError(f'Something went wrong flashing esp device: {e}') if replyOnSiteId: self.MqttManager.say(text=self.TalkManager.randomTalk( 'espFailed', skill='Tasmota'), client=replyOnSiteId) self._broadcastFlag.clear() return self.logInfo('Tasmota flash done') if replyOnSiteId: self.MqttManager.say(text=self.TalkManager.randomTalk( 'espFlashedUnplugReplug', skill='Tasmota'), client=replyOnSiteId) found = self.DeviceManager.findUSBPort(timeout=60) if found: if replyOnSiteId: self.MqttManager.say(text=self.TalkManager.randomTalk( 'espFoundReadyForConf', skill='Tasmota'), client=replyOnSiteId) time.sleep(10) uid = self.DeviceManager.getFreeUID(mac) tasmotaConfigs = TasmotaConfigs( deviceType=device.getDeviceType().ESPTYPE, uid=uid) confs = tasmotaConfigs.getBacklogConfigs( device.getMainLocation().getSaveName()) if not confs: self.logError( 'Something went wrong getting tasmota configuration') if replyOnSiteId: self.MqttManager.say(text=self.TalkManager.randomTalk( 'espFailed', skill='Tasmota'), client=replyOnSiteId) else: ser = serial.Serial() ser.baudrate = 115200 ser.port = port ser.open() try: for group in confs: command = ';'.join(group['cmds']) if len(group['cmds']) > 1: command = f'Backlog {command}' arr = list() if len(command) > 50: while len(command) > 50: arr.append(command[:50]) command = command[50:] arr.append(f'{command}\r\n') else: arr.append(f'{command}\r\n') for piece in arr: ser.write(piece.encode()) self.logInfo('Sent {}'.format( piece.replace('\r\n', ''))) time.sleep(0.5) time.sleep(group['waitAfter']) ser.close() self.logInfo('Tasmota flashing and configuring done') if replyOnSiteId: self.MqttManager.say(text=self.TalkManager.randomTalk( 'espFlashingDone', skill='Tasmota'), client=replyOnSiteId) # setting the uid marks the addition as complete device.pairingDone(uid=uid) self._broadcastFlag.clear() except Exception as e: self.logError( f'Something went wrong writting configuration to esp device: {e}' ) if replyOnSiteId: self.MqttManager.say(text=self.TalkManager.randomTalk( 'espFailed', skill='Tasmota'), client=replyOnSiteId) self._broadcastFlag.clear() ser.close() else: if replyOnSiteId: self.MqttManager.say(text=self.TalkManager.randomTalk( 'espFailed', skill='Tasmota'), client=replyOnSiteId) self._broadcastFlag.clear()