Version FC

Dependencies:   DmTftLibrary eeprom SX1280Lib filesystem mbed

Fork of MSNV2-Terminal_V1-5 by Francis CHATAIN

Revision:
21:8524d815c587
Parent:
20:b0281e8a375a
Child:
24:92c30dabfda4
--- a/Service.hpp	Sun Sep 02 22:24:14 2018 +0000
+++ b/Service.hpp	Tue Sep 04 13:34:36 2018 +0000
@@ -7,258 +7,270 @@
 #include "mbed.h"
 #endif
 
+#ifdef CPLUSPLUS_99
+#include <stdint.h>
+#endif
+
 #include <iostream>
 #include <sstream>
 #include <string>
 
+#include "Value.hpp"
+
 
 namespace misnet {
-    class Service;
+  class Service;
 }
 
 
 class misnet::Service {
 
-    public:
-        enum VALUE_TYPE {
-            NOT_SET         = 0 ,   // Not initialised
-            BOOLEAN         = 1 ,   // Who knows ?
-            CHAR            = 2 ,   // Single character
-            UINT8_T         = 3 ,
-            INT8_T          = 4 ,
-            UINT16_T        = 5 ,
-            INT16_T         = 6 ,
-            UINT32_T        = 7 ,
-            INT32_T         = 8 ,
-            FLOAT           = 9 ,   // Floating value on 32 bits
-            DOUBLE          = 10 ,  // Floating value on 64 bits
-            TIME            = 11    // Unix time
-        } ;
+public:
+
+  typedef uint8_t MISNET_CODE ;
+
+  enum DEVICE_TYPE {
+    SENSOR    = 1, 
+    ACTUATOR  = 2, 
+    RECORDER   = 3
+  } ;
+
+  // A compléter au fur et a mesure et remonter l'info sur les centres applicatifs
+  enum DEVICE_ID {
+    NOT_IDENTIFIED  = 0 ,   //  Configuration nouvelle ou non référencée
+    IKS01A2         = 1 ,   //  Liste des composants
+    SMART_TERMINAL  = 2     //  BME280 + .... 
+  } ;
+
+  typedef uint8_t  GROUP ;
+
+  //typedef uint8_t VALUE_TYPE ;
+
+  enum STATE {
+    ENABLED   = 1,  //   ACTIVE
+    DISABLED  = 0   //   ASLEEP
+  } ;
 
-        typedef struct {
-            union {
-                bool                    bool_value;
-                char                    char_value;
-                uint8_t                 uint8_value;
-                int8_t                  int8_value;
-                uint16_t                uint16_value;
-                int16_t                 int16_value;
-                uint32_t                uint32_value;
-                int32_t                 int32_value;
-                float                   float_value;
-                double                  double_value;
-                uint32_t                time_value;
-            } value;
-            VALUE_TYPE type;
-        } GENERIC_VALUE;
+  enum ACCESS_TYPE {
+    GPIO_    = 1,
+    I2C_     = 2,
+    SPI_     = 3,
+    UART_    = 4
+  } ;
+ 
+  typedef uint8_t ACCESS_PIN ;
 
-        typedef uint8_t MISNET_CODE ;
-
-        enum DEVICE_TYPE {
-            SENSOR    = 1, 
-            ACTUATOR  = 2, 
-            RECORDER   = 3
-        } ;
+  enum UP_MODE {
+    BY_CHANGE = 0,  // Check that current value differs from previous value
+    BY_DELTA  = 1,  // Check a delta between current value and previous value
+    BY_RANGE  = 2   // Check whether current value lies within a range
+  } ;
 
-        // A compléter au fur et a mesure et remonter l'info sur les centres applicatifs
-        enum DEVICE_ID {
-            NOT_IDENTIFIED  = 0 ,   //  Configuration nouvelle ou non référencée
-            IKS01A2         = 1 ,   //  Liste des composants
-            SMART_TERMINAL  = 2     //  BME280 + .... 
-        } ;
+  enum REQUEST_MODE {
+    IRQ_   = 1, // wakeup by irq
+    TIME_  = 2  // wakeup by watchdog timer
+  } ; 
 
-        typedef uint8_t  GROUP ;
+  //uint32_t TIMER_DIVIDER ;
 
-        //typedef uint8_t VALUE_TYPE ;
+  /*
+  typedef float DELTA_THRESHOLD ;
+  typedef float LOW_THRESHOLD_LIMIT ;
+  typedef float HIGH_THRESHOLD_LIMIT ;
+  */
 
-        enum STATE {
-            ENABLED   = 1,  //   ACTIVE
-            DISABLED  = 0   //   ASLEEP
-        } ;
+  enum ACTION {
+    MESSAGE         = 1, // Send Message
+    MESSAGERELAY    = 2  // Send Message + ON/OFF internal relay
+  } ;
+
+  enum OUTPUT_MODE {
+    IO        = 1, // ON/OFF 
+    PWD       = 2  // PWD modulation 
+  } ;
 
-        enum ACCESS_TYPE {
-            GPIO_    = 1,
-            I2C_     = 2,
-            SPI_     = 3,
-            UART_    = 4
-        } ;
- 
-        typedef uint8_t ACCESS_PIN ;
+  // Constructor
+  Service(            DEVICE_TYPE type,
+              MISNET_CODE misnet_code,
+              STATE state,
+              ACCESS_TYPE access_type,
+              REQUEST_MODE request_mode,
+              UP_MODE up_mode,
+              ACCESS_PIN access_pins[6],
+              uint32_t subsample_rate,
+              ACTION action,
+              OUTPUT_MODE output_mode,
+              std::string comment);
 
-        enum UP_MODE {
-            BY_VALUE      = 1, // wakeup by irq
-            BY_THRESHOLD  = 2  // wakeup by watchdog timer
-        } ;
-
-        enum REQUEST_MODE {
-            IRQ_   = 1, // wakeup by irq
-            TIME_  = 2  // wakeup by watchdog timer
-        } ; 
-
-        uint32_t TIMER_DIVIDER ;
+  DEVICE_TYPE getDeviceType() {
+    return this->device_type;
+  }
 
-        typedef float THRESHOLD_DELTA ;
-        typedef float THRESHOLD_UP ;
-        typedef float THRESHOLD_DOWN ;
+  MISNET_CODE getMisnetCode() {
+    return this->misnet_code;
+  }
 
-        enum ACTION {
-            MESSAGE         = 1, // Send Message
-            MESSAGERELAY    = 2  // Send Message + ON/OFF internal relay
-        } ;
+  STATE getState() {
+    return this->state;
+  }
 
-        enum OUTPUT_MODE {
-            IO        = 1, // ON/OFF 
-            PWD       = 2  // PWD modulation 
-        } ;
+  ACCESS_TYPE getAccessType() {
+    return this->access_type;
+  }
 
-        // Constructor
-        Service(            DEVICE_TYPE type,
-                            MISNET_CODE misnet_code,
-                            STATE state,
-                            ACCESS_TYPE access_type,
-                            REQUEST_MODE request_mode,
-                            UP_MODE up_mode,
-                            ACCESS_PIN access_pins[6],
-                            uint32_t subsample_rate,
-                            ACTION action,
-                            OUTPUT_MODE output_mode,
-                            std::string comment);
+  REQUEST_MODE getRequestMode() {
+    return this->request_mode;
+  }
+
+  UP_MODE getUpMode() {
+    return this->up_mode;
+  }
 
-        DEVICE_TYPE getDeviceType() {
-            return this->device_type;
-        }
+  ACCESS_PIN* getAccessPins() {
+    return this->access_pins;
+  }
+
+  ACCESS_PIN getAccessPin(short index) {
+    return this->access_pins[index - 1];
+  }
 
-        MISNET_CODE getMisnetCode() {
-            return this->misnet_code;
-        }
+  uint32_t getSubsampleRate() {
+    return this->subsample_rate;
+  }
 
-        STATE getState() {
-            return this->state;
-        }
-
-        ACCESS_TYPE getAccessType() {
-            return this->access_type;
-        }
+  ACTION getAction() {
+    return this->action;
+  }
 
-        REQUEST_MODE getRequestMode() {
-            return this->request_mode;
-        }
+  OUTPUT_MODE getOutputMode() {
+    return this->output_mode;
+  }
 
-        UP_MODE getUpMode() {
-            return this->up_mode;
-        }
+  std::string getComment() {
+    return this->comment;
+  }
 
-        ACCESS_PIN* getAccessPins() {
-            return this->access_pins;
-        }
+  uint32_t getActivationNb() {
+    return this->activation_nb;
+  }
 
-        ACCESS_PIN getAccessPin(short index) {
-            return this->access_pins[index - 1];
-        }
+  // This method is used to check whether the service is ready to be sampled
+  // (ENABLED state and subsampling rate OK)
+  bool readyToSample() const {
+    return ((this->activation_nb + 1) == this->subsample_rate);
+  }
 
-        uint32_t getSubsampleRate() {
-            return this->subsample_rate;
-        }
-
-        ACTION getAction() {
-            return this->action;
-        }
+  // This method is used to process a heartbeat : it checks whether the service
+  // is ready to be sampled, and updates accordingly the service activation number
+  // (reset to 0 if the service is ready ti be sampled, otherwise increments it)
+  bool processHeartbeat();
 
-        OUTPUT_MODE getOutputMode() {
-            return this->output_mode;
-        }
+  void incrementActivationNb() {
+    ++(this->activation_nb);
+  }
 
-        std::string getComment() {
-            return this->comment;
-        }
+  void resetActivationNb() {
+    this->activation_nb = 0;
+  }
 
-        uint32_t getActivationNb() {
-            return this->activation_nb;
-        }
-
-        // This method is used to check whether the service is ready to be sampled
-        // (ENABLED state and subsampling rate OK)
-        bool readyToSample() const {
-            return ((this->activation_nb + 1) == this->subsample_rate);
-        }
+  // This method writes the value passed as argument in the value field of the service.
+  // The caller must write the value in "value" field of the srtucture AND set the
+  // value of the "type" field of this structure.
+  void setValue(Value& value) {
+    this->previous_value = this->current_value;
+    this->current_value = value;
+  }
 
-        // This method is used to process a heartbeat : it checks whether the service
-        // is ready to be sampled, and updates accordingly the service activation number
-        // (reset to 0 if the service is ready ti be sampled, otherwise increments it)
-        bool processHeartbeat();
+  // This other method returns the address of the area where sensor values must be written.
+  // The caller must write the value in "value" field of the structure AND set the
+  // value of the "type" field of this structure.
+  // This method is quicker than the previous one since there is no neeed to copy the value
+  // as in the previous method.
+  // Drawback : the next method (savePreviousValue) must be called before this one,
+  // so that the previous value may be saved. This is necessary when the UP_MODE is BY_RANGE
+  // and the delta_threshold field is not empty.
+  Value & getValueAddress() {
+    return this->current_value;
+  }
 
-        void incrementActivationNb() {
-            ++(this->activation_nb);
-        }
+  void savePreviousValue() {
+    this->previous_value = this->current_value;
+  }
 
-        void resetActivationNb() {
-            this->activation_nb = 0;
-        }
+  // Check whether current value must be sent to gateway
+  bool valueToBeSentToGateway();
 
-        // This method writes the value passed as argument in the value field of the service.
-        // The caller must write the value in "value" field of the srtucture AND set the
-        // value of the "type" field of this structure.
-        void setValue(GENERIC_VALUE& value) {
-            this->previous_value = this->current_value;
-            this->current_value = value;
-        }
+  
+  std::string toString() {
+    std::ostringstream stringStream;
+    stringStream << "Device type : " << this->device_type << ", MISNet code : " << (int) this->misnet_code;
+    return stringStream.str();
+  }
+
+  // Returns a string representing a value. Used to build messages and to debug.
+  // std::string getValueAsString(Value& value);
+
+  // Returns a string representing the current value.
+  std::string getCurrentValueAsString() {
+    return this->current_value.getValueAsString();
+  }
 
-        // This other method returns the address of the area where sensor values must be written.
-        // The caller must write the value in "value" field of the structure AND set the
-        // value of the "type" field of this structure.
-        // This method is quicker than the previous one since there is no neeed to copy the value
-        // as in the previous method.
-        // Drawback : the next method (savePreviousValue) must be called before this one,
-        // so that the previous value may be saved. This is necessary when the UP_MODE is BY_THRESHOLD
-        // and the threshold_delta field is not empty.
-        GENERIC_VALUE & getValueAddress() {
-            return this->current_value;
-        }
+  // Returns a string representing the previous value.
+  std::string getPreviousValueAsString() {
+    return this->previous_value.getValueAsString();
+  }
 
-        void savePreviousValue() {
-            this->previous_value = this->current_value;
-        }
+  // Set the delta threshold
+  void setDeltaThreshold(Value delta) {
+    this->delta_threshold = delta;
+  }
+
+  // Get the delta threshold
+  Value getDeltaThreshold() {
+    return this->delta_threshold;
+  }
 
-        std::string toString() {
-            std::ostringstream stringStream;
-            stringStream << "Device type : " << this->device_type << ", MISNet code : " << (int) this->misnet_code;
-            return stringStream.str();
-        }
+  // Set the low threshold limit
+  void setLowThresholdLimit(Value low_limit) {
+    this->low_threshold_limit = low_limit;
+  }
 
-        // Returns a string representing a value. Used to build messages and to debug.
-        std::string getValueAsString(GENERIC_VALUE& value);
+  // Get the delta threshold
+  Value getLowThresholdLimit() {
+    return this->low_threshold_limit;
+  }
 
-        // Returns a string representing the current value.
-        std::string getCurrentValueAsString() {
-            return this->getValueAsString(this->current_value);
-        }
+  // Set the high threshold limit
+  void setHighThresholdLimit(Value high_limit) {
+    this->high_threshold_limit = high_limit;
+  }
 
-        // Returns a string representing the previous value.
-        std::string getPreviousValueAsString() {
-            return this->getValueAsString(this->previous_value);
-        }
+  // Get the delta threshold
+  Value getHighThresholdLimit() {
+    return this->high_threshold_limit;
+  }
 
 
-    private:
-        DEVICE_TYPE         device_type;
-        MISNET_CODE         misnet_code;
-        STATE               state;
-        ACCESS_TYPE         access_type;
-        ACCESS_PIN          access_pins[6];
-        REQUEST_MODE        request_mode;
-        UP_MODE             up_mode;
-        uint32_t            subsample_rate;
-        uint32_t            threshold_delta;
-        uint32_t            threshold_up;
-        uint32_t            threshold_down;
-        ACTION              action;
-        OUTPUT_MODE         output_mode;
-        std::string         comment;
+private:
+  DEVICE_TYPE         device_type;
+  MISNET_CODE         misnet_code;
+  STATE               state;
+  ACCESS_TYPE         access_type;
+  ACCESS_PIN          access_pins[6];
+  REQUEST_MODE        request_mode;
+  UP_MODE             up_mode;
+  uint32_t            subsample_rate;
+  Value           delta_threshold;
+  Value           low_threshold_limit;
+  Value           high_threshold_limit;
+  ACTION              action;
+  OUTPUT_MODE         output_mode;
+  std::string         comment;
 
-        uint32_t            activation_nb;
+  uint32_t      activation_nb;
 
-        GENERIC_VALUE       current_value;
-        GENERIC_VALUE       previous_value;
+  Value         current_value;
+  Value         previous_value;
 };
 
 #endif // __SERVICE_HPP__