1use json::{array, JsonValue, object};
2
3pub struct Protocol {}
4
5impl Protocol {
6 pub fn new(data: Vec<u8>) -> JsonValue {
7 let res = mode1(data.clone());
8 if !res.is_empty() {
9 return res;
10 }
11 let res = mode2(data.clone());
12 if !res.is_empty() {
13 return res;
14 }
15 return res;
16 }
17}
18
19fn mode1(data: Vec<u8>) -> JsonValue {
20 let key = format!("{}{}", data[0], data[1]);
21 if key == "" {
22 return json::Null;
23 }
24 let text = data[2..data.len() - 1].escape_ascii().to_string();
25 let value = match text.parse::<f64>() {
26 Ok(e) => e,
27 Err(_) => {
28 return json::Null;
29 }
30 };
31 return object! {"id":key,data_type:"value",data_mode:"mode1".to_string(),value:value};
32}
33
34fn mode2(data: Vec<u8>) -> JsonValue {
35 let mut num = 0;
36 let index = data.len() / 11;
37 let mut list = array![];
38 for _ in 0..index {
39 let t = &data[num..num + 11];
40 let res = mode1(t.to_vec().clone());
41 if res.is_empty() {
42 return json::Null;
43 }
44 list.push(res.clone()).unwrap();
45 num += 11;
46 }
47 if list.len() > 0 {
48 return object! {"id":list[0]["id"].clone(),data_type:"values",data_mode:"mode2".to_string(),value:list.clone()};
49 }
50 return json::Null;
51}