KPN IoT / senml

Fork of kpn_senml by KPN IoT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers senml_float_record.cpp Source File

senml_float_record.cpp

00001 /*  _  __  ____    _   _ 
00002  * | |/ / |  _ \  | \ | |
00003  * | ' /  | |_) | |  \| |
00004  * | . \  |  __/  | |\  |
00005  * |_|\_\ |_|     |_| \_|
00006  * 
00007  * (c) 2018 KPN
00008  * License: MIT License.
00009  * Author: Jan Bogaerts
00010  * 
00011  * support for float sensors
00012  */
00013 
00014 
00015 #include <senml_float_record.h>
00016 #include <senml_helpers.h>
00017 #include <cbor.h>
00018 #include <senml_double_pack.h> 
00019 #include <senml_int_pack.h> 
00020 
00021 SenMLFloatRecord::SenMLFloatRecord(const char* name): SenMLRecordTemplate(name)
00022 {
00023 }
00024 
00025 SenMLFloatRecord::SenMLFloatRecord(const char* name, SenMLUnit unit): SenMLRecordTemplate(name, unit)
00026 {
00027 }
00028 
00029 float SenMLFloatRecord::getAdjustedValue()
00030 {
00031     float adjustedValue = this->get();
00032     if(_streamCtx->baseDataType == CBOR_TYPE_DOUBLE){
00033         if(this->asSum())
00034             adjustedValue -= ((SenMLDoublePack*)this->getRoot())->getBaseSum();
00035         else
00036             adjustedValue -= ((SenMLDoublePack*)this->getRoot())->getBaseValue();
00037     }
00038     else if( _streamCtx->baseDataType == CBOR_TYPE_INT){
00039         if(this->asSum())
00040             adjustedValue -= ((SenMLIntPack*)this->getRoot())->getBaseSum();
00041         else
00042             adjustedValue -= ((SenMLIntPack*)this->getRoot())->getBaseValue();
00043     }
00044     return adjustedValue;
00045 }
00046 
00047 void SenMLFloatRecord::fieldsToJson()
00048 {
00049     SenMLRecord::fieldsToJson();
00050     if(this->asSum())
00051         printText(",\"s\":", 5);
00052     else
00053         printText(",\"v\":", 5);
00054     printDouble(this->getAdjustedValue(), SENML_MAX_DOUBLE_PRECISION);
00055 }
00056 
00057 int SenMLFloatRecord::fieldsToCbor()
00058 {
00059     int res = SenMLRecord::fieldsToCbor();
00060     if(this->asSum())
00061         res += cbor_serialize_int(SENML_CBOR_S_LABEL);
00062     else
00063         res += cbor_serialize_int(SENML_CBOR_V_LABEL);
00064     res += cbor_serialize_double(this->getAdjustedValue());
00065     return res;
00066 }
00067 
00068 
00069 
00070 
00071 
00072 
00073