@@ -105,6 +105,21 @@ mod app_network {
105105 sys:: console, encoding:: coap_context:: * ,
106106 libs:: { sensor_network} , coap, d, Strn } ;
107107 use mynewt_macros:: strn;
108+ /// Aggregate the sensor value with other sensor data before transmitting to server.
109+ /// If the sensor value is a GPS geolocation, we remember it and attach it to other sensor data for transmission.
110+ pub fn aggregate_sensor_data ( sensor_value : & SensorValue )
111+ -> MynewtResult < ( ) > {
112+ if let SensorValueType :: Geolocation { .. } = sensor_value. value {
113+ unsafe { CURRENT_GEOLOCATION = sensor_value. value } ;
114+ Ok ( ( ) )
115+ } else {
116+ let transmit_value =
117+ SensorValue { key : sensor_value. key ,
118+ value : sensor_value. value ,
119+ geo : unsafe { CURRENT_GEOLOCATION } , } ;
120+ send_sensor_data ( & transmit_value)
121+ }
122+ }
108123 /// Compose a CoAP JSON message with the Sensor Key (field name) and Value in `val`
109124 /// and send to the CoAP server. The message will be enqueued for transmission by the CoAP / OIC
110125 /// Background Task so this function will return without waiting for the message to be transmitted.
@@ -116,7 +131,7 @@ mod app_network {
116131 /// {"key":"t", "value":1715}
117132 /// ]}
118133 /// ```
119- pub fn send_sensor_data ( val : & SensorValue ) -> MynewtResult < ( ) > {
134+ fn send_sensor_data ( val : & SensorValue ) -> MynewtResult < ( ) > {
120135 console:: print ( "Rust send_sensor_data\n " ) ;
121136 let device_id = sensor_network:: get_device_id ( ) ?;
122137 let rc = sensor_network:: init_server_post ( & Strn :: new ( b"\0 " ) ) ?;
@@ -295,6 +310,8 @@ mod app_network {
295310 console:: print ( "\n " ) ;
296311 Ok ( ( ) )
297312 }
313+ /// Current geolocation recorded from GPS
314+ static mut CURRENT_GEOLOCATION : SensorValueType = SensorValueType :: None ;
298315}
299316mod app_sensor {
300317 //! Poll the temperature sensor every 10 seconds. Transmit the sensor data to the CoAP server after polling.
@@ -305,7 +322,7 @@ mod app_sensor {
305322 sensor_type_t, SensorValue , SensorValueType } ,
306323 sys:: console, fill_zero, Strn } ;
307324 use mynewt_macros:: { init_strn} ;
308- use crate :: app_network:: send_sensor_data ;
325+ use crate :: app_network;
309326 /// Sensor to be polled: `temp_stm32_0` is the internal temperature sensor
310327 static SENSOR_DEVICE : Strn =
311328 Strn { rep : mynewt:: StrnRep :: ByteStr ( b"temp_stm32_0\x00 " ) , } ;
@@ -376,7 +393,7 @@ mod app_sensor {
376393 }
377394 } ;
378395 }
379- let res = send_sensor_data ( & sensor_value) ;
396+ let res = app_network :: aggregate_sensor_data ( & sensor_value) ;
380397 if let Err ( err) = res {
381398 if err == MynewtError :: SYS_EAGAIN {
382399 console:: print ( "TMP network not ready\n " ) ;
@@ -502,6 +519,7 @@ mod gps_sensor {
502519 sensor_type_t, SensorValue , SensorValueType } ,
503520 sys:: console, fill_zero, Strn } ;
504521 use mynewt_macros:: { init_strn} ;
522+ use crate :: app_network;
505523 /// Sensor to be polled: `gps_l70r_0` is the Quectel L70-R GPS module
506524 static GPS_DEVICE : Strn =
507525 Strn { rep : mynewt:: StrnRep :: ByteStr ( b"gps_l70r_0\x00 " ) , } ;
@@ -570,7 +588,7 @@ mod gps_sensor {
570588 console:: print ( "\n " ) ;
571589 console:: flush ( ) ;
572590 }
573- aggregate_sensor_data ( sensor_value) ;
591+ app_network :: aggregate_sensor_data ( & sensor_value) ;
574592 MynewtError :: SYS_EOK
575593 }
576594 /// Convert the geolocation value received from Mynewt into a Geolocation `SensorValue` for transmission.
@@ -647,18 +665,6 @@ mod gps_sensor {
647665 }
648666 } , }
649667 }
650- static mut current_geolocation: SensorValueType = SensorValueType :: None ;
651- /// Aggregate the sensor value with other sensor data before transmitting to server.
652- fn aggregate_sensor_data ( sensor_value : SensorValue ) {
653- if let SensorValueType :: Geolocation { .. } = sensor_value. value {
654- unsafe { current_geolocation = sensor_value. value } ;
655- } else {
656- let transmit_value =
657- SensorValue { key : sensor_value. key ,
658- value : sensor_value. value ,
659- geo : unsafe { current_geolocation } , } ;
660- }
661- }
662668}
663669use core:: panic:: PanicInfo ;
664670use cortex_m:: asm:: bkpt;
0 commit comments