Mail protocol testing
This commit is contained in:
18 files changed
+1345
-98
No files matched your search
@@ -36,6 +36,10 @@ pub const MSG_ID_MOTION: u32 = 33;
|
||||
pub const MSG_ID_SET_SERVICE_PORTS: u32 = 36;
|
||||
/// Get service ports
|
||||
pub const MSG_ID_GET_SERVICE_PORTS: u32 = 37;
|
||||
/// Get Email setting
|
||||
pub const MSG_ID_GET_EMAIL: u32 = 42;
|
||||
/// Set email settings
|
||||
pub const MSG_ID_SET_EMAIL: u32 = 43;
|
||||
/// Version messages have this ID
|
||||
pub const MSG_ID_VERSION: u32 = 80;
|
||||
/// Ping messages have this ID
|
||||
@@ -50,6 +54,8 @@ pub const MSG_ID_SNAP: u32 = 109;
|
||||
pub const MSG_ID_UID: u32 = 114;
|
||||
/// Used to pass the token and client ID for push notifications
|
||||
pub const MSG_ID_PUSH_INFO: u32 = 124;
|
||||
/// Send a test email configuration
|
||||
pub const MSG_ID_TEST_EMAIL: u32 = 141;
|
||||
/// StreamInfoList messages have this ID
|
||||
pub const MSG_ID_STREAM_INFO_LIST: u32 = 146;
|
||||
/// Used to get the abilities of a user
|
||||
@@ -70,6 +76,10 @@ pub const MSG_ID_SET_LED_STATUS: u32 = 209;
|
||||
pub const MSG_ID_GET_PIR_ALARM: u32 = 212;
|
||||
/// Setting PIR status messages have this ID
|
||||
pub const MSG_ID_START_PIR_ALARM: u32 = 213;
|
||||
/// Set Email Task
|
||||
pub const MSG_ID_SET_EMAIL_TASK: u32 = 216;
|
||||
/// Get Email Task
|
||||
pub const MSG_ID_GET_EMAIL_TASK: u32 = 217;
|
||||
/// UDP Keep alive
|
||||
pub const MSG_ID_UDP_KEEP_ALIVE: u32 = 234;
|
||||
/// Battery message initiaed by the camera
|
||||
|
||||
@@ -132,6 +132,12 @@ pub struct BcXml {
|
||||
/// For changing rtmp server port
|
||||
#[serde(rename = "OnvifPort", skip_serializing_if = "Option::is_none")]
|
||||
pub onvif_port: Option<OnvifPort>,
|
||||
/// Email for setting the email notifications
|
||||
#[serde(rename = "Email", skip_serializing_if = "Option::is_none")]
|
||||
pub email: Option<Email>,
|
||||
/// EmailTask for turning the email notifications on/off
|
||||
#[serde(rename = "EmailTask", skip_serializing_if = "Option::is_none")]
|
||||
pub email_task: Option<EmailTask>,
|
||||
}
|
||||
|
||||
impl BcXml {
|
||||
@@ -490,8 +496,9 @@ pub struct TimeBlockList {
|
||||
pub struct TimeBlock {
|
||||
/// Whether to enable or disable for this time block
|
||||
pub enable: u8,
|
||||
/// The day of the week for this block
|
||||
pub weekDay: String,
|
||||
/// The day of the week for this block, Monday, Tuesday, Etc
|
||||
#[serde(rename = "weekDay")]
|
||||
pub week_day: String,
|
||||
/// Time to start this block
|
||||
#[serde(rename = "beginHour")]
|
||||
pub begin_hour: u8,
|
||||
@@ -930,7 +937,7 @@ pub struct FloodlightTask {
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub brightness_min: Option<u32>,
|
||||
/// Schedule fot auto floodlight
|
||||
pub schedule: Schedule,
|
||||
pub schedule: ScheduleFloodLight,
|
||||
/// Threshold settings for light sensor to consider nightime
|
||||
#[serde(rename = "lightSensThreshold")]
|
||||
pub light_sens_threshold: LightSensThreshold,
|
||||
@@ -947,7 +954,7 @@ pub struct FloodlightTask {
|
||||
|
||||
/// Schedule for Floodlight Task
|
||||
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize)]
|
||||
pub struct Schedule {
|
||||
pub struct ScheduleFloodLight {
|
||||
/// startHour
|
||||
#[serde(rename = "startHour")]
|
||||
pub start_hour: u32,
|
||||
@@ -1478,6 +1485,87 @@ pub struct OnvifPort {
|
||||
pub enable: Option<u32>,
|
||||
}
|
||||
|
||||
/// Email settings for notificaitons
|
||||
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize, Clone)]
|
||||
pub struct Email {
|
||||
/// XML Version
|
||||
#[serde(rename = "@version")]
|
||||
pub version: String,
|
||||
/// SMTP server address
|
||||
#[serde(rename = "smtpServer")]
|
||||
pub smtp_server: String,
|
||||
/// SMTP username
|
||||
#[serde(rename = "userName")]
|
||||
pub user_name: String,
|
||||
/// SMTP password
|
||||
pub password: String,
|
||||
/// Send email address
|
||||
pub address1: String,
|
||||
/// Send email address can be empty
|
||||
pub address2: String,
|
||||
/// Send email address can be empty
|
||||
pub address3: String,
|
||||
/// 465
|
||||
#[serde(rename = "smtpPort")]
|
||||
pub smtp_port: u16,
|
||||
/// Name of recipient to use on the email
|
||||
#[serde(rename = "sendNickname")]
|
||||
pub send_nickname: String,
|
||||
/// Observed value `1`
|
||||
pub attachment: u8,
|
||||
/// Observed value `picture`, `video`
|
||||
#[serde(rename = "attachmentType", skip_serializing_if = "Option::is_none")]
|
||||
pub attachment_type: Option<String>,
|
||||
/// Observed value `withText`
|
||||
#[serde(rename = "textType")]
|
||||
pub text_type: String,
|
||||
/// Observed value `1`
|
||||
pub ssl: u8,
|
||||
/// Observed value `30`
|
||||
pub interval: u32,
|
||||
/// Max length of message. Observed value `127`
|
||||
/// Read Only
|
||||
#[serde(rename = "senderMaxLen", skip_serializing_if = "Option::is_none")]
|
||||
pub sender_max_len: Option<u32>,
|
||||
}
|
||||
|
||||
/// EmailTask settings that controls the times/enables the email
|
||||
/// notifications
|
||||
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize)]
|
||||
pub struct EmailTask {
|
||||
/// XML Version
|
||||
#[serde(rename = "@version")]
|
||||
pub version: String,
|
||||
/// Channel number
|
||||
#[serde(rename = "channelId")]
|
||||
pub channel_id: u8,
|
||||
/// 1 for enable 0 for disable
|
||||
#[serde(rename = "enable")]
|
||||
pub enable: u8,
|
||||
/// The list of schedule to turn on/off the email notifications
|
||||
#[serde(rename = "ScheduleList", skip_serializing_if = "Option::is_none")]
|
||||
pub schedule_list: Option<ScheduleList>,
|
||||
}
|
||||
|
||||
/// List of schedule items for turning on/off the notifications
|
||||
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize)]
|
||||
pub struct ScheduleList {
|
||||
/// List of schedules
|
||||
#[serde(rename = "Schedule")]
|
||||
pub schedule: Schedule,
|
||||
}
|
||||
|
||||
/// Schedule item for turning on/off the notifications
|
||||
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize)]
|
||||
pub struct Schedule {
|
||||
/// The alarm type. Observed values: `MD`
|
||||
#[serde(rename = "alarmType")]
|
||||
pub alarm_type: String,
|
||||
/// The list of time blocks
|
||||
#[serde(rename = "timeBlockList")]
|
||||
pub time_block_list: TimeBlockList,
|
||||
}
|
||||
|
||||
/// Convience function to return the xml version used throughout the library
|
||||
pub fn xml_ver() -> String {
|
||||
"1.1".to_string()
|
||||
|
||||
@@ -16,6 +16,7 @@ mod abilityinfo;
|
||||
mod battery;
|
||||
mod connection;
|
||||
mod credentials;
|
||||
mod email;
|
||||
mod errors;
|
||||
mod floodlight;
|
||||
mod keepalive;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use super::DiscoveryResult;
|
||||
use crate::bc::codex::BcCodex;
|
||||
use crate::bc::model::*;
|
||||
use crate::bc_protocol::errors::BcUdpDropReciverKind;
|
||||
use crate::bcudp::codex::BcUdpCodex;
|
||||
use crate::bcudp::{model::*, xml::*};
|
||||
use crate::{Credentials, Error, Result};
|
||||
@@ -354,10 +355,10 @@ impl UdpPayloadInner {
|
||||
}
|
||||
packet = inner.next() => {
|
||||
log::trace!("Cam->App");
|
||||
let packet = packet.ok_or(Error::BcUdpDropReciver)??;
|
||||
let packet = packet.ok_or(Error::BcUdpDropReciver(BcUdpDropReciverKind::NoneRecieved))??;
|
||||
recv_timeout.as_mut().reset(Instant::now() + Duration::from_secs(TIME_OUT));
|
||||
// let packet = socket_rx.next().await.ok_or(Error::BcUdpDropReciver)??;
|
||||
socket_out_tx.try_send(packet).map_err(|_| Error::BcUdpDropReciver)?;
|
||||
socket_out_tx.try_send(packet).map_err(|e| Error::BcUdpDropReciver(BcUdpDropReciverKind::SendFailed(format!("{e:?}"))))?;
|
||||
continue;
|
||||
},
|
||||
packet = socket_in_rx.next() => {
|
||||
@@ -509,7 +510,7 @@ impl UdpPayloadInner {
|
||||
log::trace!("Camera->App");
|
||||
// Incomming from socket
|
||||
// Outgoing to application
|
||||
let (item, addr) = v.ok_or(Error::BcUdpDropReciver)?;
|
||||
let (item, addr) = v.ok_or(Error::BcUdpDropReciver(BcUdpDropReciverKind::NoneRecieved))?;
|
||||
if addr == camera_addr {
|
||||
match item {
|
||||
BcUdp::Discovery(UdpDiscovery{
|
||||
|
||||
@@ -0,0 +1,264 @@
|
||||
//! Email controlling methods
|
||||
//!
|
||||
use super::{BcCamera, Error, Result};
|
||||
use crate::bc::{model::*, xml::*};
|
||||
|
||||
impl BcCamera {
|
||||
/// Get the current Email XML
|
||||
pub async fn get_email(&self) -> Result<Email> {
|
||||
let connection = self.get_connection();
|
||||
let msg_num = self.new_message_num();
|
||||
let mut sub_get = connection.subscribe(MSG_ID_GET_EMAIL, msg_num).await?;
|
||||
let get = Bc {
|
||||
meta: BcMeta {
|
||||
msg_id: MSG_ID_GET_EMAIL,
|
||||
channel_id: self.channel_id,
|
||||
msg_num,
|
||||
response_code: 0,
|
||||
stream_type: 0,
|
||||
class: 0x6414,
|
||||
},
|
||||
body: BcBody::ModernMsg(ModernMsg {
|
||||
extension: None,
|
||||
payload: None,
|
||||
}),
|
||||
};
|
||||
|
||||
sub_get.send(get).await?;
|
||||
let msg = sub_get.recv().await?;
|
||||
if msg.meta.response_code != 200 {
|
||||
return Err(Error::CameraServiceUnavailable {
|
||||
id: msg.meta.msg_id,
|
||||
code: msg.meta.response_code,
|
||||
});
|
||||
}
|
||||
|
||||
if let BcBody::ModernMsg(ModernMsg {
|
||||
payload:
|
||||
Some(BcPayloads::BcXml(BcXml {
|
||||
email: Some(email), ..
|
||||
})),
|
||||
..
|
||||
}) = msg.body
|
||||
{
|
||||
Ok(email)
|
||||
} else {
|
||||
Err(Error::UnintelligibleReply {
|
||||
reply: std::sync::Arc::new(Box::new(msg)),
|
||||
why: "Expected Email xml but it was not recieved",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Set the Email XML
|
||||
pub async fn set_email(&self, email: Email) -> Result<()> {
|
||||
let connection = self.get_connection();
|
||||
let msg_num = self.new_message_num();
|
||||
let mut sub_get = connection.subscribe(MSG_ID_SET_EMAIL, msg_num).await?;
|
||||
let get = Bc {
|
||||
meta: BcMeta {
|
||||
msg_id: MSG_ID_SET_EMAIL,
|
||||
channel_id: self.channel_id,
|
||||
msg_num,
|
||||
response_code: 0,
|
||||
stream_type: 0,
|
||||
class: 0x6414,
|
||||
},
|
||||
body: BcBody::ModernMsg(ModernMsg {
|
||||
extension: None,
|
||||
payload: Some(BcPayloads::BcXml(BcXml {
|
||||
email: Some(email),
|
||||
..Default::default()
|
||||
})),
|
||||
}),
|
||||
};
|
||||
|
||||
sub_get.send(get).await?;
|
||||
let msg = sub_get.recv().await?;
|
||||
if msg.meta.response_code != 200 {
|
||||
return Err(Error::CameraServiceUnavailable {
|
||||
id: msg.meta.msg_id,
|
||||
code: msg.meta.response_code,
|
||||
});
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Test the Email with this XML
|
||||
pub async fn test_email(&self, email: Email) -> Result<()> {
|
||||
let connection = self.get_connection();
|
||||
let msg_num = self.new_message_num();
|
||||
let mut sub_get = connection.subscribe(MSG_ID_TEST_EMAIL, msg_num).await?;
|
||||
let get = Bc {
|
||||
meta: BcMeta {
|
||||
msg_id: MSG_ID_TEST_EMAIL,
|
||||
channel_id: self.channel_id,
|
||||
msg_num,
|
||||
response_code: 0,
|
||||
stream_type: 0,
|
||||
class: 0x6414,
|
||||
},
|
||||
body: BcBody::ModernMsg(ModernMsg {
|
||||
extension: None,
|
||||
payload: Some(BcPayloads::BcXml(BcXml {
|
||||
email: Some(email),
|
||||
..Default::default()
|
||||
})),
|
||||
}),
|
||||
};
|
||||
|
||||
sub_get.send(get).await?;
|
||||
let msg = sub_get.recv().await?;
|
||||
if msg.meta.response_code != 200 {
|
||||
return Err(Error::CameraServiceUnavailable {
|
||||
id: msg.meta.msg_id,
|
||||
code: msg.meta.response_code,
|
||||
});
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Get the current EmailTask XML
|
||||
pub async fn get_email_task(&self) -> Result<EmailTask> {
|
||||
let connection = self.get_connection();
|
||||
let msg_num = self.new_message_num();
|
||||
let mut sub_get = connection.subscribe(MSG_ID_GET_EMAIL_TASK, msg_num).await?;
|
||||
let get = Bc {
|
||||
meta: BcMeta {
|
||||
msg_id: MSG_ID_GET_EMAIL_TASK,
|
||||
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: None,
|
||||
}),
|
||||
};
|
||||
|
||||
sub_get.send(get).await?;
|
||||
let msg = sub_get.recv().await?;
|
||||
if msg.meta.response_code != 200 {
|
||||
return Err(Error::CameraServiceUnavailable {
|
||||
id: msg.meta.msg_id,
|
||||
code: msg.meta.response_code,
|
||||
});
|
||||
}
|
||||
|
||||
if let BcBody::ModernMsg(ModernMsg {
|
||||
payload:
|
||||
Some(BcPayloads::BcXml(BcXml {
|
||||
email_task: Some(email_task),
|
||||
..
|
||||
})),
|
||||
..
|
||||
}) = msg.body
|
||||
{
|
||||
Ok(email_task)
|
||||
} else {
|
||||
Err(Error::UnintelligibleReply {
|
||||
reply: std::sync::Arc::new(Box::new(msg)),
|
||||
why: "Expected EmailTask xml but it was not recieved",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Setup the Email Task
|
||||
pub async fn set_email_task(&self, email_task: EmailTask) -> Result<()> {
|
||||
let connection = self.get_connection();
|
||||
let msg_num = self.new_message_num();
|
||||
let mut sub_get = connection.subscribe(MSG_ID_SET_EMAIL_TASK, msg_num).await?;
|
||||
let get = Bc {
|
||||
meta: BcMeta {
|
||||
msg_id: MSG_ID_SET_EMAIL_TASK,
|
||||
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 {
|
||||
email_task: Some(email_task),
|
||||
..Default::default()
|
||||
})),
|
||||
}),
|
||||
};
|
||||
|
||||
sub_get.send(get).await?;
|
||||
let msg = sub_get.recv().await?;
|
||||
if msg.meta.response_code != 200 {
|
||||
return Err(Error::CameraServiceUnavailable {
|
||||
id: msg.meta.msg_id,
|
||||
code: msg.meta.response_code,
|
||||
});
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Turn on Email notifications
|
||||
pub async fn email_on(&self) -> Result<()> {
|
||||
self.set_email_task(EmailTask {
|
||||
version: xml_ver(),
|
||||
channel_id: self.channel_id,
|
||||
enable: 1,
|
||||
schedule_list: None,
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
/// Turn off Email notifications
|
||||
pub async fn email_off(&self) -> Result<()> {
|
||||
self.set_email_task(EmailTask {
|
||||
version: xml_ver(),
|
||||
channel_id: self.channel_id,
|
||||
enable: 0,
|
||||
schedule_list: None,
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
/// Turn on Email notifications all the time
|
||||
pub async fn email_on_always(&self) -> Result<()> {
|
||||
const DOW: [&str; 7] = [
|
||||
"Sunday",
|
||||
"Monday",
|
||||
"Tuesday",
|
||||
"Wednesday",
|
||||
"Thursday",
|
||||
"Friday",
|
||||
"Saturday",
|
||||
];
|
||||
self.set_email_task(EmailTask {
|
||||
version: xml_ver(),
|
||||
channel_id: self.channel_id,
|
||||
enable: 1,
|
||||
schedule_list: Some(ScheduleList {
|
||||
schedule: Schedule {
|
||||
alarm_type: "MD".to_owned(),
|
||||
time_block_list: TimeBlockList {
|
||||
time_block: DOW
|
||||
.iter()
|
||||
.map(|d| TimeBlock {
|
||||
enable: 1,
|
||||
week_day: d.to_string(),
|
||||
begin_hour: 0,
|
||||
end_hour: 23,
|
||||
})
|
||||
.collect(),
|
||||
},
|
||||
},
|
||||
}),
|
||||
})
|
||||
.await
|
||||
}
|
||||
}
|
||||
@@ -89,8 +89,8 @@ pub enum Error {
|
||||
BcUdpTimeout,
|
||||
|
||||
/// Raised when a BcUdp incomming connection is dropped
|
||||
#[error("BcUDP reciver dropped")]
|
||||
BcUdpDropReciver,
|
||||
#[error("BcUDP reciver dropped: {0:?}")]
|
||||
BcUdpDropReciver(BcUdpDropReciverKind),
|
||||
|
||||
/// Raised when a BcUdp outgoing connection is dropped
|
||||
#[error("BcUDP sender dropped")]
|
||||
@@ -236,6 +236,12 @@ pub enum Error {
|
||||
OtherString(String),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum BcUdpDropReciverKind {
|
||||
NoneRecieved,
|
||||
SendFailed(String),
|
||||
}
|
||||
|
||||
impl From<std::io::Error> for Error {
|
||||
fn from(k: std::io::Error) -> Self {
|
||||
// Check for other error that is already an Error of this type
|
||||
|
||||
Reference in new issue
Block a user