Version FC

Dependencies:   DmTftLibrary eeprom SX1280Lib filesystem mbed

Fork of MSNV2-Terminal_V1-5 by Francis CHATAIN

Revision:
20:b0281e8a375a
Parent:
19:4b147d8f9164
Child:
21:8524d815c587
Child:
25:d29a462afefd
--- a/Service.hpp	Thu Aug 30 08:48:19 2018 +0000
+++ b/Service.hpp	Sun Sep 02 22:24:14 2018 +0000
@@ -1,15 +1,57 @@
-#ifndef __CHANNEL_HPP__
-#define __CHANNEL_HPP__
+#ifndef __SERVICE_HPP__
+#define __SERVICE_HPP__
 
+#include "Context.h"
+
+#ifndef TEST_ENVIRONMENT
 #include "mbed.h"
+#endif
+
 #include <iostream>
+#include <sstream>
 #include <string>
-#include "ServiceExceptions.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
+        } ;
+
+        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;
+
         typedef uint8_t MISNET_CODE ;
 
         enum DEVICE_TYPE {
@@ -27,11 +69,11 @@
 
         typedef uint8_t  GROUP ;
 
-        typedef uint16_t VALUE_TYPE ;
+        //typedef uint8_t VALUE_TYPE ;
 
         enum STATE {
-            ENABLE_   = 1,  //   ACTIVE
-            DISABLE_  = 0   //   ASLEEP
+            ENABLED   = 1,  //   ACTIVE
+            DISABLED  = 0   //   ASLEEP
         } ;
 
         enum ACCESS_TYPE {
@@ -77,6 +119,7 @@
                             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);
@@ -110,13 +153,11 @@
         }
 
         ACCESS_PIN getAccessPin(short index) {
-            /*
-            if (index < 1 || index > 6) {
-                throw ServiceException();
-            }
-            */
+            return this->access_pins[index - 1];
+        }
 
-            return this->access_pins[index - 1];
+        uint32_t getSubsampleRate() {
+            return this->subsample_rate;
         }
 
         ACTION getAction() {
@@ -131,7 +172,71 @@
             return this->comment;
         }
 
-        friend std::ostream& operator<<(std::ostream&, const Service &);
+        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 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();
+
+        void incrementActivationNb() {
+            ++(this->activation_nb);
+        }
+
+        void resetActivationNb() {
+            this->activation_nb = 0;
+        }
+
+        // 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;
+        }
+
+        // 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;
+        }
+
+        void savePreviousValue() {
+            this->previous_value = this->current_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(GENERIC_VALUE& value);
+
+        // Returns a string representing the current value.
+        std::string getCurrentValueAsString() {
+            return this->getValueAsString(this->current_value);
+        }
+
+        // Returns a string representing the previous value.
+        std::string getPreviousValueAsString() {
+            return this->getValueAsString(this->previous_value);
+        }
 
 
     private:
@@ -142,15 +247,18 @@
         ACCESS_PIN          access_pins[6];
         REQUEST_MODE        request_mode;
         UP_MODE             up_mode;
-        /* The following fields are currently unused.
-        uint32_t    timer_divider;
-        uint32_t    threshold_delta;
-        uint32_t    threshold_up;
-        uint32_t    threshold_down;
-        */
+        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;
+
+        uint32_t            activation_nb;
+
+        GENERIC_VALUE       current_value;
+        GENERIC_VALUE       previous_value;
 };
 
-#endif // __CHANNEL_HPP__
+#endif // __SERVICE_HPP__