Merge branch 'master' of github.com:QuantumEntangledAndy/neolink
This commit is contained in:
11 files changed
+514
-72
No files matched your search
@@ -16,6 +16,8 @@ pub const MSG_ID_VIDEO_STOP: u32 = 4;
|
||||
pub const MSG_ID_TALKABILITY: u32 = 10;
|
||||
/// TalkReset messages have this ID
|
||||
pub const MSG_ID_TALKRESET: u32 = 11;
|
||||
/// PtzControl messages have this ID
|
||||
pub const MSG_ID_PTZ_CONTROL: u32 = 18;
|
||||
/// Reboot messages have this ID
|
||||
pub const MSG_ID_REBOOT: u32 = 23;
|
||||
/// Request motion detection messages
|
||||
@@ -50,7 +52,7 @@ pub const EMPTY_LEGACY_PASSWORD: &str =
|
||||
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
|
||||
|
||||
/// Top level bc message
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct Bc {
|
||||
/// header part of the message
|
||||
pub meta: BcMeta,
|
||||
@@ -61,7 +63,7 @@ pub struct Bc {
|
||||
///
|
||||
/// The body of a bc message is either legacy or modern
|
||||
///
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
#[derive(Debug, PartialEq)]
|
||||
#[allow(clippy::large_enum_variant)]
|
||||
pub enum BcBody {
|
||||
/// Legacy is unsupported except for login where it is used
|
||||
@@ -86,7 +88,7 @@ pub enum BcBody {
|
||||
/// `extension` or `payload` these are header only messages. This usually occurs to acknoledge receipt
|
||||
/// of a command. In such cases the header `response_code` should be checked.
|
||||
///
|
||||
#[derive(Debug, Default, PartialEq, Eq)]
|
||||
#[derive(Debug, Default, PartialEq)]
|
||||
pub struct ModernMsg {
|
||||
/// Extension describes the following payload such as which channel id it is for
|
||||
pub extension: Option<Extension>,
|
||||
|
||||
@@ -10,7 +10,7 @@ use yaserde_derive::{YaDeserialize, YaSerialize};
|
||||
use indoc::indoc;
|
||||
|
||||
/// There are two types of payloads xml and binary
|
||||
#[derive(PartialEq, Eq, Debug, YaDeserialize)]
|
||||
#[derive(PartialEq, Debug, YaDeserialize)]
|
||||
#[yaserde(flatten)]
|
||||
#[allow(clippy::large_enum_variant)]
|
||||
pub enum BcPayloads {
|
||||
@@ -31,7 +31,7 @@ impl Default for BcPayloads {
|
||||
}
|
||||
|
||||
/// The top level BC Xml
|
||||
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)]
|
||||
#[derive(PartialEq, Default, Debug, YaDeserialize, YaSerialize)]
|
||||
#[yaserde(rename = "body")]
|
||||
pub struct BcXml {
|
||||
/// Encryption xml is received during login and contain the NONCE
|
||||
@@ -73,6 +73,9 @@ pub struct BcXml {
|
||||
/// Received when motion is detected
|
||||
#[yaserde(rename = "AlarmEventList")]
|
||||
pub alarm_event_list: Option<AlarmEventList>,
|
||||
/// Sent to move the camera
|
||||
#[yaserde(rename = "PtzControl")]
|
||||
pub ptz_control: Option<PtzControl>,
|
||||
}
|
||||
|
||||
impl BcXml {
|
||||
@@ -445,6 +448,21 @@ pub struct AlarmEvent {
|
||||
pub timeStamp: i32,
|
||||
}
|
||||
|
||||
/// The Ptz messages used to move the camera
|
||||
#[derive(PartialEq, Default, Debug, YaDeserialize, YaSerialize)]
|
||||
pub struct PtzControl {
|
||||
/// XML Version
|
||||
#[yaserde(attribute)]
|
||||
pub version: String,
|
||||
/// The channel the event occured on. Usually zero unless from an NVR
|
||||
#[yaserde(rename = "channelId")]
|
||||
pub channel_id: u8,
|
||||
/// The amount of movement to perform
|
||||
pub speed: f32,
|
||||
/// The direction to transverse. Known directions: "right"
|
||||
pub command: String,
|
||||
}
|
||||
|
||||
/// Convience function to return the xml version used throughout the library
|
||||
pub fn xml_ver() -> String {
|
||||
"1.1".to_string()
|
||||
|
||||
@@ -17,6 +17,7 @@ mod logout;
|
||||
mod motion;
|
||||
mod ping;
|
||||
mod pirstate;
|
||||
mod ptz;
|
||||
mod reboot;
|
||||
mod resolution;
|
||||
mod stream;
|
||||
@@ -31,6 +32,7 @@ pub use errors::Error;
|
||||
pub use ledstate::LightState;
|
||||
pub use motion::{MotionData, MotionStatus};
|
||||
pub use pirstate::PirState;
|
||||
pub use ptz::Direction;
|
||||
pub use resolution::*;
|
||||
use std::sync::Arc;
|
||||
pub use stream::{Stream, StreamData};
|
||||
|
||||
@@ -68,9 +68,6 @@ impl MotionData {
|
||||
Err(e) => return Err(Error::from(e)),
|
||||
}
|
||||
}
|
||||
for res in results.iter() {
|
||||
log::info!("Mption: {:?}", res);
|
||||
}
|
||||
if let Some(last) = results.last() {
|
||||
self.last_update = *last;
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ use crate::bc::{model::*, xml::*};
|
||||
|
||||
impl BcCamera {
|
||||
/// Get the [RfAlarmCfg] xml which contains the PIR status of the camera
|
||||
pub fn get_pirstate(&mut self) -> Result<RfAlarmCfg> {
|
||||
pub fn get_pirstate(&self) -> Result<RfAlarmCfg> {
|
||||
let connection = self.get_connection();
|
||||
let msg_num = self.new_message_num();
|
||||
let sub_get = connection.subscribe(msg_num)?;
|
||||
@@ -50,7 +50,7 @@ impl BcCamera {
|
||||
}
|
||||
|
||||
/// Set the PIR sensor using the [RfAlarmCfg] xml
|
||||
pub fn set_pirstate(&mut self, rf_alarm_cfg: RfAlarmCfg) -> Result<()> {
|
||||
pub fn set_pirstate(&self, rf_alarm_cfg: RfAlarmCfg) -> Result<()> {
|
||||
let connection = self.get_connection();
|
||||
let msg_num = self.new_message_num();
|
||||
let sub_set = connection.subscribe(msg_num)?;
|
||||
@@ -97,7 +97,7 @@ impl BcCamera {
|
||||
|
||||
/// This is a convience function to control the PIR status
|
||||
/// True is on and false is off
|
||||
pub fn pir_set(&mut self, state: bool) -> Result<()> {
|
||||
pub fn pir_set(&self, state: bool) -> Result<()> {
|
||||
let mut pir_state = self.get_pirstate()?;
|
||||
// println!("{:?}", pir_state);
|
||||
pir_state.enable = match state {
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
use super::{BcCamera, Error, Result, RX_TIMEOUT};
|
||||
use crate::bc::{model::*, xml::*};
|
||||
|
||||
/// Directions used for Ptz
|
||||
pub enum Direction {
|
||||
/// To move the camera Up
|
||||
Up,
|
||||
/// To move the camera Down
|
||||
Down,
|
||||
/// To move the camera Left
|
||||
Left,
|
||||
/// To move the camera Right
|
||||
Right,
|
||||
/// To zoom the camera In (may be done with cropping depending on camera model)
|
||||
In,
|
||||
/// To zoom the camera Out (may be done with cropping depending on camera model)
|
||||
Out,
|
||||
}
|
||||
|
||||
impl BcCamera {
|
||||
/// Send a PTZ message to the camera
|
||||
pub fn send_ptz(&self, direction: Direction, amount: f32) -> Result<()> {
|
||||
let connection = self.get_connection();
|
||||
let msg_num = self.new_message_num();
|
||||
let sub_set = connection.subscribe(msg_num)?;
|
||||
|
||||
let direction_str = match direction {
|
||||
Direction::Up => "up",
|
||||
Direction::Down => "down",
|
||||
Direction::Left => "left",
|
||||
Direction::Right => "right",
|
||||
Direction::In => {
|
||||
unimplemented!()
|
||||
}
|
||||
Direction::Out => {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
.to_string();
|
||||
let send = Bc {
|
||||
meta: BcMeta {
|
||||
msg_id: MSG_ID_PTZ_CONTROL,
|
||||
channel_id: self.channel_id,
|
||||
msg_num,
|
||||
response_code: 0,
|
||||
stream_type: 0,
|
||||
class: 0x6414,
|
||||
},
|
||||
body: BcBody::ModernMsg(ModernMsg {
|
||||
extension: Some(Extension {
|
||||
channel_id: Some(self.channel_id),
|
||||
..Default::default()
|
||||
}),
|
||||
payload: Some(BcPayloads::BcXml(BcXml {
|
||||
ptz_control: Some(PtzControl {
|
||||
version: xml_ver(),
|
||||
channel_id: self.channel_id,
|
||||
speed: amount,
|
||||
command: direction_str,
|
||||
}),
|
||||
..Default::default()
|
||||
})),
|
||||
}),
|
||||
};
|
||||
|
||||
sub_set.send(send)?;
|
||||
let msg = sub_set.rx.recv_timeout(RX_TIMEOUT)?;
|
||||
|
||||
if let BcMeta {
|
||||
response_code: 200, ..
|
||||
} = msg.meta
|
||||
{
|
||||
Ok(())
|
||||
} else {
|
||||
Err(Error::UnintelligibleReply {
|
||||
reply: Box::new(msg),
|
||||
why: "The camera did not accept the PtzControl xml",
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
+41
-4
@@ -2182,7 +2182,7 @@ Message have zero to two payloads.
|
||||
- Client
|
||||
|
||||
- Extension
|
||||
|
||||
|
||||
```xml
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<Extension version="1.1">
|
||||
@@ -2190,7 +2190,7 @@ Message have zero to two payloads.
|
||||
</Extension>
|
||||
```
|
||||
|
||||
- Payload
|
||||
- Payload
|
||||
|
||||
```xml
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
@@ -2480,7 +2480,7 @@ Message have zero to two payloads.
|
||||
<FloodlightTask version="1.1">
|
||||
<channel>0</channel>
|
||||
<alarmMode>1</alarmMode>
|
||||
<enable>1</enable>
|
||||
<enable>1</enable>
|
||||
<lastAlarmMode>2</lastAlarmMode>
|
||||
<preview_auto>0</preview_auto>
|
||||
<duration>300</duration>
|
||||
@@ -2522,4 +2522,41 @@ Message have zero to two payloads.
|
||||
<detectType>none</detectType>
|
||||
</FloodlightTask>
|
||||
</body>
|
||||
```
|
||||
```
|
||||
|
||||
- 252: BatteryList
|
||||
|
||||
- Client
|
||||
|
||||
Camera Only Message.
|
||||
|
||||
- Camera
|
||||
|
||||
- Header
|
||||
|
||||
| Magic | Message ID | Message Length | Encryption Offset | Status Code | Message Class | Payload Offset |
|
||||
|--------------|--------------|----------------|-------------------|-------------------|---------------|----------------|
|
||||
| f0 de bc 0a | fc 00 00 00 | 1c 02 00 00 | 00 00 00 00 | c8 00 | 00 00 | 00 00 00 00 |
|
||||
|
||||
- Payload
|
||||
|
||||
```xml
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<body>
|
||||
<BatteryList version="1.1">
|
||||
<BatteryInfo>
|
||||
<channelId>0</channelId>
|
||||
<chargeStatus>chargeComplete</chargeStatus>
|
||||
<adapterStatus>solarPanel</adapterStatus>
|
||||
<voltage>3999</voltage>
|
||||
<current>0</current>
|
||||
<temperature>21</temperature>
|
||||
<batteryPercent>100</batteryPercent>
|
||||
<lowPower>0</lowPower>
|
||||
<batteryVersion>2</batteryVersion>
|
||||
</BatteryInfo>
|
||||
</BatteryList>
|
||||
</body>
|
||||
```
|
||||
|
||||
**Notes**: Sent after login, with a message handle of 0 (usually 0 means not sent in reply to a specific request). TODO: Find out how to request on demand.
|
||||
+126
-38
@@ -2,9 +2,11 @@ use super::App;
|
||||
use crate::config::CameraConfig;
|
||||
use crate::utils::AddressOrUid;
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use crossbeam_channel::{unbounded, Receiver, Sender, TryRecvError};
|
||||
use crossbeam_channel::{bounded, unbounded, Receiver, Sender, TryRecvError};
|
||||
use log::*;
|
||||
use neolink_core::bc_protocol::{BcCamera, Error as BcError, LightState, MotionStatus};
|
||||
use neolink_core::bc_protocol::{
|
||||
BcCamera, Direction as BcDirection, Error as BcError, LightState, MotionStatus,
|
||||
};
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
pub(crate) enum Messages {
|
||||
@@ -18,13 +20,32 @@ pub(crate) enum Messages {
|
||||
IRLedOn,
|
||||
IRLedOff,
|
||||
IRLedAuto,
|
||||
Battery,
|
||||
Ptz(Direction),
|
||||
}
|
||||
|
||||
pub(crate) enum Direction {
|
||||
Up(f32),
|
||||
Down(f32),
|
||||
Left(f32),
|
||||
Right(f32),
|
||||
In(f32),
|
||||
Out(f32),
|
||||
}
|
||||
|
||||
enum ToCamera {
|
||||
Send(Messages),
|
||||
SendAndReply {
|
||||
message: Messages,
|
||||
reply: Sender<String>,
|
||||
},
|
||||
}
|
||||
|
||||
pub(crate) struct EventCam<'a> {
|
||||
config: &'a CameraConfig,
|
||||
app: Arc<App>,
|
||||
channel_out: Arc<Mutex<Option<Receiver<Messages>>>>,
|
||||
channel_in: Arc<Mutex<Option<Sender<Messages>>>>,
|
||||
channel_in: Arc<Mutex<Option<Sender<ToCamera>>>>,
|
||||
}
|
||||
|
||||
impl<'a> EventCam<'a> {
|
||||
@@ -53,7 +74,7 @@ impl<'a> EventCam<'a> {
|
||||
if let Ok(ref mut channel_in) = self.channel_in.lock() {
|
||||
if let Some(channel_in) = channel_in.as_mut() {
|
||||
channel_in
|
||||
.send(msg)
|
||||
.send(ToCamera::Send(msg))
|
||||
.context("Failed to send message from camera")
|
||||
} else {
|
||||
Err(anyhow!("Failed to send camera data over crossbeam"))
|
||||
@@ -63,6 +84,26 @@ impl<'a> EventCam<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn send_message_with_reply(&self, msg: Messages) -> Result<String> {
|
||||
if let Ok(ref mut channel_in) = self.channel_in.lock() {
|
||||
if let Some(channel_in) = channel_in.as_mut() {
|
||||
let (tx, rx) = bounded(1);
|
||||
channel_in
|
||||
.send(ToCamera::SendAndReply {
|
||||
message: msg,
|
||||
reply: tx,
|
||||
})
|
||||
.context("Failed to send message from camera")?;
|
||||
rx.recv_timeout(std::time::Duration::from_secs(3))
|
||||
.context("Failed to recieve reply")
|
||||
} else {
|
||||
Err(anyhow!("Failed to send camera data over crossbeam"))
|
||||
}
|
||||
} else {
|
||||
Err(anyhow!("Failed to lock"))
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn abort(&self) {
|
||||
self.app.abort(&self.config.name);
|
||||
}
|
||||
@@ -94,7 +135,7 @@ impl<'a> EventCam<'a> {
|
||||
fn cam_run(
|
||||
camera_config: &CameraConfig,
|
||||
tx: Arc<Mutex<Sender<Messages>>>,
|
||||
rx: Arc<Mutex<Receiver<Messages>>>,
|
||||
rx: Arc<Mutex<Receiver<ToCamera>>>,
|
||||
app: Arc<App>,
|
||||
) -> Result<()> {
|
||||
let camera_addr =
|
||||
@@ -196,7 +237,7 @@ impl MotionThread {
|
||||
|
||||
struct MessageHandler {
|
||||
app: Arc<App>,
|
||||
rx: Arc<Mutex<Receiver<Messages>>>,
|
||||
rx: Arc<Mutex<Receiver<ToCamera>>>,
|
||||
camera: Arc<BcCamera>,
|
||||
name: String,
|
||||
}
|
||||
@@ -207,45 +248,92 @@ impl MessageHandler {
|
||||
// Try and lock don't worry if not
|
||||
if let Ok(ref mut channel_out) = self.rx.try_lock() {
|
||||
match channel_out.try_recv() {
|
||||
Ok(message) => match message {
|
||||
Messages::Reboot => {
|
||||
if self.camera.reboot().is_err() {
|
||||
error!("Failed to reboot the camera");
|
||||
self.abort()
|
||||
Ok(to_camera) => {
|
||||
let (replier, message) = match to_camera {
|
||||
ToCamera::Send(message) => (None, message),
|
||||
ToCamera::SendAndReply { message, reply } => (Some(reply), message),
|
||||
};
|
||||
let reply = match message {
|
||||
Messages::Reboot => {
|
||||
if self.camera.reboot().is_err() {
|
||||
error!("Failed to reboot the camera");
|
||||
self.abort();
|
||||
"FAIL".to_string()
|
||||
} else {
|
||||
"OK".to_string()
|
||||
}
|
||||
}
|
||||
}
|
||||
Messages::StatusLedOn => {
|
||||
if self.camera.led_light_set(true).is_err() {
|
||||
error!("Failed to turn on the status light");
|
||||
self.abort();
|
||||
Messages::StatusLedOn => {
|
||||
if self.camera.led_light_set(true).is_err() {
|
||||
error!("Failed to turn on the status light");
|
||||
self.abort();
|
||||
"FAIL".to_string()
|
||||
} else {
|
||||
"OK".to_string()
|
||||
}
|
||||
}
|
||||
}
|
||||
Messages::StatusLedOff => {
|
||||
if self.camera.led_light_set(false).is_err() {
|
||||
error!("Failed to turn off the status light");
|
||||
self.abort();
|
||||
Messages::StatusLedOff => {
|
||||
if self.camera.led_light_set(false).is_err() {
|
||||
error!("Failed to turn off the status light");
|
||||
self.abort();
|
||||
"FAIL".to_string()
|
||||
} else {
|
||||
"OK".to_string()
|
||||
}
|
||||
}
|
||||
}
|
||||
Messages::IRLedOn => {
|
||||
if self.camera.irled_light_set(LightState::On).is_err() {
|
||||
error!("Failed to turn on the status light");
|
||||
self.abort();
|
||||
Messages::IRLedOn => {
|
||||
if self.camera.irled_light_set(LightState::On).is_err() {
|
||||
error!("Failed to turn on the status light");
|
||||
self.abort();
|
||||
"FAIL".to_string()
|
||||
} else {
|
||||
"OK".to_string()
|
||||
}
|
||||
}
|
||||
}
|
||||
Messages::IRLedOff => {
|
||||
if self.camera.irled_light_set(LightState::Off).is_err() {
|
||||
error!("Failed to turn on the status light");
|
||||
self.abort();
|
||||
Messages::IRLedOff => {
|
||||
if self.camera.irled_light_set(LightState::Off).is_err() {
|
||||
error!("Failed to turn on the status light");
|
||||
self.abort();
|
||||
"FAIL".to_string()
|
||||
} else {
|
||||
"OK".to_string()
|
||||
}
|
||||
}
|
||||
}
|
||||
Messages::IRLedAuto => {
|
||||
if self.camera.irled_light_set(LightState::Auto).is_err() {
|
||||
error!("Failed to turn on the status light");
|
||||
self.abort();
|
||||
Messages::IRLedAuto => {
|
||||
if self.camera.irled_light_set(LightState::Auto).is_err() {
|
||||
error!("Failed to turn on the status light");
|
||||
self.abort();
|
||||
"FAIL".to_string()
|
||||
} else {
|
||||
"OK".to_string()
|
||||
}
|
||||
}
|
||||
Messages::Battery => {
|
||||
unimplemented!()
|
||||
}
|
||||
Messages::Ptz(direction) => {
|
||||
let (bc_direction, amount) = match direction {
|
||||
Direction::Up(amount) => (BcDirection::Up, amount),
|
||||
Direction::Down(amount) => (BcDirection::Down, amount),
|
||||
Direction::Left(amount) => (BcDirection::Left, amount),
|
||||
Direction::Right(amount) => (BcDirection::Right, amount),
|
||||
Direction::In(amount) => (BcDirection::In, amount),
|
||||
Direction::Out(amount) => (BcDirection::Out, amount),
|
||||
};
|
||||
if self.camera.send_ptz(bc_direction, amount).is_err() {
|
||||
error!("Failed to turn on the status light");
|
||||
self.abort();
|
||||
"FAIL".to_string()
|
||||
} else {
|
||||
"OK".to_string()
|
||||
}
|
||||
}
|
||||
_ => "UNKNOWN COMMAND".to_string(),
|
||||
};
|
||||
if let Some(replier) = replier {
|
||||
let _ = replier.send(reply);
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
}
|
||||
Err(TryRecvError::Empty) => (),
|
||||
Err(TryRecvError::Disconnected) => self.abort(),
|
||||
}
|
||||
|
||||
+92
-9
@@ -4,12 +4,52 @@ use log::*;
|
||||
///
|
||||
/// Handles incoming and outgoing MQTT messages
|
||||
///
|
||||
/// This acts as a bridge between cameras and MQTT servers
|
||||
///
|
||||
/// Messages are prefixed with `neolink/{CAMERANAME}`
|
||||
///
|
||||
/// Control messages:
|
||||
///
|
||||
/// - `/control/led [on|off]` Turns status LED on/off
|
||||
/// - `/control/ir [on|off|auti]` 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
|
||||
///
|
||||
/// 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`
|
||||
///
|
||||
/// Query Messages:
|
||||
///
|
||||
/// `/query/battery` Request that the camera reports its battery level (Not Yet Implemented)
|
||||
///
|
||||
///
|
||||
/// # Usage
|
||||
///
|
||||
/// ```bash
|
||||
/// neolink mqtt --config=config.toml
|
||||
/// ```
|
||||
///
|
||||
/// # Example Config
|
||||
///
|
||||
/// ```toml
|
||||
// [[cameras]]
|
||||
// name = "Cammy"
|
||||
// username = "****"
|
||||
// password = "****"
|
||||
// address = "****:9000"
|
||||
// [cameras.mqtt]
|
||||
// server = "127.0.0.1"
|
||||
// port = 1883
|
||||
// credentials = ["username", "password"]
|
||||
// ```
|
||||
//
|
||||
// `server` is the mqtt server
|
||||
// `port` is the mqtt server's port
|
||||
// `credentials` are the username and password required to identify with the mqtt server
|
||||
//
|
||||
use std::sync::Arc;
|
||||
|
||||
mod app;
|
||||
@@ -18,28 +58,31 @@ mod event_cam;
|
||||
mod mqttc;
|
||||
|
||||
use crate::config::{CameraConfig, Config, MqttConfig};
|
||||
use anyhow::Result;
|
||||
use anyhow::{anyhow, Result};
|
||||
pub(crate) use app::App;
|
||||
pub(crate) use cmdline::Opt;
|
||||
use event_cam::EventCam;
|
||||
pub(crate) use event_cam::Messages;
|
||||
pub(crate) use event_cam::{Direction, Messages};
|
||||
use mqttc::{Mqtt, MqttReplyRef};
|
||||
|
||||
/// Entry point for the reboot subcommand
|
||||
/// Entry point for the mqtt subcommand
|
||||
///
|
||||
/// Opt is the command line options
|
||||
pub(crate) fn main(_: Opt, config: Config) -> Result<()> {
|
||||
let app = App::new();
|
||||
let arc_app = Arc::new(app);
|
||||
|
||||
let mut mqtt_count: u8 = 0;
|
||||
if config.cameras.iter().all(|config| config.mqtt.is_none()) {
|
||||
return Err(anyhow!(
|
||||
"MQTT command run, but no cameras configured with MQTT settings. Exiting."
|
||||
));
|
||||
}
|
||||
|
||||
let _ = crossbeam::scope(|s| {
|
||||
for camera_config in &config.cameras {
|
||||
if let Some(mqtt_config) = camera_config.mqtt.as_ref() {
|
||||
let loop_arc_app = arc_app.clone();
|
||||
info!("{}: Setting up mqtt", camera_config.name);
|
||||
mqtt_count = mqtt_count + 1;
|
||||
s.spawn(move |_| {
|
||||
while loop_arc_app.running("app") {
|
||||
let _ = listen_on_camera(camera_config, mqtt_config, loop_arc_app.clone());
|
||||
@@ -49,10 +92,6 @@ pub(crate) fn main(_: Opt, config: Config) -> Result<()> {
|
||||
}
|
||||
});
|
||||
|
||||
if mqtt_count == 0 {
|
||||
error!("MQTT command run, but no cameras configured with MQTT settings. Exiting.");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -157,6 +196,50 @@ fn listen_on_camera(
|
||||
error!("Failed to set camera status light off");
|
||||
}
|
||||
}
|
||||
MqttReplyRef {
|
||||
topic: "control/ptz",
|
||||
message,
|
||||
} => {
|
||||
let lowercase_message = message.to_lowercase();
|
||||
let mut words = lowercase_message.split_whitespace();
|
||||
if let Some(direction_txt) = words.next() {
|
||||
let amount = words.next().unwrap_or("32.0");
|
||||
if let Ok(amount) = amount.parse::<f32>() {
|
||||
let direction = match direction_txt {
|
||||
"up" => Direction::Up(amount),
|
||||
"down" => Direction::Down(amount),
|
||||
"left" => Direction::Left(amount),
|
||||
"right" => Direction::Right(amount),
|
||||
"in" => Direction::In(amount),
|
||||
"out" => Direction::Out(amount),
|
||||
_ => {
|
||||
error!("Unrecongnized PTZ direction");
|
||||
continue;
|
||||
}
|
||||
};
|
||||
if event_cam.send_message(Messages::Ptz(direction)).is_err() {
|
||||
error!("Failed to send PTZ");
|
||||
}
|
||||
} else {
|
||||
error!("No PTZ direction speed was not a valid number");
|
||||
}
|
||||
} else {
|
||||
error!(
|
||||
"No PTZ Direction given. Please add up/down/left/right/in/out"
|
||||
);
|
||||
}
|
||||
}
|
||||
MqttReplyRef {
|
||||
topic: "query/battery",
|
||||
..
|
||||
} => match event_cam.send_message_with_reply(Messages::Battery) {
|
||||
Ok(reply) => {
|
||||
if mqtt.send_message("status/battery", &reply, false).is_err() {
|
||||
error!("Failed to send battery status reply");
|
||||
}
|
||||
}
|
||||
Err(_) => error!("Failed to set camera status light off"),
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
+112
-6
@@ -9,7 +9,7 @@ pub(crate) use self::maybe_inputselect::MaybeInputSelect;
|
||||
// use super::adpcm::adpcm_to_pcm;
|
||||
// use super::errors::Error;
|
||||
use gstreamer::prelude::Cast;
|
||||
use gstreamer::{Bin, Structure};
|
||||
use gstreamer::{Bin, ElementFactory, Structure};
|
||||
use gstreamer_app::AppSrc;
|
||||
//use gstreamer_rtsp::RTSPLowerTrans;
|
||||
use anyhow::{anyhow, Context};
|
||||
@@ -344,17 +344,123 @@ impl GstOutputs {
|
||||
|
||||
impl Default for RtspServer {
|
||||
fn default() -> RtspServer {
|
||||
Self::new()
|
||||
Self::new().unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl RtspServer {
|
||||
pub(crate) fn new() -> RtspServer {
|
||||
gstreamer::init().expect("Gstreamer should not explode");
|
||||
RtspServer {
|
||||
pub(crate) fn new() -> AnyResult<RtspServer> {
|
||||
gstreamer::init().context("Gstreamer failed to initialise")?;
|
||||
let needed_elements = &[
|
||||
(
|
||||
"appsrc",
|
||||
"app (gst-plugins-base)",
|
||||
"Cannot run without an appsrc",
|
||||
),
|
||||
(
|
||||
"audioconvert",
|
||||
"audioconvert (gst-plugins-base)",
|
||||
"Required for audio",
|
||||
),
|
||||
("adpcmdec", "adpcmdec", "Required for audio"),
|
||||
(
|
||||
"h264parse",
|
||||
"videoparsersbad (gst-plugins-bad)",
|
||||
"Required for certain types of camera",
|
||||
),
|
||||
(
|
||||
"h265parse",
|
||||
"videoparsersbad (gst-plugins-bad)",
|
||||
"Required for certain types of camera",
|
||||
),
|
||||
(
|
||||
"rtph264pay",
|
||||
"rtp (gst-plugins-good)",
|
||||
"Required for certain types of camera",
|
||||
),
|
||||
(
|
||||
"rtph265pay",
|
||||
"rtp (gst-plugins-good)",
|
||||
"Required for certain types of camera",
|
||||
),
|
||||
(
|
||||
"aacparse",
|
||||
"audioparsers (gst-plugins-good)",
|
||||
"Required for certain types of camera's audio",
|
||||
),
|
||||
("rtpL16pay", "rtp (gst-plugins-good)", "Required for audio"),
|
||||
];
|
||||
let sometimes_needed_elements = &[
|
||||
(
|
||||
"x264enc",
|
||||
"x264 (gst-plugins-ugly)",
|
||||
"Required to paused certain cameras",
|
||||
),
|
||||
(
|
||||
"x265enc",
|
||||
"x265 (gst-plugins-bad)",
|
||||
"Required to paused certain cameras",
|
||||
),
|
||||
(
|
||||
"avdec_h264",
|
||||
"libav (gst-libav)",
|
||||
"Required to paused certain cameras",
|
||||
),
|
||||
(
|
||||
"avdec_h265",
|
||||
"libav (gst-libav)",
|
||||
"Required to paused certain cameras",
|
||||
),
|
||||
(
|
||||
"videotestsrc",
|
||||
"videotestsrc (gst-plugins-base)",
|
||||
"Required to paused certain cameras",
|
||||
),
|
||||
(
|
||||
"imagefreeze",
|
||||
"imagefreeze (gst-plugins-good)",
|
||||
"Required to paused certain cameras",
|
||||
),
|
||||
(
|
||||
"audiotestsrc",
|
||||
"audiotestsrc (gst-plugins-base)",
|
||||
"Required for pausing",
|
||||
),
|
||||
(
|
||||
"decodebin",
|
||||
"playback (gst-plugins-good)",
|
||||
"Required for pausing",
|
||||
),
|
||||
];
|
||||
let mut fatal = false;
|
||||
for (element, plugin, msg) in needed_elements.iter() {
|
||||
if ElementFactory::find(element).is_none() {
|
||||
error!(
|
||||
"Missing required gstreamer plugin `{}` for `{}` element. {}",
|
||||
plugin, element, msg
|
||||
);
|
||||
fatal = true;
|
||||
}
|
||||
}
|
||||
|
||||
for (element, plugin, msg) in sometimes_needed_elements.iter() {
|
||||
if ElementFactory::find(element).is_none() {
|
||||
warn!(
|
||||
"Missing the gstreamer plugin `{}` for `{}` element. {}",
|
||||
plugin, element, msg
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if fatal {
|
||||
return Err(anyhow!(
|
||||
"Required Gstreamer Elements are missing. Ensure gstreamer is installed correctly"
|
||||
));
|
||||
}
|
||||
Ok(RtspServer {
|
||||
server: GstRTSPServer::new(),
|
||||
main_loop: glib::MainLoop::new(None, false),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn add_stream<T: AsRef<str>>(
|
||||
|
||||
+32
-4
@@ -28,6 +28,30 @@
|
||||
/// neolink rtsp --config=config.toml
|
||||
/// ```
|
||||
///
|
||||
/// # Example Config
|
||||
///
|
||||
/// ```toml
|
||||
// [[cameras]]
|
||||
// name = "Cammy"
|
||||
// username = "****"
|
||||
// password = "****"
|
||||
// address = "****:9000"
|
||||
// [cameras.pause]
|
||||
// on_motion = false
|
||||
// on_client = false
|
||||
// mode = "none"
|
||||
// timeout = 1.0
|
||||
// ```
|
||||
//
|
||||
// - When `on_motion` is true the camera will pause streaming when motion is stopped and resume it when motion is started
|
||||
// - When `on_client` is true the camera will pause while there is no client connected.
|
||||
// - `timeout` handels how long to wait after motion stops before pausing the stream
|
||||
// - `mode` has the following values:
|
||||
// - `"black"`: Switches to a black screen. Requires more cpu as the stream is fully reencoded
|
||||
// - `"still"`: Switches to a still image. Requires more cpu as the stream is fully reencoded
|
||||
// - `"test"`: Switches to the gstreamer test image. Requires more cpu as the stream is fully reencoded
|
||||
// - `"none"`: Resends the last iframe the camera. This does not reencode at all. **Most use cases should use this one as it has the least effort on the cpu and gives what you would expect**
|
||||
//
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use crossbeam::utils::Backoff;
|
||||
use log::*;
|
||||
@@ -50,7 +74,7 @@ use gst::{RtspServer, TlsAuthenticationMode};
|
||||
///
|
||||
/// Opt is the command line options
|
||||
pub(crate) fn main(_opt: Opt, mut config: Config) -> Result<()> {
|
||||
let rtsp = Arc::new(RtspServer::new());
|
||||
let rtsp = Arc::new(RtspServer::new()?);
|
||||
|
||||
set_up_tls(&config, &rtsp);
|
||||
|
||||
@@ -144,6 +168,7 @@ fn camera_main(
|
||||
};
|
||||
|
||||
let motion_timeout: Duration = Duration::from_secs_f64(config.pause.motion_timeout);
|
||||
let mut ready_to_pause_print = false;
|
||||
loop {
|
||||
match camera.get_state() {
|
||||
StateInfo::Streaming => {
|
||||
@@ -153,7 +178,8 @@ fn camera_main(
|
||||
if can_pause {
|
||||
info!("Pause on disconnect");
|
||||
camera.pause().map_err(CameraFailureKind::Retry)?;
|
||||
} else {
|
||||
} else if !ready_to_pause_print {
|
||||
ready_to_pause_print = true;
|
||||
warn!("Not ready to pause");
|
||||
}
|
||||
}
|
||||
@@ -169,7 +195,8 @@ fn camera_main(
|
||||
if can_pause {
|
||||
info!("Pause on motion");
|
||||
camera.pause().map_err(CameraFailureKind::Retry)?;
|
||||
} else {
|
||||
} else if !ready_to_pause_print {
|
||||
ready_to_pause_print = true;
|
||||
warn!("Not ready to pause");
|
||||
}
|
||||
}
|
||||
@@ -177,7 +204,8 @@ fn camera_main(
|
||||
if can_pause {
|
||||
info!("Pause on motion (start)");
|
||||
camera.pause().map_err(CameraFailureKind::Retry)?;
|
||||
} else {
|
||||
} else if !ready_to_pause_print {
|
||||
ready_to_pause_print = true;
|
||||
warn!("Not ready to pause");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user