The KPN SenML library helps you create and parse senml documents in both json and cbor format. The library can be used for sending sensor data and receiving actuator commands.
senml_record_t.h@0:a9259748d982, 2018-05-19 (annotated)
- Committer:
- kpniot
- Date:
- Sat May 19 17:35:20 2018 +0000
- Revision:
- 0:a9259748d982
first commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kpniot | 0:a9259748d982 | 1 | /* _ __ ____ _ _ |
kpniot | 0:a9259748d982 | 2 | * | |/ / | _ \ | \ | | |
kpniot | 0:a9259748d982 | 3 | * | ' / | |_) | | \| | |
kpniot | 0:a9259748d982 | 4 | * | . \ | __/ | |\ | |
kpniot | 0:a9259748d982 | 5 | * |_|\_\ |_| |_| \_| |
kpniot | 0:a9259748d982 | 6 | * |
kpniot | 0:a9259748d982 | 7 | * (c) 2018 KPN |
kpniot | 0:a9259748d982 | 8 | * License: MIT License. |
kpniot | 0:a9259748d982 | 9 | * Author: Jan Bogaerts |
kpniot | 0:a9259748d982 | 10 | * |
kpniot | 0:a9259748d982 | 11 | * record base class for simple data types |
kpniot | 0:a9259748d982 | 12 | */ |
kpniot | 0:a9259748d982 | 13 | |
kpniot | 0:a9259748d982 | 14 | #ifndef SENMLRECORDTEMPLATE |
kpniot | 0:a9259748d982 | 15 | #define SENMLRECORDTEMPLATE |
kpniot | 0:a9259748d982 | 16 | |
kpniot | 0:a9259748d982 | 17 | #include <senml_record.h> |
kpniot | 0:a9259748d982 | 18 | |
kpniot | 0:a9259748d982 | 19 | |
kpniot | 0:a9259748d982 | 20 | /** |
kpniot | 0:a9259748d982 | 21 | * A template class that can be used to create new SenMLRecord types that store a value with a basic |
kpniot | 0:a9259748d982 | 22 | * data type (no structs or classes). |
kpniot | 0:a9259748d982 | 23 | * When you create a new class, you should always implement the following functions in order |
kpniot | 0:a9259748d982 | 24 | * for the new class to operate correctly: fieldsToJson() and fieldsToCbor() |
kpniot | 0:a9259748d982 | 25 | */ |
kpniot | 0:a9259748d982 | 26 | template <class T> |
kpniot | 0:a9259748d982 | 27 | class SenMLRecordTemplate: public SenMLRecord |
kpniot | 0:a9259748d982 | 28 | { |
kpniot | 0:a9259748d982 | 29 | public: |
kpniot | 0:a9259748d982 | 30 | SenMLRecordTemplate(const char* name): SenMLRecord(name), _valAsSum(false){}; |
kpniot | 0:a9259748d982 | 31 | SenMLRecordTemplate(const char* name, SenMLUnit unit): SenMLRecord(name, unit), _valAsSum(false){}; |
kpniot | 0:a9259748d982 | 32 | SenMLRecordTemplate(const char* name, SenMLUnit unit, T value): SenMLRecord(name, unit), _value(value), _valAsSum(false) {}; |
kpniot | 0:a9259748d982 | 33 | // ~SenMLRecordTemplate(){}; |
kpniot | 0:a9259748d982 | 34 | |
kpniot | 0:a9259748d982 | 35 | /** |
kpniot | 0:a9259748d982 | 36 | * Get the value assigned to this SenMLRecord. |
kpniot | 0:a9259748d982 | 37 | * This function always returns the absolute value, also when the record is part of a pack that |
kpniot | 0:a9259748d982 | 38 | * has a base value assigned. |
kpniot | 0:a9259748d982 | 39 | * @returns: the value (including the base-value when applicable). |
kpniot | 0:a9259748d982 | 40 | */ |
kpniot | 0:a9259748d982 | 41 | T get() {return _value; } ; |
kpniot | 0:a9259748d982 | 42 | |
kpniot | 0:a9259748d982 | 43 | /** |
kpniot | 0:a9259748d982 | 44 | * Get if the value should be interpreted as a sum or regular value. These are exclusive: a SenMLRecord |
kpniot | 0:a9259748d982 | 45 | * either has a sum or a value, but never both. |
kpniot | 0:a9259748d982 | 46 | * Sum and value are always assigned with the function set(). |
kpniot | 0:a9259748d982 | 47 | * @returns: True when the value is interpreted as a sum, otherwise false. |
kpniot | 0:a9259748d982 | 48 | */ |
kpniot | 0:a9259748d982 | 49 | bool asSum() {return this->_valAsSum; }; |
kpniot | 0:a9259748d982 | 50 | |
kpniot | 0:a9259748d982 | 51 | #ifdef __MBED__ |
kpniot | 0:a9259748d982 | 52 | |
kpniot | 0:a9259748d982 | 53 | /** |
kpniot | 0:a9259748d982 | 54 | * Assign a value to the SenMLRecord. You can optionally assign the time at which the measurement was taken |
kpniot | 0:a9259748d982 | 55 | * and if it should be interpreted as a sum or regular value. |
kpniot | 0:a9259748d982 | 56 | * No timestamp is added and the value is interpreted as a regular value, not as a sum |
kpniot | 0:a9259748d982 | 57 | * @param value the value to store in the record |
kpniot | 0:a9259748d982 | 58 | * @returns: true if the operation was succesful. See setTime() for more info when the operation can fail. |
kpniot | 0:a9259748d982 | 59 | */ |
kpniot | 0:a9259748d982 | 60 | bool set(T value) |
kpniot | 0:a9259748d982 | 61 | { |
kpniot | 0:a9259748d982 | 62 | return this->set(value, (double)NAN, false); |
kpniot | 0:a9259748d982 | 63 | }; |
kpniot | 0:a9259748d982 | 64 | |
kpniot | 0:a9259748d982 | 65 | /** |
kpniot | 0:a9259748d982 | 66 | * Assign a value to the SenMLRecord. You can optionally assign the time at which the measurement was taken |
kpniot | 0:a9259748d982 | 67 | * and if it should be interpreted as a sum or regular value. |
kpniot | 0:a9259748d982 | 68 | * The value is interpreted as a regular value, not as a sum |
kpniot | 0:a9259748d982 | 69 | * @param value the value to store in the record |
kpniot | 0:a9259748d982 | 70 | * @param time optional (default = NAN, meaning not time info). The time at which the measurement was taken. |
kpniot | 0:a9259748d982 | 71 | * This should always be the absolute time value which will be converted relative to the base |
kpniot | 0:a9259748d982 | 72 | * time when applicable (if the root is a SenMLPack with baseTime) . If you want to set the time |
kpniot | 0:a9259748d982 | 73 | * manually relative to the basetime of the root-pack, then use setTime() instead. |
kpniot | 0:a9259748d982 | 74 | * @returns: true if the operation was succesful. See setTime() for more info when the operation can fail. |
kpniot | 0:a9259748d982 | 75 | */ |
kpniot | 0:a9259748d982 | 76 | bool set(T value, double time) |
kpniot | 0:a9259748d982 | 77 | { |
kpniot | 0:a9259748d982 | 78 | return this->set(value, time, false); |
kpniot | 0:a9259748d982 | 79 | }; |
kpniot | 0:a9259748d982 | 80 | |
kpniot | 0:a9259748d982 | 81 | /** |
kpniot | 0:a9259748d982 | 82 | * Assign a value to the SenMLRecord. You can optionally assign the time at which the measurement was taken |
kpniot | 0:a9259748d982 | 83 | * and if it should be interpreted as a sum or regular value. |
kpniot | 0:a9259748d982 | 84 | * @param value the value to store in the record |
kpniot | 0:a9259748d982 | 85 | * @param time optional (default = NAN, meaning not time info). The time at which the measurement was taken. |
kpniot | 0:a9259748d982 | 86 | * This should always be the absolute time value which will be converted relative to the base |
kpniot | 0:a9259748d982 | 87 | * time when applicable (if the root is a SenMLPack with baseTime) . If you want to set the time |
kpniot | 0:a9259748d982 | 88 | * manually relative to the basetime of the root-pack, then use setTime() instead. |
kpniot | 0:a9259748d982 | 89 | * @param asSum when true, the value will be interpreted as a sum, otherwise as a regular value. |
kpniot | 0:a9259748d982 | 90 | * @returns: true if the operation was succesful. See setTime() for more info when the operation can fail. |
kpniot | 0:a9259748d982 | 91 | */ |
kpniot | 0:a9259748d982 | 92 | bool set(T value, double time, bool asSum) |
kpniot | 0:a9259748d982 | 93 | |
kpniot | 0:a9259748d982 | 94 | #else |
kpniot | 0:a9259748d982 | 95 | |
kpniot | 0:a9259748d982 | 96 | /** |
kpniot | 0:a9259748d982 | 97 | * Assign a value to the SenMLRecord. You can optionally assign the time at which the measurement was taken |
kpniot | 0:a9259748d982 | 98 | * and if it should be interpreted as a sum or regular value. |
kpniot | 0:a9259748d982 | 99 | * @param value the value to store in the record |
kpniot | 0:a9259748d982 | 100 | * @param time optional (default = NAN, meaning not time info). The time at which the measurement was taken. |
kpniot | 0:a9259748d982 | 101 | * This should always be the absolute time value which will be converted relative to the base |
kpniot | 0:a9259748d982 | 102 | * time when applicable (if the root is a SenMLPack with baseTime) . If you want to set the time |
kpniot | 0:a9259748d982 | 103 | * manually relative to the basetime of the root-pack, then use setTime() instead. |
kpniot | 0:a9259748d982 | 104 | * @param asSum when true, the value will be interpreted as a sum, otherwise as a regular value. |
kpniot | 0:a9259748d982 | 105 | * @returns: true if the operation was succesful. See setTime() for more info when the operation can fail. |
kpniot | 0:a9259748d982 | 106 | */ |
kpniot | 0:a9259748d982 | 107 | bool set(T value, double time = (double)NAN, bool asSum = false) |
kpniot | 0:a9259748d982 | 108 | #endif |
kpniot | 0:a9259748d982 | 109 | { |
kpniot | 0:a9259748d982 | 110 | this->_value = value; |
kpniot | 0:a9259748d982 | 111 | this->_valAsSum = asSum; |
kpniot | 0:a9259748d982 | 112 | return this->setTime(time); |
kpniot | 0:a9259748d982 | 113 | }; |
kpniot | 0:a9259748d982 | 114 | |
kpniot | 0:a9259748d982 | 115 | protected: |
kpniot | 0:a9259748d982 | 116 | |
kpniot | 0:a9259748d982 | 117 | |
kpniot | 0:a9259748d982 | 118 | private: |
kpniot | 0:a9259748d982 | 119 | T _value; |
kpniot | 0:a9259748d982 | 120 | bool _valAsSum; |
kpniot | 0:a9259748d982 | 121 | }; |
kpniot | 0:a9259748d982 | 122 | |
kpniot | 0:a9259748d982 | 123 | #endif // SENMLRECORDTEMPLATE |
kpniot | 0:a9259748d982 | 124 | |
kpniot | 0:a9259748d982 | 125 | |
kpniot | 0:a9259748d982 | 126 | |
kpniot | 0:a9259748d982 | 127 | |
kpniot | 0:a9259748d982 | 128 | |
kpniot | 0:a9259748d982 | 129 | |
kpniot | 0:a9259748d982 | 130 |