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:
36:14a8da4108d5
avec radio;

Who changed what in which revision?

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