Add PIR to mqtt

This commit is contained in:
QuantumEntangledAndy committed 2023-04-03 15:06:05 +07:00
1 parent 77be9483b0
commit 82a39a2fa1
4 files changed
+83 -5

No files matched your search

+3 -1
View File
@@ -130,10 +130,12 @@ Control messages:
Status Messages:
`/status offline` Sent when the neolink goes offline this is a LastWill message
`/status disconnected` Sent when the camera goes offline
`/status/battery` Sent in reply to a `/query/battery` send an XML encoded version of the battery status
`/status/battery` Sent in reply to a `/query/battery` an XML encoded version of the battery status
`/status/pir` Sent in reply to a `/query/pir` an XML encoded version of the pir status
Query Messages:
`/query/battery` Request that the camera reports its battery level
`/query/pir` Request that the camera reports its pir status
### Pause
@@ -254,7 +254,7 @@ impl UdpPayloadSource {
fn handle_ack(&mut self, ack: UdpAck) {
let start = ack.packet_id;
// -1 is the special case of no messages recieved
if start != 0xFFFFFFFF {
if start != 0xFFFFFFFF {
self.sent.retain(|&k, _| k > start);
for (idx, &value) in ack.payload.iter().enumerate() {
+47
View File
@@ -25,6 +25,9 @@ pub(crate) enum Messages {
IRLedOff,
IRLedAuto,
Battery,
PIROn,
PIROff,
PIRQuery,
Ptz(Direction),
}
@@ -349,6 +352,50 @@ impl MessageHandler {
}
}
},
Messages::PIRQuery => match self.camera.get_pirstate().await {
Err(_) => {
error!("Failed to get pir status");
"FAIL".to_string()
}
Ok(pir_info) => {
let bytes_res = yaserde::ser::serialize_with_writer(
&pir_info,
vec![],
&Default::default(),
);
match bytes_res {
Ok(bytes) => match String::from_utf8(bytes) {
Ok(str) => str,
Err(_) => {
error!("Failed to encode pir status");
"FAIL".to_string()
}
},
Err(_) => {
error!("Failed to serialise pir status");
"FAIL".to_string()
}
}
}
},
Messages::PIROn => {
if self.camera.pir_set(true).await.is_err() {
error!("Failed to turn on the pir");
self.abort();
"FAIL".to_string()
} else {
"OK".to_string()
}
}
Messages::PIROff => {
if self.camera.pir_set(false).await.is_err() {
error!("Failed to turn on the pir");
self.abort();
"FAIL".to_string()
} else {
"OK".to_string()
}
}
Messages::Ptz(direction) => {
let (bc_direction, amount) = match direction {
Direction::Up(amount) => (BcDirection::Up, amount),
+32 -3
View File
@@ -11,6 +11,7 @@ use log::*;
/// Control messages:
///
/// - `/control/led [on|off]` Turns status LED on/off
/// - `/control/pir [on|off]` Turns PIR on/off
/// - `/control/ir [on|off|auto]` Turn IR lights on/off or automatically via light detection
/// - `/control/reboot` Reboot the camera
/// - `/control/ptz` [up|down|left|right|in|out] (amount) Control the PTZ movements, amount defaults to 32.0
@@ -20,10 +21,12 @@ use log::*;
/// `/status offline` Sent when the neolink goes offline this is a LastWill message
/// `/status disconnected` Sent when the camera goes offline
/// `/status/battery` Sent in reply to a `/query/battery`
/// `/status/pir` Sent in reply to a `/query/pir`
///
/// Query Messages:
///
/// `/query/battery` Request that the camera reports its battery level (Not Yet Implemented)
/// `/query/battery` Request that the camera reports its battery level
/// `/query/pir` Request that the camera reports its pir status
///
///
/// # Usage
@@ -193,7 +196,7 @@ async fn listen_on_camera(
message: "on",
} => {
if event_cam.send_message(Messages::IRLedOn).await.is_err() {
error!("Failed to set camera status light off");
error!("Failed to set camera status light on");
}
}
MqttReplyRef {
@@ -255,6 +258,22 @@ async fn listen_on_camera(
error!("No PTZ Direction given. Please add up/down/left/right/in/out");
}
}
MqttReplyRef {
topic: "control/pir",
message: "on",
} => {
if event_cam.send_message(Messages::PIROn).await.is_err() {
error!("Failed to set pir on");
}
}
MqttReplyRef {
topic: "control/pir",
message: "off",
} => {
if event_cam.send_message(Messages::PIROff).await.is_err() {
error!("Failed to set pir off");
}
}
MqttReplyRef {
topic: "query/battery",
..
@@ -264,7 +283,17 @@ async fn listen_on_camera(
error!("Failed to send battery status reply");
}
}
Err(_) => error!("Failed to set camera status light off"),
Err(_) => error!("Failed to get battery status"),
},
MqttReplyRef {
topic: "query/pir", ..
} => match event_cam.send_message_with_reply(Messages::PIRQuery).await {
Ok(reply) => {
if mqtt.send_message("status/pir", &reply, false).is_err() {
error!("Failed to send pir status reply");
}
}
Err(_) => error!("Failed to get pir status"),
},
_ => {}
}