Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ louvre = "0.2.1"
rustbus = "0.19.3"
sha256 = "1.5.0"
sysinfo = "0.35.2"
tzf-rs = { version = "1.1.3", default-features = false }
localzone = { version = "0.3.1", features = ["auto_validation"] }

[target.'cfg(target_env = "musl")'.dependencies]
mimalloc = "0.1.43"
Expand Down
18 changes: 16 additions & 2 deletions src/device_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use core::fmt;
use std::collections::HashMap;
// Read camera config file
use crate::detection_mask::DetectionMask;
use crate::set_system_timezone;
use byteorder::{LittleEndian, WriteBytesExt};
use chrono::{
DateTime, Duration, FixedOffset, Local, NaiveDate, NaiveDateTime, NaiveTime, TimeZone, Utc,
Expand All @@ -17,12 +18,15 @@ use std::io::{Cursor, Write};
use std::ops::Add;
use std::path::Path;
use std::str::FromStr;
use std::sync::LazyLock;
use std::sync::mpsc::{Receiver, Sender, TryRecvError};
use std::{fs, process};
use sun_times::sun_times;
use toml::Value;
use toml::value::Offset;

pub static TZ_FINDER: LazyLock<tzf_rs::DefaultFinder> = LazyLock::new(tzf_rs::DefaultFinder::new);

fn default_constant_recorder() -> bool {
false
}
Expand Down Expand Up @@ -582,9 +586,10 @@ impl DeviceConfig {
}

pub fn lat_lng(&self) -> (f32, f32) {
let location = self.location.as_ref().expect("A device location is required");
(
self.location.as_ref().unwrap().latitude.unwrap(),
self.location.as_ref().unwrap().longitude.unwrap(),
location.latitude.expect("A valid latitude is required"),
location.longitude.expect("A valid longitude is required"),
)
}
pub fn location_timestamp(&self) -> Option<u64> {
Expand Down Expand Up @@ -911,6 +916,15 @@ pub fn watch_local_config_file_changes(
if config != current_config {
current_config = config;
warn!("Config updated");

let (lat, lng) = current_config.lat_lng();
if let Err(e) =
set_system_timezone(TZ_FINDER.get_tz_name(lng as f64, lat as f64))
{
error!("{e}");
process::exit(1);
}

let _ = config_tx.send(current_config.clone());
}
}
Expand Down
66 changes: 58 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,32 @@ mod utils;

use rppal::gpio::Gpio;

use localzone::get_local_zone;
use std::fs;
use std::process;
use std::process::Command;
use std::sync::Arc;
use std::sync::atomic::AtomicBool;
use std::sync::mpsc::channel;
use std::thread::sleep;

use std::{thread, time::Duration};
use thread_priority::ThreadBuilderExt;
use thread_priority::*;

use log::{error, info, warn};
use rustbus::connection::Timeout;
use rustbus::{DuplexConn, get_system_bus_path};
use simplelog::*;
use sysinfo::Disks;

use crate::camera_transfer_state::enter_camera_transfer_loop;
use crate::dbus_attiny_i2c::exit_if_attiny_version_is_not_as_expected;
use crate::dbus_managementd::setup_dbus_managementd_recording_service;
use crate::device_config::DeviceConfig;
use crate::device_config::watch_local_config_file_changes;
use crate::device_config::{DeviceConfig, TZ_FINDER};
use crate::frame_socket_server::spawn_frame_socket_server_thread;
use crate::mode_config::ModeConfig;
use crate::program_rp2040::check_if_rp2040_needs_programming;
use crate::recording_state::RecordingState;
use log::{error, info, warn};
use rustbus::connection::Timeout;
use rustbus::{DuplexConn, get_system_bus_path};
use simplelog::*;
use sysinfo::Disks;

const AUDIO_SHEBANG: u16 = 1;

Expand Down Expand Up @@ -147,6 +147,13 @@ fn main() {
let _dbus_audio_thread = setup_dbus_managementd_recording_service(&recording_state);

let current_config = device_config.unwrap();

let (lat, lng) = current_config.lat_lng();
if let Err(e) = set_system_timezone(TZ_FINDER.get_tz_name(lng as f64, lat as f64)) {
error!("{e}");
process::exit(1);
}

let initial_config = current_config.clone();
let (device_config_change_channel_tx, device_config_change_channel_rx) = channel();
let _file_watcher =
Expand Down Expand Up @@ -227,3 +234,46 @@ fn main() {
process::exit(1);
}
}

pub fn set_system_timezone(timezone: &str) -> Result<(), String> {
let local_tz = get_local_zone();
if local_tz.is_none() {
return Err("Error getting system time zone".to_string());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to not just and try to set the timezone even if we can't get the current systems timezone?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems reasonable, have updated.

}
let local_tz = local_tz.unwrap();
if timezone == local_tz {
info!("System timezone already set to {timezone}");
return Ok(());
}
match Command::new("sudo").arg("timedatectl").arg("set-timezone").arg(timezone).output() {
Ok(output) => {
if output.status.success() {
info!("System timezone successfully set to: {}", timezone);
match Command::new("sudo")
.arg("dpkg-reconfigure")
.arg("tzdata")
.arg("-f")
.arg("noninteractive")
.output()
{
Ok(output) => {
if output.status.success() {
info!("Successfully updated tzdata");
Ok(())
} else {
let stderr = String::from_utf8_lossy(&output.stderr);
Err(format!("Failed to update tzdata: {stderr}"))
}
}
Err(e) => Err(format!(
"Error executing dpkg-reconfigure tzdata -f noninteractive: {e}"
)),
}
} else {
let stderr = String::from_utf8_lossy(&output.stderr);
Err(format!("Failed to set system timezone: {stderr}"))
}
}
Err(e) => Err(format!("Error executing timedatectl: {e}")),
}
}
Loading