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 KPN IoT

Committer:
kpniot
Date:
Sat May 19 17:35:20 2018 +0000
Revision:
0:a9259748d982
first commit

Who changed what in which revision?

UserRevisionLine numberNew 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 * parse json
kpniot 0:a9259748d982 12 */
kpniot 0:a9259748d982 13
kpniot 0:a9259748d982 14 #include "senml_json_parser.h"
kpniot 0:a9259748d982 15 #include "senml_helpers.h"
kpniot 0:a9259748d982 16 #include "senml_JsonListener.h"
kpniot 0:a9259748d982 17
kpniot 0:a9259748d982 18 #define NONE 0
kpniot 0:a9259748d982 19 #define BN_VALUE 1
kpniot 0:a9259748d982 20 #define BU_VALUE 2
kpniot 0:a9259748d982 21 #define N_VALUE 3
kpniot 0:a9259748d982 22 #define V_VALUE 4
kpniot 0:a9259748d982 23 #define VB_VALUE 5
kpniot 0:a9259748d982 24 #define VD_VALUE 6
kpniot 0:a9259748d982 25 #define VS_VALUE 7
kpniot 0:a9259748d982 26 #define BV_VALUE 8
kpniot 0:a9259748d982 27
kpniot 0:a9259748d982 28
kpniot 0:a9259748d982 29 #ifdef __MBED__
kpniot 0:a9259748d982 30 #define KEY1 key[1]
kpniot 0:a9259748d982 31 #else
kpniot 0:a9259748d982 32 #define KEY1 key.charAt(1)
kpniot 0:a9259748d982 33 #endif
kpniot 0:a9259748d982 34
kpniot 0:a9259748d982 35
kpniot 0:a9259748d982 36 void SenMLJsonListener::key(String key) {
kpniot 0:a9259748d982 37 //Serial.println("key: " + key);
kpniot 0:a9259748d982 38 this->expected = NONE;
kpniot 0:a9259748d982 39 if(key.length() > 0){ //using faseter char comare instead of string compare
kpniot 0:a9259748d982 40 #ifdef __MBED__
kpniot 0:a9259748d982 41 char first = key[0];
kpniot 0:a9259748d982 42 #else
kpniot 0:a9259748d982 43 char first = key.charAt(0);
kpniot 0:a9259748d982 44 #endif
kpniot 0:a9259748d982 45 switch (first)
kpniot 0:a9259748d982 46 {
kpniot 0:a9259748d982 47 case 'b':
kpniot 0:a9259748d982 48 if(key.length() == 2){
kpniot 0:a9259748d982 49 switch (KEY1)
kpniot 0:a9259748d982 50 {
kpniot 0:a9259748d982 51 case 'n': this->expected = BN_VALUE; break;
kpniot 0:a9259748d982 52 case 'u': this->expected = BU_VALUE; break;
kpniot 0:a9259748d982 53 case 'v': this->expected = BV_VALUE; break;
kpniot 0:a9259748d982 54 }
kpniot 0:a9259748d982 55 }
kpniot 0:a9259748d982 56 break;
kpniot 0:a9259748d982 57 case 'n':
kpniot 0:a9259748d982 58 this->expected = N_VALUE; break;
kpniot 0:a9259748d982 59 case 'v':
kpniot 0:a9259748d982 60 if(key.length() == 2){
kpniot 0:a9259748d982 61 switch (KEY1)
kpniot 0:a9259748d982 62 {
kpniot 0:a9259748d982 63 case 'b': this->expected = VB_VALUE; break;
kpniot 0:a9259748d982 64 case 'd': this->expected = VD_VALUE; break;
kpniot 0:a9259748d982 65 case 's': this->expected = VS_VALUE; break;
kpniot 0:a9259748d982 66 }
kpniot 0:a9259748d982 67 }
kpniot 0:a9259748d982 68 else if(key.length() == 1)
kpniot 0:a9259748d982 69 this->expected = V_VALUE;
kpniot 0:a9259748d982 70 break;
kpniot 0:a9259748d982 71 }
kpniot 0:a9259748d982 72 }
kpniot 0:a9259748d982 73
kpniot 0:a9259748d982 74 }
kpniot 0:a9259748d982 75
kpniot 0:a9259748d982 76 void SenMLJsonListener::value(String value) {
kpniot 0:a9259748d982 77 double dblVal;
kpniot 0:a9259748d982 78 bool boolVal;
kpniot 0:a9259748d982 79 switch (this->expected)
kpniot 0:a9259748d982 80 {
kpniot 0:a9259748d982 81 case BV_VALUE: this->baseValue = atof(value.c_str());
kpniot 0:a9259748d982 82 case BN_VALUE: this->setCurrentPack(value); break;
kpniot 0:a9259748d982 83 case BU_VALUE: this->checkBaseUnit(value); break;
kpniot 0:a9259748d982 84 case N_VALUE: this->setCurrentRecord(value); break;
kpniot 0:a9259748d982 85 case V_VALUE:
kpniot 0:a9259748d982 86 dblVal = atof(value.c_str()) + this->baseValue;
kpniot 0:a9259748d982 87 this->setValue(&dblVal, sizeof(double), SENML_TYPE_NR);
kpniot 0:a9259748d982 88 break;
kpniot 0:a9259748d982 89 case VB_VALUE:
kpniot 0:a9259748d982 90 boolVal = strcmp(value.c_str(), "true") == 0;
kpniot 0:a9259748d982 91 this->setValue(&boolVal, sizeof(bool), SENML_TYPE_BOOL);
kpniot 0:a9259748d982 92 break;
kpniot 0:a9259748d982 93 case VD_VALUE: this->setValue(value.c_str(), value.length(), SENML_TYPE_DATA); break;
kpniot 0:a9259748d982 94 case VS_VALUE: this->setValue(value.c_str(), value.length(), SENML_TYPE_STRING); break;
kpniot 0:a9259748d982 95 }
kpniot 0:a9259748d982 96 }
kpniot 0:a9259748d982 97
kpniot 0:a9259748d982 98 void SenMLJsonListener::setValue(const void* value, int length, SenMLDataType dataType)
kpniot 0:a9259748d982 99 {
kpniot 0:a9259748d982 100 if(this->curRec){
kpniot 0:a9259748d982 101 this->curRec->actuate(value, length, dataType);
kpniot 0:a9259748d982 102 }
kpniot 0:a9259748d982 103 else {
kpniot 0:a9259748d982 104 SenMLPack* pack = this->curPack;
kpniot 0:a9259748d982 105 if(!pack)
kpniot 0:a9259748d982 106 pack = this->root;
kpniot 0:a9259748d982 107 if(pack)
kpniot 0:a9259748d982 108 pack->actuate(this->curPackName.c_str(), this->curRecName.c_str(), value, length, dataType);
kpniot 0:a9259748d982 109 }
kpniot 0:a9259748d982 110 }
kpniot 0:a9259748d982 111
kpniot 0:a9259748d982 112 /*
kpniot 0:a9259748d982 113
kpniot 0:a9259748d982 114 void SenMLJsonListener::startDocument() {
kpniot 0:a9259748d982 115 Serial.println("start document");
kpniot 0:a9259748d982 116 }
kpniot 0:a9259748d982 117
kpniot 0:a9259748d982 118 void SenMLJsonListener::endArray() {
kpniot 0:a9259748d982 119 Serial.println("end array. ");
kpniot 0:a9259748d982 120 }
kpniot 0:a9259748d982 121
kpniot 0:a9259748d982 122 void SenMLJsonListener::endDocument() {
kpniot 0:a9259748d982 123 Serial.println("end document. ");
kpniot 0:a9259748d982 124 }
kpniot 0:a9259748d982 125
kpniot 0:a9259748d982 126 void SenMLJsonListener::startArray() {
kpniot 0:a9259748d982 127 Serial.println("start array. ");
kpniot 0:a9259748d982 128 }
kpniot 0:a9259748d982 129
kpniot 0:a9259748d982 130 void SenMLJsonListener::startObject() {
kpniot 0:a9259748d982 131 Serial.println("start object. ");
kpniot 0:a9259748d982 132 }
kpniot 0:a9259748d982 133 */
kpniot 0:a9259748d982 134
kpniot 0:a9259748d982 135
kpniot 0:a9259748d982 136
kpniot 0:a9259748d982 137
kpniot 0:a9259748d982 138
kpniot 0:a9259748d982 139
kpniot 0:a9259748d982 140