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_json_parser.cpp
00001 /* _ __ ____ _ _ 00002 * | |/ / | _ \ | \ | | 00003 * | ' / | |_) | | \| | 00004 * | . \ | __/ | |\ | 00005 * |_|\_\ |_| |_| \_| 00006 * 00007 * (c) 2018 KPN 00008 * License: MIT License. 00009 * Author: Jan Bogaerts 00010 * 00011 * parse json 00012 */ 00013 00014 #include "senml_json_parser.h" 00015 #include "senml_helpers.h" 00016 #include "senml_JsonListener.h" 00017 00018 #define NONE 0 00019 #define BN_VALUE 1 00020 #define BU_VALUE 2 00021 #define N_VALUE 3 00022 #define V_VALUE 4 00023 #define VB_VALUE 5 00024 #define VD_VALUE 6 00025 #define VS_VALUE 7 00026 #define BV_VALUE 8 00027 00028 00029 #ifdef __MBED__ 00030 #define KEY1 key[1] 00031 #else 00032 #define KEY1 key.charAt(1) 00033 #endif 00034 00035 00036 void SenMLJsonListener::key(String key) { 00037 //Serial.println("key: " + key); 00038 this->expected = NONE; 00039 if(key.length() > 0){ //using faseter char comare instead of string compare 00040 #ifdef __MBED__ 00041 char first = key[0]; 00042 #else 00043 char first = key.charAt(0); 00044 #endif 00045 switch (first) 00046 { 00047 case 'b': 00048 if(key.length() == 2){ 00049 switch (KEY1) 00050 { 00051 case 'n': this->expected = BN_VALUE; break; 00052 case 'u': this->expected = BU_VALUE; break; 00053 case 'v': this->expected = BV_VALUE; break; 00054 } 00055 } 00056 break; 00057 case 'n': 00058 this->expected = N_VALUE; break; 00059 case 'v': 00060 if(key.length() == 2){ 00061 switch (KEY1) 00062 { 00063 case 'b': this->expected = VB_VALUE; break; 00064 case 'd': this->expected = VD_VALUE; break; 00065 case 's': this->expected = VS_VALUE; break; 00066 } 00067 } 00068 else if(key.length() == 1) 00069 this->expected = V_VALUE; 00070 break; 00071 } 00072 } 00073 00074 } 00075 00076 void SenMLJsonListener::value(String value) { 00077 double dblVal; 00078 bool boolVal; 00079 switch (this->expected) 00080 { 00081 case BV_VALUE: this->baseValue = atof(value.c_str()); 00082 case BN_VALUE: this->setCurrentPack(value); break; 00083 case BU_VALUE: this->checkBaseUnit(value); break; 00084 case N_VALUE: this->setCurrentRecord(value); break; 00085 case V_VALUE: 00086 dblVal = atof(value.c_str()) + this->baseValue; 00087 this->setValue(&dblVal, sizeof(double), SENML_TYPE_NR); 00088 break; 00089 case VB_VALUE: 00090 boolVal = strcmp(value.c_str(), "true") == 0; 00091 this->setValue(&boolVal, sizeof(bool), SENML_TYPE_BOOL); 00092 break; 00093 case VD_VALUE: this->setValue(value.c_str(), value.length(), SENML_TYPE_DATA); break; 00094 case VS_VALUE: this->setValue(value.c_str(), value.length(), SENML_TYPE_STRING); break; 00095 } 00096 } 00097 00098 void SenMLJsonListener::setValue(const void* value, int length, SenMLDataType dataType) 00099 { 00100 if(this->curRec){ 00101 this->curRec->actuate(value, length, dataType); 00102 } 00103 else { 00104 SenMLPack* pack = this->curPack; 00105 if(!pack) 00106 pack = this->root; 00107 if(pack) 00108 pack->actuate(this->curPackName.c_str(), this->curRecName.c_str(), value, length, dataType); 00109 } 00110 } 00111 00112 /* 00113 00114 void SenMLJsonListener::startDocument() { 00115 Serial.println("start document"); 00116 } 00117 00118 void SenMLJsonListener::endArray() { 00119 Serial.println("end array. "); 00120 } 00121 00122 void SenMLJsonListener::endDocument() { 00123 Serial.println("end document. "); 00124 } 00125 00126 void SenMLJsonListener::startArray() { 00127 Serial.println("start array. "); 00128 } 00129 00130 void SenMLJsonListener::startObject() { 00131 Serial.println("start object. "); 00132 } 00133 */ 00134 00135 00136 00137 00138 00139 00140
Generated on Tue Jul 12 2022 23:07:22 by
