Represents a single device. Take measurements, send commands. Needs SDI12 bus instance as argument. Allows to detect devices on the bus

Revision:
17:5cabfc047b40
Parent:
16:44daef8babd6
Child:
18:365e8aa4002c
--- a/SDI12_device.h	Mon Aug 06 17:43:24 2018 +0000
+++ b/SDI12_device.h	Tue Aug 07 11:42:28 2018 +0000
@@ -48,11 +48,11 @@
         }
         return found;
     };
-    
-    SDI12_device(){
+
+    SDI12_device() {
         debug("DEFAULT\r\n");
-        }
-    
+    }
+
     SDI12_device (SDI12 *inst, char address, EventQueue &mainDispatcher) : _sdi12(inst) {
         sensorQueue = &EventQueue(4*EVENTS_QUEUE_SIZE);
         setAddress(address);
@@ -88,7 +88,6 @@
         // unchain event queue
 //        sensorQueue.cancel(_measurementEventId);
         if (sensorQueue != NULL) sensorQueue->chain(NULL);
-        
     }
 
     // concurrent means that device under measurement won't signal its prempt readiness to the bus
@@ -161,20 +160,20 @@
         char buffer[bufferSize+1];
         sdi12.getRxBuffer(buffer);
         buffer[bufferSize] = '\0';
-        std::string response(buffer);
+        const std::string response(buffer);
 
 //        debug("parser. recv: %s\r\n", response);
         if (response[0] == _address) {
             // parser here
-            response = response.substr(1); // to limit repeting this operation later. i.e. extract only values e.g. +21.3-123+123
+            // const!!! response = response.substr(1); // to limit repeting this operation later. i.e. extract only values e.g. +21.3-123+123
             // the only two possible delimeters of a value
-            size_t next_start_index = response.find_first_of("-+", 0);
+            size_t next_start_index = response.find_first_of("-+", 0 + 1); // address offset!
             size_t next_end_index;
             // ready, steady, parse!!
             while(next_start_index != std::string::npos) {
                 // determine start index:
                 next_end_index = response.find_first_of("-+", next_start_index + 1); // std::substr is prepared to take std::npos
-                
+
                 float value = std::atof(response.substr(next_start_index, next_end_index).c_str());
 //                debug("parsed: %f\r\n", value);
                 _measurement.values[_valuesReadyCount++] = value;
@@ -212,6 +211,7 @@
 
     SDI12 *_sdi12;
     char _address;
+
     int _measurementEventId;
     Measurement_struct _measurement;
     size_t _valuesReadyCount;
@@ -238,25 +238,22 @@
         char buffer[bufferSize+1];
         sdi12.getRxBuffer(buffer);
         buffer[bufferSize] = '\0';
-        std::string response(buffer);
+        const std::string response(buffer);
 
         if (response[0] == _address) {
             // e.g.: 113DECAGON GS3   402
 
             ident.sdi_version = std::atoi(response.substr(1, 2).c_str());
-            std::string tempStr = response.substr(3, 8);
+            const std::string tempStr = response.substr(3, 8);
             strcpy(ident.company, tempStr.c_str());
 
-            tempStr = response.substr(3+8, 6);
-            strcpy(ident.model, tempStr.c_str());
+            strcpy(ident.model, response.substr(3+8, 6).c_str());
 
-            tempStr = response.substr(3+8+6, 3);
-            strcpy(ident.version, tempStr.c_str());
+            strcpy(ident.version, response.substr(3+8+6, 3).c_str());
             return true;
         }
         return false;
     }
-
 };
 
 const std::string SDI12_device::ADDRRICES = "0123456789abcdefgijklmnoprstuvwxyzABCDEFGHIJKLMNOPRSTUVWXYZ";
\ No newline at end of file