Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of kpn_senml by
senml_int_record.cpp
00001 /* _ __ ____ _ _ 00002 * | |/ / | _ \ | \ | | 00003 * | ' / | |_) | | \| | 00004 * | . \ | __/ | |\ | 00005 * |_|\_\ |_| |_| \_| 00006 * 00007 * (c) 2018 KPN 00008 * License: MIT License. 00009 * Author: Jan Bogaerts 00010 * 00011 * support for int sensors 00012 */ 00013 00014 #include <senml_int_record.h> 00015 #include <cbor.h> 00016 #include <senml_helpers.h> 00017 #include <senml_int_pack.h> 00018 #include <senml_double_pack.h> 00019 00020 SenMLIntRecord::SenMLIntRecord(const char* name): SenMLRecordTemplate(name) 00021 { 00022 } 00023 00024 SenMLIntRecord::SenMLIntRecord(const char* name, SenMLUnit unit): SenMLRecordTemplate(name, unit) 00025 { 00026 } 00027 00028 int SenMLIntRecord::getAdjustedValue() 00029 { 00030 int adjustedValue = this->get(); 00031 if(_streamCtx->baseDataType == CBOR_TYPE_INT){ 00032 if(this->asSum()) 00033 adjustedValue -= ((SenMLIntPack*)this->getRoot())->getBaseSum(); 00034 else 00035 adjustedValue -= ((SenMLIntPack*)this->getRoot())->getBaseValue(); 00036 } 00037 return adjustedValue; 00038 } 00039 00040 double SenMLIntRecord::getAdjustedValueD() 00041 { 00042 double adjustedValue = this->get(); 00043 if(_streamCtx->baseDataType == CBOR_TYPE_DOUBLE){ 00044 if(this->asSum()) 00045 adjustedValue -= ((SenMLDoublePack*)this->getRoot())->getBaseSum(); 00046 else 00047 adjustedValue -= ((SenMLDoublePack*)this->getRoot())->getBaseValue(); 00048 } 00049 return adjustedValue; 00050 } 00051 00052 void SenMLIntRecord::fieldsToJson() 00053 { 00054 SenMLRecord::fieldsToJson(); 00055 if(this->asSum()) 00056 printText(",\"s\":", 5); 00057 else 00058 printText(",\"v\":", 5); 00059 00060 //if the parent pack has a base value or base sum of type double, then we need to render a double value. 00061 if(_streamCtx->baseDataType == CBOR_TYPE_INT || _streamCtx->baseDataType == CBOR_TYPE_DATA){ 00062 #ifdef __MBED__ 00063 char buf[10]; 00064 sprintf(buf, "%d", this->getAdjustedValue()); 00065 String val = buf; 00066 #else 00067 String val(this->getAdjustedValue()); 00068 #endif 00069 printText(val.c_str(), val.length()); 00070 } 00071 else{ 00072 printDouble(this->getAdjustedValueD(), SENML_MAX_DOUBLE_PRECISION); 00073 } 00074 } 00075 00076 00077 int SenMLIntRecord::fieldsToCbor() 00078 { 00079 int res = SenMLRecord::fieldsToCbor(); 00080 if(this->asSum()) 00081 res += cbor_serialize_int(SENML_CBOR_S_LABEL); 00082 else 00083 res += cbor_serialize_int(SENML_CBOR_V_LABEL); 00084 00085 //if the parent pack has a base value or base sum of type double, then we need to render a double value. 00086 if(_streamCtx->baseDataType == CBOR_TYPE_INT || _streamCtx->baseDataType == CBOR_TYPE_DATA) 00087 res += cbor_serialize_int(this->getAdjustedValue()); 00088 else 00089 res += cbor_serialize_double(this->getAdjustedValueD()); 00090 return res; 00091 } 00092 00093 00094 00095 00096 00097 00098
Generated on Tue Jul 12 2022 23:07:22 by
