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]]
name = "tesla-charge-controller"
version = "1.0.10"
version = "1.0.11"
dependencies = [
"async-channel",
"chrono",

View file

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

View file

@ -4,6 +4,7 @@ use std::{
time::Duration,
};
use chrono::Timelike;
use metrics::{gauge, Gauge, Label};
use serde::{Deserialize, Serialize};
use serialport::SerialPort;
@ -140,6 +141,7 @@ impl RegulatorGauges {
pub enum PliRequest {
ReadRam(u8),
ReadEeprom(u8),
SyncTime,
}
impl Pli {
@ -210,6 +212,22 @@ impl Pli {
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>
where
T: Into<u8>,
@ -278,6 +304,8 @@ impl Pli {
enum PlRamAddress {
Dutycyc,
Min,
Hour,
Batv,
Battemp,
Rstate,
@ -290,6 +318,8 @@ impl From<PlRamAddress> for u8 {
fn from(value: PlRamAddress) -> Self {
match value {
PlRamAddress::Dutycyc => 39,
PlRamAddress::Min => 47,
PlRamAddress::Hour => 48,
PlRamAddress::Batv => 50,
PlRamAddress::Battemp => 52,
PlRamAddress::Rstate => 101,

View file

@ -62,6 +62,7 @@ fn rocket(state: ServerState) -> rocket::Rocket<rocket::Build> {
disable_control,
enable_control,
metrics,
sync_time,
read_ram,
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>")]
async fn read_ram(address: u8, state: &State<ServerState>) -> String {
let _ = state.pli_requests.send(PliRequest::ReadRam(address)).await;