From 50580050317a67d38d59b68d6b073ad589eec467 Mon Sep 17 00:00:00 2001 From: Ivan Li Date: Sun, 7 Aug 2022 16:15:59 +0800 Subject: [PATCH] chore: remove unused code and dependencies. --- Cargo.toml | 1 - src/main.rs | 7 +++--- src/time.rs | 71 ----------------------------------------------------- src/wifi.rs | 2 +- 4 files changed, 4 insertions(+), 77 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 494b084..ac29427 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,7 +22,6 @@ env_logger = "0.9.0" esp-idf-svc = "0.42.1" esp-idf-sys = { version = "0.31.6", features = ["binstart"] } log = "0.4.17" -sntpc = "0.3.1" [build-dependencies] diff --git a/src/main.rs b/src/main.rs index 7afc4a1..2cc21d0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,4 @@ -use std::{env, thread::sleep, time::{Duration, UNIX_EPOCH}}; - -use anyhow::{bail, Ok, Result}; +use std::{env, thread::sleep, time::{Duration}}; use embedded_svc::mqtt::client::{Publish, QoS}; use esp_idf_sys as _; use log::*; @@ -22,7 +20,7 @@ fn main() { println!("Hello, world!"); - let wifi = Internet::new().unwrap(); + let _wifi = Internet::new().unwrap(); let mut mq = MessageQueue::new(); mq.init().unwrap(); @@ -34,6 +32,7 @@ fn main() { sleep(Duration::from_millis(1000)); if let Some(ref mut mq_client) = mq.client { let timestamps = time.get_time().as_millis(); + info!("timestamps {}", timestamps); mq_client.publish( "ups-0.2/heartbeat", diff --git a/src/time.rs b/src/time.rs index 8ffa0c4..93a2d73 100644 --- a/src/time.rs +++ b/src/time.rs @@ -3,19 +3,15 @@ use embedded_svc::sys_time::SystemTime; use esp_idf_svc::sntp::{self, EspSntp}; use esp_idf_svc::systime::EspSystemTime; use log::{warn, info}; -use sntpc::{Error, NtpContext, NtpResult, NtpTimestampGenerator, NtpUdpSocket, Result}; -use std::net::{SocketAddr, ToSocketAddrs, UdpSocket}; use std::time::{Duration}; pub struct Time { - result: Option, sntp: Option>, } impl Time { pub fn new () -> Time { return Time { - result: None, sntp: None } } @@ -35,71 +31,4 @@ impl Time { EspSystemTime {}.now() } - // pub fn sync(&mut self) -> anyhow::Result<()> { - // let mut error = None; - - // for _ in 0..10 { - // let socket = UdpSocket::bind("0.0.0.0:0").expect("Unable to crate UDP socket"); - // socket - // .set_read_timeout(Some(Duration::from_secs(2))) - // .expect("Unable to set UDP socket read timeout"); - // let sock_wrapper = UdpSocketWrapper(socket); - // let ntp_context = NtpContext::new(StdTimestampGen::default()); - - // let result = sntpc::get_time("pool.ntp.org:123", sock_wrapper, ntp_context); - // match result { - // Ok(res_time) => { - // println!("Got time: {}.{}", res_time.sec(), res_time.sec_fraction()); - // self.result = Some(res_time); - // return anyhow::Ok(()) - // } - // Err(err) => { - // error = Some(err); - // warn!("fetch dateTime from ntp failed: {:?}", err); - // }, - // } - // } - // bail!("fetch dateTime from ntp failed: {:?}", error) - // } - -} - -#[derive(Copy, Clone, Default)] -struct StdTimestampGen { - duration: Duration, -} - -impl NtpTimestampGenerator for StdTimestampGen { - fn init(&mut self) { - self.duration = std::time::SystemTime::now() - .duration_since(std::time::SystemTime::UNIX_EPOCH) - .unwrap(); - } - - fn timestamp_sec(&self) -> u64 { - self.duration.as_secs() - } - - fn timestamp_subsec_micros(&self) -> u32 { - self.duration.subsec_micros() - } -} - -#[derive(Debug)] -struct UdpSocketWrapper(UdpSocket); - -impl NtpUdpSocket for UdpSocketWrapper { - fn send_to(&self, buf: &[u8], addr: T) -> Result { - match self.0.send_to(buf, addr) { - Ok(usize) => Ok(usize), - Err(_) => Err(Error::Network), - } - } - - fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, SocketAddr)> { - match self.0.recv_from(buf) { - Ok((size, addr)) => Ok((size, addr)), - Err(_) => Err(Error::Network), - } - } } diff --git a/src/wifi.rs b/src/wifi.rs index 830a447..cd90536 100644 --- a/src/wifi.rs +++ b/src/wifi.rs @@ -33,7 +33,7 @@ impl Internet { fn connect_ap( &mut self, ) -> Result<()> { - const SSID: &str = "Ivan Li"; + const SSID: &str = "Ivan"; const PASSWORD: &str = "ivanli.cc"; info!("Wifi created, about to scan");