From 00f30b8972a2abedf35291291d5bc5386cd500f2 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Wed, 7 Jun 2023 15:57:07 +0700 Subject: [PATCH] Use enum for config --- src/config.rs | 4 +++- src/mqtt/discovery.rs | 42 +++++++++++++++++++++++++++--------------- src/mqtt/mod.rs | 1 + 3 files changed, 31 insertions(+), 16 deletions(-) diff --git a/src/config.rs b/src/config.rs index d57fadd..87de87a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,8 +1,10 @@ +use crate::mqtt::Discoveries; use lazy_static::lazy_static; use neolink_core::bc_protocol::{DiscoveryMethods, PrintFormat}; use regex::Regex; use serde::Deserialize; use std::clone::Clone; +use std::collections::HashSet; use validator::{Validate, ValidationError}; use validator_derive::Validate; @@ -155,7 +157,7 @@ pub(crate) struct MqttConfig { pub(crate) struct MqttDiscoveryConfig { pub(crate) topic: String, - pub(crate) features: Vec, + pub(crate) features: HashSet, } fn validate_mqtt_config(config: &MqttConfig) -> Result<(), ValidationError> { diff --git a/src/mqtt/discovery.rs b/src/mqtt/discovery.rs index 8264b97..9cad071 100644 --- a/src/mqtt/discovery.rs +++ b/src/mqtt/discovery.rs @@ -9,7 +9,25 @@ use std::sync::Arc; use super::mqttc::MqttSender; use crate::config::{CameraConfig, MqttDiscoveryConfig}; -use serde::{Serialize, Serializer}; +use serde::{Deserialize, Serialize, Serializer}; + +#[derive(Debug, Deserialize, Clone, PartialEq, Eq, Copy, Hash)] +pub(crate) enum Discoveries { + #[serde(alias = "floodlight", alias = "light")] + Floodlight, + #[serde(alias = "camera")] + Camera, + #[serde(alias = "motion", alias = "md", alias = "pir")] + Motion, + #[serde(alias = "led")] + Led, + #[serde(alias = "ir")] + Ir, + #[serde(alias = "reboot")] + Reboot, + #[serde(alias = "pt")] + Pt, +} #[derive(Debug, Clone)] struct DiscoveryConnection { @@ -210,8 +228,8 @@ pub(crate) async fn enable_discovery( }; for feature in &discovery_config.features { - match feature.as_str() { - "floodlight" => { + match feature { + Discoveries::Floodlight => { let config_data = DiscoveryLight { // Common across all potential features device: device.clone(), @@ -255,7 +273,7 @@ pub(crate) async fn enable_discovery( ) })?; } - "camera" => { + Discoveries::Camera => { let config_data = DiscoveryCamera { // Common across all potential features device: device.clone(), @@ -292,7 +310,7 @@ pub(crate) async fn enable_discovery( ) })?; } - "led" => { + Discoveries::Led => { let config_data = DiscoverySwitch { // Common across all potential features device: device.clone(), @@ -333,7 +351,7 @@ pub(crate) async fn enable_discovery( ) })?; } - "ir" => { + Discoveries::Ir => { let config_data = DiscoverySelect { // Common across all potential features device: device.clone(), @@ -371,7 +389,7 @@ pub(crate) async fn enable_discovery( ) })?; } - "motion" => { + Discoveries::Motion => { let config_data = DiscoveryBinarySensor { // Common across all potential features device: device.clone(), @@ -409,7 +427,7 @@ pub(crate) async fn enable_discovery( ) })?; } - "reboot" => { + Discoveries::Reboot => { let config_data = DiscoveryButton { // Common across all potential features device: device.clone(), @@ -446,7 +464,7 @@ pub(crate) async fn enable_discovery( ) })?; } - "pt" => { + Discoveries::Pt => { for dir in ["left", "right", "up", "down"] { let config_data = DiscoveryButton { // Common across all potential features @@ -485,12 +503,6 @@ pub(crate) async fn enable_discovery( })?; } } - _ => { - error!( - "Unsupported MQTT feature {} for {}", - feature, cam_config.name - ); - } } } diff --git a/src/mqtt/mod.rs b/src/mqtt/mod.rs index 4f8db09..ad806ef 100644 --- a/src/mqtt/mod.rs +++ b/src/mqtt/mod.rs @@ -68,6 +68,7 @@ use anyhow::{anyhow, Context, Error, Result}; pub(crate) use cmdline::Opt; use event_cam::EventCam; pub(crate) use event_cam::{Direction, Messages}; +pub(crate) use discovery::Discoveries; use log::*; use mqttc::{Mqtt, MqttReplyRef};