Repository navigation
Expand file tree
/
Copy pathprotocol.rs
More file actions
175 lines (174 loc) · 5.14 KB
/
Copy pathprotocol.rs
File metadata and controls
175 lines (174 loc) · 5.14 KB
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
169
170
171
172
173
174
175
use crate::{ensure, identifier, Result};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_json::Value;
pub const MAX_PACKET: usize = 16 * 1024;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Envelope {
pub v: u8,
pub kind: String,
pub message_id: String,
pub device_id: String,
pub model_revision: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub boot_id: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub seq: Option<u64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub session_id: Option<String>,
pub body: Value,
}
impl Envelope {
pub fn decode(raw: &[u8]) -> Result<Self> {
let value = crate::validation::bounded_document(raw, MAX_PACKET, 16, 2048)
.ok_or_else(|| crate::err("invalid_message"))?;
let message: Self =
serde_json::from_value(value).map_err(|_| crate::err("invalid_message"))?;
message.validate()?;
Ok(message)
}
pub fn validate(&self) -> Result<()> {
ensure(self.v == 1, "unsupported_version")?;
ensure(
identifier(&self.device_id) && identifier(&self.message_id),
"invalid_message",
)?;
ensure(
self.seq.map(|v| v <= 9_007_199_254_740_991).unwrap_or(true),
"invalid_sequence",
)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Operation {
#[serde(rename = "type")]
pub kind: String,
pub path: String,
pub value: Value,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct CommandRequest {
pub space_id: String,
pub device_id: String,
pub model_revision: String,
pub idempotency_key: String,
pub expires_at: DateTime<Utc>,
pub operation: Operation,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ActionEvidence {
#[serde(rename = "type")]
pub kind: String,
pub path: String,
pub input_digest: String,
pub output: Value,
pub completion: String,
pub observed_at: DateTime<Utc>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged, deny_unknown_fields)]
pub enum Trigger {
Event {
device: String,
event: String,
},
Property {
device: String,
property: PropertyTrigger,
},
Schedule {
schedule: crate::rule_schedule::Schedule,
},
}
impl Trigger {
pub fn device(&self) -> Option<&str> {
match self {
Self::Event { device, .. } | Self::Property { device, .. } => Some(device),
Self::Schedule { .. } => None,
}
}
pub fn validate_model(&self, model: &crate::model::Model) -> Result<()> {
match self {
Self::Event { event, .. } => model.event(event).map(|_| ()),
Self::Property { property, .. } => property.validate_model(model),
Self::Schedule { schedule } => schedule.validate(),
}
}
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Edge {
Rising,
Falling,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct PropertyTrigger {
pub path: String,
pub unit: String,
pub edge: Edge,
#[serde(deserialize_with = "required_value")]
pub threshold: Value,
#[serde(deserialize_with = "required_value")]
pub hysteresis: Value,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct SetValue {
pub path: String,
#[serde(deserialize_with = "required_value")]
pub value: Value,
}
fn required_value<'de, D: serde::Deserializer<'de>>(
deserializer: D,
) -> std::result::Result<Value, D::Error> {
Value::deserialize(deserializer)
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct InvokeValue {
pub path: String,
#[serde(deserialize_with = "required_value")]
pub input: Value,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged, deny_unknown_fields)]
pub enum Action {
Set { device: String, set: SetValue },
Invoke { device: String, invoke: InvokeValue },
}
impl Action {
pub fn device(&self) -> &str {
match self {
Self::Set { device, .. } | Self::Invoke { device, .. } => device,
}
}
pub fn operation(&self) -> Operation {
match self {
Self::Set { set, .. } => Operation {
kind: "property.set".into(),
path: set.path.clone(),
value: set.value.clone(),
},
Self::Invoke { invoke, .. } => Operation {
kind: "action.invoke".into(),
path: invoke.path.clone(),
value: invoke.input.clone(),
},
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Rule {
pub version: u8,
pub id: String,
pub trigger: Trigger,
pub conditions: Vec<crate::rule_conditions::Condition>,
pub actions: Vec<Action>,
pub cooldown_ms: u64,
}