async def test_receiver(self):
        signatures = [
            BinarySignal.load_signature_from_file(
                '../src/bubot/devices/DooyaCurtain433/signature.json'),
        ]
        await self.open_port()
        while True:
            data = await self.reader.readline()
            data = data.decode()
            pass
            try:
                signal = BinarySignal()
                try:
                    res = signal.decode(data)
                except KeyError:
                    continue
                except Exception as e:
                    print('signal decode err', e)
                    continue
                if res:
                    found = False
                    for signature in signatures:
                        res2 = signal.detect_signal(signature)
                        if res2:
                            found = True
                            print(res2.n, res2.cmd, res2.di)
                    if not found:
                        # print('{bit_length};{data:x}'.format(data=res, bit_length=signal.bit_length))
                        print('{data:x}'.format(data=res,
                                                bit_length=signal.bit_length))
                else:
                    print(data)

            except KeyError as e:
                print('err', str(e), data)
    async def signal_handler(self, raw):
        self.log.debug('signal decoding: {}'.format(raw))
        try:
            signal = BinarySignal()
            try:
                data = signal.decode(raw)
                self.log.debug('signal decoded: {}'.format(data))
                detect = []
                for driver in self.radio_drivers:
                    try:
                        _driver = self.radio_drivers[driver][
                            'driver'].detect_signal(signal)
                        if _driver:
                            detect.append(_driver)
                    except Exception as e:
                        continue
                if len(detect) > 1:
                    self.log.warning('multi detect for signal {}'.format(data))
                if detect:
                    signal = detect[0]

                await self.notify('/radio_msg', signal.props)
            except KeyError as e:
                self.log.warning('{0} (signal: {1})'.format(str(e), raw))
            except Exception as e:
                self.log.error('signal decode: {0} (signal: {1})'.format(
                    str(e), raw))
        except KeyError as e:
            self.log.warning('{0} (signal: {1})'.format(str(e), raw))
 def test_signal_decode_encode_dooya(self):
     # data = 'L8000;H4724;L1433;H375;L695;H735;L348;H370;L703;H726;L353;H718;L366;H352;L717;H720;L359;H716;L360;H714;L363;H353;L718;H716;L366;H354;L713;H720;L361;H714;L368;H707;L364;H710;L369;H706;L370;H348;L719;H715;L368;H351;L718;H716;L363;H713;L364;H709;L369;H708;L367;H350;L720;H359;L720;H357;L712;H366;L714;H717;L361;H357;L712;H366;L711;H719;L358;H362;L715;H717;L362;H356;L711;H722;L360;H357;L715;H720;L363;H356;L713;H720;'
     data = 'L8000;H4730;L1433;H355;L710;H710;L355;H355;L710;H710;L355;H710;L355;H355;L710;H710;L355;H710;L355;H710;L355;H355;L710;H710;L355;H355;L710;H710;L355;H710;L355;H710;L355;H710;L355;H710;L355;H355;L710;H710;L355;H355;L710;H710;L355;H710;L355;H710;L355;H710;L355;H355;L710;H355;L710;H355;L710;H355;L710;H710;L355;H355;L710;H355;L710;H710;L355;H355;L710;H355;L710;H355;L710;H710;L355;H355;L710;H355;L710;H355;L710;H710;L2000;'
     signal = BinarySignal()
     data = signal.decode(data)
     res = DooyaCurtain433.detect_signal(signal)
     self.assertEqual(res.n, 'Dooya DC1651')
     signal2 = BinarySignal(di=res.di, cmd=res.cmd, n=res.n)
     raw = signal2.encode(DooyaCurtain433.signature)
     self.assertEqual(signal2.data, data)
     pass
    async def test_learn(self):
        result = []
        index = dict()
        learn = dict()
        hist = []
        i = 0
        max = 20
        await self.open_port()
        while True:
            data = await self.reader.readline()
            data = data.decode()
            pass
            try:
                signal = BinarySignal()
                try:
                    res = signal.decode(data)
                    if res:
                        if res not in index:
                            result.append(
                                dict(data=data, count=0, bit_length=[]))
                            index[res] = i
                            i += 1
                        j = index[res]
                        result[j]['count'] += 1
                        result[j]['bit_length'].append(signal.bit_length)
                        print(i, result[j]['count'], signal.bit_length,
                              '{0:x}'.format(res))
                        if result[index[res]]['count'] == max:
                            break
                except KeyError:
                    continue
                except Exception as e:
                    print('signal decode err', e)
                    continue

            except KeyError as e:
                print('err', str(e), data)

        learn = sorted(learn, key=lambda x: x['count'] - max)
        i = 0
        print(learn[i]['count'], learn[i]['data'],
              sum(learn[i]['bit_length']) / len(learn[i]['bit_length']))
        i = 1
        print(learn[i]['count'], learn[i]['data'],
              sum(learn[i]['bit_length']) / len(learn[i]['bit_length']))
 def test_detect_interval(self):
     source = 'L12000;H331;L8751;H4729;L1471;H333;L717;H712;L387;H326;L727;H716;L351;H724;L381;H333;L727;H700;L376;H712;L361;H707;L374;H347;L733;H699;L374;H705;L362;H699;L408;H677;L407;H656;L403;H683;L379;H686;L433;H291;L743;H703;L366;H336;L751;H677;L415;H664;L417;H681;L401;H675;L413;H293;L742;H322;L763;H327;L726;H339;L754;H695;L381;H329;L760;H312;L743;H696;L413;H292;L769;H686;L375;H327;L737;H696;L391;H337;L754;H671;L374;H329;L793;H659;L12000;'
     signal = BinarySignal()
     result = signal.decode(source)
     result1 = signal.detect()
     pass
 def test_signal_decode(self):
     data = 'L8000;H4139;L38;H658;L171;H266;L171;H989;L216;H256;L216;H402;L270;H1413;L198;H2528;\r\n'
     signal = BinarySignal()
     res = signal.decode(data)
     pass