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
mod cipher;
mod crc;
pub mod error;
pub mod mesparse;
pub mod tuyadevice;
extern crate num;
extern crate num_derive;
#[macro_use]
extern crate lazy_static;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::convert::TryFrom;
use std::fmt::Display;
use crate::error::ErrorKind;
use std::convert::TryInto;
pub type Result<T> = std::result::Result<T, ErrorKind>;
#[derive(Debug, Clone, PartialEq)]
pub enum Payload {
Struct(PayloadStruct),
String(String),
}
impl Payload {
pub fn new(
dev_id: String,
gw_id: Option<String>,
uid: Option<String>,
t: Option<u32>,
dps: HashMap<String, serde_json::Value>,
) -> Payload {
Payload::Struct(PayloadStruct {
dev_id,
gw_id,
uid,
t,
dps,
})
}
}
impl Display for Payload {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Payload::Struct(s) => write!(f, "{}", s),
Payload::String(s) => write!(f, "{}", s),
}
}
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct PayloadStruct {
#[serde(rename = "devId")]
pub dev_id: String,
#[serde(rename = "gwId", skip_serializing_if = "Option::is_none")]
pub gw_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub uid: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub t: Option<u32>,
pub dps: HashMap<String, serde_json::Value>,
}
pub trait Truncate {
fn truncate(&self) -> Self;
fn truncate_str(text: &str) -> &str {
if let Some((i, _)) = text.char_indices().rev().nth(5) {
return &text[i..];
}
text
}
}
impl TryFrom<Vec<u8>> for Payload {
type Error = ErrorKind;
fn try_from(vec: Vec<u8>) -> Result<Self> {
match serde_json::from_slice(&vec)? {
serde_json::Value::String(s) => Ok(Payload::String(s)),
value => Ok(Payload::Struct(serde_json::from_value(value)?)),
}
}
}
impl TryInto<Vec<u8>> for Payload {
type Error = ErrorKind;
fn try_into(self) -> Result<Vec<u8>> {
match self {
Payload::Struct(s) => Ok(serde_json::to_vec(&s)?),
Payload::String(s) => Ok(s.as_bytes().to_vec()),
}
}
}
impl Truncate for PayloadStruct {
fn truncate(&self) -> PayloadStruct {
PayloadStruct {
dev_id: String::from("...") + Self::truncate_str(&self.dev_id),
gw_id: self
.gw_id
.as_ref()
.map(|gwid| String::from("...") + Self::truncate_str(gwid)),
t: self.t,
uid: self.uid.clone(),
dps: self.dps.clone(),
}
}
}
impl Display for PayloadStruct {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let full_display = std::env::var("TUYA_FULL_DISPLAY").map_or_else(|_| false, |_| true);
if full_display {
write!(f, "{}", serde_json::to_string(self).unwrap())
} else {
write!(f, "{}", serde_json::to_string(&self.truncate()).unwrap())
}
}
}