Browse Source

base-station: Implement storing/retrieving the most recent set of values.

Mathias Gottschlag 5 years ago
parent
commit
2325b6e49c
1 changed files with 26 additions and 7 deletions
  1. 26
    7
      base-station/software/src/current.rs

+ 26
- 7
base-station/software/src/current.rs View File

@@ -1,20 +1,39 @@
1
+use std::collections::HashMap;
2
+
3
+use log::error;
4
+
1 5
 use protocol::{Location, Value, ValueType};
2 6
 
3 7
 pub struct CurrentValues {
4
-    // TODO
8
+    map: HashMap<Key, i64>,
5 9
 }
6 10
 
7 11
 impl CurrentValues {
8 12
     pub fn new() -> CurrentValues {
9
-        CurrentValues {}
13
+        CurrentValues {
14
+            map: HashMap::new(),
15
+        }
10 16
     }
11 17
 
12
-    pub fn set(&mut self, location: Location, _value: Value) {
13
-        // TODO
18
+    pub fn set(&mut self, location: Location, value: Value) {
19
+        let (type_, value) = match value.split() {
20
+            Some(x) => x,
21
+            None => {
22
+                error!("Invalid value passed to CurrentValues::set().");
23
+                return;
24
+            }
25
+        };
26
+        self.map.insert(Key { location, type_ }, value);
14 27
     }
15 28
 
16
-    pub fn get(&self, location: Location, _value: ValueType) -> Option<Value> {
17
-        // TODO
18
-        None
29
+    pub fn get(&self, location: Location, type_: ValueType) -> Option<Value> {
30
+        let entry = self.map.get(&Key { location, type_ })?.clone();
31
+        Some(Value::combine(type_, entry))
19 32
     }
20 33
 }
34
+
35
+#[derive(Copy, Clone, PartialEq, Eq, Hash)]
36
+struct Key {
37
+    location: Location,
38
+    type_: ValueType,
39
+}

Loading…
Cancel
Save