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.
Fork of kpn_senml by
senml_int_record.cpp@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 | * support for int sensors |
kpniot | 0:a9259748d982 | 12 | */ |
kpniot | 0:a9259748d982 | 13 | |
kpniot | 0:a9259748d982 | 14 | #include <senml_int_record.h> |
kpniot | 0:a9259748d982 | 15 | #include <cbor.h> |
kpniot | 0:a9259748d982 | 16 | #include <senml_helpers.h> |
kpniot | 0:a9259748d982 | 17 | #include <senml_int_pack.h> |
kpniot | 0:a9259748d982 | 18 | #include <senml_double_pack.h> |
kpniot | 0:a9259748d982 | 19 | |
kpniot | 0:a9259748d982 | 20 | SenMLIntRecord::SenMLIntRecord(const char* name): SenMLRecordTemplate(name) |
kpniot | 0:a9259748d982 | 21 | { |
kpniot | 0:a9259748d982 | 22 | } |
kpniot | 0:a9259748d982 | 23 | |
kpniot | 0:a9259748d982 | 24 | SenMLIntRecord::SenMLIntRecord(const char* name, SenMLUnit unit): SenMLRecordTemplate(name, unit) |
kpniot | 0:a9259748d982 | 25 | { |
kpniot | 0:a9259748d982 | 26 | } |
kpniot | 0:a9259748d982 | 27 | |
kpniot | 0:a9259748d982 | 28 | int SenMLIntRecord::getAdjustedValue() |
kpniot | 0:a9259748d982 | 29 | { |
kpniot | 0:a9259748d982 | 30 | int adjustedValue = this->get(); |
kpniot | 0:a9259748d982 | 31 | if(_streamCtx->baseDataType == CBOR_TYPE_INT){ |
kpniot | 0:a9259748d982 | 32 | if(this->asSum()) |
kpniot | 0:a9259748d982 | 33 | adjustedValue -= ((SenMLIntPack*)this->getRoot())->getBaseSum(); |
kpniot | 0:a9259748d982 | 34 | else |
kpniot | 0:a9259748d982 | 35 | adjustedValue -= ((SenMLIntPack*)this->getRoot())->getBaseValue(); |
kpniot | 0:a9259748d982 | 36 | } |
kpniot | 0:a9259748d982 | 37 | return adjustedValue; |
kpniot | 0:a9259748d982 | 38 | } |
kpniot | 0:a9259748d982 | 39 | |
kpniot | 0:a9259748d982 | 40 | double SenMLIntRecord::getAdjustedValueD() |
kpniot | 0:a9259748d982 | 41 | { |
kpniot | 0:a9259748d982 | 42 | double adjustedValue = this->get(); |
kpniot | 0:a9259748d982 | 43 | if(_streamCtx->baseDataType == CBOR_TYPE_DOUBLE){ |
kpniot | 0:a9259748d982 | 44 | if(this->asSum()) |
kpniot | 0:a9259748d982 | 45 | adjustedValue -= ((SenMLDoublePack*)this->getRoot())->getBaseSum(); |
kpniot | 0:a9259748d982 | 46 | else |
kpniot | 0:a9259748d982 | 47 | adjustedValue -= ((SenMLDoublePack*)this->getRoot())->getBaseValue(); |
kpniot | 0:a9259748d982 | 48 | } |
kpniot | 0:a9259748d982 | 49 | return adjustedValue; |
kpniot | 0:a9259748d982 | 50 | } |
kpniot | 0:a9259748d982 | 51 | |
kpniot | 0:a9259748d982 | 52 | void SenMLIntRecord::fieldsToJson() |
kpniot | 0:a9259748d982 | 53 | { |
kpniot | 0:a9259748d982 | 54 | SenMLRecord::fieldsToJson(); |
kpniot | 0:a9259748d982 | 55 | if(this->asSum()) |
kpniot | 0:a9259748d982 | 56 | printText(",\"s\":", 5); |
kpniot | 0:a9259748d982 | 57 | else |
kpniot | 0:a9259748d982 | 58 | printText(",\"v\":", 5); |
kpniot | 0:a9259748d982 | 59 | |
kpniot | 0:a9259748d982 | 60 | //if the parent pack has a base value or base sum of type double, then we need to render a double value. |
kpniot | 0:a9259748d982 | 61 | if(_streamCtx->baseDataType == CBOR_TYPE_INT || _streamCtx->baseDataType == CBOR_TYPE_DATA){ |
kpniot | 0:a9259748d982 | 62 | #ifdef __MBED__ |
kpniot | 0:a9259748d982 | 63 | char buf[10]; |
kpniot | 0:a9259748d982 | 64 | sprintf(buf, "%d", this->getAdjustedValue()); |
kpniot | 0:a9259748d982 | 65 | String val = buf; |
kpniot | 0:a9259748d982 | 66 | #else |
kpniot | 0:a9259748d982 | 67 | String val(this->getAdjustedValue()); |
kpniot | 0:a9259748d982 | 68 | #endif |
kpniot | 0:a9259748d982 | 69 | printText(val.c_str(), val.length()); |
kpniot | 0:a9259748d982 | 70 | } |
kpniot | 0:a9259748d982 | 71 | else{ |
kpniot | 0:a9259748d982 | 72 | printDouble(this->getAdjustedValueD(), SENML_MAX_DOUBLE_PRECISION); |
kpniot | 0:a9259748d982 | 73 | } |
kpniot | 0:a9259748d982 | 74 | } |
kpniot | 0:a9259748d982 | 75 | |
kpniot | 0:a9259748d982 | 76 | |
kpniot | 0:a9259748d982 | 77 | int SenMLIntRecord::fieldsToCbor() |
kpniot | 0:a9259748d982 | 78 | { |
kpniot | 0:a9259748d982 | 79 | int res = SenMLRecord::fieldsToCbor(); |
kpniot | 0:a9259748d982 | 80 | if(this->asSum()) |
kpniot | 0:a9259748d982 | 81 | res += cbor_serialize_int(SENML_CBOR_S_LABEL); |
kpniot | 0:a9259748d982 | 82 | else |
kpniot | 0:a9259748d982 | 83 | res += cbor_serialize_int(SENML_CBOR_V_LABEL); |
kpniot | 0:a9259748d982 | 84 | |
kpniot | 0:a9259748d982 | 85 | //if the parent pack has a base value or base sum of type double, then we need to render a double value. |
kpniot | 0:a9259748d982 | 86 | if(_streamCtx->baseDataType == CBOR_TYPE_INT || _streamCtx->baseDataType == CBOR_TYPE_DATA) |
kpniot | 0:a9259748d982 | 87 | res += cbor_serialize_int(this->getAdjustedValue()); |
kpniot | 0:a9259748d982 | 88 | else |
kpniot | 0:a9259748d982 | 89 | res += cbor_serialize_double(this->getAdjustedValueD()); |
kpniot | 0:a9259748d982 | 90 | return res; |
kpniot | 0:a9259748d982 | 91 | } |
kpniot | 0:a9259748d982 | 92 | |
kpniot | 0:a9259748d982 | 93 | |
kpniot | 0:a9259748d982 | 94 | |
kpniot | 0:a9259748d982 | 95 | |
kpniot | 0:a9259748d982 | 96 | |
kpniot | 0:a9259748d982 | 97 | |
kpniot | 0:a9259748d982 | 98 |