sync time
All checks were successful
Build .deb on release / Build-Deb (push) Successful in 1m51s

This commit is contained in:
Alex Janka 2024-01-21 09:08:21 +11:00
parent 8cb97059d7
commit d456c3321c
4 changed files with 39 additions and 2 deletions

2
Cargo.lock generated
View file

@ -2567,7 +2567,7 @@ dependencies = [
[[package]] [[package]]
name = "tesla-charge-controller" name = "tesla-charge-controller"
version = "1.0.10" version = "1.0.11"
dependencies = [ dependencies = [
"async-channel", "async-channel",
"chrono", "chrono",

View file

@ -1,6 +1,6 @@
[package] [package]
name = "tesla-charge-controller" name = "tesla-charge-controller"
version = "1.0.10" version = "1.0.11"
edition = "2021" edition = "2021"
license = "MITNFA" license = "MITNFA"
description = "Controls Tesla charge rate based on solar charge data" description = "Controls Tesla charge rate based on solar charge data"

View file

@ -4,6 +4,7 @@ use std::{
time::Duration, time::Duration,
}; };
use chrono::Timelike;
use metrics::{gauge, Gauge, Label}; use metrics::{gauge, Gauge, Label};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serialport::SerialPort; use serialport::SerialPort;
@ -140,6 +141,7 @@ impl RegulatorGauges {
pub enum PliRequest { pub enum PliRequest {
ReadRam(u8), ReadRam(u8),
ReadEeprom(u8), ReadEeprom(u8),
SyncTime,
} }
impl Pli { impl Pli {
@ -210,6 +212,22 @@ impl Pli {
log::warn!("Read EEPROM at {address}: data {data}"); log::warn!("Read EEPROM at {address}: data {data}");
} }
} }
PliRequest::SyncTime => {
let now = chrono::Local::now();
let timestamp = (((now.hour() * 10) + (now.minute() / 6)) & 0xFF) as u8;
let min = (now.minute() % 6) as u8;
log::warn!("Setting time: {now} corresponds to {timestamp} + minutes {min}");
if self
.write_ram(PlRamAddress::Hour, timestamp)
.some_or_print_with("Setting time")
.is_none()
{
return;
}
let _ = self
.write_ram(PlRamAddress::Min, min)
.some_or_print_with("Setting time (minutes)");
}
} }
} }
@ -262,6 +280,14 @@ impl Pli {
} }
} }
fn write_ram<T>(&mut self, address: T, data: u8) -> Result<(), PliError>
where
T: Into<u8>,
{
self.send_command(command(152, address.into(), data))?;
Ok(())
}
fn read_eeprom<T>(&mut self, address: T) -> Result<u8, PliError> fn read_eeprom<T>(&mut self, address: T) -> Result<u8, PliError>
where where
T: Into<u8>, T: Into<u8>,
@ -278,6 +304,8 @@ impl Pli {
enum PlRamAddress { enum PlRamAddress {
Dutycyc, Dutycyc,
Min,
Hour,
Batv, Batv,
Battemp, Battemp,
Rstate, Rstate,
@ -290,6 +318,8 @@ impl From<PlRamAddress> for u8 {
fn from(value: PlRamAddress) -> Self { fn from(value: PlRamAddress) -> Self {
match value { match value {
PlRamAddress::Dutycyc => 39, PlRamAddress::Dutycyc => 39,
PlRamAddress::Min => 47,
PlRamAddress::Hour => 48,
PlRamAddress::Batv => 50, PlRamAddress::Batv => 50,
PlRamAddress::Battemp => 52, PlRamAddress::Battemp => 52,
PlRamAddress::Rstate => 101, PlRamAddress::Rstate => 101,

View file

@ -62,6 +62,7 @@ fn rocket(state: ServerState) -> rocket::Rocket<rocket::Build> {
disable_control, disable_control,
enable_control, enable_control,
metrics, metrics,
sync_time,
read_ram, read_ram,
read_eeprom read_eeprom
], ],
@ -137,6 +138,12 @@ fn metrics() -> Result<String, ServerError> {
) )
} }
#[get("/sync-time")]
async fn sync_time(state: &State<ServerState>) -> String {
let _ = state.pli_requests.send(PliRequest::SyncTime).await;
String::from("syncing time...")
}
#[get("/read-ram/<address>")] #[get("/read-ram/<address>")]
async fn read_ram(address: u8, state: &State<ServerState>) -> String { async fn read_ram(address: u8, state: &State<ServerState>) -> String {
let _ = state.pli_requests.send(PliRequest::ReadRam(address)).await; let _ = state.pli_requests.send(PliRequest::ReadRam(address)).await;