コード例 #1
0
ファイル: virtual_device.py プロジェクト: pioneers/PieCentral
 async def request_heartbeats(self):
     """Request heartbeats on a regular basis."""
     await self._ready.wait()
     while not self.transport.is_closing():
         await asyncio.sleep(self.HEARTBEAT_DELAY_MS / 1000,
                             loop=self.event_loop)
         hm.send(self.transport, hm.make_heartbeat_request())
コード例 #2
0
ファイル: virtual_device.py プロジェクト: pioneers/PieCentral
 def _process_sub_request(self, msg):
     """Respond to a subscription request with an appropriate response."""
     self.update_time = time.time()
     dev_id = hm.uid_to_device_id(self.uid)
     self.verbose_log("Subscription request received")
     params, delay = struct.unpack("<HH", msg.get_payload())
     subscribed_params = hm.decode_params(dev_id, params)
     hm.send(
         self.transport,
         hm.make_subscription_response(dev_id, subscribed_params, delay,
                                       self.uid))
     self.delay = delay
     self.subscribed_params.update(set(subscribed_params))
コード例 #3
0
ファイル: virtual_device.py プロジェクト: pioneers/PieCentral
    def _process_device_read(self, msg):
        self.verbose_log("Device read received")
        device_id = hm.uid_to_device_id(self.uid)
        # Send a device data with the requested param and value tuples
        params, = struct.unpack("<H", msg.get_payload())
        read_params = hm.decode_params(device_id, params)
        read_data = []

        for param in read_params:
            if not (hm.readable(device_id, param)
                    and param in self.param_values):
                raise ValueError(
                    "Tried to read unreadable parameter {}".format(param))
            read_data.append((param, self.param_values[param]))
        hm.send(self.transport, hm.make_device_data(device_id, read_data))
コード例 #4
0
 def test_read_device_data(self):
     """ Check that DeviceData packets are received and decoded. """
     start_time = time.time()
     hibike_message.send(
         self.dummy_device,
         hibike_message.make_subscription_request(self.dummy_dev_id,
                                                  self.dummy_params, 40))
     for packet in hibike_message.blocking_read_generator(
             self.dummy_device):
         if packet.get_message_id(
         ) == hibike_message.MESSAGE_TYPES["DeviceData"]:
             return
         self.assertLessEqual(
             time.time() - start_time, self.STOP_TIMEOUT,
             "did not read DeviceData packet before timeout")
コード例 #5
0
 def test_stop_event(self):
     """
     Make sure that triggering the stop event prevents
     additional reads
     """
     hibike_message.send(
         self.dummy_device,
         hibike_message.make_subscription_request(self.dummy_dev_id,
                                                  self.dummy_params, 40))
     start_time = time.time()
     stop_event = threading.Event()
     for _ in hibike_message.blocking_read_generator(
             self.dummy_device, stop_event):
         stop_event.set()
         self.assertLessEqual(
             time.time() - start_time, self.STOP_TIMEOUT,
             "blocking_read_generator didn't stop when stop_event triggered"
         )
コード例 #6
0
ファイル: virtual_device.py プロジェクト: pioneers/PieCentral
 async def send_subscribed_params(self):
     """Send values of subscribed parameters at a regular interval."""
     await self._ready.wait()
     device_id = hm.uid_to_device_id(self.uid)
     while not self.transport.is_closing():
         await asyncio.sleep(0.005, loop=self.event_loop)
         if self.update_time != 0 and self.delay != 0:
             if time.time() - self.update_time >= self.delay * 0.001:
                 # If the time equal to the delay has elapsed since the previous device data,
                 # send a device data with the device id
                 # and the device's subscribed params and values
                 data = []
                 for param in self.subscribed_params:
                     data.append((param, self.param_values[param]))
                 hm.send(self.transport,
                         hm.make_device_data(device_id, data))
                 self.update_time = time.time()
                 self.verbose_log("Regular data update sent from {}",
                                  hm.uid_to_device_name(self.uid))
コード例 #7
0
ファイル: virtual_device.py プロジェクト: pioneers/PieCentral
    def _process_device_write(self, msg):
        # Write to requested parameters
        # and return the values of the parameters written to using a device data
        self.verbose_log("Device write received")
        device_id = hm.uid_to_device_id(self.uid)
        write_params_and_values = hm.decode_device_write(msg, device_id)

        for (param, value) in write_params_and_values:
            if not (hm.writable(device_id, param)
                    and param in self.param_values):
                raise ValueError(
                    "Tried to write read-only parameter: {}".format(param))
            self.param_values[param] = value

        updated_params = []
        for (param, value) in write_params_and_values:
            if hm.readable(device_id, param):
                updated_params.append((param, value))
        hm.send(self.transport, hm.make_device_data(device_id, updated_params))
コード例 #8
0
ファイル: virtual_device.py プロジェクト: pioneers/PieCentral
 def _process_ping(self, msg):
     """Respond to a ping packet."""
     self.verbose_log("Ping received")
     dev_id = hm.uid_to_device_id(self.uid)
     hm.send(self.transport,
             hm.make_subscription_response(dev_id, [], 0, self.uid))
コード例 #9
0
 def encode_packet(packet):
     """ Turn a HibikeMessage into an encoded packet. """
     fake_port = FakeSerialPort()
     hibike_message.send(fake_port, packet)
     return fake_port.drain()