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 std::io::prelude::*;
use std::net::TcpStream;
use std::io::{Error,ErrorKind};
extern crate md5;
const AUTH: char = '1';
const NOAUTH: char = '0';
static PORT: &'static str = "4352";
fn pjlink_error(error_msg: &str) -> Error {
match &error_msg[7..11] {
"ERR1" => Error::new(ErrorKind::InvalidData, format!("Undefined command")),
"ERR2" => Error::new(ErrorKind::InvalidData, format!("Invalid parameter")),
"ERR3" => Error::new(ErrorKind::InvalidData, format!("Unavaiable at this time")),
"ERR4" => Error::new(ErrorKind::InvalidData, format!("Projector/Display Failure")),
"ERRA" => Error::new(ErrorKind::PermissionDenied, format!("Authorization Error")),
_ => Error::new(ErrorKind::InvalidData, format!("Error reported from the projector {}", error_msg)),
}
}
pub enum PowerStatus {
Off,
On,
Cooling,
Warmup,
}
pub struct PjlinkDevice {
host: String,
password: String,
managed: bool,
}
impl PjlinkDevice {
pub fn new(host: &str) -> Result<PjlinkDevice,Box<Error>> {
let pwd = String::from("");
PjlinkDevice::new_with_password(host, &pwd)
}
pub fn new_with_password(host: &str, password: &str) -> Result<PjlinkDevice,Box<Error>> {
Ok(PjlinkDevice {
host: host.to_string(),
password: String::from(password),
managed: false,
})
}
pub fn send_command(&self, command: &str) -> Result<String, Error> {
let host_port = [&self.host, ":", PORT].concat();
let mut client_buffer = [0u8; 256];
let mut stream = try!(TcpStream::connect(host_port));
let _ = stream.read(&mut client_buffer);
let cmd: String = match client_buffer[7] as char {
AUTH => {
let rnd_num = String::from_utf8_lossy(&client_buffer[9..17]).to_string();
if &self.password != "" {
let pwd_str = format!("{}{}", rnd_num, &self.password);
let digest = md5::compute(pwd_str);
format!("{:x}%1{}\r", digest, command)
} else {
return Err(Error::new(ErrorKind::InvalidInput, "This device requires a password and one was not supplied."))
}
},
NOAUTH => {
format!("%1{}\r", command)
},
_ => return Err(Error::new(ErrorKind::InvalidInput, "Invalid response or is not a PJLink device"))
};
let result = stream.write(cmd.as_bytes());
match result {
Ok(_) => (),
Err(e) => return Err(e),
};
let result = stream.read(&mut client_buffer);
let len = match result {
Ok(len) => len,
Err(e) => return Err(e),
};
let response = String::from_utf8_lossy(&client_buffer[0..len]).to_string();
Ok(response)
}
pub fn get_power_status(&self) -> Result<PowerStatus, Error> {
match self.send_command("POWR ?") {
Ok(result) => {
if &result[2..6] == "POWR" {
match &result[7..8] {
"0" => Ok(PowerStatus::Off),
"1" => Ok(PowerStatus::On),
"2" => Ok(PowerStatus::Cooling),
"3" => Ok(PowerStatus::Warmup),
"E" => Err(pjlink_error(&result)),
_ => Err(Error::new(ErrorKind::InvalidInput, format!("Invalid Response: {}", result))),
}
} else {
Err(Error::new(ErrorKind::InvalidInput, format!("Got a response we didn't expect: {}", result)))
}
},
Err(e) => Err(e),
}
}
pub fn power_on(&self) -> Result<PowerStatus, Error> {
match self.send_command("POWR 1") {
Ok(result) => {
if &result[2..6] == "POWR" {
match &result[7..9] {
"OK" => {
match self.get_power_status() {
Ok(status) => Ok(status),
Err(e) => Err(e),
}
},
"ER" => Err(pjlink_error(&result)),
_ => Err(Error::new(ErrorKind::InvalidInput, format!("Invalid Response: {}", result))),
}
} else {
Err(Error::new(ErrorKind::InvalidInput, format!("Got a response we didn't expect: {}", result)))
}
},
Err(e) => Err(e),
}
}
pub fn power_off(&self) -> Result<PowerStatus, Error> {
match self.send_command("POWR 0") {
Ok(result) => {
if &result[2..6] == "POWR" {
match &result[7..9] {
"OK" => {
match self.get_power_status() {
Ok(status) => Ok(status),
Err(e) => Err(e),
}
},
"ER" => Err(pjlink_error(&result)),
_ => Err(Error::new(ErrorKind::InvalidInput, format!("Invalid Response: {}", result))),
}
} else {
Err(Error::new(ErrorKind::InvalidInput, format!("Got a response we didn't expect: {}", result)))
}
},
Err(e) => Err(e),
}
}
}