v0.2:重写项目。 #6
@ -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]
|
||||
|
@ -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",
|
||||
|
71
src/time.rs
71
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<NtpResult>,
|
||||
sntp: Option<Box<EspSntp>>,
|
||||
}
|
||||
|
||||
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<T: ToSocketAddrs>(&self, buf: &[u8], addr: T) -> Result<usize> {
|
||||
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),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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");
|
||||
|
Loading…
Reference in New Issue
Block a user