Merge pull request #1 from mekkanized/liconway/dotenv_config

Add basic config for TM/MQTT host
master
Noah Metz 2024-01-22 20:12:15 -07:00 committed by GitHub
commit 43f270ab28
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 29 additions and 5 deletions

@ -0,0 +1,3 @@
TM_HOST=127.0.0.1
TM_PASSWORD=
MQTT_HOST=localhost

1
.gitignore vendored

@ -1 +1,2 @@
/target
.env

7
Cargo.lock generated

@ -211,6 +211,12 @@ dependencies = [
"crypto-common",
]
[[package]]
name = "dotenv"
version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f"
[[package]]
name = "either"
version = "1.9.0"
@ -1080,6 +1086,7 @@ version = "0.1.0"
dependencies = [
"backtrace-on-stack-overflow",
"bytes",
"dotenv",
"env_logger",
"log",
"openssl",

@ -12,6 +12,7 @@ prost-build = "0.12.3"
[dependencies]
backtrace-on-stack-overflow = "0.3.0"
bytes = "1.5.0"
dotenv = "0.15.0"
env_logger = "0.11.0"
log = "0.4.20"
openssl = "0.10.63"

@ -1,3 +1,5 @@
extern crate dotenv;
use rumqttc::{MqttOptions, Client, QoS, LastWill};
use bytes::Bytes;
use std::time::Duration;
@ -16,6 +18,9 @@ use sha2::{Sha256, Digest};
use std::net::TcpStream;
use std::sync::mpsc;
use dotenv::dotenv;
use std::env;
// MQTT Topics:
// - division/{division_id}/{round}/{match}/score
// - division/{division_id}/ranking
@ -551,7 +556,7 @@ struct TMClient {
const TCP_BUFFER_SIZE: usize = 10000;
impl TMClient {
fn new(uuid: [u8; 16], client_name: [u8; 32], password: String, username: [u8; 16]) -> (TMClient, TMConnection) {
fn new(uuid: [u8; 16], client_name: [u8; 32], host: String, password: String, username: [u8; 16]) -> (TMClient, TMConnection) {
let (work_tx, work_rx) = mpsc::channel();
let (response_tx, response_rx) = mpsc::channel();
let (request_tx, request_rx) = mpsc::channel();
@ -562,7 +567,8 @@ impl TMClient {
let connector = builder.build();
let stream = TcpStream::connect("127.0.0.1:5000").unwrap();
log::debug!("Connecting to TM using address {host}:5000");
let stream = TcpStream::connect(format!("{host}:5000")).unwrap();
let mut stream_config = connector.configure().unwrap();
stream_config.set_verify_hostname(false);
@ -570,7 +576,7 @@ impl TMClient {
stream_config.set_private_key_file("tm.crt", openssl::ssl::SslFiletype::PEM).unwrap();
stream_config.set_use_server_name_indication(false);
let stream = stream_config.connect("127.0.0.1", stream).unwrap();
let stream = stream_config.connect(&host, stream).unwrap();
stream.get_ref().set_read_timeout(Some(Duration::from_millis(100))).expect("Failed to set read timeout on socket");
@ -1269,6 +1275,8 @@ enum Work {
}
fn main() {
dotenv().ok();
env_logger::init();
let mut callbacks: HashMap<tm::NoticeId, NoticeCallback> = HashMap::new();
@ -1280,7 +1288,9 @@ fn main() {
callbacks.insert(tm::NoticeId::NoticeFieldTimerStopped, on_timer_stop);
callbacks.insert(tm::NoticeId::NoticeFieldResetTimer, on_timer_reset);
let mut mqttoptions = MqttOptions::new("vex-bridge", "localhost", 1883);
let mqtt_host = env::var("MQTT_HOST").unwrap_or(String::from("localhost"));
log::debug!("Connecting to MQTT using address {mqtt_host}:1883");
let mut mqttoptions = MqttOptions::new("vex-bridge", mqtt_host, 1883);
mqttoptions.set_keep_alive(Duration::from_secs(5));
mqttoptions.set_last_will(LastWill{
topic: String::from("bridge/status"),
@ -1303,8 +1313,10 @@ fn main() {
rand::thread_rng().fill_bytes(&mut uuid);
let mut client_name = [0u8;32];
rand::thread_rng().fill_bytes(&mut client_name);
let tm_host = env::var("TM_HOST").unwrap_or(String::from("127.0.0.1"));
let username: [u8;16] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
let (mut tm_client, mut tm_connection) = TMClient::new(uuid, client_name, String::from(""), username);
let password = env::var("TM_PASSWORD").unwrap_or(String::from(""));
let (mut tm_client, mut tm_connection) = TMClient::new(uuid, client_name, tm_host, password, username);
let tm_thread = thread::spawn(move ||
while running {
tm_client.process();