Skip to content

Commit 2b79116

Browse files
committed
temperatures: add topic for temperature warnings
There is already a topic that provides raw temperature information, but that topic provides updates at a fixed 2Hz rate. Add a new topic that only provides warnings and only publishes when the warning state changes. Signed-off-by: Leonard Göhrs <l.goehrs@pengutronix.de>
1 parent 24bd697 commit 2b79116

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

openapi.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,21 @@ paths:
283283
schema:
284284
$ref: '#/components/schemas/Measurement'
285285

286+
/v1/tac/temperatures/warning:
287+
get:
288+
summary: Get the current temperature warning state
289+
tags: [System]
290+
responses:
291+
'200':
292+
content:
293+
application/json:
294+
schema:
295+
type: string
296+
enum:
297+
- Okay
298+
- SocHigh
299+
- SocCritical
300+
286301
/v1/tac/info/uname:
287302
get:
288303
summary: Get the information commonly accessed via "uname"

src/temperatures.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use std::time::Duration;
2121

2222
use async_std::sync::Arc;
2323
use async_std::task::spawn_blocking;
24+
use serde::{Deserialize, Serialize};
2425

2526
use crate::broker::{BrokerBuilder, Topic};
2627
use crate::measurement::Measurement;
@@ -59,19 +60,43 @@ mod hw {
5960
use hw::{HwMon, SysClass};
6061

6162
const UPDATE_INTERVAL: Duration = Duration::from_millis(500);
63+
const TEMPERATURE_SOC_CRITICAL: f32 = 90.0;
64+
const TEMPERATURE_SOC_HIGH: f32 = 70.0;
65+
66+
#[derive(Serialize, Deserialize, PartialEq, Eq, Clone)]
67+
pub enum Warning {
68+
Okay,
69+
SocHigh,
70+
SocCritical,
71+
}
72+
73+
impl Warning {
74+
fn from_temperatures(soc: f32) -> Self {
75+
if soc > TEMPERATURE_SOC_CRITICAL {
76+
Self::SocCritical
77+
} else if soc > TEMPERATURE_SOC_HIGH {
78+
Self::SocHigh
79+
} else {
80+
Self::Okay
81+
}
82+
}
83+
}
6284

6385
pub struct Temperatures {
6486
pub soc_temperature: Arc<Topic<Measurement>>,
87+
pub warning: Arc<Topic<Warning>>,
6588
run: Option<Arc<AtomicBool>>,
6689
}
6790

6891
impl Temperatures {
6992
pub fn new(bb: &mut BrokerBuilder) -> Self {
7093
let run = Arc::new(AtomicBool::new(true));
7194
let soc_temperature = bb.topic_ro("/v1/tac/temperatures/soc", None);
95+
let warning = bb.topic_ro("/v1/tac/temperatures/warning", None);
7296

7397
let run_thread = run.clone();
7498
let soc_temperature_thread = soc_temperature.clone();
99+
let warning_thread = warning.clone();
75100

76101
spawn_blocking(move || {
77102
while run_thread.load(Ordering::Relaxed) {
@@ -82,7 +107,15 @@ impl Temperatures {
82107
.input()
83108
.unwrap();
84109

85-
let meas = Measurement::now(val as f32 / 1000.0);
110+
let val = val as f32 / 1000.0;
111+
112+
// Provide a topic that only provides "is overheating"/"is okay"
113+
// updates and not the 2Hz temperature feed.
114+
// Subscribing to this topic is cheaper w.r.t. cpu/network use.
115+
let warning = Warning::from_temperatures(val);
116+
warning_thread.set_if_changed(warning);
117+
118+
let meas = Measurement::now(val);
86119
soc_temperature_thread.set(meas);
87120

88121
sleep(UPDATE_INTERVAL);
@@ -91,6 +124,7 @@ impl Temperatures {
91124

92125
Self {
93126
soc_temperature,
127+
warning,
94128
run: Some(run),
95129
}
96130
}

0 commit comments

Comments
 (0)