def gen_random_sub_response(): """ Generate a random subscription response packet. """ uid = random_uid() device_id = hibike_message.uid_to_device_id(uid) params = random_params(device_id) delay = random.randrange(100) msg = hibike_message.make_subscription_response( device_id, params, delay, uid) return (ParsingTests.encode_packet(msg), )
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))
def subscribe_all(self): """ Subscribe to all devices with all parameters. """ for uid in self.uids: dev_id = hibike_message.uid_to_device_id(uid) all_params = hibike_message.all_params_for_device_id(dev_id) readable_params = [] for param in all_params: if hibike_message.readable(dev_id, param): readable_params.append(param) self.pipe_to_child.send([ "subscribe_device", [uid, self.DEFAULT_DELAY, readable_params] ])
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))
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))
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))
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))
def gen_random_device_id_and_params(): """ Generate a random device ID and set of parameters. """ device_id = hibike_message.uid_to_device_id(random_uid()) params = random_params(device_id) return (device_id, params)