operator_advisor/
lib.rs

1use serde::{Deserialize, Serialize};
2use serde_json;
3use std::fs::File;
4use std::io::prelude::*;
5use std::path::PathBuf;
6use std::vec::Vec;
7pub mod model;
8pub mod observation;
9mod tests;
10
11pub type Position = [f32; 3];
12
13/// data from the simulator https://gitlab.com/forestry-operator-digital-twin/data
14#[derive(Deserialize, Serialize, Clone, Debug)]
15pub struct Data {
16    pub data: Vec<InternalData>,
17    pub metadata: Metadata,
18}
19
20#[derive(Serialize, Deserialize, Clone, Debug)]
21pub struct InternalData {
22    pub step: i32,
23    pub tree_1: Position,
24    pub tree_2: Position,
25    pub tree_3: Position,
26    pub tree_4: Position,
27    pub tree_5: Position,
28    pub tree_6: Position,
29    pub tree_7: Position,
30    pub tree_8: Position,
31    pub tree_9: Position,
32    pub tree_10: Position,
33    pub position_excavator: Position,
34    pub position_stick: Position,
35    pub position_boom: Position,
36    pub position_hand: Position,
37    pub position_finger_1: Position,
38    pub position_finger_2: Position,
39    pub angular_speed_chassis: f32,
40    pub speed_excavator: Position,
41    pub angular_pos_chassis: f32,
42}
43
44/// metadata of the simulation
45#[derive(Serialize, Deserialize, Clone, Debug)]
46pub struct Metadata {
47    pub time_interval: f32,
48    pub unit_of_time: String,
49    pub unit_of_distance: String,
50    pub unit_of_angle: String,
51    pub starting_time_of_recording_unix: f32,
52}
53
54impl Data {
55    pub fn from_file(path: PathBuf) -> Result<Self, String> {
56        match File::open(path) {
57            Err(e) => Err(format!("{}", e)),
58            Ok(mut file) => {
59                let mut json_string = String::new();
60                if let Err(e) = file.read_to_string(&mut json_string) {
61                    return Err(format!("{}", e));
62                }
63                let json: Result<Self, serde_json::error::Error> =
64                    serde_json::from_str(json_string.as_str());
65                match json {
66                    Err(e) => Err(format!("{}", e)),
67                    Ok(res) => Ok(res),
68                }
69            }
70        }
71    }
72
73    pub fn from_string(json_string: &String) -> Result<Self, String> {
74        let json: Result<Self, serde_json::error::Error> =
75            serde_json::from_str(json_string.as_str());
76        match json {
77            Err(e) => Err(format!("{}", e)),
78            Ok(res) => Ok(res),
79        }
80    }
81}