uhppote-rs 0.1.0

Rust bindings for the UHPPOTE library
Documentation
use super::{Request, RequestResponseType, Response, HEADER};
use bincode::{Decode, Encode};

#[derive(Encode, Request)]
pub struct GetDoorControlStateRequest {
    header: u8,
    message_type: u8,
    _unused: u16,
    device_id: u32,
    door: u8,
}

impl GetDoorControlStateRequest {
    pub fn new(device_id: u32, door: u8) -> Self {
        GetDoorControlStateRequest {
            header: HEADER,
            message_type: RequestResponseType::GetDoorControlState.into(),
            _unused: 0,
            device_id,
            door,
        }
    }
}

#[test]
fn get_door_control_state_request_to_bytes() {
    let expected = [
        0x17, 0x82, 0x00, 0x00, 0x2d, 0x55, 0x39, 0x19, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00,
    ];

    let r = GetDoorControlStateRequest::new(423187757, 4);

    let actual = r.to_bytes();
    assert_eq!(expected, actual);
}

#[derive(Decode, Response, Debug)]
pub struct GetDoorControlStateResponse {
    pub header: u8,
    pub message_type: u8,
    _unused: u16,
    pub device_id: u32,
    pub door: u8,
    pub control_state: u8,
    pub delay: u8,
}

#[test]
fn get_door_control_state_response_from_bytes() {
    let bytes: [u8; 64] = [
        0x17, 0x82, 0x00, 0x00, 0x2d, 0x55, 0x39, 0x19, 0x04, 0x02, 0x05, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00,
    ];

    let r = GetDoorControlStateResponse::from_bytes(&bytes).unwrap();
    assert_eq!(
        r.message_type,
        RequestResponseType::GetDoorControlState.into()
    );
    assert_eq!(r.device_id, 423187757);
    assert_eq!(r.door, 4);
    assert_eq!(r.control_state, 2);
    assert_eq!(r.delay, 5);
}