Skip to content

Commit f0db956

Browse files
committed
Adding geolocation to sensor values
1 parent bc5cf30 commit f0db956

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

rust/app/src/app_sensor.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ fn convert_sensor_data(sensor_data: sensor_data_ptr, sensor_type: sensor_type_t)
120120
console::print("TMP listener got rawtmp\n");
121121
// Construct and return a new `SensorValue` (without semicolon)
122122
SensorValue {
123-
key: &TEMP_SENSOR_KEY, // Sensor data key is `t`
123+
key: &TEMP_SENSOR_KEY, // Sensor data key is `t`
124+
loc: SensorValueType::None, // No location
124125
val: match sensor_type {
125126
SENSOR_TYPE_AMBIENT_TEMPERATURE_RAW => { // If this is raw temperature...
126127
// Interpret the sensor data as a `sensor_temp_raw_data` struct that contains raw temp.
@@ -133,7 +134,7 @@ fn convert_sensor_data(sensor_data: sensor_data_ptr, sensor_type: sensor_type_t)
133134
SensorValueType::Uint(rawtempdata.strd_temp_raw) // Raw Temperature in integer (0 to 4095)
134135
}
135136
// Unknown type of sensor value
136-
// _ => { assert!(false, "sensor type"); SensorValueType::Uint(0) }
137+
// _ => { assert!(false, "sensor type"); SensorValueType::None }
137138
}
138139
}
139140
}

rust/app/src/gps_sensor.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ fn convert_gps_data(sensor_data: sensor_data_ptr, sensor_type: sensor_type_t) ->
109109
// Construct and return a new `SensorValue` (without semicolon)
110110
SensorValue {
111111
key: &GPS_SENSOR_KEY, // Sensor data key is `geolocation`
112+
loc: SensorValueType::None,
112113
val: match sensor_type {
113114
SENSOR_TYPE_GEOLOCATION => { // If sensor data is GPS geolocation...
114115
// Interpret the sensor data as a `sensor_geolocation_data` struct that contains GPS geolocation.
@@ -136,6 +137,16 @@ fn convert_gps_data(sensor_data: sensor_data_ptr, sensor_type: sensor_type_t) ->
136137
}
137138
}
138139

140+
static mut current_geolocation: SensorValueType = SensorValueType::None;
141+
139142
/// Aggregate the sensor value with other sensor data before transmitting to server.
140-
fn aggregate_sensor_data(_sensor_value: &SensorValue) {
143+
fn aggregate_sensor_data(sensor_value: SensorValue) {
144+
if let SensorValueType::Geolocation {..} = sensor_value.val {
145+
// Save the geolocation for later transmission.
146+
unsafe { current_geolocation = sensor_value.val };
147+
} else {
148+
// Attach the current geolocation to the sensor data for transmission.
149+
sensor_value.loc = current_geolocation;
150+
// TODO: Transmit sensor value with geolocation.
151+
}
141152
}

rust/mynewt/src/hw/sensor.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ fn convert_sensor_data(sensor_data: sensor_data_ptr, sensor_key: &'static Strn,
157157
// Construct and return a new `SensorValue` (without semicolon)
158158
SensorValue {
159159
key: sensor_key,
160+
loc: SensorValueType::None,
160161
val: match sensor_type {
161162
SENSOR_TYPE_AMBIENT_TEMPERATURE_RAW => { // If this is raw temperature...
162163
// Interpret the sensor data as a `sensor_temp_raw_data` struct that contains raw temp.
@@ -169,7 +170,7 @@ fn convert_sensor_data(sensor_data: sensor_data_ptr, sensor_key: &'static Strn,
169170
SensorValueType::Uint(rawtempdata.strd_temp_raw) // Raw Temperature in integer (0 to 4095)
170171
}
171172
// Unknown type of sensor value
172-
_ => { assert!(false, "sensor type"); SensorValueType::Uint(0) }
173+
_ => { assert!(false, "sensor type"); SensorValueType::None }
173174
}
174175
}
175176
}
@@ -240,11 +241,14 @@ pub const SENSOR_TYPE_GEOLOCATION: sensor_type_t =
240241

241242
/// Represents a decoded sensor data value. Since temperature may be integer (raw)
242243
/// or float (computed), we use the struct to return both integer and float values.
244+
#[derive(Clone, Copy)] // Sensor values may be copied
243245
pub struct SensorValue {
244246
/// Null-terminated string for the key. `t` for raw temp, `tmp` for computed. When transmitted to CoAP Server or Collector Node, the key (field name) to be used.
245247
pub key: &'static Strn,
246248
/// The type of the sensor value and the value.
247249
pub val: SensorValueType,
250+
/// Geolocation associated with the sensor value.
251+
pub loc: SensorValueType,
248252
}
249253

250254
/// Default sensor value is `None`
@@ -254,11 +258,13 @@ impl Default for SensorValue {
254258
SensorValue {
255259
key: &init_strn!(""),
256260
val: SensorValueType::None,
261+
loc: SensorValueType::None,
257262
}
258263
}
259264
}
260265

261266
/// Represents the type and value of a sensor data value.
267+
#[derive(Clone, Copy)] // Sensor values may be copied
262268
pub enum SensorValueType {
263269
/// No value.
264270
None,

0 commit comments

Comments
 (0)