Example #1
0
	def startBroadcast(self, device: Device, uid: str, replyOnSiteId: str = ''):
		self._broadcastFlag.set()
		location = device.getMainLocation()
		while self._broadcastFlag.isSet():
			self._broadcastSocket.sendto(bytes(f'{self.Commons.getLocalIp()}:{self._listenPort}:{location.getSaveName()}:{uid}', encoding='utf8'), ('<broadcast>', self._broadcastPort))
			try:
				sock, address = self._listenSocket.accept()
				sock.settimeout(None)
				answer = sock.recv(1024).decode()

				deviceIp = answer.split(':')[0]

				device.pairingDone(uid=uid)
				self.logWarning(f'Device with uid {uid} successfully paired')
				if replyOnSiteId:
					self.MqttManager.say(text=self.TalkManager.randomTalk('newDeviceAdditionSuccess'), client=replyOnSiteId)

				self.ThreadManager.doLater(interval=5, func=self.WakewordRecorder.uploadToNewDevice, args=[uid])

				self._broadcastSocket.sendto(bytes('ok', encoding='utf8'), (deviceIp, self._broadcastPort))
				self.stopBroadcasting()
			except socket.timeout:
				self.logInfo('No device query received')
Example #2
0
    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()