Exemplo n.º 1
0
def _can2mqtt_ping(can_frame):
    """ Parse HomeCan CAN frame containing ping message
    """
    node_id = Node.can_decode(can_frame.arbitration_id)
    device_id = Device.can_decode(can_frame.arbitration_id)
    msg = Message.can_decode(can_frame.arbitration_id)
    op = Operation.can_decode(can_frame.arbitration_id)
    if msg == Message.PING:
        raw_payload, = unpack('<H', can_frame.data)
        payload = '0x{:4X}'.format(raw_payload)
    else:
        raise HomeCanMessageNotSupported('can message {} type not '
                'supported by {}'.format(msg.name,
                    sys._getframe().f_code.co_name))
    return Mqtt.message('NODE/{:X}/{}/{:X}/{}'.format(
                node_id, msg.name, device_id, op.name),
            payload)
Exemplo n.º 2
0
def _can2mqtt_cover(can_frame):
    """ Parse HomeCan CAN frame containing cover message
    """
    node_id = Node.can_decode(can_frame.arbitration_id)
    device_id = Device.can_decode(can_frame.arbitration_id)
    msg = Message.can_decode(can_frame.arbitration_id)
    op = Operation.can_decode(can_frame.arbitration_id)
    if msg == Message.COVER:
        cmd_raw, position, = unpack('<BB', can_frame.data)
        cmd = Cover(cmd_raw).name
        payload = json.dumps({"cmd": cmd, "position": position})
    else:
        raise HomeCanMessageNotSupported('can message {} type not '
                'supported by {}'.format(msg.name,
                    sys._getframe().f_code.co_name))
    return Mqtt.message('NODE/{:X}/{}/{:X}/{}'.format(
                node_id, msg.name, device_id, op.name),
            payload)
Exemplo n.º 3
0
def _can2mqtt_digital_output(can_frame):
    """ Parse HomeCan CAN frame containing digital output message
    """
    node_id = Node.can_decode(can_frame.arbitration_id)
    device_id = Device.can_decode(can_frame.arbitration_id)
    msg = Message.can_decode(can_frame.arbitration_id)
    op = Operation.can_decode(can_frame.arbitration_id)
    if msg == Message.DIGITAL_OUTPUT:
        cmd_raw, = unpack('<B', can_frame.data)
        cmd = DigitalOutput(cmd_raw).name
        payload = bytes(json.dumps({"state": cmd}), 'utf-8')
    else:
        raise HomeCanMessageNotSupported('can message {} type not '
                'supported by {}'.format(msg.name,
                    sys._getframe().f_code.co_name))
    return Mqtt.message('NODE/{:X}/{}/{:X}/{}'.format(
                node_id, msg.name, device_id, op.name),
            payload)
Exemplo n.º 4
0
def _can2mqtt_key(can_frame):
    """ Parse HomeCan CAN frame containing key message
    """
    node_id = Node.can_decode(can_frame.arbitration_id)
    device_id = Device.can_decode(can_frame.arbitration_id)
    msg = Message.can_decode(can_frame.arbitration_id)
    op = Operation.can_decode(can_frame.arbitration_id)
    if msg == Message.KEY:
        keycode, key_action_raw, ar_count, mp_count = unpack('<BBBB', can_frame.data)
        key_action = KeyAction(key_action_raw).name
        payload = json.dumps({"keycode": keycode, "action": key_action,
                             "ar_count": ar_count, "mp_count": mp_count})
    else:
        raise HomeCanMessageNotSupported('can message {} type not '
                'supported by {}'.format(msg.name,
                    sys._getframe().f_code.co_name))
    return Mqtt.message('NODE/{:X}/{}/{:X}/{}'.format(
                node_id, msg.name, device_id, op.name),
            payload)
Exemplo n.º 5
0
def _can2mqtt_datetime(can_frame):
    """ Parse HomeCan CAN frame containing date/time message
    """
    node_id = Node.can_decode(can_frame.arbitration_id)
    device_id = Device.can_decode(can_frame.arbitration_id)
    msg = Message.can_decode(can_frame.arbitration_id)
    op = Operation.can_decode(can_frame.arbitration_id)
    if msg == Message.DATETIME:
        year, month, day, hour, minute, second, weekday = unpack('<HBBBBBB', can_frame.data)
        payload = json.dumps({"year": year, "month": month, "day": day,
                             "hour": hour, "minute": minute, "second": second,
                             "dayofweek": weekday})
    else:
        raise HomeCanMessageNotSupported('can message {} type not '
                'supported by {}'.format(msg.name,
                    sys._getframe().f_code.co_name))
    return Mqtt.message('NODE/{:X}/{}/{:X}/{}'.format(
                node_id, msg.name, device_id, op.name),
            payload)
Exemplo n.º 6
0
def _can2mqtt_simple_sensor_report(can_frame):
    """ Parse HomeCan CAN frame containing simple sensor message
    """
    node_id = Node.can_decode(can_frame.arbitration_id)
    device_id = Device.can_decode(can_frame.arbitration_id)
    msg = Message.can_decode(can_frame.arbitration_id)
    op = Operation.can_decode(can_frame.arbitration_id)
    if msg in [Message.TEMPERATURE, Message.RHUMIDITY]:
        raw_payload, = unpack('<f', can_frame.data)
        payload = '{:0.2f}'.format(raw_payload)
    elif msg in [Message.ILLUMINANCE, Message.PRESSURE]:
        raw_payload, = unpack('<H', can_frame.data)
        payload = '{:d}'.format(raw_payload)
    else:
        raise HomeCanMessageNotSupported('can message {} type not '
                'supported by {}'.format(msg.name,
                    sys._getframe().f_code.co_name))
    return Mqtt.message('NODE/{:X}/{}/{:X}/{}'.format(
                node_id, msg.name, device_id, op.name),
            payload)
Exemplo n.º 7
0
def _can2mqtt_dust(can_frame):
    """ Parse HomeCan CAN frame containing data from dust sensors
        data[0-3] - data: pm2.5
        data[4-7] - data: pm10
    """
    node_id = Node.can_decode(can_frame.arbitration_id)
    device_id = Device.can_decode(can_frame.arbitration_id)
    msg = Message.can_decode(can_frame.arbitration_id)
    ##
    op = Operation.can_decode(can_frame.arbitration_id)
    if op != Operation.STATE:
        raise HomeCanBridgingForbidden('operation {} not supported for {} '
                                       'messages'.format(op.name, msg.name))
    ##
    pm2_5, pm10, = unpack('<ff', can_frame.data)
    ##
    return Mqtt.message(
        'NODE/{:X}/{}/{:X}/{}'.format(node_id, msg.name, device_id,
                                      Operation.STATE.name),
        bytes(json.dumps({
            "pm2.5": round(pm2_5, 2),
            "pm10": round(pm10, 2)
        }), 'utf-8'))
Exemplo n.º 8
0
def _can2mqtt_pca963x(can_frame):
    """ Parse HomeCan CAN frame containing commands for PCA963x modules
        data[0] - channel: red, green, blue, amber, rgb, rgba
        data[1] - command: set, brightness, sleep
        data[2-x] - color, brightness, time, etc.
    """
    node_id = Node.can_decode(can_frame.arbitration_id)
    device_id = Device.can_decode(can_frame.arbitration_id)
    msg = Message.can_decode(can_frame.arbitration_id)
    op = Operation.can_decode(can_frame.arbitration_id)
    ##
    ch = PCA963x.Channel(can_frame.data[0])
    cmd = PCA963x.Command(can_frame.data[1])
    ##
    if ch in [
            PCA963x.Channel.RED0, PCA963x.Channel.GREEN0,
            PCA963x.Channel.BLUE0, PCA963x.Channel.AMBER0,
            PCA963x.Channel.RED1, PCA963x.Channel.GREEN1,
            PCA963x.Channel.BLUE1, PCA963x.Channel.AMBER1
    ]:
        ##
        if cmd in [
                PCA963x.Command.OFF, PCA963x.Command.ON, PCA963x.Command.TOGGLE
        ]:
            payload = cmd.name
            return Mqtt.message(
                'NODE/{:X}/{}/{:X}/{}/{}/{}'.format(node_id, msg.name,
                                                    device_id, ch.name,
                                                    'SWITCH', op.name),
                payload)
        elif cmd == PCA963x.Command.PWM_VALUE:
            payload = '{:X}'.format(can_frame.data[2])
            return Mqtt.message(
                'NODE/{:X}/{}/{:X}/{}/{}/{}'.format(node_id, msg.name,
                                                    device_id, ch.name,
                                                    cmd.name, op.name),
                payload)
        elif cmd == PCA963x.Command.PWM_BRIGHTNESS:
            payload = '{:X}'.format(can_frame.data[2])
            return Mqtt.message(
                'NODE/{:X}/{}/{:X}/{}/{}/{}'.format(node_id, msg.name,
                                                    device_id, ch.name,
                                                    cmd.name, op.name),
                payload)
        elif cmd == PCA963x.Command.PWM_SLEEP:
            payload = '{:X}'.format(can_frame.data[2] +
                                    (can_frame.data[3] << 8))
            return Mqtt.message(
                'NODE/{:X}/{}/{:X}/{}/{}/{}'.format(node_id, msg.name,
                                                    device_id, ch.name,
                                                    cmd.name, op.name),
                payload)
    elif ch in [PCA963x.Channel.RGB0, PCA963x.Channel.RGB1]:
        ##
        if cmd == PCA963x.Command.PWM_VALUE:
            payload = '{:X},{:X},{:X}'.format(can_frame.data[2],
                                              can_frame.data[3],
                                              can_frame.data[4])
            return Mqtt.message(
                'NODE/{:X}/{}/{:X}/{}/{}/{}'.format(node_id, msg.name,
                                                    device_id, ch.name,
                                                    cmd.name, op.name),
                payload)
        elif cmd == PCA963x.Command.PWM_BRIGHTNESS:
            payload = '{:X}'.format(can_frame.data[2])
            return Mqtt.message(
                'NODE/{:X}/{}/{:X}/{}/{}/{}'.format(node_id, msg.name,
                                                    device_id, ch.name,
                                                    cmd.name, op.name),
                payload)
        elif cmd == PCA963x.Command.PWM_SLEEP:
            payload = '{:X}'.format(can_frame.data[2] +
                                    (can_frame.data[3] << 8))
            return Mqtt.message(
                'NODE/{:X}/{}/{:X}/{}/{}/{}'.format(node_id, msg.name,
                                                    device_id, ch.name,
                                                    cmd.name, op.name),
                payload)

    raise HomeCanMessageNotSupported('pca963x message configuration not '
                                     'supported by {}'.format(
                                         sys._getframe().f_code.co_name))