Version FC

Dependencies:   DmTftLibrary eeprom SX1280Lib filesystem mbed

Fork of MSNV2-Terminal_V1-5 by Francis CHATAIN

Committer:
FCH_31
Date:
Mon Oct 22 09:37:50 2018 +0000
Revision:
41:5a436163dddf
Parent:
27:a1c37c596bfc
avec radio;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lo 27:a1c37c596bfc 1 #ifndef __SERVICE_HPP__
lo 27:a1c37c596bfc 2 #define __SERVICE_HPP__
lo 27:a1c37c596bfc 3
lo 27:a1c37c596bfc 4 #include "Context.h"
lo 27:a1c37c596bfc 5
lo 27:a1c37c596bfc 6 #ifndef TEST_ENVIRONMENT
lo 27:a1c37c596bfc 7 #include "mbed.h"
lo 27:a1c37c596bfc 8 #endif
lo 27:a1c37c596bfc 9
lo 27:a1c37c596bfc 10 #ifdef CPLUSPLUS_99
lo 27:a1c37c596bfc 11 #include <stdint.h>
lo 27:a1c37c596bfc 12 #endif
lo 27:a1c37c596bfc 13
lo 27:a1c37c596bfc 14 #include <iostream>
lo 27:a1c37c596bfc 15 #include <sstream>
lo 27:a1c37c596bfc 16 #include <string>
lo 27:a1c37c596bfc 17
lo 27:a1c37c596bfc 18 #include "Value.hpp"
lo 27:a1c37c596bfc 19
lo 27:a1c37c596bfc 20
lo 27:a1c37c596bfc 21 namespace misnet {
lo 27:a1c37c596bfc 22 class Service;
lo 27:a1c37c596bfc 23 }
lo 27:a1c37c596bfc 24
lo 27:a1c37c596bfc 25
lo 27:a1c37c596bfc 26 class misnet::Service {
lo 27:a1c37c596bfc 27
lo 27:a1c37c596bfc 28 public:
lo 27:a1c37c596bfc 29
lo 27:a1c37c596bfc 30 typedef uint8_t MISNET_CODE ;
lo 27:a1c37c596bfc 31
lo 27:a1c37c596bfc 32 enum DEVICE_TYPE {
lo 27:a1c37c596bfc 33 SENSOR = 1,
lo 27:a1c37c596bfc 34 ACTUATOR = 2,
lo 27:a1c37c596bfc 35 RECORDER = 3
lo 27:a1c37c596bfc 36 } ;
lo 27:a1c37c596bfc 37
lo 27:a1c37c596bfc 38 // A compléter au fur et a mesure et remonter l'info sur les centres applicatifs
lo 27:a1c37c596bfc 39 enum DEVICE_ID {
lo 27:a1c37c596bfc 40 NOT_IDENTIFIED = 0 , // Configuration nouvelle ou non référencée
lo 27:a1c37c596bfc 41 IKS01A2 = 1 , // Liste des composants
lo 27:a1c37c596bfc 42 SMART_TERMINAL = 2 // BME280 + ....
lo 27:a1c37c596bfc 43 } ;
lo 27:a1c37c596bfc 44
lo 27:a1c37c596bfc 45 typedef uint8_t GROUP ;
lo 27:a1c37c596bfc 46
lo 27:a1c37c596bfc 47 //typedef uint8_t VALUE_TYPE ;
lo 27:a1c37c596bfc 48
lo 27:a1c37c596bfc 49 enum STATE {
lo 27:a1c37c596bfc 50 ENABLED = 1, // ACTIVE
lo 27:a1c37c596bfc 51 DISABLED = 0 // ASLEEP
lo 27:a1c37c596bfc 52 } ;
lo 27:a1c37c596bfc 53
lo 27:a1c37c596bfc 54 enum ACCESS_TYPE {
lo 27:a1c37c596bfc 55 GPIO_ = 1,
lo 27:a1c37c596bfc 56 I2C_ = 2,
lo 27:a1c37c596bfc 57 SPI_ = 3,
lo 27:a1c37c596bfc 58 UART_ = 4
lo 27:a1c37c596bfc 59 } ;
lo 27:a1c37c596bfc 60
lo 27:a1c37c596bfc 61 typedef uint8_t ACCESS_PIN ;
lo 27:a1c37c596bfc 62
lo 27:a1c37c596bfc 63 enum UP_MODE {
lo 27:a1c37c596bfc 64 BY_CHANGE = 0, // Check that current value differs from previous value
lo 27:a1c37c596bfc 65 BY_DELTA = 1, // Check a delta between current value and previous value
lo 27:a1c37c596bfc 66 BY_RANGE = 2 // Check whether current value lies within a range
lo 27:a1c37c596bfc 67 } ;
lo 27:a1c37c596bfc 68
lo 27:a1c37c596bfc 69 enum REQUEST_MODE {
lo 27:a1c37c596bfc 70 IRQ_ = 1, // wakeup by irq
lo 27:a1c37c596bfc 71 TIME_ = 2 // wakeup by watchdog timer
lo 27:a1c37c596bfc 72 } ;
lo 27:a1c37c596bfc 73
lo 27:a1c37c596bfc 74 //uint32_t TIMER_DIVIDER ;
lo 27:a1c37c596bfc 75
lo 27:a1c37c596bfc 76 /*
lo 27:a1c37c596bfc 77 typedef float DELTA_THRESHOLD ;
lo 27:a1c37c596bfc 78 typedef float LOW_THRESHOLD_LIMIT ;
lo 27:a1c37c596bfc 79 typedef float HIGH_THRESHOLD_LIMIT ;
lo 27:a1c37c596bfc 80 */
lo 27:a1c37c596bfc 81
lo 27:a1c37c596bfc 82 enum ACTION {
lo 27:a1c37c596bfc 83 MESSAGE = 1, // Send Message
lo 27:a1c37c596bfc 84 MESSAGERELAY = 2 // Send Message + ON/OFF internal relay
lo 27:a1c37c596bfc 85 } ;
lo 27:a1c37c596bfc 86
lo 27:a1c37c596bfc 87 enum OUTPUT_MODE {
lo 27:a1c37c596bfc 88 IO = 1, // ON/OFF
lo 27:a1c37c596bfc 89 PWD = 2 // PWD modulation
lo 27:a1c37c596bfc 90 } ;
lo 27:a1c37c596bfc 91
lo 27:a1c37c596bfc 92 // Constructor
lo 27:a1c37c596bfc 93 Service( DEVICE_TYPE type,
lo 27:a1c37c596bfc 94 MISNET_CODE misnet_code,
lo 27:a1c37c596bfc 95 STATE state,
lo 27:a1c37c596bfc 96 ACCESS_TYPE access_type,
lo 27:a1c37c596bfc 97 REQUEST_MODE request_mode,
lo 27:a1c37c596bfc 98 UP_MODE up_mode,
lo 27:a1c37c596bfc 99 ACCESS_PIN access_pins[6],
lo 27:a1c37c596bfc 100 uint32_t subsample_rate,
lo 27:a1c37c596bfc 101 Value& delta_threshold,
lo 27:a1c37c596bfc 102 Value& low_threshold_limit,
lo 27:a1c37c596bfc 103 Value& high_threshold_limit,
lo 27:a1c37c596bfc 104 ACTION action,
lo 27:a1c37c596bfc 105 OUTPUT_MODE output_mode,
lo 27:a1c37c596bfc 106 std::string comment);
lo 27:a1c37c596bfc 107
lo 27:a1c37c596bfc 108 DEVICE_TYPE getDeviceType() {
lo 27:a1c37c596bfc 109 return this->device_type;
lo 27:a1c37c596bfc 110 }
lo 27:a1c37c596bfc 111
lo 27:a1c37c596bfc 112 MISNET_CODE getMisnetCode() {
lo 27:a1c37c596bfc 113 return this->misnet_code;
lo 27:a1c37c596bfc 114 }
lo 27:a1c37c596bfc 115
lo 27:a1c37c596bfc 116 STATE getState() {
lo 27:a1c37c596bfc 117 return this->state;
lo 27:a1c37c596bfc 118 }
lo 27:a1c37c596bfc 119
lo 27:a1c37c596bfc 120 ACCESS_TYPE getAccessType() {
lo 27:a1c37c596bfc 121 return this->access_type;
lo 27:a1c37c596bfc 122 }
lo 27:a1c37c596bfc 123
lo 27:a1c37c596bfc 124 REQUEST_MODE getRequestMode() {
lo 27:a1c37c596bfc 125 return this->request_mode;
lo 27:a1c37c596bfc 126 }
lo 27:a1c37c596bfc 127
lo 27:a1c37c596bfc 128 UP_MODE getUpMode() {
lo 27:a1c37c596bfc 129 return this->up_mode;
lo 27:a1c37c596bfc 130 }
lo 27:a1c37c596bfc 131
lo 27:a1c37c596bfc 132 ACCESS_PIN* getAccessPins() {
lo 27:a1c37c596bfc 133 return this->access_pins;
lo 27:a1c37c596bfc 134 }
lo 27:a1c37c596bfc 135
lo 27:a1c37c596bfc 136 ACCESS_PIN getAccessPin(short index) {
lo 27:a1c37c596bfc 137 return this->access_pins[index - 1];
lo 27:a1c37c596bfc 138 }
lo 27:a1c37c596bfc 139
lo 27:a1c37c596bfc 140 uint32_t getSubsampleRate() {
lo 27:a1c37c596bfc 141 return this->subsample_rate;
lo 27:a1c37c596bfc 142 }
lo 27:a1c37c596bfc 143
lo 27:a1c37c596bfc 144 ACTION getAction() {
lo 27:a1c37c596bfc 145 return this->action;
lo 27:a1c37c596bfc 146 }
lo 27:a1c37c596bfc 147
lo 27:a1c37c596bfc 148 OUTPUT_MODE getOutputMode() {
lo 27:a1c37c596bfc 149 return this->output_mode;
lo 27:a1c37c596bfc 150 }
lo 27:a1c37c596bfc 151
lo 27:a1c37c596bfc 152 std::string getComment() {
lo 27:a1c37c596bfc 153 return this->comment;
lo 27:a1c37c596bfc 154 }
lo 27:a1c37c596bfc 155
lo 27:a1c37c596bfc 156 uint32_t getActivationNb() {
lo 27:a1c37c596bfc 157 return this->activation_nb;
lo 27:a1c37c596bfc 158 }
lo 27:a1c37c596bfc 159
lo 27:a1c37c596bfc 160 // This method is used to check whether the service is ready to be sampled
lo 27:a1c37c596bfc 161 // (ENABLED state and subsampling rate OK)
lo 27:a1c37c596bfc 162 bool readyToSample() const {
lo 27:a1c37c596bfc 163 return ((this->activation_nb + 1) == this->subsample_rate);
lo 27:a1c37c596bfc 164 }
lo 27:a1c37c596bfc 165
lo 27:a1c37c596bfc 166 // This method is used to process a heartbeat : it checks whether the service
lo 27:a1c37c596bfc 167 // is ready to be sampled, and updates accordingly the service activation number
lo 27:a1c37c596bfc 168 // (reset to 0 if the service is ready ti be sampled, otherwise increments it)
lo 27:a1c37c596bfc 169 bool processHeartbeat();
lo 27:a1c37c596bfc 170
lo 27:a1c37c596bfc 171 void incrementActivationNb() {
lo 27:a1c37c596bfc 172 ++(this->activation_nb);
lo 27:a1c37c596bfc 173 }
lo 27:a1c37c596bfc 174
lo 27:a1c37c596bfc 175 void resetActivationNb() {
lo 27:a1c37c596bfc 176 this->activation_nb = 0;
lo 27:a1c37c596bfc 177 }
lo 27:a1c37c596bfc 178
lo 27:a1c37c596bfc 179 // This method writes the value passed as argument in the value field of the service.
lo 27:a1c37c596bfc 180 // The caller must write the value in "value" field of the srtucture AND set the
lo 27:a1c37c596bfc 181 // value of the "type" field of this structure.
lo 27:a1c37c596bfc 182 void setValue(Value& value) {
lo 27:a1c37c596bfc 183 this->previous_value = this->current_value;
lo 27:a1c37c596bfc 184 this->current_value = value;
lo 27:a1c37c596bfc 185 }
lo 27:a1c37c596bfc 186
lo 27:a1c37c596bfc 187 // This other method returns the address of the area where sensor values must be written.
lo 27:a1c37c596bfc 188 // The caller must write the value in "value" field of the structure AND set the
lo 27:a1c37c596bfc 189 // value of the "type" field of this structure.
lo 27:a1c37c596bfc 190 // This method is quicker than the previous one since there is no neeed to copy the value
lo 27:a1c37c596bfc 191 // as in the previous method.
lo 27:a1c37c596bfc 192 // Drawback : the next method (savePreviousValue) must be called before this one,
lo 27:a1c37c596bfc 193 // so that the previous value may be saved. This is necessary when the UP_MODE is BY_RANGE
lo 27:a1c37c596bfc 194 // and the delta_threshold field is not empty.
lo 27:a1c37c596bfc 195 Value & getValueAddress() {
lo 27:a1c37c596bfc 196 return this->current_value;
lo 27:a1c37c596bfc 197 }
lo 27:a1c37c596bfc 198
lo 27:a1c37c596bfc 199 void savePreviousValue() {
lo 27:a1c37c596bfc 200 this->previous_value = this->current_value;
lo 27:a1c37c596bfc 201 }
lo 27:a1c37c596bfc 202
lo 27:a1c37c596bfc 203 // Check whether current value must be sent to gateway
lo 27:a1c37c596bfc 204 bool valueToBeSentToGateway();
lo 27:a1c37c596bfc 205
lo 27:a1c37c596bfc 206
lo 27:a1c37c596bfc 207 std::string toString() {
lo 27:a1c37c596bfc 208 std::ostringstream stringStream;
lo 27:a1c37c596bfc 209 stringStream << "Device type : " << this->device_type << ", MISNet code : " << (int) this->misnet_code;
lo 27:a1c37c596bfc 210 return stringStream.str();
lo 27:a1c37c596bfc 211 }
lo 27:a1c37c596bfc 212
lo 27:a1c37c596bfc 213 // Returns a string representing a value. Used to build messages and to debug.
lo 27:a1c37c596bfc 214 // std::string toString(Value& value);
lo 27:a1c37c596bfc 215
lo 27:a1c37c596bfc 216 // Returns a string representing the current value.
lo 27:a1c37c596bfc 217 std::string getCurrentValueAsString() {
lo 27:a1c37c596bfc 218 return this->current_value.toString();
lo 27:a1c37c596bfc 219 }
lo 27:a1c37c596bfc 220
lo 27:a1c37c596bfc 221 // Returns a string representing the previous value.
lo 27:a1c37c596bfc 222 std::string getPreviousValueAsString() {
lo 27:a1c37c596bfc 223 return this->previous_value.toString();
lo 27:a1c37c596bfc 224 }
lo 27:a1c37c596bfc 225
lo 27:a1c37c596bfc 226 // Set the delta threshold
lo 27:a1c37c596bfc 227 void setDeltaThreshold(Value delta) {
lo 27:a1c37c596bfc 228 this->delta_threshold = delta;
lo 27:a1c37c596bfc 229 }
lo 27:a1c37c596bfc 230
lo 27:a1c37c596bfc 231 // Get the delta threshold
lo 27:a1c37c596bfc 232 Value getDeltaThreshold() {
lo 27:a1c37c596bfc 233 return this->delta_threshold;
lo 27:a1c37c596bfc 234 }
lo 27:a1c37c596bfc 235
lo 27:a1c37c596bfc 236 // Set the low threshold limit
lo 27:a1c37c596bfc 237 void setLowThresholdLimit(Value low_limit) {
lo 27:a1c37c596bfc 238 this->low_threshold_limit = low_limit;
lo 27:a1c37c596bfc 239 }
lo 27:a1c37c596bfc 240
lo 27:a1c37c596bfc 241 // Get the delta threshold
lo 27:a1c37c596bfc 242 Value getLowThresholdLimit() {
lo 27:a1c37c596bfc 243 return this->low_threshold_limit;
lo 27:a1c37c596bfc 244 }
lo 27:a1c37c596bfc 245
lo 27:a1c37c596bfc 246 // Set the high threshold limit
lo 27:a1c37c596bfc 247 void setHighThresholdLimit(Value high_limit) {
lo 27:a1c37c596bfc 248 this->high_threshold_limit = high_limit;
lo 27:a1c37c596bfc 249 }
lo 27:a1c37c596bfc 250
lo 27:a1c37c596bfc 251 // Get the delta threshold
lo 27:a1c37c596bfc 252 Value getHighThresholdLimit() {
lo 27:a1c37c596bfc 253 return this->high_threshold_limit;
lo 27:a1c37c596bfc 254 }
lo 27:a1c37c596bfc 255
lo 27:a1c37c596bfc 256
lo 27:a1c37c596bfc 257 private:
lo 27:a1c37c596bfc 258 DEVICE_TYPE device_type;
lo 27:a1c37c596bfc 259 MISNET_CODE misnet_code;
lo 27:a1c37c596bfc 260 STATE state;
lo 27:a1c37c596bfc 261 ACCESS_TYPE access_type;
lo 27:a1c37c596bfc 262 ACCESS_PIN access_pins[6];
lo 27:a1c37c596bfc 263 REQUEST_MODE request_mode;
lo 27:a1c37c596bfc 264 UP_MODE up_mode;
lo 27:a1c37c596bfc 265 uint32_t subsample_rate;
lo 27:a1c37c596bfc 266 Value delta_threshold;
lo 27:a1c37c596bfc 267 Value low_threshold_limit;
lo 27:a1c37c596bfc 268 Value high_threshold_limit;
lo 27:a1c37c596bfc 269 ACTION action;
lo 27:a1c37c596bfc 270 OUTPUT_MODE output_mode;
lo 27:a1c37c596bfc 271 std::string comment;
lo 27:a1c37c596bfc 272
lo 27:a1c37c596bfc 273 uint32_t activation_nb;
lo 27:a1c37c596bfc 274
lo 27:a1c37c596bfc 275 Value current_value;
lo 27:a1c37c596bfc 276 Value previous_value;
lo 27:a1c37c596bfc 277 };
lo 27:a1c37c596bfc 278
lo 27:a1c37c596bfc 279 #endif // __SERVICE_HPP__