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
176
177
#![allow(clippy::never_loop)]
#![allow(clippy::result_large_err)]
pub mod client;
pub mod error;
pub mod prelude;
// Public re-exports
pub use error::{EIMZOError as Error, Result};
use chrono::{Local, NaiveDateTime};
use client::{Client, Connected, Disconnected};
use locale_rs::Locale;
use prelude::*;
use serde_json::json;
use std::str::FromStr;
use tungstenite::Message;
pub struct EIMZO<State> {
client: Client<State>,
}
impl EIMZO<Disconnected> {
pub fn new() -> Result<EIMZO<Connected>> {
Ok(EIMZO {
client: Client::connect::<String>(None)?,
})
}
}
impl EIMZO<Connected> {
/// Информацио о версии JVM
/// doc: https://127.0.0.1:64443/apidoc.html#app.show_menu
///
///```
/// let mut eimzo = EIMZO::new().unwrap();
/// match eimzo.show_menu() {
/// Ok(res) => println!("show_menu: {res:#?}"),
/// Err(e) => println!("{e}"),
///}
///```
pub fn show_menu(&mut self) -> Result<(), error::EIMZOError> {
let cmd: serde_json::Value = json!({
"plugin" :"app",
"name" :"show_menu",
});
self.client
.send_and_wait(Message::Text(cmd.to_string().into()))?;
Ok(())
}
/// Информацио о версии JVM
/// doc: https://127.0.0.1:64443/apidoc.html#app.get_jvm_version
///
///```
/// let mut eimzo = EIMZO::new().unwrap();
/// match eimzo.get_jvm_version() {
/// Ok(res) => println!("get_jvm_version: {res:#?}"),
/// Err(e) => println!("{e}"),
///}
///```
pub fn get_jvm_version(&mut self) -> Result<String, error::EIMZOError> {
let cmd: serde_json::Value = json!({
"plugin" :"app",
"name" :"get_jvm_version",
});
let response = self
.client
.send_and_wait(Message::Text(cmd.to_string().into()))?;
let msg = match response {
Message::Text(msg) => msg,
_ => todo!(), // pattern matching handled on send_and_wait so do something with this
};
let version = serde_json::from_str::<GenericTextMessage>(&msg)?;
Ok(version.message)
}
/// Изменить язык интерфейса (не сохраняя в настройках)
/// doc: https://127.0.0.1:64443/apidoc.html#app.change_ui_lang
///
///```
/// let mut eimzo = EIMZO::new().unwrap();
/// eimzo.change_ui_lang("uz")
///```
pub fn change_ui_lang<T>(&mut self, lang: T) -> Result<(), error::EIMZOError>
where
T: AsRef<str> + serde::Serialize,
{
let locale = Locale::from_str(lang.as_ref())?;
let cmd: serde_json::Value = json!({
"plugin" :"app",
"name" :"change_ui_lang",
"arguments": [
locale.as_str()
],
});
self.client
.send_and_wait(Message::Text(cmd.to_string().into()))?;
Ok(())
}
/// Получить список всех сертификатов пользователя
/// doc: https://127.0.0.1:64443/apidoc.html#pfx.list_all_certificates
///
/// ```
/// let mut eimzo = EIMZO::new()?;
/// match eimzo.list_all_certificates() {
/// Ok(pfx) => {
/// let a: Vec<_> = pfx.iter().map(|c| (c, c.get_alias())).collect();
/// println!("this is resut list_all_certificates; {a:?}");
/// pfx.iter().map(|c| (c, c.get_alias())).for_each(|(c, a)| {
/// let validfrom: Vec<_> = a.get("validfrom").unwrap().split(" ").collect();
/// let mut year_month_day: Vec<_> = validfrom[0].split(".").collect();
/// year_month_day.reverse();
///
/// println!("CERT: {c:#?}");
/// println!("ALIAS: {a:#?}");
/// println!("-----");
/// println!("DATE: {:#?}", year_month_day.join("."));
/// });
/// }
/// Err(e) => println!("{e}"),
/// }
///```
pub fn list_all_certificates(&mut self) -> Result<Vec<Certificate>, error::EIMZOError> {
let cmd: serde_json::Value = json!({
"plugin": "pfx",
"name": "list_all_certificates",
});
let response = self
.client
.send_and_wait(Message::Text(cmd.to_string().into()))?;
let msg = match response {
Message::Text(msg) => msg,
_ => todo!(), // pattern matching handled on send_and_wait so do something with this
};
let certs = serde_json::from_str::<ListAllCertificatesResponse>(&msg)
.unwrap_or_default()
.certificates
.into_iter()
.map(|mut x| {
let _a = x.get_alias();
x.valid_from = Some(
NaiveDateTime::parse_from_str(
_a.get("validfrom").unwrap(),
"%Y.%m.%d %H:%M:%S",
)
.unwrap_or_default(),
);
x.valid_to = Some(
NaiveDateTime::parse_from_str(_a.get("validto").unwrap(), "%Y.%m.%d %H:%M:%S")
.unwrap_or_default(),
);
let now = Local::now().naive_local();
x.is_expired =
Some(now.signed_duration_since(x.valid_to.unwrap()).num_seconds() > 0);
x
})
.collect();
Ok(certs)
}
}