internal data sources (clock, static text), connection indicator, rendering fixes

DataSourceConfig refactored to enum: External/Clock/StaticText. Clock
generates formatted time via chrono, static text emits configured string.

ESP32: connection status indicator (green/red dot bottom-right), per-widget
clear before redraw, RenderEvent enum for local + server messages.

Polling uses DataUpdate instead of ScreenUpdate to avoid wiping widget state.
Empty mappings passthrough raw source data for internal sources.
This commit is contained in:
2026-06-19 11:26:49 +02:00
parent b448fa15fe
commit a51d22649a
25 changed files with 630 additions and 214 deletions

117
Cargo.lock generated
View File

@@ -52,6 +52,15 @@ version = "0.2.21"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923" checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923"
[[package]]
name = "android_system_properties"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "anyhow" name = "anyhow"
version = "1.0.102" version = "1.0.102"
@@ -217,6 +226,8 @@ version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"application", "application",
"chrono",
"chrono-tz",
"config-sqlite", "config-sqlite",
"domain", "domain",
"dotenvy", "dotenvy",
@@ -266,6 +277,29 @@ version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
[[package]]
name = "chrono"
version = "0.4.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1aa79e62e7697b8e29b513a68abacf485adcd1fe8284a4316c5ae868e6633327"
dependencies = [
"iana-time-zone",
"js-sys",
"num-traits",
"wasm-bindgen",
"windows-link",
]
[[package]]
name = "chrono-tz"
version = "0.10.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a6139a8597ed92cf816dfb33f5dd6cf0bb93a6adc938f11039f371bc5bcd26c3"
dependencies = [
"chrono",
"phf",
]
[[package]] [[package]]
name = "cipher" name = "cipher"
version = "0.4.4" version = "0.4.4"
@@ -974,6 +1008,30 @@ dependencies = [
"windows-registry", "windows-registry",
] ]
[[package]]
name = "iana-time-zone"
version = "0.1.65"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e31bc9ad994ba00e440a8aa5c9ef0ec67d5cb5e5cb0cc7f8b744a35b389cc470"
dependencies = [
"android_system_properties",
"core-foundation-sys",
"iana-time-zone-haiku",
"js-sys",
"log",
"wasm-bindgen",
"windows-core",
]
[[package]]
name = "iana-time-zone-haiku"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f"
dependencies = [
"cc",
]
[[package]] [[package]]
name = "icu_collections" name = "icu_collections"
version = "2.2.0" version = "2.2.0"
@@ -1502,6 +1560,24 @@ version = "2.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220"
[[package]]
name = "phf"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "913273894cec178f401a31ec4b656318d95473527be05c0752cc41cdc32be8b7"
dependencies = [
"phf_shared",
]
[[package]]
name = "phf_shared"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06005508882fb681fd97892ecff4b7fd0fee13ef1aa569f8695dae7ab9099981"
dependencies = [
"siphasher",
]
[[package]] [[package]]
name = "pin-project-lite" name = "pin-project-lite"
version = "0.2.17" version = "0.2.17"
@@ -2017,6 +2093,12 @@ dependencies = [
"time", "time",
] ]
[[package]]
name = "siphasher"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ee5873ec9cce0195efcb7a4e9507a04cd49aec9c83d0389df45b1ef7ba2e649"
[[package]] [[package]]
name = "slab" name = "slab"
version = "0.4.12" version = "0.4.12"
@@ -2842,6 +2924,41 @@ dependencies = [
"wasite", "wasite",
] ]
[[package]]
name = "windows-core"
version = "0.62.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8e83a14d34d0623b51dce9581199302a221863196a1dde71a7663a4c2be9deb"
dependencies = [
"windows-implement",
"windows-interface",
"windows-link",
"windows-result",
"windows-strings",
]
[[package]]
name = "windows-implement"
version = "0.60.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "windows-interface"
version = "0.59.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "windows-link" name = "windows-link"
version = "0.2.1" version = "0.2.1"

View File

@@ -57,3 +57,5 @@ postcard = { version = "1.1", default-features = false, features = ["alloc"] }
tokio = { version = "1.0", features = ["macros", "rt", "rt-multi-thread", "net", "sync", "time", "io-util"] } tokio = { version = "1.0", features = ["macros", "rt", "rt-multi-thread", "net", "sync", "time", "io-util"] }
tower = "0.5" tower = "0.5"
reqwest = { version = "0.12", features = ["json"] } reqwest = { version = "0.12", features = ["json"] }
chrono = "0.4"
chrono-tz = "0.10"

View File

@@ -18,6 +18,8 @@ pub fn data_source_type_to_str(t: &DataSourceType) -> &'static str {
DataSourceType::Rss => "rss", DataSourceType::Rss => "rss",
DataSourceType::HttpJson => "http_json", DataSourceType::HttpJson => "http_json",
DataSourceType::Webhook => "webhook", DataSourceType::Webhook => "webhook",
DataSourceType::Clock => "clock",
DataSourceType::StaticText => "static_text",
} }
} }
@@ -28,6 +30,8 @@ fn data_source_type_from_str(s: &str) -> Result<DataSourceType, SqliteConfigErro
"rss" => Ok(DataSourceType::Rss), "rss" => Ok(DataSourceType::Rss),
"http_json" => Ok(DataSourceType::HttpJson), "http_json" => Ok(DataSourceType::HttpJson),
"webhook" => Ok(DataSourceType::Webhook), "webhook" => Ok(DataSourceType::Webhook),
"clock" => Ok(DataSourceType::Clock),
"static_text" => Ok(DataSourceType::StaticText),
_ => Err(SqliteConfigError::Serialization(format!( _ => Err(SqliteConfigError::Serialization(format!(
"unknown source type: {s}" "unknown source type: {s}"
))), ))),
@@ -38,33 +42,54 @@ pub fn data_source_config_to_json(
config: &DataSourceConfig, config: &DataSourceConfig,
secrets: Option<&(dyn SecretStore + Send + Sync)>, secrets: Option<&(dyn SecretStore + Send + Sync)>,
) -> Result<String, SqliteConfigError> { ) -> Result<String, SqliteConfigError> {
let api_key = config.api_key.as_ref().map(|k| match secrets { let v = match config {
Some(s) => s.encrypt(k), DataSourceConfig::External {
None => k.clone(), url,
}); headers,
api_key,
} => {
let api_key = api_key.as_ref().map(|k| match secrets {
Some(s) => s.encrypt(k),
None => k.clone(),
});
let headers: Vec<(String, String)> = config let headers: Vec<(String, String)> = headers
.headers .iter()
.iter() .map(|(k, v)| {
.map(|(k, v)| { let val = if is_sensitive_key(k) {
let val = if is_sensitive_key(k) { match secrets {
match secrets { Some(s) => s.encrypt(v),
Some(s) => s.encrypt(v), None => v.clone(),
None => v.clone(), }
} } else {
} else { v.clone()
v.clone() };
}; (k.clone(), val)
(k.clone(), val) })
}) .collect();
.collect();
let v = serde_json::json!({ serde_json::json!({
"url": config.url, "type": "external",
"headers": headers, "url": url,
"api_key": api_key, "headers": headers,
"encrypted": secrets.is_some(), "api_key": api_key,
}); "encrypted": secrets.is_some(),
})
}
DataSourceConfig::Clock { format, timezone } => {
serde_json::json!({
"type": "clock",
"format": format,
"timezone": timezone,
})
}
DataSourceConfig::StaticText { text } => {
serde_json::json!({
"type": "static_text",
"text": text,
})
}
};
serde_json::to_string(&v).map_err(|e| SqliteConfigError::Serialization(e.to_string())) serde_json::to_string(&v).map_err(|e| SqliteConfigError::Serialization(e.to_string()))
} }
@@ -75,47 +100,61 @@ fn data_source_config_from_json(
let v: serde_json::Value = let v: serde_json::Value =
serde_json::from_str(json).map_err(|e| SqliteConfigError::Serialization(e.to_string()))?; serde_json::from_str(json).map_err(|e| SqliteConfigError::Serialization(e.to_string()))?;
let encrypted = v["encrypted"].as_bool().unwrap_or(false); let config_type = v["type"].as_str().unwrap_or("external");
let url = v["url"].as_str().map(String::from); match config_type {
"clock" => {
let api_key = v["api_key"].as_str().map(|k| { let format = v["format"].as_str().unwrap_or("%H:%M:%S").to_string();
if encrypted { let timezone = v["timezone"].as_str().unwrap_or("UTC").to_string();
match secrets { Ok(DataSourceConfig::Clock { format, timezone })
Some(s) => s.decrypt(k),
None => k.to_string(),
}
} else {
k.to_string()
} }
}); "static_text" => {
let text = v["text"].as_str().unwrap_or("").to_string();
Ok(DataSourceConfig::StaticText { text })
}
_ => {
let encrypted = v["encrypted"].as_bool().unwrap_or(false);
let url = v["url"].as_str().map(String::from);
let headers = match v["headers"].as_array() { let api_key = v["api_key"].as_str().map(|k| {
Some(arr) => arr if encrypted {
.iter()
.filter_map(|h| {
let pair = h.as_array()?;
let key: String = pair[0].as_str()?.into();
let raw_val: &str = pair[1].as_str()?;
let val = if encrypted && is_sensitive_key(&key) {
match secrets { match secrets {
Some(s) => s.decrypt(raw_val), Some(s) => s.decrypt(k),
None => raw_val.to_string(), None => k.to_string(),
} }
} else { } else {
raw_val.to_string() k.to_string()
}; }
Some((key, val)) });
})
.collect(),
None => vec![],
};
Ok(DataSourceConfig { let headers = match v["headers"].as_array() {
url, Some(arr) => arr
headers, .iter()
api_key, .filter_map(|h| {
}) let pair = h.as_array()?;
let key: String = pair[0].as_str()?.into();
let raw_val: &str = pair[1].as_str()?;
let val = if encrypted && is_sensitive_key(&key) {
match secrets {
Some(s) => s.decrypt(raw_val),
None => raw_val.to_string(),
}
} else {
raw_val.to_string()
};
Some((key, val))
})
.collect(),
None => vec![],
};
Ok(DataSourceConfig::External {
url,
headers,
api_key,
})
}
}
} }
pub fn data_source_from_row( pub fn data_source_from_row(

View File

@@ -36,7 +36,7 @@ fn weather_source() -> DataSource {
name: "openweather".into(), name: "openweather".into(),
source_type: DataSourceType::Weather, source_type: DataSourceType::Weather,
poll_interval: Duration::from_secs(300), poll_interval: Duration::from_secs(300),
config: DataSourceConfig { config: DataSourceConfig::External {
url: Some("https://api.openweather.org".into()), url: Some("https://api.openweather.org".into()),
headers: vec![], headers: vec![],
api_key: Some("test-key".into()), api_key: Some("test-key".into()),
@@ -125,8 +125,13 @@ async fn save_and_retrieve_data_source() {
assert_eq!(ds.name, "openweather"); assert_eq!(ds.name, "openweather");
assert_eq!(ds.source_type, DataSourceType::Weather); assert_eq!(ds.source_type, DataSourceType::Weather);
assert_eq!(ds.poll_interval, Duration::from_secs(300)); assert_eq!(ds.poll_interval, Duration::from_secs(300));
assert_eq!(ds.config.url, Some("https://api.openweather.org".into())); match &ds.config {
assert_eq!(ds.config.api_key, Some("test-key".into())); DataSourceConfig::External { url, api_key, .. } => {
assert_eq!(*url, Some("https://api.openweather.org".into()));
assert_eq!(*api_key, Some("test-key".into()));
}
_ => panic!("expected External config"),
}
} }
#[tokio::test] #[tokio::test]

View File

@@ -171,9 +171,7 @@ async fn create_and_get_data_source() {
"name": "weather_api", "name": "weather_api",
"source_type": "weather", "source_type": "weather",
"poll_interval_secs": 300, "poll_interval_secs": 300,
"url": "https://api.openweather.org", "config": {"type": "external", "url": "https://api.openweather.org", "api_key": "test-key", "headers": []}
"api_key": "test-key",
"headers": []
}"#; }"#;
let resp = app let resp = app

View File

@@ -47,15 +47,23 @@ impl DataSourcePort for HttpJsonAdapter {
type Error = HttpJsonError; type Error = HttpJsonError;
async fn poll(&self, source: &DataSource) -> Result<Value, Self::Error> { async fn poll(&self, source: &DataSource) -> Result<Value, Self::Error> {
let url = source.config.url.as_ref().ok_or(HttpJsonError::NoUrl)?; let domain::DataSourceConfig::External {
ref url,
ref headers,
ref api_key,
} = source.config
else {
return Err(HttpJsonError::NoUrl);
};
let url = url.as_ref().ok_or(HttpJsonError::NoUrl)?;
let mut req = self.client.get(url); let mut req = self.client.get(url);
for (key, val) in &source.config.headers { for (key, val) in headers {
req = req.header(key, val); req = req.header(key, val);
} }
if let Some(api_key) = &source.config.api_key { if let Some(api_key) = api_key {
req = req.header("Authorization", format!("Bearer {api_key}")); req = req.header("Authorization", format!("Bearer {api_key}"));
} }

View File

@@ -34,7 +34,7 @@ fn make_source(url: String) -> DataSource {
name: "test".into(), name: "test".into(),
source_type: DataSourceType::HttpJson, source_type: DataSourceType::HttpJson,
poll_interval: Duration::from_secs(60), poll_interval: Duration::from_secs(60),
config: DataSourceConfig { config: DataSourceConfig::External {
url: Some(url), url: Some(url),
headers: vec![], headers: vec![],
api_key: None, api_key: None,
@@ -82,7 +82,7 @@ async fn returns_error_when_no_url() {
name: "bad".into(), name: "bad".into(),
source_type: DataSourceType::HttpJson, source_type: DataSourceType::HttpJson,
poll_interval: Duration::from_secs(60), poll_interval: Duration::from_secs(60),
config: DataSourceConfig { config: DataSourceConfig::External {
url: None, url: None,
headers: vec![], headers: vec![],
api_key: None, api_key: None,

View File

@@ -38,11 +38,19 @@ impl DataSourcePort for MediaAdapter {
type Error = MediaError; type Error = MediaError;
async fn poll(&self, source: &DataSource) -> Result<Value, Self::Error> { async fn poll(&self, source: &DataSource) -> Result<Value, Self::Error> {
let base_url = source.config.url.as_ref().ok_or(MediaError::NoUrl)?; let domain::DataSourceConfig::External {
let username = find_header(&source.config.headers, "username") ref url,
.ok_or(MediaError::MissingField("username"))?; ref headers,
let password = find_header(&source.config.headers, "password") ..
.ok_or(MediaError::MissingField("password"))?; } = source.config
else {
return Err(MediaError::NoUrl);
};
let base_url = url.as_ref().ok_or(MediaError::NoUrl)?;
let username =
find_header(headers, "username").ok_or(MediaError::MissingField("username"))?;
let password =
find_header(headers, "password").ok_or(MediaError::MissingField("password"))?;
let salt: String = (0..12).map(|_| fastrand::alphanumeric()).collect(); let salt: String = (0..12).map(|_| fastrand::alphanumeric()).collect();
let token = subsonic_token(password, &salt); let token = subsonic_token(password, &salt);

View File

@@ -45,7 +45,7 @@ fn make_source(url: String) -> DataSource {
name: "navidrome".into(), name: "navidrome".into(),
source_type: DataSourceType::Media, source_type: DataSourceType::Media,
poll_interval: Duration::from_secs(5), poll_interval: Duration::from_secs(5),
config: DataSourceConfig { config: DataSourceConfig::External {
url: Some(url), url: Some(url),
headers: vec![ headers: vec![
("username".into(), "test".into()), ("username".into(), "test".into()),

View File

@@ -28,7 +28,10 @@ impl DataSourcePort for RssAdapter {
type Error = RssError; type Error = RssError;
async fn poll(&self, source: &DataSource) -> Result<Value, Self::Error> { async fn poll(&self, source: &DataSource) -> Result<Value, Self::Error> {
let url = source.config.url.as_ref().ok_or(RssError::NoUrl)?; let domain::DataSourceConfig::External { ref url, .. } = source.config else {
return Err(RssError::NoUrl);
};
let url = url.as_ref().ok_or(RssError::NoUrl)?;
let resp = self let resp = self
.client .client

View File

@@ -2,60 +2,128 @@ use domain::*;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::time::Duration; use std::time::Duration;
#[derive(Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum DataSourceConfigDto {
#[serde(rename = "external")]
External {
#[serde(default)]
url: Option<String>,
#[serde(default)]
api_key: Option<String>,
#[serde(default)]
headers: Vec<(String, String)>,
},
#[serde(rename = "clock")]
Clock {
#[serde(default = "default_clock_format")]
format: String,
#[serde(default = "default_timezone")]
timezone: String,
},
#[serde(rename = "static_text")]
StaticText {
#[serde(default)]
text: String,
},
}
fn default_clock_format() -> String {
"%H:%M:%S".into()
}
fn default_timezone() -> String {
"UTC".into()
}
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub struct DataSourceDto { pub struct DataSourceDto {
pub id: u16, pub id: u16,
pub name: String, pub name: String,
pub source_type: String, pub source_type: String,
pub poll_interval_secs: u64, pub poll_interval_secs: u64,
pub url: Option<String>, pub config: DataSourceConfigDto,
pub api_key: Option<String>, }
pub headers: Vec<(String, String)>,
fn source_type_to_str(t: &DataSourceType) -> &'static str {
match t {
DataSourceType::Weather => "weather",
DataSourceType::Media => "media",
DataSourceType::Rss => "rss",
DataSourceType::HttpJson => "http_json",
DataSourceType::Webhook => "webhook",
DataSourceType::Clock => "clock",
DataSourceType::StaticText => "static_text",
}
}
fn source_type_from_str(s: &str) -> Result<DataSourceType, String> {
match s {
"weather" => Ok(DataSourceType::Weather),
"media" => Ok(DataSourceType::Media),
"rss" => Ok(DataSourceType::Rss),
"http_json" => Ok(DataSourceType::HttpJson),
"webhook" => Ok(DataSourceType::Webhook),
"clock" => Ok(DataSourceType::Clock),
"static_text" => Ok(DataSourceType::StaticText),
t => Err(format!("unknown source_type: {t}")),
}
} }
impl From<&DataSource> for DataSourceDto { impl From<&DataSource> for DataSourceDto {
fn from(ds: &DataSource) -> Self { fn from(ds: &DataSource) -> Self {
let config = match &ds.config {
DataSourceConfig::External {
url,
api_key,
headers,
} => DataSourceConfigDto::External {
url: url.clone(),
api_key: api_key.clone(),
headers: headers.clone(),
},
DataSourceConfig::Clock { format, timezone } => DataSourceConfigDto::Clock {
format: format.clone(),
timezone: timezone.clone(),
},
DataSourceConfig::StaticText { text } => {
DataSourceConfigDto::StaticText { text: text.clone() }
}
};
Self { Self {
id: ds.id, id: ds.id,
name: ds.name.clone(), name: ds.name.clone(),
source_type: match ds.source_type { source_type: source_type_to_str(&ds.source_type).into(),
DataSourceType::Weather => "weather",
DataSourceType::Media => "media",
DataSourceType::Rss => "rss",
DataSourceType::HttpJson => "http_json",
DataSourceType::Webhook => "webhook",
}
.into(),
poll_interval_secs: ds.poll_interval.as_secs(), poll_interval_secs: ds.poll_interval.as_secs(),
url: ds.config.url.clone(), config,
api_key: ds.config.api_key.clone(),
headers: ds.config.headers.clone(),
} }
} }
} }
impl DataSourceDto { impl DataSourceDto {
pub fn into_domain(self) -> Result<DataSource, String> { pub fn into_domain(self) -> Result<DataSource, String> {
let source_type = match self.source_type.as_str() { let source_type = source_type_from_str(&self.source_type)?;
"weather" => DataSourceType::Weather, let config = match self.config {
"media" => DataSourceType::Media, DataSourceConfigDto::External {
url,
"rss" => DataSourceType::Rss, api_key,
"http_json" => DataSourceType::HttpJson, headers,
"webhook" => DataSourceType::Webhook, } => DataSourceConfig::External {
t => return Err(format!("unknown source_type: {t}")), url,
api_key,
headers,
},
DataSourceConfigDto::Clock { format, timezone } => {
DataSourceConfig::Clock { format, timezone }
}
DataSourceConfigDto::StaticText { text } => DataSourceConfig::StaticText { text },
}; };
Ok(DataSource { Ok(DataSource {
id: self.id, id: self.id,
name: self.name, name: self.name,
source_type, source_type,
poll_interval: Duration::from_secs(self.poll_interval_secs), poll_interval: Duration::from_secs(self.poll_interval_secs),
config: DataSourceConfig { config,
url: self.url,
api_key: self.api_key,
headers: self.headers,
},
}) })
} }
} }

View File

@@ -47,7 +47,7 @@ async fn create_data_source_rejects_invalid() {
name: "bad".into(), name: "bad".into(),
source_type: DataSourceType::HttpJson, source_type: DataSourceType::HttpJson,
poll_interval: Duration::from_secs(60), poll_interval: Duration::from_secs(60),
config: DataSourceConfig { config: DataSourceConfig::External {
url: None, url: None,
headers: vec![], headers: vec![],
api_key: None, api_key: None,
@@ -70,7 +70,7 @@ async fn create_data_source_persists_valid_and_emits_event() {
name: "weather".into(), name: "weather".into(),
source_type: DataSourceType::Weather, source_type: DataSourceType::Weather,
poll_interval: Duration::from_secs(300), poll_interval: Duration::from_secs(300),
config: DataSourceConfig { config: DataSourceConfig::External {
url: Some("https://api.weather.com".into()), url: Some("https://api.weather.com".into()),
headers: vec![], headers: vec![],
api_key: None, api_key: None,

View File

@@ -19,3 +19,5 @@ anyhow.workspace = true
tracing.workspace = true tracing.workspace = true
tracing-subscriber.workspace = true tracing-subscriber.workspace = true
dotenvy.workspace = true dotenvy.workspace = true
chrono.workspace = true
chrono-tz.workspace = true

View File

@@ -1,13 +1,16 @@
use anyhow::Result; use anyhow::Result;
use application::DataProjection; use application::DataProjection;
use chrono::Utc;
use chrono_tz::Tz;
use config_sqlite::SqliteConfigStore; use config_sqlite::SqliteConfigStore;
use domain::{ use domain::{
BroadcastPort, ConfigRepository, DataSource, DataSourcePort, DataSourceType, Value, WidgetState, BroadcastPort, ConfigRepository, DataSource, DataSourceConfig, DataSourcePort, DataSourceType,
Value, WidgetState,
}; };
use http_json::HttpJsonAdapter; use http_json::HttpJsonAdapter;
use media_adapter::MediaAdapter; use media_adapter::MediaAdapter;
use rss_adapter::RssAdapter; use rss_adapter::RssAdapter;
use std::collections::HashMap; use std::collections::{BTreeMap, HashMap};
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
use tcp_server::TcpBroadcaster; use tcp_server::TcpBroadcaster;
@@ -117,14 +120,6 @@ async fn poll_loop(
} }
}; };
let layout = match config.get_layout().await {
Ok(l) => l,
Err(e) => {
warn!(error = %e, "failed to fetch layout");
continue;
}
};
let changed: Vec<(u16, WidgetState)> = projection let changed: Vec<(u16, WidgetState)> = projection
.apply_poll_result(source.id, &result, &widgets) .apply_poll_result(source.id, &result, &widgets)
.await; .await;
@@ -137,9 +132,7 @@ async fn poll_loop(
Some((*id, hint, state.clone())) Some((*id, hint, state.clone()))
}) })
.collect(); .collect();
if let Some(l) = &layout if let Err(e) = broadcaster.push_data_update(&with_hints).await {
&& let Err(e) = broadcaster.push_screen_update(l, &with_hints).await
{
warn!(error = %e, "failed to push update"); warn!(error = %e, "failed to push update");
} }
info!(source = %source.name, count = changed.len(), "pushed widget updates"); info!(source = %source.name, count = changed.len(), "pushed widget updates");
@@ -166,8 +159,33 @@ async fn poll_source(
.poll(source) .poll(source)
.await .await
.map_err(|e| anyhow::anyhow!("{e}")), .map_err(|e| anyhow::anyhow!("{e}")),
DataSourceType::Clock => Ok(generate_clock(&source.config)),
DataSourceType::StaticText => Ok(generate_static_text(&source.config)),
DataSourceType::Webhook => Err(anyhow::anyhow!( DataSourceType::Webhook => Err(anyhow::anyhow!(
"webhook sources are push-based, not polled" "webhook sources are push-based, not polled"
)), )),
} }
} }
fn generate_clock(config: &DataSourceConfig) -> Value {
let (fmt, tz_name) = match config {
DataSourceConfig::Clock { format, timezone } => (format.as_str(), timezone.as_str()),
_ => ("%H:%M:%S", "UTC"),
};
let tz: Tz = tz_name.parse().unwrap_or(chrono_tz::UTC);
let now = Utc::now().with_timezone(&tz);
let formatted = now.format(fmt).to_string();
let mut map = BTreeMap::new();
map.insert("time".into(), Value::String(formatted));
Value::Object(map)
}
fn generate_static_text(config: &DataSourceConfig) -> Value {
let text = match config {
DataSourceConfig::StaticText { text } => text.clone(),
_ => String::new(),
};
let mut map = BTreeMap::new();
map.insert("text".into(), Value::String(text));
Value::Object(map)
}

View File

@@ -3,7 +3,7 @@ use embedded_graphics::{
mono_font::{ascii::FONT_6X10, ascii::FONT_10X20, MonoTextStyle}, mono_font::{ascii::FONT_6X10, ascii::FONT_10X20, MonoTextStyle},
pixelcolor::Rgb565, pixelcolor::Rgb565,
prelude::*, prelude::*,
primitives::{PrimitiveStyle, Rectangle}, primitives::{Circle, PrimitiveStyle, Rectangle},
text::Text, text::Text,
}; };
@@ -27,6 +27,7 @@ pub struct Esp32DisplayAdapter {
trait ErasedDisplay { trait ErasedDisplay {
fn draw_text_span(&mut self, text: &str, x: u16, y: u16, color: Rgb565, font: FontSize) -> Result<(), DisplayError>; fn draw_text_span(&mut self, text: &str, x: u16, y: u16, color: Rgb565, font: FontSize) -> Result<(), DisplayError>;
fn fill_rect(&mut self, bounds: BoundingBox, color: Rgb565) -> Result<(), DisplayError>; fn fill_rect(&mut self, bounds: BoundingBox, color: Rgb565) -> Result<(), DisplayError>;
fn fill_circle(&mut self, x: u16, y: u16, diameter: u16, color: Rgb565) -> Result<(), DisplayError>;
fn flush(&mut self) -> Result<(), DisplayError>; fn flush(&mut self) -> Result<(), DisplayError>;
} }
@@ -57,6 +58,14 @@ where
Ok(()) Ok(())
} }
fn fill_circle(&mut self, x: u16, y: u16, diameter: u16, color: Rgb565) -> Result<(), DisplayError> {
Circle::new(Point::new(x as i32, y as i32), diameter as u32)
.into_styled(PrimitiveStyle::with_fill(color))
.draw(self)
.map_err(|e| DisplayError::Draw(format!("{e:?}")))?;
Ok(())
}
fn flush(&mut self) -> Result<(), DisplayError> { fn flush(&mut self) -> Result<(), DisplayError> {
Ok(()) Ok(())
} }
@@ -100,3 +109,9 @@ impl DisplayPort for Esp32DisplayAdapter {
self.inner.flush() self.inner.flush()
} }
} }
impl Esp32DisplayAdapter {
pub fn fill_circle(&mut self, x: u16, y: u16, diameter: u16, color: Color) -> Result<(), DisplayError> {
self.inner.fill_circle(x, y, diameter, to_rgb565(color))
}
}

View File

@@ -50,7 +50,7 @@ fn run_station(
info!("Connecting WiFi..."); info!("Connecting WiFi...");
match hal::wifi::init(modem, sysloop.clone(), nvs.clone(), &cfg.wifi_ssid, &cfg.wifi_pass) { match hal::wifi::init(modem, sysloop.clone(), nvs.clone(), &cfg.wifi_ssid, &cfg.wifi_pass) {
Ok(_wifi) => { Ok(_wifi) => {
let (tx, rx) = mpsc::channel(); let (tx, rx) = mpsc::channel::<tasks::RenderEvent>();
tasks::network::spawn(cfg.server_addr, tx); tasks::network::spawn(cfg.server_addr, tx);
tasks::render::run(config::SCREEN, display, rx); tasks::render::run(config::SCREEN, display, rx);
} }

View File

@@ -1,2 +1,9 @@
pub mod network; pub mod network;
pub mod render; pub mod render;
use protocol::ServerMessage;
pub enum RenderEvent {
Server(ServerMessage),
ConnectionStatus(bool),
}

View File

@@ -1,12 +1,13 @@
use std::sync::mpsc; use std::sync::mpsc;
use std::thread; use std::thread;
use client_domain::NetworkPort; use client_domain::NetworkPort;
use protocol::{ServerMessage, decode_server_message}; use protocol::decode_server_message;
use super::RenderEvent;
use crate::config::{NET_THREAD_STACK_SIZE, NET_POLL_INTERVAL, NET_RECONNECT_DELAY}; use crate::config::{NET_THREAD_STACK_SIZE, NET_POLL_INTERVAL, NET_RECONNECT_DELAY};
use crate::adapters::network::Esp32Network; use crate::adapters::network::Esp32Network;
use log::*; use log::*;
pub fn spawn(server_addr: String, tx: mpsc::Sender<ServerMessage>) { pub fn spawn(server_addr: String, tx: mpsc::Sender<RenderEvent>) {
thread::Builder::new() thread::Builder::new()
.stack_size(NET_THREAD_STACK_SIZE) .stack_size(NET_THREAD_STACK_SIZE)
.name("net".into()) .name("net".into())
@@ -14,16 +15,20 @@ pub fn spawn(server_addr: String, tx: mpsc::Sender<ServerMessage>) {
.expect("failed to spawn network thread"); .expect("failed to spawn network thread");
} }
fn run(server_addr: String, tx: mpsc::Sender<ServerMessage>) { fn run(server_addr: String, tx: mpsc::Sender<RenderEvent>) {
let mut net = Esp32Network::new(); let mut net = Esp32Network::new();
loop { loop {
if !net.is_connected() { if !net.is_connected() {
info!("Connecting to server {server_addr}..."); info!("Connecting to server {server_addr}...");
match net.connect(&server_addr) { match net.connect(&server_addr) {
Ok(()) => info!("Server connected"), Ok(()) => {
info!("Server connected");
let _ = tx.send(RenderEvent::ConnectionStatus(true));
}
Err(e) => { Err(e) => {
error!("Connection failed: {e}, retrying..."); error!("Connection failed: {e}, retrying...");
let _ = tx.send(RenderEvent::ConnectionStatus(false));
thread::sleep(NET_RECONNECT_DELAY); thread::sleep(NET_RECONNECT_DELAY);
continue; continue;
} }
@@ -33,7 +38,7 @@ fn run(server_addr: String, tx: mpsc::Sender<ServerMessage>) {
match net.receive() { match net.receive() {
Ok(Some(payload)) => { Ok(Some(payload)) => {
match decode_server_message(&payload) { match decode_server_message(&payload) {
Ok(msg) => { let _ = tx.send(msg); } Ok(msg) => { let _ = tx.send(RenderEvent::Server(msg)); }
Err(e) => error!("Decode error: {e}"), Err(e) => error!("Decode error: {e}"),
} }
} }
@@ -43,6 +48,7 @@ fn run(server_addr: String, tx: mpsc::Sender<ServerMessage>) {
Err(e) => { Err(e) => {
error!("Receive error: {e}, reconnecting..."); error!("Receive error: {e}, reconnecting...");
let _ = net.disconnect(); let _ = net.disconnect();
let _ = tx.send(RenderEvent::ConnectionStatus(false));
thread::sleep(NET_RECONNECT_DELAY); thread::sleep(NET_RECONNECT_DELAY);
} }
} }

View File

@@ -2,16 +2,21 @@ use std::sync::mpsc;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
use std::collections::HashMap; use std::collections::HashMap;
use client_domain::{ use client_domain::{
BoundingBox, DisplayPort, FontMetrics, RenderEngine, ScrollState, ThemeConfig, BoundingBox, Color, DisplayPort, FontMetrics, RenderEngine, ScrollState, ThemeConfig,
}; };
use client_application::{ClientApp, RepaintCommand}; use client_application::{ClientApp, RepaintCommand};
use domain::{DisplayHint, Value}; use domain::{DisplayHint, Value};
use protocol::ServerMessage; use protocol::ServerMessage;
use super::RenderEvent;
use crate::config::RENDER_POLL_INTERVAL; use crate::config::RENDER_POLL_INTERVAL;
use crate::adapters::display::Esp32DisplayAdapter; use crate::adapters::display::Esp32DisplayAdapter;
use log::*; use log::*;
const SCROLL_TICK: Duration = Duration::from_millis(50); const SCROLL_TICK: Duration = Duration::from_millis(50);
const INDICATOR_DIAMETER: u16 = 8;
const INDICATOR_MARGIN: u16 = 4;
const COLOR_CONNECTED: Color = Color(0, 200, 0);
const COLOR_DISCONNECTED: Color = Color(200, 0, 0);
struct WidgetCache { struct WidgetCache {
hint: DisplayHint, hint: DisplayHint,
@@ -23,7 +28,7 @@ struct WidgetCache {
pub fn run( pub fn run(
screen: BoundingBox, screen: BoundingBox,
mut display: Esp32DisplayAdapter, mut display: Esp32DisplayAdapter,
rx: mpsc::Receiver<ServerMessage>, rx: mpsc::Receiver<RenderEvent>,
) { ) {
let metrics = FontMetrics { let metrics = FontMetrics {
small: (6, 10), small: (6, 10),
@@ -34,13 +39,23 @@ pub fn run(
let mut widgets: HashMap<u16, WidgetCache> = HashMap::new(); let mut widgets: HashMap<u16, WidgetCache> = HashMap::new();
let mut first_update = true; let mut first_update = true;
let mut last_tick = Instant::now(); let mut last_tick = Instant::now();
let mut connected = false;
info!("Render loop started"); info!("Render loop started");
draw_indicator(&mut display, screen, connected);
display.flush().unwrap();
loop { loop {
let timeout = RENDER_POLL_INTERVAL.min(SCROLL_TICK); let timeout = RENDER_POLL_INTERVAL.min(SCROLL_TICK);
match rx.recv_timeout(timeout) { match rx.recv_timeout(timeout) {
Ok(msg) => { Ok(RenderEvent::ConnectionStatus(status)) => {
if status != connected {
connected = status;
draw_indicator(&mut display, screen, connected);
display.flush().unwrap();
}
}
Ok(RenderEvent::Server(msg)) => {
let is_screen_update = matches!(msg, ServerMessage::ScreenUpdate { .. }); let is_screen_update = matches!(msg, ServerMessage::ScreenUpdate { .. });
let repaints = app.handle_message(msg); let repaints = app.handle_message(msg);
@@ -49,19 +64,20 @@ pub fn run(
engine.set_theme(app.theme().clone()); engine.set_theme(app.theme().clone());
} }
let bg = engine.theme().background;
if !repaints.is_empty() && (first_update || is_screen_update || theme_changed) { if !repaints.is_empty() && (first_update || is_screen_update || theme_changed) {
let bg = engine.theme().background;
display.fill_rect(screen, bg).unwrap(); display.fill_rect(screen, bg).unwrap();
first_update = false; first_update = false;
} }
for cmd in &repaints { for cmd in &repaints {
let cache = update_cache(&engine, cmd); let cache = update_cache(&engine, cmd);
display.fill_rect(cache.bounds, bg).unwrap();
draw_widget(&engine, &mut display, &cache); draw_widget(&engine, &mut display, &cache);
widgets.insert(cmd.widget_id, cache); widgets.insert(cmd.widget_id, cache);
} }
if !repaints.is_empty() { if !repaints.is_empty() {
draw_indicator(&mut display, screen, connected);
display.flush().unwrap(); display.flush().unwrap();
} }
} }
@@ -86,11 +102,19 @@ pub fn run(
} }
} }
if needs_flush { if needs_flush {
draw_indicator(&mut display, screen, connected);
display.flush().unwrap(); display.flush().unwrap();
} }
} }
} }
fn draw_indicator(display: &mut Esp32DisplayAdapter, screen: BoundingBox, connected: bool) {
let color = if connected { COLOR_CONNECTED } else { COLOR_DISCONNECTED };
let x = screen.x + screen.width - INDICATOR_DIAMETER - INDICATOR_MARGIN;
let y = screen.y + screen.height - INDICATOR_DIAMETER - INDICATOR_MARGIN;
display.fill_circle(x, y, INDICATOR_DIAMETER, color).unwrap();
}
fn update_cache(engine: &RenderEngine, cmd: &RepaintCommand) -> WidgetCache { fn update_cache(engine: &RenderEngine, cmd: &RepaintCommand) -> WidgetCache {
let hint: DisplayHint = cmd.display_hint.clone().into(); let hint: DisplayHint = cmd.display_hint.clone().into();
let data: Vec<(String, Value)> = cmd.state.data let data: Vec<(String, Value)> = cmd.state.data

View File

@@ -9,13 +9,24 @@ pub enum DataSourceType {
Rss, Rss,
HttpJson, HttpJson,
Webhook, Webhook,
Clock,
StaticText,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct DataSourceConfig { pub enum DataSourceConfig {
pub url: Option<String>, External {
pub headers: Vec<(String, String)>, url: Option<String>,
pub api_key: Option<String>, headers: Vec<(String, String)>,
api_key: Option<String>,
},
Clock {
format: String,
timezone: String,
},
StaticText {
text: String,
},
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@@ -38,18 +49,28 @@ impl DataSource {
pub fn validate(&self) -> Vec<DataSourceValidationError> { pub fn validate(&self) -> Vec<DataSourceValidationError> {
let mut errors = Vec::new(); let mut errors = Vec::new();
let is_webhook = self.source_type == DataSourceType::Webhook; match self.source_type {
DataSourceType::Webhook => {
if is_webhook { if !self.poll_interval.is_zero() {
if !self.poll_interval.is_zero() { errors.push(DataSourceValidationError::PollIntervalNotAllowed);
errors.push(DataSourceValidationError::PollIntervalNotAllowed); }
} }
} else { DataSourceType::Clock | DataSourceType::StaticText => {
if self.poll_interval.is_zero() { // Internal sources: poll_interval optional, no url needed
errors.push(DataSourceValidationError::PollIntervalRequired);
} }
if self.requires_url() && self.config.url.is_none() { _ => {
errors.push(DataSourceValidationError::UrlRequired); if self.poll_interval.is_zero() {
errors.push(DataSourceValidationError::PollIntervalRequired);
}
if self.requires_url() {
let has_url = matches!(
&self.config,
DataSourceConfig::External { url: Some(_), .. }
);
if !has_url {
errors.push(DataSourceValidationError::UrlRequired);
}
}
} }
} }

View File

@@ -35,6 +35,15 @@ impl WidgetConfig {
} }
pub fn extract(&self, raw: &Value) -> WidgetState { pub fn extract(&self, raw: &Value) -> WidgetState {
let has_mappings = self.mappings.iter().any(|m| !m.source_path.is_empty());
if !has_mappings {
let data = match raw {
Value::Object(map) => map.clone(),
_ => BTreeMap::new(),
};
return WidgetState { data, error: None };
}
let budget = self.max_data_size as usize; let budget = self.max_data_size as usize;
let mut used = 0usize; let mut used = 0usize;
let mut data = BTreeMap::new(); let mut data = BTreeMap::new();

View File

@@ -7,7 +7,7 @@ fn make_source(source_type: DataSourceType, url: Option<&str>, poll: Duration) -
name: "test".into(), name: "test".into(),
source_type, source_type,
poll_interval: poll, poll_interval: poll,
config: DataSourceConfig { config: DataSourceConfig::External {
url: url.map(Into::into), url: url.map(Into::into),
headers: vec![], headers: vec![],
api_key: None, api_key: None,

View File

@@ -7,7 +7,7 @@ export interface DisplayHint {
h_align: HAlign h_align: HAlign
v_align: VAlign v_align: VAlign
} }
export type SourceType = "weather" | "media" | "rss" | "http_json" | "webhook" export type SourceType = "weather" | "media" | "rss" | "http_json" | "webhook" | "clock" | "static_text"
export type SizingType = "fixed" | "flex" export type SizingType = "fixed" | "flex"
export type Direction = "row" | "column" export type Direction = "row" | "column"
export type JustifyContent = "start" | "center" | "end" | "space_between" | "space_evenly" export type JustifyContent = "start" | "center" | "end" | "space_between" | "space_evenly"
@@ -29,14 +29,17 @@ export interface Widget {
export type CreateWidget = Widget export type CreateWidget = Widget
export type DataSourceConfig =
| { type: "external"; url: string | null; api_key: string | null; headers: [string, string][] }
| { type: "clock"; format: string; timezone: string }
| { type: "static_text"; text: string }
export interface DataSource { export interface DataSource {
id: number id: number
name: string name: string
source_type: SourceType source_type: SourceType
poll_interval_secs: number poll_interval_secs: number
url: string | null config: DataSourceConfig
api_key: string | null
headers: [string, string][]
} }
export interface Sizing { export interface Sizing {

View File

@@ -5,7 +5,7 @@ import {
useUpdateDataSource, useUpdateDataSource,
useDeleteDataSource, useDeleteDataSource,
} from "@/api/data-sources" } from "@/api/data-sources"
import type { DataSource, SourceType } from "@/api/types" import type { DataSource, DataSourceConfig, SourceType } from "@/api/types"
import { Button } from "@/components/ui/button" import { Button } from "@/components/ui/button"
import { import {
Card, Card,
@@ -50,16 +50,24 @@ const SOURCE_TYPES: SourceType[] = [
"rss", "rss",
"http_json", "http_json",
"webhook", "webhook",
"clock",
"static_text",
] ]
const EXTERNAL_TYPES: SourceType[] = ["weather", "media", "rss", "http_json", "webhook"]
function defaultConfigFor(sourceType: SourceType): DataSourceConfig {
if (sourceType === "clock") return { type: "clock", format: "%H:%M:%S", timezone: "UTC" }
if (sourceType === "static_text") return { type: "static_text", text: "" }
return { type: "external", url: null, api_key: null, headers: [] }
}
const EMPTY: DataSource = { const EMPTY: DataSource = {
id: 0, id: 0,
name: "", name: "",
source_type: "http_json", source_type: "http_json",
poll_interval_secs: 300, poll_interval_secs: 300,
url: null, config: { type: "external", url: null, api_key: null, headers: [] },
api_key: null,
headers: [],
} }
export function DataSourcesPage() { export function DataSourcesPage() {
@@ -145,10 +153,10 @@ export function DataSourcesPage() {
<CardTitle className="text-base">{ds.name}</CardTitle> <CardTitle className="text-base">{ds.name}</CardTitle>
<CardDescription className="flex items-center gap-2"> <CardDescription className="flex items-center gap-2">
<Badge variant="secondary">{ds.source_type}</Badge> <Badge variant="secondary">{ds.source_type}</Badge>
<span>every {ds.poll_interval_secs}s</span> {ds.poll_interval_secs > 0 && <span>every {ds.poll_interval_secs}s</span>}
{ds.url && ( {ds.config.type === "external" && ds.config.url && (
<span className="text-muted-foreground max-w-xs truncate text-xs"> <span className="text-muted-foreground max-w-xs truncate text-xs">
{ds.url} {ds.config.url}
</span> </span>
)} )}
</CardDescription> </CardDescription>
@@ -196,9 +204,13 @@ export function DataSourcesPage() {
onClick={save} onClick={save}
disabled={ disabled={
!editing?.name || !editing?.name ||
(editing.source_type !== "webhook" && (EXTERNAL_TYPES.includes(editing.source_type) &&
editing.source_type !== "webhook" &&
editing.poll_interval_secs <= 0) || editing.poll_interval_secs <= 0) ||
(editing.source_type !== "webhook" && !editing.url) (EXTERNAL_TYPES.includes(editing.source_type) &&
editing.source_type !== "webhook" &&
editing.config.type === "external" &&
!editing.config.url)
} }
> >
Save Save
@@ -302,6 +314,17 @@ function DataSourceForm({
const set = <K extends keyof DataSource>(k: K, v: DataSource[K]) => const set = <K extends keyof DataSource>(k: K, v: DataSource[K]) =>
onChange({ ...value, [k]: v }) onChange({ ...value, [k]: v })
const setConfig = (patch: Partial<DataSourceConfig>) =>
onChange({ ...value, config: { ...value.config, ...patch } as DataSourceConfig })
const onSourceTypeChange = (t: SourceType) => {
onChange({ ...value, source_type: t, config: defaultConfigFor(t) })
}
const isExternal = value.config.type === "external"
const isClock = value.config.type === "clock"
const isStaticText = value.config.type === "static_text"
return ( return (
<div className="grid gap-4 py-2"> <div className="grid gap-4 py-2">
<div className="grid gap-2"> <div className="grid gap-2">
@@ -316,7 +339,7 @@ function DataSourceForm({
<Label>Source Type</Label> <Label>Source Type</Label>
<Select <Select
value={value.source_type} value={value.source_type}
onValueChange={(v) => set("source_type", v as SourceType)} onValueChange={(v) => onSourceTypeChange(v as SourceType)}
> >
<SelectTrigger> <SelectTrigger>
<SelectValue /> <SelectValue />
@@ -330,23 +353,61 @@ function DataSourceForm({
</SelectContent> </SelectContent>
</Select> </Select>
</div> </div>
<div className="grid gap-2">
<Label>URL</Label> {isExternal && (
<Input <>
value={value.url ?? ""} <div className="grid gap-2">
onChange={(e) => set("url", e.target.value || null)} <Label>URL</Label>
placeholder="https://..." <Input
/> value={value.config.url ?? ""}
</div> onChange={(e) => setConfig({ url: e.target.value || null })}
<div className="grid gap-2"> placeholder="https://..."
<Label>API Key</Label> />
<Input </div>
type="password" <div className="grid gap-2">
value={value.api_key ?? ""} <Label>API Key</Label>
onChange={(e) => set("api_key", e.target.value || null)} <Input
placeholder="Optional" type="password"
/> value={value.config.api_key ?? ""}
</div> onChange={(e) => setConfig({ api_key: e.target.value || null })}
placeholder="Optional"
/>
</div>
</>
)}
{isClock && (
<>
<div className="grid gap-2">
<Label>Format</Label>
<Input
value={value.config.format}
onChange={(e) => setConfig({ format: e.target.value })}
placeholder="%H:%M:%S"
/>
</div>
<div className="grid gap-2">
<Label>Timezone</Label>
<Input
value={value.config.timezone}
onChange={(e) => setConfig({ timezone: e.target.value })}
placeholder="Europe/Warsaw"
/>
</div>
</>
)}
{isStaticText && (
<div className="grid gap-2">
<Label>Text</Label>
<Input
value={value.config.text}
onChange={(e) => setConfig({ text: e.target.value })}
placeholder="Hello world"
/>
</div>
)}
<div className="grid gap-2"> <div className="grid gap-2">
<Label>Poll Interval (seconds)</Label> <Label>Poll Interval (seconds)</Label>
<Input <Input
@@ -356,41 +417,44 @@ function DataSourceForm({
min={1} min={1}
/> />
</div> </div>
<div className="grid gap-2">
<div className="flex items-center justify-between"> {isExternal && (
<Label>Headers</Label> <div className="grid gap-2">
<Button <div className="flex items-center justify-between">
variant="outline" <Label>Headers</Label>
size="sm" <Button
onClick={() => variant="outline"
set("headers", [...value.headers, ["", ""]]) size="sm"
} onClick={() =>
> setConfig({ headers: [...value.config.headers, ["", ""]] })
<Plus className="mr-1 h-3 w-3" /> }
Add >
</Button> <Plus className="mr-1 h-3 w-3" />
Add
</Button>
</div>
{value.config.headers.map(([k, v], i) => (
<HeaderRow
key={i}
headerKey={k}
headerValue={v}
onChangeKey={(newKey) => {
const next = [...value.config.headers] as [string, string][]
next[i] = [newKey, v]
setConfig({ headers: next })
}}
onChangeValue={(newVal) => {
const next = [...value.config.headers] as [string, string][]
next[i] = [k, newVal]
setConfig({ headers: next })
}}
onRemove={() =>
setConfig({ headers: value.config.headers.filter((_, idx) => idx !== i) })
}
/>
))}
</div> </div>
{value.headers.map(([k, v], i) => ( )}
<HeaderRow
key={i}
headerKey={k}
headerValue={v}
onChangeKey={(newKey) => {
const next = [...value.headers] as [string, string][]
next[i] = [newKey, v]
set("headers", next)
}}
onChangeValue={(newVal) => {
const next = [...value.headers] as [string, string][]
next[i] = [k, newVal]
set("headers", next)
}}
onRemove={() =>
set("headers", value.headers.filter((_, idx) => idx !== i))
}
/>
))}
</div>
</div> </div>
) )
} }

View File

@@ -199,8 +199,7 @@ export function WidgetsPage() {
onClick={save} onClick={save}
disabled={ disabled={
!editing?.name || !editing?.name ||
!editing.data_source_id || !editing.data_source_id
editing.mappings.length === 0
} }
> >
Save Save