1use super::{Request, RequestResponseType, Response, HEADER};
2use crate::messages::utils::types::{DateBCD, TimeWithoutSecondsBCD};
3use bincode::{Decode, Encode};
4
5#[derive(Encode, Request)]
6pub struct GetTimeProfileRequest {
7 header: u8,
8 message_type: u8,
9 _unused: u16,
10 device_id: u32,
11 profile_id: u8,
12}
13
14impl GetTimeProfileRequest {
15 pub fn new(device_id: u32, profile_id: u8) -> Self {
16 Self {
17 header: HEADER,
18 message_type: RequestResponseType::GetTimeProfile.into(),
19 _unused: 0,
20 device_id,
21 profile_id,
22 }
23 }
24}
25#[test]
26fn get_time_profile_request_to_bytes() {
27 let expected = [
28 0x17, 0x98, 0x00, 0x00, 0x2d, 0x55, 0x39, 0x19, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
29 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
30 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
31 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
32 0x00, 0x00, 0x00, 0x00,
33 ];
34
35 let r = GetTimeProfileRequest::new(423187757, 4);
36
37 let actual = r.to_bytes();
38 assert_eq!(expected, actual);
39}
40
41#[derive(Decode, Response, Debug)]
42pub struct GetTimeProfileResponse {
43 pub header: u8,
44 pub message_type: u8,
45 _unused: u16,
46 pub device_id: u32,
47 pub profile_id: u8,
48 pub from: DateBCD,
49 pub to: DateBCD,
50 pub monday: bool,
51 pub tuesday: bool,
52 pub wednesday: bool,
53 pub thursday: bool,
54 pub friday: bool,
55 pub saturday: bool,
56 pub sunday: bool,
57 pub segment1_start: TimeWithoutSecondsBCD,
58 pub segment1_end: TimeWithoutSecondsBCD,
59 pub segment2_start: TimeWithoutSecondsBCD,
60 pub segment2_end: TimeWithoutSecondsBCD,
61 pub segment3_start: TimeWithoutSecondsBCD,
62 pub segment3_end: TimeWithoutSecondsBCD,
63 pub linked_profile_id: u8,
64}
65
66#[test]
67fn get_time_profile_response_from_bytes() {
68 let bytes: [u8; 64] = [
69 0x17, 0x98, 0x00, 0x00, 0x2d, 0x55, 0x39, 0x19, 0x04, 0x20, 0x21, 0x04, 0x01, 0x20, 0x21,
70 0x12, 0x29, 0x01, 0x01, 0x00, 0x01, 0x00, 0x01, 0x01, 0x08, 0x30, 0x09, 0x45, 0x11, 0x35,
71 0x13, 0x15, 0x14, 0x01, 0x17, 0x59, 0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
72 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
73 0x00, 0x00, 0x00, 0x00,
74 ];
75
76 let r = GetTimeProfileResponse::from_bytes(&bytes).unwrap();
77 assert_eq!(r.message_type, RequestResponseType::GetTimeProfile.into());
78 assert_eq!(r.device_id, 423187757);
79 assert_eq!(r.profile_id, 4);
80 assert_eq!(r.from, DateBCD::new(2021, 4, 1));
81 assert_eq!(r.to, DateBCD::new(2021, 12, 29));
82 assert!(r.monday);
83 assert!(r.tuesday);
84 assert!(!r.wednesday);
85 assert!(r.thursday);
86 assert!(!r.friday);
87 assert!(r.saturday);
88 assert!(r.sunday);
89 assert_eq!(r.segment1_start, TimeWithoutSecondsBCD::new(8, 30));
90 assert_eq!(r.segment1_end, TimeWithoutSecondsBCD::new(9, 45));
91 assert_eq!(r.segment2_start, TimeWithoutSecondsBCD::new(11, 35));
92 assert_eq!(r.segment2_end, TimeWithoutSecondsBCD::new(13, 15));
93 assert_eq!(r.segment3_start, TimeWithoutSecondsBCD::new(14, 1));
94 assert_eq!(r.segment3_end, TimeWithoutSecondsBCD::new(17, 59));
95 assert_eq!(r.linked_profile_id, 19);
96}