From 3b063b25e40fea82b921f7dec35c7b575a311cc2 Mon Sep 17 00:00:00 2001 From: Thomas Gatzweiler Date: Fri, 24 Mar 2017 22:21:56 +0100 Subject: [PATCH] Implement server-side TimeSlot calculation --- src/frontend/assets/index.html | 2 +- src/frontend/assets/main.js | 9 --------- src/main.rs | 10 ++++++++++ src/pocsag/mod.rs | 2 +- src/pocsag/timeslots.rs | 6 +++++- src/status.rs | 8 +++++--- 6 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/frontend/assets/index.html b/src/frontend/assets/index.html index 8fc77e9..0b58f1f 100644 --- a/src/frontend/assets/index.html +++ b/src/frontend/assets/index.html @@ -214,7 +214,7 @@

Status

-
+
{{index.toString(16).toUpperCase()}}
diff --git a/src/frontend/assets/main.js b/src/frontend/assets/main.js index a47e9e8..cf6a8db 100644 --- a/src/frontend/assets/main.js +++ b/src/frontend/assets/main.js @@ -2,7 +2,6 @@ var vm = new Vue({ el: "#wrapper", created() { this.connect(); - this.update_timeslot(); }, data: { connected: false, @@ -17,7 +16,6 @@ var vm = new Vue({ raspager: {} }, status: {}, - activeTimeslot: 0, message: "", addr: localStorage ? (localStorage.pager_addr || 0) : 0 }, @@ -102,13 +100,6 @@ var vm = new Vue({ this.send(req); this.message = ""; - }, - update_timeslot: function(event) { - var date = Date.now(); - var timeslot = (Math.floor(date / 100) / 64) & 0b1111; - var next_timeslot = (((Math.floor(date / 100) / 64) + 1) * 64) * 100; - this.activeTimeslot = timeslot; - setTimeout(this.update_timeslot, next_timeslot - date); } } }); diff --git a/src/main.rs b/src/main.rs index bf568c1..74e3da1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -50,6 +50,8 @@ fn main() { let mut config = Config::load(); let scheduler = Scheduler::new(&config); + thread::spawn(timeslot_updater); + let mut restart = true; while restart { let (stop_conn, conn_thread) = Connection::start(config.clone(), scheduler.clone()); @@ -125,3 +127,11 @@ fn main() { info!("Terminating... 73!"); thread::sleep(time::Duration::from_millis(1000)); } + +pub fn timeslot_updater() { + loop { + let timeslot = pocsag::TimeSlot::current(); + status!(timeslot: timeslot); + thread::sleep(timeslot.next().duration_until()); + } +} diff --git a/src/pocsag/mod.rs b/src/pocsag/mod.rs index 073cf4e..e53eca5 100644 --- a/src/pocsag/mod.rs +++ b/src/pocsag/mod.rs @@ -7,7 +7,7 @@ pub mod encoding; pub use self::generator::Generator; pub use self::message::{Message, MessageSpeed, MessageType, MessageFunc}; pub use self::scheduler::Scheduler; -pub use self::timeslots::TimeSlots; +pub use self::timeslots::{TimeSlots, TimeSlot}; pub use self::encoding::Encoding; pub trait MessageProvider { diff --git a/src/pocsag/timeslots.rs b/src/pocsag/timeslots.rs index 4e6a2b8..96f6434 100644 --- a/src/pocsag/timeslots.rs +++ b/src/pocsag/timeslots.rs @@ -14,7 +14,7 @@ fn deciseconds(duration: Duration) -> u64 { seconds * 10 + deciseconds } -#[derive(PartialEq)] +#[derive(Serialize, Clone, Copy, PartialEq)] pub struct TimeSlot(usize); impl TimeSlot { @@ -29,6 +29,10 @@ impl TimeSlot { *self == TimeSlot::current() } + pub fn next(&self) -> TimeSlot { + TimeSlot((self.0 + 1) % 16) + } + pub fn duration_until(&self) -> Duration { let now = unix_time(); let now_decis = deciseconds(now); diff --git a/src/status.rs b/src/status.rs index f6f3360..164449d 100644 --- a/src/status.rs +++ b/src/status.rs @@ -1,5 +1,5 @@ use std::sync::{Mutex, RwLock}; -use pocsag::TimeSlots; +use pocsag::{TimeSlots, TimeSlot}; use frontend::Responder; lazy_static! { @@ -11,7 +11,8 @@ lazy_static! { pub struct Status { pub connected: bool, pub transmitting: bool, - pub timeslots: TimeSlots + pub timeslots: TimeSlots, + pub timeslot: TimeSlot } impl Status { @@ -19,7 +20,8 @@ impl Status { Status { connected: false, transmitting: false, - timeslots: TimeSlots::new() + timeslots: TimeSlots::new(), + timeslot: TimeSlot::current() } } }