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_float_record.cpp@2:9b44be6e79ac, 2018-05-27 (annotated)
- Committer:
- kpniot
- Date:
- Sun May 27 14:31:44 2018 +0000
- Revision:
- 2:9b44be6e79ac
- Parent:
- 0:a9259748d982
try to fix repo
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 float sensors |
kpniot | 0:a9259748d982 | 12 | */ |
kpniot | 0:a9259748d982 | 13 | |
kpniot | 0:a9259748d982 | 14 | |
kpniot | 0:a9259748d982 | 15 | #include <senml_float_record.h> |
kpniot | 0:a9259748d982 | 16 | #include <senml_helpers.h> |
kpniot | 0:a9259748d982 | 17 | #include <cbor.h> |
kpniot | 0:a9259748d982 | 18 | #include <senml_double_pack.h> |
kpniot | 0:a9259748d982 | 19 | #include <senml_int_pack.h> |
kpniot | 0:a9259748d982 | 20 | |
kpniot | 0:a9259748d982 | 21 | SenMLFloatRecord::SenMLFloatRecord(const char* name): SenMLRecordTemplate(name) |
kpniot | 0:a9259748d982 | 22 | { |
kpniot | 0:a9259748d982 | 23 | } |
kpniot | 0:a9259748d982 | 24 | |
kpniot | 0:a9259748d982 | 25 | SenMLFloatRecord::SenMLFloatRecord(const char* name, SenMLUnit unit): SenMLRecordTemplate(name, unit) |
kpniot | 0:a9259748d982 | 26 | { |
kpniot | 0:a9259748d982 | 27 | } |
kpniot | 0:a9259748d982 | 28 | |
kpniot | 0:a9259748d982 | 29 | float SenMLFloatRecord::getAdjustedValue() |
kpniot | 0:a9259748d982 | 30 | { |
kpniot | 0:a9259748d982 | 31 | float adjustedValue = this->get(); |
kpniot | 0:a9259748d982 | 32 | if(_streamCtx->baseDataType == CBOR_TYPE_DOUBLE){ |
kpniot | 0:a9259748d982 | 33 | if(this->asSum()) |
kpniot | 0:a9259748d982 | 34 | adjustedValue -= ((SenMLDoublePack*)this->getRoot())->getBaseSum(); |
kpniot | 0:a9259748d982 | 35 | else |
kpniot | 0:a9259748d982 | 36 | adjustedValue -= ((SenMLDoublePack*)this->getRoot())->getBaseValue(); |
kpniot | 0:a9259748d982 | 37 | } |
kpniot | 0:a9259748d982 | 38 | else if( _streamCtx->baseDataType == CBOR_TYPE_INT){ |
kpniot | 0:a9259748d982 | 39 | if(this->asSum()) |
kpniot | 0:a9259748d982 | 40 | adjustedValue -= ((SenMLIntPack*)this->getRoot())->getBaseSum(); |
kpniot | 0:a9259748d982 | 41 | else |
kpniot | 0:a9259748d982 | 42 | adjustedValue -= ((SenMLIntPack*)this->getRoot())->getBaseValue(); |
kpniot | 0:a9259748d982 | 43 | } |
kpniot | 0:a9259748d982 | 44 | return adjustedValue; |
kpniot | 0:a9259748d982 | 45 | } |
kpniot | 0:a9259748d982 | 46 | |
kpniot | 0:a9259748d982 | 47 | void SenMLFloatRecord::fieldsToJson() |
kpniot | 0:a9259748d982 | 48 | { |
kpniot | 0:a9259748d982 | 49 | SenMLRecord::fieldsToJson(); |
kpniot | 0:a9259748d982 | 50 | if(this->asSum()) |
kpniot | 0:a9259748d982 | 51 | printText(",\"s\":", 5); |
kpniot | 0:a9259748d982 | 52 | else |
kpniot | 0:a9259748d982 | 53 | printText(",\"v\":", 5); |
kpniot | 0:a9259748d982 | 54 | printDouble(this->getAdjustedValue(), SENML_MAX_DOUBLE_PRECISION); |
kpniot | 0:a9259748d982 | 55 | } |
kpniot | 0:a9259748d982 | 56 | |
kpniot | 0:a9259748d982 | 57 | int SenMLFloatRecord::fieldsToCbor() |
kpniot | 0:a9259748d982 | 58 | { |
kpniot | 0:a9259748d982 | 59 | int res = SenMLRecord::fieldsToCbor(); |
kpniot | 0:a9259748d982 | 60 | if(this->asSum()) |
kpniot | 0:a9259748d982 | 61 | res += cbor_serialize_int(SENML_CBOR_S_LABEL); |
kpniot | 0:a9259748d982 | 62 | else |
kpniot | 0:a9259748d982 | 63 | res += cbor_serialize_int(SENML_CBOR_V_LABEL); |
kpniot | 0:a9259748d982 | 64 | res += cbor_serialize_double(this->getAdjustedValue()); |
kpniot | 0:a9259748d982 | 65 | return res; |
kpniot | 0:a9259748d982 | 66 | } |
kpniot | 0:a9259748d982 | 67 | |
kpniot | 0:a9259748d982 | 68 | |
kpniot | 0:a9259748d982 | 69 | |
kpniot | 0:a9259748d982 | 70 | |
kpniot | 0:a9259748d982 | 71 | |
kpniot | 0:a9259748d982 | 72 | |
kpniot | 0:a9259748d982 | 73 |