From 3189dc23f2abf2060f591219e8256301e7c41aed Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Fri, 20 Sep 2024 16:11:56 +0200 Subject: Generate mux json --- src/config.rs | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 97 insertions(+), 1 deletion(-) (limited to 'src/config.rs') diff --git a/src/config.rs b/src/config.rs index 54f94dd..64f3c09 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,11 +1,13 @@ -use std::fs; +use std::{collections::HashMap, fs}; use anyhow::Context; use serde::{Deserialize, Serialize}; +use serde_json::json; type Protection = u8; #[derive(Debug, Serialize, Deserialize, Clone)] pub struct Service { + pub unique_id: String, pub sid: u32, pub ecc: u8, pub label: String, @@ -23,6 +25,30 @@ impl Service { pub fn ecc_hex(&self) -> String { format!("{:02X}", self.ecc) } + + pub fn dump_to_service_json(&self) -> serde_json::Value { + json!({ + "id": self.sid, + "ecc": self.ecc, + "label": self.label, + "shortlabel": self.shortlabel, + }) + } + + pub fn dump_to_subchannel_json(&self, id: u32) -> serde_json::Value { + json!({ + "type": "dabplus", + "bitrate": self.bitrate, + "id": id, + "protection": self.protection, + + "inputproto": "edi", + "inputuri": format!("tcp://127.0.0.1:{}", self.input_port), + "buffer-management": "prebuffering", + "buffer": 40, + "prebuffering": 20 + }) + } } #[derive(Debug, Serialize, Deserialize, Clone)] @@ -62,6 +88,7 @@ impl Default for Config { output_edi_port: 8951, services: vec![ Service { + unique_id: "nothing".to_owned(), sid: 0x4DAA, ecc: 0xE1, label: "nothing".to_owned(), @@ -92,4 +119,73 @@ impl Config { fs::write(CONFIGFILE, toml::to_string_pretty(&self)?) .context("writing config file") } + + pub fn dump_to_json(&self) -> serde_json::Value { + let now = chrono::Utc::now().to_rfc3339(); + + let mut services = HashMap::new(); + for s in &self.services { + let uid = format!("srv-{}", s.unique_id); + services.insert(uid, s.dump_to_service_json()); + } + + let mut subchannels = HashMap::new(); + let mut id = 0; + for s in &self.services { + id += 1; + let uid = format!("sub-{}", s.unique_id); + subchannels.insert(uid, s.dump_to_subchannel_json(id)); + } + + let mut components = HashMap::new(); + for s in &self.services { + components.insert( + format!("comp-{}", s.unique_id), + json!({ + "service": format!("srv-{}", s.unique_id), + "subchannel": format!("sub-{}", s.unique_id), + "user-applications": { + "userapp": "slideshow" + } + })); + } + + json!({ + "_comment": format!("Generated at {} by odr-dabmux-gui", now), + "general": { + "dabmode": 1, + "nbframes": 0, + "syslog": false, + "tist": self.tist, + "tist_offset": self.tist_offset, + "managementport": 12720 + }, + "remotecontrol": { + "telnetport": 12721, + "zmqendpoint": "tcp://lo:12722" + }, + "ensemble": { + "id": self.ensemble_id, + "ecc": self.ensemble_ecc, + "local-time-offset": "auto", + "reconfig-counter": "hash", + "label": self.ensemble_label, + "shortlabel": self.ensemble_shortlabel + }, + "services": services, + "subchannels": subchannels, + "components": components, + "outputs": { + "throttle": "simul://", + "edi": { + "destinations": { + "example_tcp": { + "protocol": "tcp", + "listenport": self.output_edi_port + } + } + } + } + }) + } } -- cgit v1.2.3