Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
keesverruijt committed Sep 14, 2024
1 parent db6965f commit 11cf3b4
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 34 deletions.
33 changes: 31 additions & 2 deletions src/navico/command.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use log::{debug, trace};
use num_traits::ToPrimitive;
use std::cmp::{max, min};
use std::cmp::min;
use tokio::net::UdpSocket;

use crate::radar::{RadarError, RadarInfo};
Expand Down Expand Up @@ -68,6 +68,35 @@ impl Command {
(a + 720) % 360
}

fn valid_range(&self, i: i32) -> i32 {
let rd = self.info.range_detection.as_ref().unwrap();

if i < rd.min_range {
return rd.min_range;
}
if i > rd.max_range {
return rd.max_range;
}
let mut next = false;
let mut prev = rd.min_range;
for range in &rd.ranges {
if next {
return *range;
}
if i < *range {
return prev;
}
if i == *range {
return i;
}
if i == range + 1 {
next = true;
}
prev = *range;
}
rd.max_range
}

pub async fn set_control(&mut self, cv: &ControlValue) -> Result<(), RadarError> {
let value = cv
.value
Expand All @@ -79,7 +108,7 @@ impl Command {

match cv.id {
ControlType::Range => {
let decimeters: i32 = max(value, 50) * 10;
let decimeters: i32 = self.valid_range(value) * 10;

cmd.extend_from_slice(&[0x03, 0xc1]);
cmd.extend_from_slice(&decimeters.to_le_bytes());
Expand Down
10 changes: 0 additions & 10 deletions src/radar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use log::info;
use protobuf::Message;
use serde::ser::{SerializeMap, Serializer};
use serde::Serialize;
use std::sync::PoisonError;
use std::{
collections::HashMap,
fmt::{self, Display, Write},
Expand Down Expand Up @@ -490,15 +489,6 @@ impl SharedRadars {
}
}

pub fn read(
&self,
) -> Result<
std::sync::RwLockReadGuard<'_, Radars>,
PoisonError<std::sync::RwLockReadGuard<'_, Radars>>,
> {
self.radars.read()
}

// A radar has been found
pub fn located(&self, mut new_info: RadarInfo) -> Option<RadarInfo> {
let key = new_info.key.to_owned();
Expand Down
14 changes: 4 additions & 10 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,19 +283,13 @@ impl Control {
// }

pub fn value(&self) -> String {
if self.description.is_some() {
if self.item.is_string_value {
return self.description.clone().unwrap();
}
if let (Some(value), Some(descriptions)) = (self.value, &self.item.descriptions) {
if let Some(v) = descriptions.get(value as usize) {
return v.to_string();
}
}

return format!(
"{}",
self.value.unwrap_or(self.item.default_value.unwrap_or(0))
);
self.value
.unwrap_or(self.item.default_value.unwrap_or(0))
.to_string()
}

pub fn set_all(
Expand Down
33 changes: 21 additions & 12 deletions web/control.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const StringValue = (id, name) =>
const NumericValue = (id, name) =>
div({class: 'control'},
label({ for: id }, name),
input({ type: 'number', id: id, onchange: e => change(e) })
input({ type: 'number', id: id, onchange: e => change(e), oninput: e => do_input(e) })
)

const RangeValue = (id, name, min, max, def, descriptions) =>
Expand All @@ -33,23 +33,24 @@ window.onload = function () {
const urlParams = new URLSearchParams(window.location.search);
const id = urlParams.get('id');

if (id !== null) {
setTimeout(loadRadars(id), 200);
}
loadRadars(id);
}

function loadRadars(id) {
fetch('/v1/api/radars')
.then(res => res.json())
.then(out => radarsLoaded(id, out))
.catch(err => setTimeout(loadRadars(id), 15000));
.catch(err => restart(id));
}

function restart(id) {
setTimeout(loadRadars(id), 15000);
}
function radarsLoaded(id, d) {
radar = d[id];

if (radar === undefined || radar.controls === undefined) {
setTimeout(loadRadars(id), 15000);
restart(id);
return;
}
controls = radar.controls;
Expand All @@ -63,15 +64,18 @@ function radarsLoaded(id, d) {
}
webSocket.onclose = (e) => {
console.log("websocket close: " + e);
restart(id);
}
webSocket.onmessage = (e) => {
console.log("websocket message: " + e.data);

let v = JSON.parse(e.data);
let d = document.getElementById(v.id);
d.setAttribute('value', ('description' in v && d.type == 'text') ? v.description : v.value);
if ('descriptions' in controls[v.id] && d.type == 'range') {
let desc = d.parentNode.querySelector('.description');
if (desc) desc.innerHTML = v.description;
let i = document.getElementById(v.id);
i.setAttribute('value', v.value);
console.log("<- " + e.data + " = " + controls[v.id].name);
if ('descriptions' in controls[v.id] && i.type == 'range') {
let description = controls[v.id].descriptions[v.value];
let d = i.parentNode.querySelector('.description');
if (d) d.innerHTML = description;
}
}

Expand Down Expand Up @@ -111,4 +115,9 @@ function set_button(e) {
let cv = JSON.stringify({ id: v.id, value: v.value });
webSocket.send(cv);
console.log(controls[v.id].name + "-> " + cv);
}

function do_input(e) {
let v = e.target;
console.log("input " + e + " " + v.id + "=" + v.value);
}

0 comments on commit 11cf3b4

Please sign in to comment.