-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconf.rs
executable file
·168 lines (148 loc) · 6.71 KB
/
conf.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
use serde::{Deserialize, Serialize};
use log::{info, trace, warn, error};
use chrono::prelude::*;
const ALLOWED_PINS: [u8; 25] = [2,3,4,17,27,22,10,9,11,5,6,13,19,26,18,23,24,25,8,7,1,12,16,20,21];
/*
* This file contains the struct equivalents of the json object in the config file
* For more info see ConfigSchema.json
*/
#[derive(Serialize, Deserialize)]
pub struct HolidaySeason {
pub name: String,
pub start_date: String,
pub end_date: String
}
#[derive(Serialize, Deserialize)]
pub struct Holiday {
pub name: String,
pub date: String
}
#[derive(Serialize, Deserialize)]
pub struct Reminder {
pub label: String,
pub vocal_reminder: String,
pub light_on: String,
pub grace_period: u8
}
#[derive(Serialize, Deserialize)]
pub struct PinConfig
{
pub button: u8,
pub led: u8
}
#[derive(Serialize, Deserialize)]
pub struct User {
pub name: String,
pub reminders: Vec<Reminder>,
pub reminders_h: Vec<Reminder>,
pub pin_config: PinConfig
}
#[derive(Serialize, Deserialize)]
pub struct Configuration {
pub tts_lan: String,
pub snooze_pin: u8,
pub public_holidays: Vec<Holiday>,
pub holiday_seasons: Vec<HolidaySeason>,
pub users: Vec<User>
}
//Checks that configuration is valid and removes all invalid entries
pub fn validate_config (mut config: Configuration) {
//TODO: Check tts language
let mut used_pins: Vec<u8> = Vec::new();
//Check public holidays and discard ones with invalid dates
config.public_holidays = config.public_holidays.into_iter().filter(|day| {
if DateTime::parse_from_str(&day.date, "%d/%m").is_ok() {
true
} else {
error!("Date ({}) of {} is not in valid DD/MM format!\nIgnoring this holiday!", day.date, day.name);
false
}
}).collect();
//Check the holiday seasons and remove ones with invalid dates (start and end dates)
config.holiday_seasons = config.holiday_seasons.into_iter().filter(|holiday| {
let start_datetime = DateTime::parse_from_str(&holiday.start_date, "%d/%m");
let end_datetime = DateTime::parse_from_str(&holiday.end_date, "%d/%m");
if start_datetime.is_ok(){
if end_datetime.is_ok() {
//While we're here issue a warning if the holiday ends before it starts (spans over the new year)
if start_datetime.unwrap() > end_datetime.unwrap() {
warn!("{} ends before it starts! This season will be treated as spanning over the new year!", holiday.name);
}
//This one's all good!
true
} else {
//The configuration for this holiday is invalid so discard it
error!("End date ({}) of {} is not in valid DD/MM format!\nIgnoring this holiday season!", holiday.end_date, holiday.name);
false
}
} else {
//The configuration for this holiday is invalid so discard it
error!("Start date ({}) of {} is not in valid DD/MM format!\nIgnoring this holiday season!", holiday.start_date, holiday.name);
false
}
}).collect();
//TODO: Check all users for valid pins
config.users = config.users.into_iter().filter(|user| {
//Button pin must not be used by another user (or the snooze button)
if !used_pins.contains(&user.pin_config.button) {
//Button pin must be on the allowed list
if ALLOWED_PINS.contains(&user.pin_config.button) {
used_pins.push(user.pin_config.button);
//LED pin must not be used by another user (or the snooze button)
if !used_pins.contains(&user.pin_config.led) {
//LED pin must be on the allowed list
if ALLOWED_PINS.contains(&user.pin_config.led) {
used_pins.push(user.pin_config.led);
true
}
else {
error!("{}'s LED cannot be attached to pin {} because it is reserved for other uses.\nReminders for this user have been disabled.", &user.name, &user.pin_config.led);
false
}
} else {
error!("{}'s LED cannot be attached to pin {} because it is in use by another user.\nReminders for this user have been disabled.", &user.name, &user.pin_config.led);
false
}
}
else {
error!("{}'s Button cannot be attached to pin {} because it is reserved for other uses.\nReminders for this user have been disabled.", &user.name, &user.pin_config.button);
false
}
} else {
error!("{}'s LED cannot be attached to pin {} because it is in use by another user.\nReminders for this user have been disabled.", &user.name, &user.pin_config.button);
false
}
}).collect();
//TODO: Check all alarms for valid dates
for mut user in config.users {
//Check reminders
user.reminders = user.reminders.into_iter().filter(|reminder| {
if DateTime::parse_from_str(&reminder.light_on, "%d/%m").is_ok() {
true
} else {
//error!("Reminder");
false
}
}).collect();
}
}
//Old code for above
//Check the holiday seasons and remove invalid ones
// for holiday in &config.holiday_seasons {
// let start_date = DateTime::parse_from_str(&holiday.start_date, "%d/%m");
// let end_date = DateTime::parse_from_str(&holiday.end_date, "%d/%m");
// //If the start date is invalid
// if start_date.is_err() {
// error!("Start date ({}) of {} is not in valid DD/MM format!\nIgnoring this holiday season!", holiday.start_date, holiday.name);
// //Find this holiday season and remove it
// let index = config.holiday_seasons.iter().position(|x| (&x.start_date == &holiday.start_date && &x.name == &holiday.name)).unwrap();
// config.holiday_seasons.remove(index);
// }
// //If the dates are invalid
// else if end_date.is_err() {
// error!("End date ({}) of {} is not in valid DD/MM format!\nIgnoring this holiday season!", holiday.end_date, holiday.name);
// //Find this holiday season and remove it
// let index = config.holiday_seasons.iter().position(|&x| (x.end_date == holiday.end_date && x.name == holiday.name)).unwrap();
// config.holiday_seasons.remove(index);
// }
// }