Version FC

Dependencies:   DmTftLibrary eeprom SX1280Lib filesystem mbed

Fork of MSNV2-Terminal_V1-5 by Francis CHATAIN

Committer:
lo
Date:
Tue Sep 18 19:54:51 2018 +0000
Revision:
27:a1c37c596bfc
Parent:
24:92c30dabfda4
Child:
28:3bf2fe501aa2
disabled constructorr and added methods in Service

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