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
use crate::prelude::*;
use chrono::prelude::*;
use serde::Deserialize;
#[derive(Debug, Clone, PartialEq)]
pub struct Message {
pub type_: Type,
pub sensor: Sensor,
pub reading: Reading,
}
#[derive(Clone, Copy, PartialEq, Debug, Deserialize)]
pub enum Type {
ReadLogged,
ReadNonLogged,
ReadSnapshot,
Write,
}
pub struct Composer {
pub message: Message,
}
impl Composer {
pub fn new<S: Into<String>>(sensor: S) -> Self {
Self {
message: Message {
type_: Type::ReadLogged,
sensor: Sensor {
sensor_id: sensor.into(),
title: None,
},
reading: Reading {
timestamp: Local::now(),
value: Value::None,
},
},
}
}
pub fn type_(mut self, type_: Type) -> Self {
self.message.type_ = type_;
self
}
pub fn timestamp<T: Into<DateTime<Local>>>(mut self, timestamp: T) -> Self {
self.message.reading.timestamp = timestamp.into();
self
}
pub fn value<V: Into<Value>>(mut self, value: V) -> Self {
self.message.reading.value = value.into();
self
}
pub fn title<S: Into<Option<String>>>(mut self, title: S) -> Self {
self.message.sensor.title = title.into();
self
}
#[allow(dead_code)]
pub fn compose(self) -> Message {
self.message
}
}
impl From<Composer> for Message {
fn from(composer: Composer) -> Self {
composer.message
}
}