Browse Source

base-station: First part of refactoring to use tokio and InfluxDB.

Mathias Gottschlag 5 years ago
parent
commit
4afb52c5be

+ 244
- 2
base-station/software/Cargo.lock View File

@@ -9,6 +9,12 @@ dependencies = [
9 9
  "memchr",
10 10
 ]
11 11
 
12
+[[package]]
13
+name = "arc-swap"
14
+version = "0.4.6"
15
+source = "registry+https://github.com/rust-lang/crates.io-index"
16
+checksum = "b585a98a234c46fc563103e9278c9391fde1f4e6850334da895d27edb9580f62"
17
+
12 18
 [[package]]
13 19
 name = "atty"
14 20
 version = "0.2.14"
@@ -17,7 +23,7 @@ checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
17 23
 dependencies = [
18 24
  "hermit-abi",
19 25
  "libc",
20
- "winapi",
26
+ "winapi 0.3.8",
21 27
 ]
22 28
 
23 29
 [[package]]
@@ -33,6 +39,7 @@ dependencies = [
33 39
  "protocol",
34 40
  "rand",
35 41
  "thiserror",
42
+ "tokio",
36 43
 ]
37 44
 
38 45
 [[package]]
@@ -53,6 +60,12 @@ version = "1.3.4"
53 60
 source = "registry+https://github.com/rust-lang/crates.io-index"
54 61
 checksum = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de"
55 62
 
63
+[[package]]
64
+name = "bytes"
65
+version = "0.5.4"
66
+source = "registry+https://github.com/rust-lang/crates.io-index"
67
+checksum = "130aac562c0dd69c56b3b1cc8ffd2e17be31d0b6c25b61c96b76231aa23e39e1"
68
+
56 69
 [[package]]
57 70
 name = "cast"
58 71
 version = "0.2.3"
@@ -112,6 +125,34 @@ dependencies = [
112 125
  "termcolor",
113 126
 ]
114 127
 
128
+[[package]]
129
+name = "fnv"
130
+version = "1.0.6"
131
+source = "registry+https://github.com/rust-lang/crates.io-index"
132
+checksum = "2fad85553e09a6f881f739c29f0b00b0f01357c743266d478b68951ce23285f3"
133
+
134
+[[package]]
135
+name = "fuchsia-zircon"
136
+version = "0.3.3"
137
+source = "registry+https://github.com/rust-lang/crates.io-index"
138
+checksum = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82"
139
+dependencies = [
140
+ "bitflags",
141
+ "fuchsia-zircon-sys",
142
+]
143
+
144
+[[package]]
145
+name = "fuchsia-zircon-sys"
146
+version = "0.3.3"
147
+source = "registry+https://github.com/rust-lang/crates.io-index"
148
+checksum = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7"
149
+
150
+[[package]]
151
+name = "futures-core"
152
+version = "0.3.5"
153
+source = "registry+https://github.com/rust-lang/crates.io-index"
154
+checksum = "59f5fff90fd5d971f936ad674802482ba441b6f09ba5e15fd8b39145582ca399"
155
+
115 156
 [[package]]
116 157
 name = "getrandom"
117 158
 version = "0.1.14"
@@ -162,6 +203,25 @@ dependencies = [
162 203
  "libc",
163 204
 ]
164 205
 
206
+[[package]]
207
+name = "iovec"
208
+version = "0.1.4"
209
+source = "registry+https://github.com/rust-lang/crates.io-index"
210
+checksum = "b2b3ea6ff95e175473f8ffe6a7eb7c00d054240321b84c57051175fe3c1e075e"
211
+dependencies = [
212
+ "libc",
213
+]
214
+
215
+[[package]]
216
+name = "kernel32-sys"
217
+version = "0.2.2"
218
+source = "registry+https://github.com/rust-lang/crates.io-index"
219
+checksum = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
220
+dependencies = [
221
+ "winapi 0.2.8",
222
+ "winapi-build",
223
+]
224
+
165 225
 [[package]]
166 226
 name = "lazy_static"
167 227
 version = "1.4.0"
@@ -205,6 +265,70 @@ version = "2.3.3"
205 265
 source = "registry+https://github.com/rust-lang/crates.io-index"
206 266
 checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400"
207 267
 
268
+[[package]]
269
+name = "mio"
270
+version = "0.6.22"
271
+source = "registry+https://github.com/rust-lang/crates.io-index"
272
+checksum = "fce347092656428bc8eaf6201042cb551b8d67855af7374542a92a0fbfcac430"
273
+dependencies = [
274
+ "cfg-if",
275
+ "fuchsia-zircon",
276
+ "fuchsia-zircon-sys",
277
+ "iovec",
278
+ "kernel32-sys",
279
+ "libc",
280
+ "log",
281
+ "miow 0.2.1",
282
+ "net2",
283
+ "slab",
284
+ "winapi 0.2.8",
285
+]
286
+
287
+[[package]]
288
+name = "mio-named-pipes"
289
+version = "0.1.6"
290
+source = "registry+https://github.com/rust-lang/crates.io-index"
291
+checksum = "f5e374eff525ce1c5b7687c4cef63943e7686524a387933ad27ca7ec43779cb3"
292
+dependencies = [
293
+ "log",
294
+ "mio",
295
+ "miow 0.3.3",
296
+ "winapi 0.3.8",
297
+]
298
+
299
+[[package]]
300
+name = "mio-uds"
301
+version = "0.6.7"
302
+source = "registry+https://github.com/rust-lang/crates.io-index"
303
+checksum = "966257a94e196b11bb43aca423754d87429960a768de9414f3691d6957abf125"
304
+dependencies = [
305
+ "iovec",
306
+ "libc",
307
+ "mio",
308
+]
309
+
310
+[[package]]
311
+name = "miow"
312
+version = "0.2.1"
313
+source = "registry+https://github.com/rust-lang/crates.io-index"
314
+checksum = "8c1f2f3b1cf331de6896aabf6e9d55dca90356cc9960cca7eaaf408a355ae919"
315
+dependencies = [
316
+ "kernel32-sys",
317
+ "net2",
318
+ "winapi 0.2.8",
319
+ "ws2_32-sys",
320
+]
321
+
322
+[[package]]
323
+name = "miow"
324
+version = "0.3.3"
325
+source = "registry+https://github.com/rust-lang/crates.io-index"
326
+checksum = "396aa0f2003d7df8395cb93e09871561ccc3e785f0acb369170e8cc74ddf9226"
327
+dependencies = [
328
+ "socket2",
329
+ "winapi 0.3.8",
330
+]
331
+
208 332
 [[package]]
209 333
 name = "mqtt-protocol"
210 334
 version = "0.8.1"
@@ -223,6 +347,17 @@ version = "0.1.2"
223 347
 source = "registry+https://github.com/rust-lang/crates.io-index"
224 348
 checksum = "b1411551beb3c11dedfb0a90a0fa256b47d28b9ec2cdff34c25a2fa59e45dbdc"
225 349
 
350
+[[package]]
351
+name = "net2"
352
+version = "0.2.34"
353
+source = "registry+https://github.com/rust-lang/crates.io-index"
354
+checksum = "2ba7c918ac76704fb42afcbbb43891e72731f3dcca3bef2a19786297baf14af7"
355
+dependencies = [
356
+ "cfg-if",
357
+ "libc",
358
+ "winapi 0.3.8",
359
+]
360
+
226 361
 [[package]]
227 362
 name = "nix"
228 363
 version = "0.14.1"
@@ -236,6 +371,22 @@ dependencies = [
236 371
  "void",
237 372
 ]
238 373
 
374
+[[package]]
375
+name = "num_cpus"
376
+version = "1.13.0"
377
+source = "registry+https://github.com/rust-lang/crates.io-index"
378
+checksum = "05499f3756671c15885fee9034446956fff3f243d6077b91e5767df161f766b3"
379
+dependencies = [
380
+ "hermit-abi",
381
+ "libc",
382
+]
383
+
384
+[[package]]
385
+name = "pin-project-lite"
386
+version = "0.1.5"
387
+source = "registry+https://github.com/rust-lang/crates.io-index"
388
+checksum = "f7505eeebd78492e0f6108f7171c4948dbb120ee8119d9d77d0afa5469bef67f"
389
+
239 390
 [[package]]
240 391
 name = "ppv-lite86"
241 392
 version = "0.2.6"
@@ -315,6 +466,12 @@ dependencies = [
315 466
  "rand_core",
316 467
 ]
317 468
 
469
+[[package]]
470
+name = "redox_syscall"
471
+version = "0.1.56"
472
+source = "registry+https://github.com/rust-lang/crates.io-index"
473
+checksum = "2439c63f3f6139d1b57529d16bc3b8bb855230c8efcc5d3a896c8bea7c3b1e84"
474
+
318 475
 [[package]]
319 476
 name = "regex"
320 477
 version = "1.3.7"
@@ -378,6 +535,34 @@ dependencies = [
378 535
  "termios",
379 536
 ]
380 537
 
538
+[[package]]
539
+name = "signal-hook-registry"
540
+version = "1.2.0"
541
+source = "registry+https://github.com/rust-lang/crates.io-index"
542
+checksum = "94f478ede9f64724c5d173d7bb56099ec3e2d9fc2774aac65d34b8b890405f41"
543
+dependencies = [
544
+ "arc-swap",
545
+ "libc",
546
+]
547
+
548
+[[package]]
549
+name = "slab"
550
+version = "0.4.2"
551
+source = "registry+https://github.com/rust-lang/crates.io-index"
552
+checksum = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8"
553
+
554
+[[package]]
555
+name = "socket2"
556
+version = "0.3.12"
557
+source = "registry+https://github.com/rust-lang/crates.io-index"
558
+checksum = "03088793f677dce356f3ccc2edb1b314ad191ab702a5de3faf49304f7e104918"
559
+dependencies = [
560
+ "cfg-if",
561
+ "libc",
562
+ "redox_syscall",
563
+ "winapi 0.3.8",
564
+]
565
+
381 566
 [[package]]
382 567
 name = "spidev"
383 568
 version = "0.4.0"
@@ -456,6 +641,41 @@ dependencies = [
456 641
  "lazy_static",
457 642
 ]
458 643
 
644
+[[package]]
645
+name = "tokio"
646
+version = "0.2.20"
647
+source = "registry+https://github.com/rust-lang/crates.io-index"
648
+checksum = "05c1d570eb1a36f0345a5ce9c6c6e665b70b73d11236912c0b477616aeec47b1"
649
+dependencies = [
650
+ "bytes",
651
+ "fnv",
652
+ "futures-core",
653
+ "iovec",
654
+ "lazy_static",
655
+ "libc",
656
+ "memchr",
657
+ "mio",
658
+ "mio-named-pipes",
659
+ "mio-uds",
660
+ "num_cpus",
661
+ "pin-project-lite",
662
+ "signal-hook-registry",
663
+ "slab",
664
+ "tokio-macros",
665
+ "winapi 0.3.8",
666
+]
667
+
668
+[[package]]
669
+name = "tokio-macros"
670
+version = "0.2.5"
671
+source = "registry+https://github.com/rust-lang/crates.io-index"
672
+checksum = "f0c3acc6aa564495a0f2e1d59fab677cd7f81a19994cfc7f3ad0e64301560389"
673
+dependencies = [
674
+ "proc-macro2",
675
+ "quote",
676
+ "syn",
677
+]
678
+
459 679
 [[package]]
460 680
 name = "unicode-xid"
461 681
 version = "0.2.0"
@@ -474,6 +694,12 @@ version = "0.9.0+wasi-snapshot-preview1"
474 694
 source = "registry+https://github.com/rust-lang/crates.io-index"
475 695
 checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
476 696
 
697
+[[package]]
698
+name = "winapi"
699
+version = "0.2.8"
700
+source = "registry+https://github.com/rust-lang/crates.io-index"
701
+checksum = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"
702
+
477 703
 [[package]]
478 704
 name = "winapi"
479 705
 version = "0.3.8"
@@ -484,6 +710,12 @@ dependencies = [
484 710
  "winapi-x86_64-pc-windows-gnu",
485 711
 ]
486 712
 
713
+[[package]]
714
+name = "winapi-build"
715
+version = "0.1.1"
716
+source = "registry+https://github.com/rust-lang/crates.io-index"
717
+checksum = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc"
718
+
487 719
 [[package]]
488 720
 name = "winapi-i686-pc-windows-gnu"
489 721
 version = "0.4.0"
@@ -496,7 +728,7 @@ version = "0.1.4"
496 728
 source = "registry+https://github.com/rust-lang/crates.io-index"
497 729
 checksum = "fa515c5163a99cc82bab70fd3bfdd36d827be85de63737b40fcef2ce084a436e"
498 730
 dependencies = [
499
- "winapi",
731
+ "winapi 0.3.8",
500 732
 ]
501 733
 
502 734
 [[package]]
@@ -505,6 +737,16 @@ version = "0.4.0"
505 737
 source = "registry+https://github.com/rust-lang/crates.io-index"
506 738
 checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
507 739
 
740
+[[package]]
741
+name = "ws2_32-sys"
742
+version = "0.2.1"
743
+source = "registry+https://github.com/rust-lang/crates.io-index"
744
+checksum = "d59cefebd0c892fa2dd6de581e937301d8552cb44489cdff035c6187cb63fa5e"
745
+dependencies = [
746
+ "winapi 0.2.8",
747
+ "winapi-build",
748
+]
749
+
508 750
 [[package]]
509 751
 name = "xxtea-nostd"
510 752
 version = "0.1.0"

+ 1
- 0
base-station/software/Cargo.toml View File

@@ -16,3 +16,4 @@ log = "0.4"
16 16
 thiserror = "1.0"
17 17
 protocol = { path = "../../common/rust-protocol" }
18 18
 rand = "0.7.3"
19
+tokio = { version="0.2", features=[ "full" ] }

+ 20
- 0
base-station/software/src/current.rs View File

@@ -0,0 +1,20 @@
1
+use protocol::{Location, Value, ValueType};
2
+
3
+pub struct CurrentValues {
4
+    // TODO
5
+}
6
+
7
+impl CurrentValues {
8
+    pub fn new() -> CurrentValues {
9
+        CurrentValues {}
10
+    }
11
+
12
+    pub fn set(&mut self, location: Location, _value: Value) {
13
+        // TODO
14
+    }
15
+
16
+    pub fn get(&self, location: Location, _value: ValueType) -> Option<Value> {
17
+        // TODO
18
+        None
19
+    }
20
+}

+ 151
- 25
base-station/software/src/main.rs View File

@@ -1,41 +1,169 @@
1 1
 use std::env;
2 2
 use std::io;
3
-use std::sync::mpsc;
4
-use std::thread::sleep;
5
-use std::time::Duration;
3
+use std::sync::Arc;
4
+use std::time::Instant;
6 5
 
7
-use ::mqtt::control::variable_header::ConnectReturnCode;
8
-use log::error;
6
+use log::{error, info};
7
+use protocol::{GetValues, Packet, Report, Value, Values};
8
+use rand::{rngs::ThreadRng, Rng};
9
+use tokio::sync::Mutex;
9 10
 
10
-use crate::mqtt::MQTT;
11
+use current::CurrentValues;
12
+use publish::Publish;
13
+use radio::{Radio, RadioConfig};
14
+use tsdb::TimeSeriesDatabase;
11 15
 
12
-mod mqtt;
16
+mod current;
17
+mod publish;
13 18
 mod radio;
14 19
 mod spi;
20
+mod tsdb;
15 21
 
16
-fn main() {
22
+#[tokio::main]
23
+async fn main() {
17 24
     env::set_var(
18 25
         "RUST_LOG",
19 26
         env::var_os("RUST_LOG").unwrap_or_else(|| "info".into()),
20 27
     );
21 28
     env_logger::init();
22 29
 
23
-    let (sensor_send, sensor_recv) = mpsc::channel();
30
+    // Initialize radio, MQTT and InfluxDB.
31
+    let radio_config = RadioConfig{
32
+        // TODO: Configure pins.
33
+    };
34
+    let (radio, mut incoming_packets) = Radio::init(radio_config)
35
+        .await
36
+        .expect("radio initialization failed");
37
+    let tsdb = TimeSeriesDatabase::init();
38
+    let publish = Publish::init();
24 39
 
25
-    radio::start(sensor_send);
40
+    let mut app = Application {
41
+        radio: Arc::new(Mutex::new(radio)),
42
+        publish: Arc::new(Mutex::new(publish)),
43
+        tsdb: Arc::new(Mutex::new(tsdb)),
44
+        current: CurrentValues::new(),
45
+        rng: rand::thread_rng(),
46
+    };
26 47
 
27
-    // Whenever the MQTT connection returns an error, we wait for a couple of seconds and then try
28
-    // to reconnect.
48
+    // Wait for packets and handle the corresponding requests.
29 49
     loop {
30
-        // TODO: Pass events from ratio to mqtt connection.
31
-        //     if let Err(e) = run_mqtt_connection(&sensor_recv) {
32
-        //         error!("MQTT error: {:?}", e);
33
-        //     }
34
-        sleep(Duration::from_secs(3));
50
+        let (device_id, packet) = incoming_packets
51
+            .recv()
52
+            .await
53
+            .expect("unexpected end of packet stream");
54
+        app.handle_packet(device_id, packet).await;
35 55
     }
36 56
 }
37 57
 
38
-fn run_mqtt_connection(updates: &mpsc::Receiver<SensorUpdate>) -> Result<(), Error> {
58
+struct Application {
59
+    radio: Arc<Mutex<Radio>>,
60
+    publish: Arc<Mutex<Publish>>,
61
+    tsdb: Arc<Mutex<TimeSeriesDatabase>>,
62
+    current: CurrentValues,
63
+    rng: ThreadRng,
64
+}
65
+
66
+impl Application {
67
+    async fn handle_packet(&mut self, device_id: u8, packet: Packet) {
68
+        // TODO: More packet types to query timeseries database.
69
+        match packet {
70
+            Packet::GetSalt => {
71
+                self.handle_get_salt(device_id).await;
72
+            }
73
+            Packet::Salt(_) => {
74
+                error!("Received Salt packet from device, ignoring.");
75
+            }
76
+            Packet::Report(payload) => {
77
+                self.handle_report(device_id, payload).await;
78
+            }
79
+            Packet::GetValues(payload) => {
80
+                self.handle_get_values(device_id, payload).await;
81
+            }
82
+            Packet::Values(_payload) => {
83
+                error!("received Values packet from device.");
84
+            }
85
+        }
86
+    }
87
+
88
+    async fn handle_get_salt(&mut self, device_id: u8) {
89
+        let salt = (self.rng.gen::<u64>() & !0xff & !(1 << 63)) | device_id as u64;
90
+        // TODO: Store the salt as the expected salt in Radio.
91
+        self.radio
92
+            .lock()
93
+            .await
94
+            .send_packet_async(device_id, Packet::Salt(salt));
95
+    }
96
+
97
+    async fn handle_report(&mut self, device_id: u8, report: Report) {
98
+        let location = radio::get_device_location(device_id);
99
+        let count = report.count as usize;
100
+        // Update local copy of current values.
101
+        for i in 0..count {
102
+            match report.values[i] {
103
+                Value::Invalid => {}
104
+                Value::Time(_) => {
105
+                    error!("device tried to report a time");
106
+                }
107
+                Value::Temperature(temperature) => {
108
+                    let temperature = temperature as f32 / 10.0;
109
+                    info!("{:?} temperature: {} °C", location, temperature);
110
+                }
111
+                Value::Pressure(pressure) => {
112
+                    let pressure = pressure as f32 / 100.0;
113
+                    info!("{:?} pressure: {} HPa", location, pressure);
114
+                }
115
+                Value::Humidity(humidity) => {
116
+                    let humidity = humidity as f32 / 100.0;
117
+                    info!("{:?} humidity: {}%", location, humidity);
118
+                }
119
+            }
120
+            self.current.set(location, report.values[i]);
121
+        }
122
+
123
+        // Send values via MQTT.
124
+        let publish = self.publish.clone();
125
+        let r = report.clone();
126
+        tokio::spawn(async move {
127
+            let mut publish = publish.lock().await;
128
+            publish.publish(location, &r.values[0..count]).await;
129
+        });
130
+
131
+        // Insert values into TSDB.
132
+        let tsdb = self.tsdb.clone();
133
+        tokio::spawn(async move {
134
+            let mut tsdb = tsdb.lock().await;
135
+            tsdb.insert(Instant::now(), location, &report.values[0..count])
136
+                .await;
137
+        });
138
+    }
139
+
140
+    async fn handle_get_values(&mut self, device_id: u8, request: GetValues) {
141
+        let count = request.count as usize;
142
+        let mut response = Values {
143
+            count: 0,
144
+            location: request.location,
145
+            values: [Value::Invalid; 8],
146
+        };
147
+        for i in 0..count {
148
+            match self.current.get(request.location, request.types_[i]) {
149
+                Some(value) => {
150
+                    // During deserialization, we made sure that count can never be larger than 7.
151
+                    response.values[response.count as usize] = value;
152
+                    response.count += 1;
153
+                }
154
+                None => {
155
+                    // TODO: Tell the sender that the value was not found?
156
+                }
157
+            }
158
+        }
159
+        self.radio
160
+            .lock()
161
+            .await
162
+            .send_packet_async(device_id, Packet::Values(response));
163
+    }
164
+}
165
+
166
+/*fn run_mqtt_connection(updates: &mpsc::Receiver<SensorUpdate>) -> Result<(), Error> {
39 167
     let mut m = MQTT::connect("127.0.0.1:1883")?;
40 168
 
41 169
     loop {
@@ -70,25 +198,23 @@ fn run_mqtt_connection(updates: &mpsc::Receiver<SensorUpdate>) -> Result<(), Err
70 198
             }
71 199
         }
72 200
     }
73
-}
201
+}*/
74 202
 
75
-pub struct SensorUpdate {
203
+/*pub(crate) struct SensorUpdate {
76 204
     location: u32,
77 205
     data: Vec<SensorData>,
78 206
 }
79 207
 
80
-pub enum SensorData {
208
+pub(crate) enum SensorData {
81 209
     Temperature(f32),
82 210
     Humidity(f32),
83 211
     Pressure(f32),
84
-}
212
+}*/
85 213
 
86 214
 #[derive(thiserror::Error, Debug)]
87
-pub enum Error {
215
+pub(crate) enum Error {
88 216
     #[error("I/O error")]
89 217
     Io(#[from] io::Error),
90
-    #[error("connection refused, return code {0:?}")]
91
-    ConnectionRefused(ConnectReturnCode),
92 218
     #[error("radio error: {0:?}")]
93 219
     Radio(embedded_nrf24l01::Error<std::io::Error>),
94 220
 }

+ 3
- 3
base-station/software/src/mqtt.rs View File

@@ -10,12 +10,12 @@ use mqtt::{TopicFilter, TopicName};
10 10
 
11 11
 use super::Error;
12 12
 
13
-pub struct MQTT {
13
+pub(crate) struct MQTT {
14 14
     stream: TcpStream,
15 15
 }
16 16
 
17 17
 impl MQTT {
18
-    pub fn connect(server_addr: &str) -> Result<MQTT, Error> {
18
+    pub(crate) fn connect(server_addr: &str) -> Result<MQTT, Error> {
19 19
         info!("Connecting to MQTT broker at {}...", server_addr);
20 20
         let mut stream = TcpStream::connect(server_addr)?;
21 21
 
@@ -65,7 +65,7 @@ impl MQTT {
65 65
         Ok(MQTT { stream })
66 66
     }
67 67
 
68
-    pub fn publish(&mut self, topic: &str, value: &str) -> Result<(), Error> {
68
+    pub(crate) fn publish(&mut self, topic: &str, value: &str) -> Result<(), Error> {
69 69
         // Publish the value.
70 70
         let topic = TopicName::new(topic).unwrap();
71 71
         let publish_packet = PublishPacket::new(topic, QoSWithPacketIdentifier::Level0, value);

+ 16
- 0
base-station/software/src/publish.rs View File

@@ -0,0 +1,16 @@
1
+use protocol::{Location, Value};
2
+
3
+pub struct Publish {
4
+    // TODO
5
+}
6
+
7
+impl Publish {
8
+    pub fn init() -> Publish {
9
+        // TODO
10
+        Publish {}
11
+    }
12
+
13
+    pub async fn publish(&mut self, _location: Location, _values: &[Value]) {
14
+        // TODO
15
+    }
16
+}

+ 48
- 20
base-station/software/src/radio.rs View File

@@ -1,10 +1,10 @@
1
-use std::convert::TryInto;
1
+/*use std::convert::TryInto;
2 2
 use std::sync::mpsc;
3 3
 use std::thread;
4 4
 use std::thread::sleep;
5
-use std::time::{Duration, Instant};
5
+use std::time::{Duration, Instant};*/
6 6
 
7
-use embedded_hal::blocking::spi::Transfer;
7
+/*use embedded_hal::blocking::spi::Transfer;
8 8
 use embedded_hal::digital::v2::OutputPin;
9 9
 use embedded_nrf24l01::{Configuration, CrcMode, DataRate, RxMode, NRF24L01};
10 10
 use linux_embedded_hal::spidev::{SpiModeFlags, Spidev, SpidevOptions};
@@ -12,20 +12,48 @@ use linux_embedded_hal::sysfs_gpio::Direction;
12 12
 use linux_embedded_hal::sysfs_gpio::Error as GpioError;
13 13
 use linux_embedded_hal::Pin;
14 14
 use log::{error, info};
15
-use protocol::{Location, Packet, Value};
16 15
 use rand::rngs::ThreadRng;
17
-use rand::Rng;
16
+use rand::Rng;*/
17
+use protocol::{Location, Packet};
18
+use tokio::sync::mpsc::UnboundedReceiver;
18 19
 
19
-use crate::spi::EmbeddedHalSpidev;
20
-use crate::Error;
21
-use crate::{SensorData, SensorUpdate};
20
+/*use super::spi::EmbeddedHalSpidev;
21
+use super::Error;
22
+use super::{SensorData, SensorUpdate};*/
22 23
 
23 24
 const DISPLAY_ID: u8 = 0x20;
24
-const DISPLAY_KEY: [u8; 16] = include!("../../../common/display_key.txt");
25
+//const DISPLAY_KEY: [u8; 16] = include!("../../../common/display_key.txt");
25 26
 const WEATHER_STATION_0_ID: u8 = 0x30;
26
-const WEATHER_STATION_0_KEY: [u8; 16] = include!("../../../common/weather_station_0_key.txt");
27
+//const WEATHER_STATION_0_KEY: [u8; 16] = include!("../../../common/weather_station_0_key.txt");
27 28
 
28
-pub fn start(updates: mpsc::Sender<SensorUpdate>) {
29
+pub struct RadioConfig {
30
+    // TODO
31
+}
32
+
33
+pub struct Radio {
34
+    // TODO
35
+}
36
+
37
+impl Radio {
38
+    pub async fn init(
39
+        _config: RadioConfig,
40
+    ) -> Result<(Radio, UnboundedReceiver<(u8, Packet)>), RadioError> {
41
+        // TODO
42
+        panic!("Not yet implemented.");
43
+    }
44
+
45
+    pub fn send_packet_async(&mut self, _device_id: u8, _packet: Packet) {
46
+        // TODO
47
+        panic!("Not yet implemented.");
48
+    }
49
+}
50
+
51
+#[derive(Clone, Copy, Debug)]
52
+pub enum RadioError {
53
+    // TODO
54
+}
55
+
56
+/*pub(crate) fn start(updates: mpsc::Sender<SensorUpdate>) {
29 57
     // TODO: Channels for sensor readings.
30 58
     thread::spawn(move || {
31 59
         // Whenever a error occurs, reinitialize the radio module shortly later.
@@ -42,19 +70,19 @@ pub fn start(updates: mpsc::Sender<SensorUpdate>) {
42 70
 }
43 71
 
44 72
 fn radio_thread(updates: &mpsc::Sender<SensorUpdate>) -> Result<(), Error> {
45
-    let mut radio = Radio::init(updates)?;
73
+    let mut radio = RadioImpl::init(updates)?;
46 74
     radio.run_loop()?;
47 75
     Ok(())
48 76
 }
49 77
 
50
-struct Radio<'a> {
78
+struct RadioImpl<'a> {
51 79
     rx: Option<RxMode<NRF24L01<GpioError, Pin, Pin, EmbeddedHalSpidev>>>,
52 80
     updates: &'a mpsc::Sender<SensorUpdate>,
53 81
     rng: ThreadRng,
54 82
 }
55 83
 
56
-impl<'a> Radio<'a> {
57
-    fn init(updates: &'a mpsc::Sender<SensorUpdate>) -> Result<Radio<'a>, Error> {
84
+impl<'a> RadioImpl<'a> {
85
+    fn init(updates: &'a mpsc::Sender<SensorUpdate>) -> Result<RadioImpl<'a>, Error> {
58 86
         info!("Initializing radio...");
59 87
 
60 88
         // The NRF module is connected as follows:
@@ -136,7 +164,7 @@ impl<'a> Radio<'a> {
136 164
 
137 165
         let rx = nrf24.rx().unwrap();
138 166
 
139
-        Ok(Radio {
167
+        Ok(RadioImpl {
140 168
             rx: Some(rx),
141 169
             updates,
142 170
             rng: rand::thread_rng(),
@@ -293,9 +321,9 @@ impl<'a> Radio<'a> {
293 321
         info!("packet sent: {:?}", packet);
294 322
         Ok(())
295 323
     }
296
-}
324
+}*/
297 325
 
298
-fn get_location(device_id: u8) -> Location {
326
+pub fn get_device_location(device_id: u8) -> Location {
299 327
     match device_id {
300 328
         DISPLAY_ID => Location::Bedroom,
301 329
         WEATHER_STATION_0_ID => Location::Livingroom,
@@ -303,7 +331,7 @@ fn get_location(device_id: u8) -> Location {
303 331
     }
304 332
 }
305 333
 
306
-fn get_key(device_id: u8) -> Option<&'static [u8]> {
334
+/*fn get_key(device_id: u8) -> Option<&'static [u8]> {
307 335
     match device_id {
308 336
         DISPLAY_ID => Some(&DISPLAY_KEY),
309 337
         WEATHER_STATION_0_ID => Some(&WEATHER_STATION_0_KEY),
@@ -313,4 +341,4 @@ fn get_key(device_id: u8) -> Option<&'static [u8]> {
313 341
 
314 342
 fn get_pin_number(c: char, n: u64) -> u64 {
315 343
     (c as u64 - 'A' as u64) * 32 + n
316
-}
344
+}*/

+ 18
- 0
base-station/software/src/tsdb.rs View File

@@ -0,0 +1,18 @@
1
+use std::time::Instant;
2
+
3
+use protocol::{Location, Value};
4
+
5
+pub struct TimeSeriesDatabase {
6
+    // TODO
7
+}
8
+
9
+impl TimeSeriesDatabase {
10
+    pub fn init() -> TimeSeriesDatabase {
11
+        // TODO
12
+        TimeSeriesDatabase {}
13
+    }
14
+
15
+    pub async fn insert(&mut self, _time: Instant, _location: Location, _values: &[Value]) {
16
+        // TODO
17
+    }
18
+}

Loading…
Cancel
Save