Implement configuration file, with multiple cameras

This commit is contained in:
George Hilliard committed 2020-04-20 14:33:07 -05:00
1 parent a194586957
commit ec1e69e49c
5 files changed
+100 -18

No files matched your search

+3
View File
@@ -8,12 +8,15 @@ edition = "2018"
[dependencies]
cookie-factory = "0.3.1"
crossbeam-utils = "0.7"
env_logger = "*"
err-derive = "0.2.4"
log = { version = "0.4.8", features = ["release_max_level_warn"] }
indoc = "0.3.5"
md5 = "0.7.0"
nom = "5.1.1"
serde = { version = "1.0", features = ["derive"] }
toml = "0.5"
yaserde = { git = "https://github.com/media-io/yaserde.git" }
yaserde_derive = { git = "https://github.com/media-io/yaserde.git" }
xml-rs = "0.8.2"
+13
View File
@@ -0,0 +1,13 @@
[[cameras]]
name = "driveway"
username = "admin"
password = "12345678"
address = "192.168.1.187:9000"
serve = { port = 9900 }
[[cameras]]
name = "storage shed"
username = "admin"
password = "987654321"
address = "192.168.1.245:9000"
serve = { port = 9901 }
+1 -5
View File
@@ -2,7 +2,7 @@ use err_derive::Error;
use log::*;
use md5;
use std::io::Write;
use std::net::{IpAddr, SocketAddr, ToSocketAddrs, TcpStream};
use std::net::{SocketAddr, ToSocketAddrs, TcpStream};
use super::bc::{model::*, xml::*};
use Md5Trunc::*;
@@ -44,10 +44,6 @@ impl Drop for BcCamera {
}
impl BcCamera {
pub fn new_with_ip(ip_addr: IpAddr) -> Result<Self> {
Self::new_with_addr((ip_addr, 9000))
}
pub fn new_with_addr<T: ToSocketAddrs>(hostname: T) -> Result<Self> {
let address = hostname.to_socket_addrs()?.next()
.ok_or(Error::OtherError("Address resolution failed"))?;
+48
View File
@@ -0,0 +1,48 @@
use serde::Deserialize;
use std::fmt;
use std::net::SocketAddr;
use std::net::ToSocketAddrs;
#[derive(Debug, Deserialize)]
pub struct Config {
pub cameras: Vec<CameraConfig>,
}
#[derive(Debug, Deserialize)]
pub struct CameraConfig {
pub name: String,
#[serde(rename="address")]
pub camera_addr: SocketAddr,
#[serde(rename="serve")]
pub bind_addr: BindAddr,
pub stream: Option<String>,
pub username: String,
pub password: Option<String>,
}
#[derive(Debug, PartialEq, Eq, Deserialize)]
pub struct BindAddr {
host: Option<String>,
port: u16,
}
impl fmt::Display for BindAddr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let Some(ref host) = self.host {
write!(f, "{}:{}", host, self.port)
} else {
write!(f, "port {}", self.port)
}
}
}
impl ToSocketAddrs for BindAddr {
type Iter = std::vec::IntoIter<SocketAddr>;
fn to_socket_addrs(&self) -> std::io::Result<Self::Iter> {
let host = self.host.as_deref().unwrap_or("0.0.0.0");
(host, self.port).to_socket_addrs()
}
}
+35 -13
View File
@@ -1,34 +1,56 @@
#![allow(dead_code)] // while I'm still fleshing this out
use err_derive::Error;
use std::env::args;
use std::net::TcpListener;
mod bc;
mod bc_protocol;
mod config;
use bc_protocol::BcCamera;
use config::{Config, CameraConfig};
use crossbeam_utils::thread;
use err_derive::Error;
use std::env::args;
use std::fs;
use std::net::TcpListener;
#[derive(Debug, Error)]
pub enum Error {
#[error(display="Configuration parsing error")]
ConfigError(#[error(source)] toml::de::Error),
#[error(display="Communication error")]
ProtocolError(#[error(source)] bc_protocol::Error),
#[error(display="Socket setup error")]
#[error(display="I/O error")]
IoError(#[error(source)] std::io::Error),
}
fn main() -> Result<(), Error> {
let mut camera = BcCamera::new_with_addr(args().nth(1).unwrap())?;
camera.connect()?;
camera.login("admin", Some("12345678"))?;
let config_path = args().nth(1).expect("A config file must be supplied on the command line");
let config: Config = toml::from_str(&fs::read_to_string(config_path)?)?;
let bind_addr = "0.0.0.0:9999";
println!("Logged in to camera; awaiting connection on {}", bind_addr);
thread::scope(|s| {
for camera in config.cameras {
s.spawn(move |_| {
// TODO handle these errors
camera_main(&camera)
});
}
}).unwrap();
Ok(())
}
fn camera_main(camera_config: &CameraConfig) -> Result<(), Error> {
let mut camera = BcCamera::new_with_addr(camera_config.camera_addr)?;
println!("{}: Connecting to camera at {}", camera_config.name, camera_config.camera_addr);
camera.connect()?;
camera.login(&camera_config.username, camera_config.password.as_deref())?;
let bind_addr = &camera_config.bind_addr;
println!("{}: Logged in to camera; awaiting connection on {}", camera_config.name, bind_addr);
let listener = TcpListener::bind(bind_addr)?;
let (mut out_socket, remote_addr) = listener.accept()?;
println!("Connected to {}, starting video stream", remote_addr);
println!("{}: Connected to {}, starting video stream", camera_config.name, remote_addr);
camera.start_video(&mut out_socket)?;