From 0c18098e0de4b9da8aa002f12be4699f7fa67aab Mon Sep 17 00:00:00 2001 From: Patrik Torstensson Date: Sat, 20 Jan 2024 20:19:12 -0800 Subject: [PATCH] Cleanup and add readme --- Cargo.lock | 206 ++++++++++++++++++++++++++++++++++++++++++++++++- Cargo.toml | 6 ++ README.md | 9 ++- src/gateway.rs | 4 +- src/main.rs | 6 +- 5 files changed, 221 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 44516df..89cc572 100755 --- a/Cargo.lock +++ b/Cargo.lock @@ -52,6 +52,54 @@ dependencies = [ "libc", ] +[[package]] +name = "anstream" +version = "0.6.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e2e1ebcb11de5c03c67de28a7df593d32191b44939c482e97702baaaa6ab6a5" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7079075b41f533b8c61d2a4d073c4676e1f8b249ff94a393b0595db304e0dd87" + +[[package]] +name = "anstyle-parse" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c75ac65da39e5fe5ab759307499ddad880d724eed2f6ce5b5e8a26f4f387928c" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e28923312444cdd728e4738b3f9c9cac739500909bb3d3c94b43551b16517648" +dependencies = [ + "windows-sys 0.52.0", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1cd54b81ec8d6180e24654d0b371ad22fc3dd083b6ff8ba325b72e00c87660a7" +dependencies = [ + "anstyle", + "windows-sys 0.52.0", +] + [[package]] name = "arrayvec" version = "0.7.4" @@ -82,11 +130,15 @@ dependencies = [ "byte-unit", "clokwerk", "config", + "env_logger 0.11.0", "flexi_logger", "log", "rumqttc", "serde", "serde_json", + "test-log", + "tracing", + "tracing-subscriber", ] [[package]] @@ -260,6 +312,12 @@ dependencies = [ "chrono", ] +[[package]] +name = "colorchoice" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" + [[package]] name = "config" version = "0.13.4" @@ -339,6 +397,38 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0688c2a7f92e427f44895cd63841bff7b29f8d7a1648b9e7e07a4a365b2e1257" +[[package]] +name = "env_filter" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a009aa4810eb158359dda09d0c87378e4bbb89b5a801f016885a4707ba24f7ea" +dependencies = [ + "log", + "regex", +] + +[[package]] +name = "env_logger" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4cd405aab171cb85d6735e5c8d9db038c17d3ca007a4d2c25f337935c3d90580" +dependencies = [ + "log", +] + +[[package]] +name = "env_logger" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9eeb342678d785662fd2514be38c459bb925f02b68dd2a3e0f21d7ef82d979dd" +dependencies = [ + "anstream", + "anstyle", + "env_filter", + "humantime", + "log", +] + [[package]] name = "equivalent" version = "1.0.1" @@ -493,6 +583,12 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d77f7ec81a6d05a3abb01ab6eb7590f6083d08449fe5a1c8b1e620283546ccb7" +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + [[package]] name = "iana-time-zone" version = "0.1.58" @@ -613,6 +709,15 @@ version = "0.4.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" +[[package]] +name = "matchers" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8263075bb86c5a1b1427b5ae862e8889656f126e9f77c484496e8b47cf5c5558" +dependencies = [ + "regex-automata 0.1.10", +] + [[package]] name = "memchr" version = "2.6.4" @@ -894,8 +999,17 @@ checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343" dependencies = [ "aho-corasick", "memchr", - "regex-automata", - "regex-syntax", + "regex-automata 0.4.3", + "regex-syntax 0.8.2", +] + +[[package]] +name = "regex-automata" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" +dependencies = [ + "regex-syntax 0.6.29", ] [[package]] @@ -906,9 +1020,15 @@ checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f" dependencies = [ "aho-corasick", "memchr", - "regex-syntax", + "regex-syntax 0.8.2", ] +[[package]] +name = "regex-syntax" +version = "0.6.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" + [[package]] name = "regex-syntax" version = "0.8.2" @@ -1187,6 +1307,15 @@ dependencies = [ "digest", ] +[[package]] +name = "sharded-slab" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" +dependencies = [ + "lazy_static", +] + [[package]] name = "simdutf8" version = "0.1.4" @@ -1261,6 +1390,27 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" +[[package]] +name = "test-log" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6159ab4116165c99fc88cce31f99fa2c9dbe08d3691cb38da02fc3b45f357d2b" +dependencies = [ + "env_logger 0.10.2", + "test-log-macros", +] + +[[package]] +name = "test-log-macros" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ba277e77219e9eea169e8508942db1bf5d8a41ff2db9b20aab5a5aadc9fa25d" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.43", +] + [[package]] name = "thiserror" version = "1.0.52" @@ -1281,6 +1431,16 @@ dependencies = [ "syn 2.0.43", ] +[[package]] +name = "thread_local" +version = "1.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fdd6f064ccff2d6567adcb3873ca630700f00b5ad3f060c25b5dcfd9a4ce152" +dependencies = [ + "cfg-if", + "once_cell", +] + [[package]] name = "tinyvec" version = "1.6.0" @@ -1359,6 +1519,40 @@ dependencies = [ "winnow", ] +[[package]] +name = "tracing" +version = "0.1.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" +dependencies = [ + "pin-project-lite", + "tracing-core", +] + +[[package]] +name = "tracing-core" +version = "0.1.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" +dependencies = [ + "once_cell", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b" +dependencies = [ + "matchers", + "once_cell", + "regex", + "sharded-slab", + "thread_local", + "tracing", + "tracing-core", +] + [[package]] name = "typenum" version = "1.17.0" @@ -1415,6 +1609,12 @@ version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "86bd8d4e895da8537e5315b8254664e6b769c4ff3db18321b297a1e7004392e3" +[[package]] +name = "utf8parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" + [[package]] name = "uuid" version = "1.6.1" diff --git a/Cargo.toml b/Cargo.toml index 9766a7e..49f0532 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,3 +14,9 @@ clokwerk = "0.4.0" byte-unit = { version = "5.1.2" } flexi_logger = { version = "0.27.3", features = ["compress"] } rumqttc = { version = "0.23.0", features = ["url"] } +test-log = "0.2.14" + +[dev-dependencies] +env_logger = "*" +tracing = {version = "0.1", default-features = false} +tracing-subscriber = {version = "0.3", default-features = false, features = ["env-filter", "fmt"]} diff --git a/README.md b/README.md index 6250aaf..c9157d1 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,11 @@ [![Docker Image CI](https://github.com/ptorsten/aw-gateway-rs/actions/workflows/docker-image.yml/badge.svg)](https://github.com/ptorsten/aw-gateway-rs/actions/workflows/docker-image.yml) +Service to poll AW gateways (like OBSERVER IP) and report sensors to Home Assistant -TODO +- Supports custom sensor definitions +- Supports auto-discovery for Home Assistant for sensors +- Docker support + +TODO: + +- Testing diff --git a/src/gateway.rs b/src/gateway.rs index e9e9e3e..e38302d 100755 --- a/src/gateway.rs +++ b/src/gateway.rs @@ -2,7 +2,7 @@ // Protocol: // https://osswww.ecowitt.net/uploads/20210716/WN1900%20GW1000,1100%20WH2680,2650%20telenet%20v1.6.0%20.pdf // -use std::{collections::HashMap, sync::{Arc, Mutex}, time::Duration}; +use std::{collections::HashMap, time::Duration}; use std::net::{TcpStream, SocketAddr, Ipv4Addr}; use std::str::{self, FromStr}; use std::io::{Read, Write, Error, ErrorKind}; @@ -128,7 +128,7 @@ impl SensorGateway { mac_address: Option::None, }; - gateway.update_sensor_metadata(); + let _not_used = gateway.update_sensor_metadata(); if let Ok(firmware) = gateway.get_firmware_version() { gateway.firmware = Some(firmware); } diff --git a/src/main.rs b/src/main.rs index 6468980..1777bbb 100755 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ use std::{fs::File, io::BufReader, collections::HashMap, sync::{Mutex, Arc}, time::Duration}; use clokwerk::Interval; use flexi_logger::{LoggerHandle, Logger, Criterion, FileSpec, Naming, Cleanup, Duplicate}; -use gateway::{SensorGateway, SensorData, SensorValue, SensorMetadata}; +use gateway::{SensorGateway, SensorData, SensorValue}; use rumqttc::{MqttOptions, Client, QoS, NetworkOptions}; use serde::{Deserialize, Serialize}; use serde_json::json; @@ -137,7 +137,6 @@ struct Gateway { gateway: SensorGateway, sensor_config: Mutex>, discovered_sensor: Mutex>, - metadata: Mutex>, } struct Gateways { @@ -154,7 +153,6 @@ impl Gateway { sensor_config: Mutex::new(sensor_config), discovered_sensor: Mutex::new(HashMap::new()), mqtt: mqtt, - metadata: Mutex::new(HashMap::new()), } } @@ -175,7 +173,7 @@ impl Gateway { } fn sent_discovery(&self, name: &str) -> bool { - let l_discovered = self.discovered_sensor.lock().expect("Failed to lock discovery mutex"); + let l_discovered: std::sync::MutexGuard<'_, HashMap> = self.discovered_sensor.lock().expect("Failed to lock discovery mutex"); l_discovered.contains_key(name) }