Skip to content

Commit

Permalink
fix: config file not exists
Browse files Browse the repository at this point in the history
  • Loading branch information
fengyc committed Dec 7, 2024
1 parent dd33fe8 commit 6b76ec2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
28 changes: 18 additions & 10 deletions pisugar-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ use std::time::{Duration, Instant};
use battery::BatteryEvent;
use chrono::{DateTime, Datelike, Local, Timelike, Utc};
pub use config::{BatteryThreshold, PiSugarConfig};
use hyper::client::Client;
use rppal::i2c::Error as I2cError;

pub use model::Model;
use rsntp::{AsyncSntpClient, SntpClient};
use rsntp::AsyncSntpClient;
pub use sd3078::*;

use crate::battery::Battery;
Expand Down Expand Up @@ -320,12 +319,15 @@ impl PiSugarCore {
local_now.month(),
local_now.day()
);
let mut backup_success = false;
for i in 0..1000 {
let backup_path = format!("{}-{:03}", backup_path_template, i);
if std::fs::rename(config_path.as_path(), backup_path).is_ok() {
backup_success = true;
break;
let mut backup_success = true;
if let Ok(true) = std::fs::exists(config_path.as_path()) {
backup_success = false;
for i in 0..3 {
let backup_path = format!("{}-{:03}", backup_path_template, i);
if std::fs::rename(config_path.as_path(), backup_path).is_ok() {
backup_success = true;
break;
}
}
}
if !backup_success {
Expand Down Expand Up @@ -693,8 +695,14 @@ impl PiSugarCore {
/// Get ntp datetime.
pub async fn get_ntp_datetime() -> Result<DateTime<Utc>> {
let sntp_client = AsyncSntpClient::new();
let result = sntp_client.synchronize(NTP_ADDR).await.map_err(|e| Error::Other(format!("{}", e)))?;
Ok(result.datetime().into_chrono_datetime().map_err(|e| Error::Other(format!("{}", e)))?)
let result = sntp_client
.synchronize(NTP_ADDR)
.await
.map_err(|e| Error::Other(format!("{}", e)))?;
Ok(result
.datetime()
.into_chrono_datetime()
.map_err(|e| Error::Other(format!("{}", e)))?)
}

// Fix aarch64
Expand Down
1 change: 0 additions & 1 deletion pisugar-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use env_logger::Env;
use futures::prelude::*;
use futures::SinkExt;
use futures_channel::mpsc::unbounded;
use hyper::client::Client;
use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Response};
use hyper::{Request, Server};
Expand Down

0 comments on commit 6b76ec2

Please sign in to comment.