Ejemplo n.º 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)
Ejemplo n.º 2
0
 def setUp(self):
     self.dummy_device = \
         serial.Serial(spawn_virtual_devices.spawn_device(self.DUMMY_DEVICE_TYPE))
     self.dummy_dev_id = hibike_message.device_name_to_id(
         self.DUMMY_DEVICE_TYPE)
     self.dummy_params = hibike_message.all_params_for_device_id(
         self.dummy_dev_id)
     # Wait for the device to become active
     time.sleep(1)
Ejemplo n.º 3
0
def read_param(uid, param):
    """
    Read a single device parameter.
    """
    if uid not in HIBIKE_INSTANCE.uids:
        abort(404)
    params = hibike_message.all_params_for_device_id(hibike_message.uid_to_device_id(uid))
    if param not in params:
        abort(404)
    return json.dumps({param: HIBIKE_INSTANCE.get_last_cached(uid, param)})
Ejemplo n.º 4
0
 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]])
Ejemplo n.º 5
0
def read_all_params(uid):
    """
    Read all parameters from a device.
    """
    if uid not in HIBIKE_INSTANCE.uids:
        abort(404)
    dev_id = hibike_message.uid_to_device_id(uid)
    all_params = hibike_message.all_params_for_device_id(dev_id)
    readable_params = [p for p in all_params if hibike_message.readable(dev_id, p)]
    param_values = {}
    for param in readable_params:
        param_values[param] = HIBIKE_INSTANCE.get_last_cached(uid, param)
    return json.dumps(param_values)
Ejemplo n.º 6
0
def write_params(uid):
    """
    Write values to a device.
    """
    if uid not in HIBIKE_INSTANCE.uids:
        abort(404)
    all_params = set(hibike_message.all_params_for_device_id(hibike_message.uid_to_device_id(uid)))
    params = request.get_json(force=True, cache=False)
    if params is None:
        abort(400)
    params_and_values = []
    for (param, value) in params.items():
        if param not in all_params:
            abort(400)
        params_and_values.append((param, value))
    HIBIKE_INSTANCE.write(uid, params_and_values)
    return "wrote value"
Ejemplo n.º 7
0
def get_device_info(uid):
    """
    Get a dictionary representing information about a device.
    """
    if uid not in HIBIKE_INSTANCE.uids:
        raise DeviceNotFoundError()
    device_id = hibike_message.uid_to_device_id(uid)
    device_name = hibike_message.device_id_to_name(device_id)
    params = hibike_message.all_params_for_device_id(device_id)
    json_map = {"device_name": device_name, "params": []}
    for param in params:
        readable = hibike_message.readable(device_id, param)
        writeable = hibike_message.writable(device_id, param)
        param_type = hibike_message.param_type(device_id, param)
        json_map["params"].append({"name": param,
                                   "readable": readable,
                                   "writeable": writeable,
                                   "type": param_type})
    return json_map
Ejemplo n.º 8
0
    hm.send(s, hm.make_ping())

while SERIALS:
    remaining = []
    for s in SERIALS:
        reading = hm.blocking_read(s)
        if reading:
            params, delay, device_type, year, id = struct.unpack(
                "<HHHBQ", reading.get_payload())
            uid = (device_type << 72) | (year << 64) | id
            DEVICES.append(HibikeDevice(s, params, delay, uid))
        else:
            remaining.append(s)
    SERIALS = remaining

print("devices:", DEVICES)

for device in DEVICES:
    hm.send(
        device.serial_port,
        hm.make_subscription_request(
            device.device_type,
            hm.all_params_for_device_id(device.device_type), 10))

while True:
    for device in DEVICES:
        reading = hm.blocking_read(device.serial_port)
        if reading:
            if reading.get_message_id() == hm.MESSAGE_TYPES["DeviceData"]:
                print(reading.get_payload())
Ejemplo n.º 9
0
def random_params(device_id):
    """ Get a random sampling of parameters for a particular DEVICE_TYPE. """
    all_params = hibike_message.all_params_for_device_id(device_id)
    sample_length = random.randrange(len(all_params) + 1)
    params = random.sample(all_params, sample_length)
    return params