Пример #1
0
def main_interface():
    """
    Render the main interface page.
    """
    HIBIKE_INSTANCE.subscribe_all()
    from collections import namedtuple
    import time
    # Wait for Hibike to read from devices
    time.sleep(0.5)
    devices = []
    for uid in HIBIKE_INSTANCE.uids:
        dev_id = hibike_message.uid_to_device_id(uid)
        dev_name = hibike_message.uid_to_device_name(uid)
        all_params = hibike_message.all_params_for_device_id(dev_id)
        params_list = []
        for param_name in all_params:
            param = namedtuple(
                "Parameter",
                ("name", "type", "readable", "writeable", "init_value"))
            param.name = param_name
            param.type = hibike_message.param_type(dev_id, param_name)
            param.writeable = hibike_message.writable(dev_id, param_name)
            param.readable = hibike_message.readable(dev_id, param_name)
            param.init_value = HIBIKE_INSTANCE.get_last_cached(uid, param_name)
            params_list.append(param)
        params_list.sort(key=lambda p: p.name)
        device = namedtuple("Device", ("uid", "params", "name"))
        device.uid = uid
        device.params = params_list
        device.name = dev_name
        devices.append(device)
    return render_template("index.html", devices=devices)
Пример #2
0
    def __init__(self, uid, event_loop, verbose=False):
        self.uid = uid
        self.event_loop = event_loop
        self._ready = asyncio.Event(loop=event_loop)
        self.serial_buf = bytearray()
        self.read_queue = asyncio.Queue(loop=event_loop)
        self.verbose = verbose

        self.update_time = 0
        self.delay = 0
        self.transport = None

        self.response_map = {
            hm.MESSAGE_TYPES["Ping"]:
            self._process_ping,
            hm.MESSAGE_TYPES["SubscriptionRequest"]:
            self._process_sub_request,
            hm.MESSAGE_TYPES["DeviceRead"]:
            self._process_device_read,
            hm.MESSAGE_TYPES["DeviceWrite"]:
            self._process_device_write,
            hm.MESSAGE_TYPES["Disable"]:
            self._process_disable,
            hm.MESSAGE_TYPES["HeartBeatResponse"]:
            self._process_heartbeat_response,
        }
        self.param_values = DEFAULT_VALUES[hm.uid_to_device_name(uid)]
        self.subscribed_params = set()

        event_loop.create_task(self.process_messages())
        event_loop.create_task(self.send_subscribed_params())
        event_loop.create_task(self.request_heartbeats())
Пример #3
0
 def get_uids_and_types(self):
     """
     Returns a list of tuples of all of the uids of all devices that the
     HibikeCommunicator talks to. Tuple structure: (uid, device type name).
     """
     return [(uid, hibike_message.uid_to_device_name(uid))
             for uid in self.uids]
Пример #4
0
 def __repr__(self):
     res = ""
     res += hm.uid_to_device_name(self.uid)
     res += ":\n"
     res += "    %d" % self.uid
     res += "    %s\n" % self.serial_port.port
     res += "    %s\n" % self.data
     return res
Пример #5
0
 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))