async def power(self, value):
     message = OcfRequest(op='update',
                          to=dict(href='/power'),
                          cn=dict(value=value))
     result = await self.device.on_post_request(message)
     print(result.get('value'))
     message = OcfRequest(op='retrieve', to=dict(href='/power'))
     result = await self.device.on_get_request(message)
     print(result.get('value'))
     self.assertEqual(result['value'], value)
    async def test_power(self):
        value = True
        console_device = Device.init_from_file('Console', '4')
        console_task = await wait_run_device(console_device)

        bridge_device = Device.init_from_file('SerialBridgeToRadio', '3')
        bridge_task = await wait_run_device(bridge_device)

        self.config['/oic/con']['bridge']['anchor'] = bridge_device.link[
            'anchor']
        self.config['/oic/con']['bridge']['eps'] = bridge_device.link['eps']
        self.device = Device.init_from_config(self.config,
                                              path=self.config_path)
        device_task = await wait_run_device(self.device)

        # подписываемся на все данные приятые из эфира
        # message = OcfRequest(op='retrieve', fr=console_device.link, to=dict(href='/radio_msg'), obs=0)
        link = bridge_device.get_link('/radio_msg')
        await console_device.observe(link, console_device.on_update_console)
        message = OcfRequest(op='update',
                             to=dict(href='/curtain'),
                             cn=dict(openLevel=100))
        # while True:
        result = await self.device.on_post_request(message)
        # await asyncio.sleep(5)
        pass

        await asyncio.sleep(999)
 def test_set_light_baseline(self):
     self.device = Device.init_from_config(self.config)
     data = dict(value=True, brightness=100)
     message = OcfRequest(
         **dict(operation='update', uri_path=['light'], data=data))
     self.device.on_init()
     result = self.device.on_post_request(message)
     self.assertDictEqual(result, data)
    def test_get_light_brightness(self):
        self.device = Device.init_from_config(
            None, dict(handler=Device.__name__, data=self.config))
        message = OcfRequest(**dict(operation='get',
                                    uri='/light',
                                    query={'rt': ['oic.r.light.brightness']}))
        self.device.on_get_request(message)

        # self.devices.run()
        pass
 async def test_update_radio_msg(self):
     # raw = 'L8000;H4589;L1412;H353;L706;H706;L353;H353;L706;H706;L353;H706;L353;H353;L706;H706;L353;H706;L353;H706;L353;H353;L706;H706;L353;H706;L353;H706;L353;H706;L353;H706;L353;H706;L353;H706;L353;H353;L706;H706;L353;H353;L706;H706;L353;H706;L353;H706;L353;H706;L353;H353;L706;H353;L706;H353;L706;H353;L706;H706;L353;H353;L706;H353;L706;H706;L353;H353;L706;H353;L706;H353;L706;H706;L353;H353;L706;H353;L706;H353;L706;H706;L20000;'
     raw = '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;'
     cn = dict(raw=raw)
     bridge_device = Device.init_from_file('SerialBridgeToRadio', '3')
     bridge_task = await wait_run_device(bridge_device)
     message = OcfRequest(op='update', to=dict(href='/radio_msg'), cn=cn)
     result = await bridge_device.on_post_request(message)
     # self.assertEqual(result['openLevel'], 2)
     bridge_task.cancel()
     pass
Ejemplo n.º 6
0
    async def add_delete_device_from_update_running_device(
            self, virtual_server_class):
        device = virtual_server_class.init_from_config()
        device_task = asyncio.create_task(device.main())
        while device.get_param('/oic/mnt', 'status') == 'init':
            try:
                device_task.result()
            except asyncio.InvalidStateError:
                pass
            await asyncio.sleep(0.1)
        msg = OcfRequest(
            cn={
                "running_devices": [{
                    "dmno": "EchoDevice",
                    "n": "Test1"
                }, {
                    "dmno": "EchoDevice",
                    "n": "Test2"
                }]
            })
        await device.post_devices(msg)
        await asyncio.sleep(0.2)
        devices = device.get_param(*device.run_dev)
        self.assertEqual(len(devices), 2, 'count devices')

        # удаляем девайс
        msg = OcfRequest(cn={"running_devices": [devices[0]]})
        await device.post_devices(msg)
        await asyncio.sleep(0.1)
        devices = device.get_param(*device.run_dev)
        self.assertEqual(len(devices), 1, 'count devices')
        device_task.cancel()
        try:
            await device_task
        except asyncio.CancelledError:
            pass
 async def test_update_open_level(self):
     bridge_device = Device.init_from_file('SerialBridgeToRadio', '3')
     bridge_task = await wait_run_device(bridge_device)
     self.config['/oic/con']['bridge']['anchor'] = bridge_device.link[
         'anchor']
     self.config['/oic/con']['bridge']['eps'] = bridge_device.link['eps']
     self.device = Device.init_from_config(self.config,
                                           path=self.config_path)
     device_task = await wait_run_device(self.device)
     message = OcfRequest(op='update',
                          to=dict(href='/curtain'),
                          cn=dict(openLevel=30))
     result = await self.device.on_post_request(message)
     await asyncio.sleep(100)
     bridge_task.cancel()
     device_task.cancel()
 async def test_update_switch(self):
     value = False
     modbus_device = ModbusDevice.init_from_config(self.modbus_config,
                                                   path=self.config_path)
     modbus_task = await wait_run_device(modbus_device)
     self.config['/oic/con']['master'] = modbus_device.link
     self.device = Device.init_from_config(self.config,
                                           path=self.config_path)
     device_task = await wait_run_device(self.device)
     message = OcfRequest(op='update',
                          to=dict(href='/power'),
                          cn=dict(value=value))
     result = await self.device.on_post_request(message)
     self.assertEqual(result['value'], value)
     modbus_task.cancel()
     device_task.cancel()
     pass
 async def test_update_brightness(self):
     brightness = 0
     modbus_device = ModbusDevice.init_from_config(self.modbus_config,
                                                   path=self.config_path)
     modbus_task = await wait_run_device(modbus_device)
     self.config['/oic/con']['master'] = modbus_device.link
     self.device = Device.init_from_config(self.config,
                                           path=self.config_path)
     device_task = await wait_run_device(self.device)
     message = OcfRequest(op='update',
                          to=dict(href='/brightness'),
                          cn=dict(brightness=brightness))
     result = await self.device.on_post_request(message)
     self.assertEqual(result['brightness'], brightness)
     modbus_task.cancel()
     device_task.cancel()
     pass
Ejemplo n.º 10
0
 def test_retrieve(self):
     sender = {'href': '/light', 'eps': [{'ep': 'coap://127.0.0.1:1111'}]}
     receiver = {
         'href': '/oic/res',
         'eps': [{
             'ep': 'coap://127.0.0.1:2222'
         }]
     }
     request = OcfRequest(to=receiver,
                          fr=sender,
                          op='retrieve',
                          token=1,
                          mif=2,
                          **dict(query={'if': "oic.d"}))
     data = None
     answer = OcfResponse.generate_answer(data, request)
     pass
    async def test_retrieve_color(self):
        modbus_device = ModbusDevice.init_from_config(self.modbus_config,
                                                      path=self.config_path)
        modbus_task = await wait_run_device(modbus_device)
        self.config['/oic/con']['master'] = modbus_device.link
        self.device = Device.init_from_config(self.config,
                                              path=self.config_path)
        device_task = await wait_run_device(self.device)
        message = OcfRequest(op='retrieve', to=dict(href='/color'))
        result = await self.device.on_get_request(message)
        print(result)
        # self.assertEqual(result['color'], value)
        modbus_task.cancel()
        device_task.cancel()
        await modbus_task
        await device_task

        pass
    async def test_echo_post_message(self, **kwargs):
        device_task = await wait_run_device(self.device)
        # from aio_modbus_client.ModbusProtocolEcho import ModbusProtocolEcho
        pdu = '\x00\x02\x00\x01'
        # self.device.modbus = ModbusProtocolEcho({
        #     pdu: '\x01\x00\x01'
        # })

        data = dict(slave=0x78,
                    function=16,
                    pdu=OcfMessageRequest.b64decode(pdu.encode()),
                    answerSize=10,
                    baudRate=9600,
                    parity=0,
                    stopBits=2,
                    dataBits=8)
        message = OcfRequest(
            **dict(op='post', to=dict(href='/modbus_msg'), cn=data))
        result = await self.device.on_post_request(message)
        self.assertEqual(result, '\x01\x00\x01')
        pass
 async def test_local_discovery(self):
     msg = OcfRequest(**dict(uri_path=['oic', 'res'], operation='get'))
     res = self.device.on_get_request(msg)
     await asyncio.sleep(600)
     pass
 def test_get_light1_baseline(self):
     self.device = Device.init_from_config(
         None, dict(handler=Device.__name__, data=self.config))
     message = OcfRequest(**dict(operation='get', uri='/light'))
     result = self.device.on_get_request(message)
     pass