feat: internal date time sensors
This commit is contained in:
@@ -15,6 +15,7 @@ asterctl-lcd = { path = "../asterctl-lcd", version = "0.2.0" }
|
||||
|
||||
anyhow = "1.0.98"
|
||||
clap = { version = "4.5.42", features = ["derive"] }
|
||||
chrono = "0.4"
|
||||
image = "0.25.6"
|
||||
imageproc = { version = "0.25.0", default-features = false }
|
||||
ab_glyph = { version = "0.2.31", default-features = false, features = ["std"] }
|
||||
|
||||
@@ -7,7 +7,9 @@ use crate::cfg::{Panel, Sensor, SensorDirection, SensorMode, TextAlign};
|
||||
use crate::font::FontHandler;
|
||||
use crate::format_value;
|
||||
use crate::img::{ImageCache, Size, rotate_image};
|
||||
use crate::sensors::get_date_time_value;
|
||||
use ab_glyph::Font;
|
||||
use chrono::{DateTime, Local};
|
||||
use image::{ImageBuffer, Rgba, RgbaImage};
|
||||
use imageproc::drawing::{draw_text_mut, text_size};
|
||||
use log::{debug, error};
|
||||
@@ -160,6 +162,8 @@ impl PanelRenderer {
|
||||
values: &HashMap<String, String>,
|
||||
mut background: RgbaImage,
|
||||
) -> Result<RgbaImage, ImageProcessingError> {
|
||||
let now: DateTime<Local> = Local::now();
|
||||
|
||||
for sensor in &panel.sensor {
|
||||
let value = values.get(&sensor.label).cloned();
|
||||
let unit = values
|
||||
@@ -170,6 +174,8 @@ impl PanelRenderer {
|
||||
|
||||
if let Some(value) = value {
|
||||
self.render_sensor(&mut background, sensor, &value, &unit)?;
|
||||
} else if let Some(value) = get_date_time_value(&sensor.label, &now) {
|
||||
self.render_sensor(&mut background, sensor, &value, &unit)?;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,8 +3,11 @@
|
||||
|
||||
//! Sensor value sources.
|
||||
//!
|
||||
//! Only implementation is a file-based value provider with simple key-value pairs.
|
||||
//! Implementations:
|
||||
//! - internal date time sensors
|
||||
//! - file-based value provider with simple key-value pairs.
|
||||
|
||||
use chrono::{DateTime, Datelike, Local, Timelike};
|
||||
use log::{debug, error, info, warn};
|
||||
use notify::event::{ModifyKind, RenameMode};
|
||||
use notify::{Event, EventKind, RecursiveMode, Watcher};
|
||||
@@ -16,6 +19,46 @@ use std::path::{Path, PathBuf};
|
||||
use std::process::exit;
|
||||
use std::sync::{Arc, RwLock, mpsc};
|
||||
|
||||
pub fn get_date_time_value(label: &str, now: &DateTime<Local>) -> Option<String> {
|
||||
if !label.starts_with("DATE_") {
|
||||
return None;
|
||||
}
|
||||
|
||||
let year = now.year();
|
||||
let month = format!("{:02}", now.month());
|
||||
let day = format!("{:02}", now.day());
|
||||
let hour = format!("{:02}", now.hour());
|
||||
let minute = format!("{:02}", now.minute());
|
||||
let second = format!("{:02}", now.second());
|
||||
|
||||
// same formatting logic as in AOOSTAR-X
|
||||
let value = match label {
|
||||
"DATE_year" => year.to_string(),
|
||||
"DATE_month" => month,
|
||||
"DATE_day" => day,
|
||||
"DATE_hour" => hour,
|
||||
"DATE_minute" => minute,
|
||||
"DATE_second" => second,
|
||||
"DATE_m_d_h_m_1" => format!("{month}月{day}日 {hour}:{minute}"),
|
||||
"DATE_m_d_h_m_2" => format!("{month}/{day} {hour}:{minute}"),
|
||||
"DATE_m_d_1" => format!("{month}月{day}日"),
|
||||
"DATE_m_d_2" => format!("{month}-{day}"),
|
||||
"DATE_y_m_d_1" => format!("{year}年{month}月{day}日"),
|
||||
"DATE_y_m_d_2" => format!("{year}-{month}-{day}"),
|
||||
"DATE_y_m_d_3" => format!("{year}/{month}/{day}"),
|
||||
"DATE_y_m_d_4" => format!("{year} {month} {day}"),
|
||||
"DATE_h_m_s_1" => format!("{hour}:{minute}:{second}"),
|
||||
"DATE_h_m_s_2" => format!("{hour}时{minute}分{second}秒"),
|
||||
"DATE_h_m_s_3" => format!("{hour} {minute} {second}"),
|
||||
"DATE_h_m_1" => format!("{hour}时{minute}分"),
|
||||
"DATE_h_m_2" => format!("{hour} : {minute}"),
|
||||
"DATE_h_m_3" => format!("{hour}:{minute}"),
|
||||
_ => return None,
|
||||
};
|
||||
|
||||
Some(value)
|
||||
}
|
||||
|
||||
/// Read all sensor value source files from the given path and stort monitoring for changes.
|
||||
///
|
||||
/// The source path is either a single sensor source file or a directory containing multiple sensor
|
||||
|
||||
Reference in New Issue
Block a user