Skip to content

Commit

Permalink
Make a few refactorings
Browse files Browse the repository at this point in the history
Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com>
  • Loading branch information
zhiburt authored and patrickelectric committed Apr 26, 2023
1 parent 603cd14 commit f57e119
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 203 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
**/*.rs.bk
/src/html
28 changes: 17 additions & 11 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
extern crate reqwest;
extern crate vergen;
use std::{fs::File, io, path::Path};

use vergen::{vergen, Config};

fn main() {
// Generate the 'cargo:' key output
vergen(Config::default()).expect("Something is wrong!");

let artifacts_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("src/html/");
std::fs::create_dir_all(&artifacts_dir).expect("failed to create a dir");

for remote_file in [
"https://unpkg.com/vue@3.0.5/dist/vue.global.js",
"https://unpkg.com/highlight.js@10.6.0/styles/github.css",
"https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.6.0/highlight.min.js",
] {
let mut resp = reqwest::blocking::get(remote_file)
.expect(&format!("Failed to download vue file: {remote_file}"));

let filename = remote_file.split('/').last().unwrap();
let vue_file_path =
std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join(format!("src/html/{filename}"));
let mut output_file = std::fs::File::create(&vue_file_path)
.expect(&format!("Failed to create vue file: {vue_file_path:?}"));
std::io::copy(&mut resp, &mut output_file).expect("Failed to copy content.");
download_file(remote_file, &artifacts_dir);
}
}

fn download_file(remote_file: &str, dir: &Path) {
let mut resp = reqwest::blocking::get(remote_file)
.unwrap_or_else(|_| panic!("Failed to download vue file: {}", remote_file));

let filename = remote_file.split('/').last().unwrap();
let file_path = dir.join(filename);
let mut output_file = File::create(&file_path)
.unwrap_or_else(|_| panic!("Failed to create artifact file: {:?}", file_path));

io::copy(&mut resp, &mut output_file).expect("Failed to copy content.");
}
12 changes: 3 additions & 9 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use clap;
use lazy_static::lazy_static;
use std::sync::Arc;

#[derive(Debug)]
Expand Down Expand Up @@ -61,13 +61,7 @@ pub fn mavlink_system_and_component_id() -> (u8, u8) {
.parse::<u8>()
.expect("Component ID should be a value between 1-255.");

return (system_id, component_id);
}

// Return the command line used to start this application
#[allow(dead_code)]
pub fn command_line_string() -> String {
return std::env::args().collect::<Vec<String>>().join(" ");
(system_id, component_id)
}

//TODO: Move to the top
Expand Down Expand Up @@ -142,7 +136,7 @@ mod tests {

#[test]
fn default_arguments() {
assert_eq!(is_verbose(), false);
assert!(!is_verbose());
assert_eq!(mavlink_connection_string(), "udpin:0.0.0.0:14550");
assert_eq!(server_address(), "0.0.0.0:8088");
assert_eq!(mavlink_version(), 2);
Expand Down
87 changes: 28 additions & 59 deletions src/data.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::HashMap;
use std::sync::{Arc, Mutex};

use lazy_static::lazy_static;
use mavlink::{self, Message};
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -33,23 +34,15 @@ impl Temporal {
}
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[derive(Default, Clone, Debug, Deserialize, Serialize)]
struct Status {
time: Temporal,
}

impl Default for Status {
fn default() -> Self {
Self {
time: Temporal::default(),
}
}
}

impl Status {
fn update(&mut self) -> &mut Self {
self.time.update();
return self;
self
}
}

Expand Down Expand Up @@ -80,23 +73,14 @@ struct MAVLinkVehicleComponentData {

impl MAVLinkVehicleComponentData {
fn update(&mut self, message: &MAVLinkMessage<mavlink::ardupilotmega::MavMessage>) {
// If message does not exist, add it
let message_name = message.message.message_name().into();
if !self.messages.contains_key(&message_name) {
self.messages.insert(
message_name,
MAVLinkMessageStatus {
message: message.message.clone(),
status: Status::default(),
},
);
return;
}

let message_name = message.message.message_name().to_string();
self.messages
.get_mut(&message_name)
.unwrap()
.update(&message);
.entry(message_name)
.or_insert(MAVLinkMessageStatus {
message: message.message.clone(),
status: Status::default(),
})
.update(message);
}
}

Expand All @@ -108,22 +92,14 @@ struct MAVLinkVehicleData {

impl MAVLinkVehicleData {
fn update(&mut self, message: &MAVLinkMessage<mavlink::ardupilotmega::MavMessage>) {
// If component does not exist, adds it
let component_id = message.header.component_id;
if !self.components.contains_key(&component_id) {
self.components.insert(
component_id,
MAVLinkVehicleComponentData {
id: component_id,
messages: HashMap::new(),
},
);
}

self.components
.get_mut(&component_id)
.unwrap()
.update(&message);
.entry(component_id)
.or_insert(MAVLinkVehicleComponentData {
id: component_id,
messages: HashMap::new(),
})
.update(message);
}
}

Expand All @@ -135,26 +111,22 @@ pub struct MAVLinkVehiclesData {
impl MAVLinkVehiclesData {
//TODO: Move message to reference
fn update(&mut self, message: MAVLinkMessage<mavlink::ardupilotmega::MavMessage>) {
// If vehicle does not exist for us, adds it
let vehicle_id = message.header.system_id;
if !self.vehicles.contains_key(&vehicle_id) {
self.vehicles.insert(
vehicle_id,
MAVLinkVehicleData {
id: vehicle_id,
components: HashMap::new(),
},
);
}

self.vehicles.get_mut(&vehicle_id).unwrap().update(&message);
self.vehicles
.entry(vehicle_id)
.or_insert(MAVLinkVehicleData {
id: vehicle_id,
components: HashMap::new(),
})
.update(&message);
}

pub fn pointer(&self, path: &str) -> String {
let path = format!("/{}", path);
if path == "/" {
if path.is_empty() {
return serde_json::to_string_pretty(self).unwrap();
};
}

let path = format!("/{path}");

dbg!(&path);

Expand Down Expand Up @@ -186,12 +158,9 @@ pub fn update((header, message): (mavlink::MavHeader, mavlink::ardupilotmega::Ma
.lock()
.unwrap()
.update(MAVLinkMessage { header, message });

//let messages = DATA.messages.lock().unwrap();
//println!(">{} {:#?}", messages.len(), messages);
}

pub fn messages() -> MAVLinkVehiclesData {
let messages = DATA.messages.lock().unwrap();
return messages.clone();
messages.clone()
}
Loading

0 comments on commit f57e119

Please sign in to comment.