File size: 9,898 Bytes
69c3cbe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
###############################################################################
#
# Temboo Arduino library
#
# Copyright 2017, Temboo Inc.
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
# http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
# either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
#
###############################################################################
*/

#include <stddef.h>
#include <avr/pgmspace.h>
#include "ChoreoSensorInputFormatter.h"
#include "ChoreoSensorInputSet.h"

static const char TAG_INPUTS_START[] PROGMEM = "\"sensorInputs\":{";
static const char TAG_SENSOR_NAME_SEPERATOR[] PROGMEM = "\":{";
static const char TAG_VALUE_START[] PROGMEM = "\"value\":";
static const char TAG_CALIBRATION_START[] PROGMEM = ",\"calibration\":";
static const char TAG_RAW_LOW_START[] PROGMEM = ",\"rawLow\":";
static const char TAG_RAW_HIGH_START[] PROGMEM = ",\"rawHigh\":";
static const char TAG_SCALE_LOW_START[] PROGMEM = ",\"scaleLow\":";
static const char TAG_SCALE_HIGH_START[] PROGMEM = ",\"scaleHigh\":";
static const char TAG_CONVERSION_START[] PROGMEM = ",\"conversion\":";

ChoreoSensorInputFormatter::ChoreoSensorInputFormatter(const ChoreoSensorInputSet* inputSet) {
    m_inputSet = inputSet;
    reset();
}

void ChoreoSensorInputFormatter::reset() {
    m_currentInput = NULL;
    m_nextChar = NULL;
    if (m_inputSet == NULL || m_inputSet->isEmpty()) {
        m_nextState = END;
    } else {
        m_nextState = START;
    }
}

bool ChoreoSensorInputFormatter::hasNext() {
    return m_nextState != END;
}

char ChoreoSensorInputFormatter::next() {
    char c;
    switch(m_nextState) {
        case START:
            c = readStartTagChar(TAG_INPUTS_START, SENSOR_INPUT_TAG);
            break;
            
        case SENSOR_INPUT_TAG:
            c = readTagChar(SENSOR_NAME_START);
            if (m_nextState == SENSOR_NAME_START) {
                m_currentInput= m_inputSet->getFirstInput();
            }
            break;
            
        case SENSOR_NAME_START:
            c = '"';
            m_nextChar = m_currentInput->getName();
            if ((NULL == m_nextChar) || ('\0' == *m_nextChar)) {
                m_nextState = SENSOR_NAME_END;
            } else {
                m_nextState = SENSOR_NAME;
            }
            break;
            
        case SENSOR_NAME:
            c = readValueChar(SENSOR_NAME_END);
            break;
            
        case SENSOR_NAME_END:
            c = readStartTagChar(TAG_SENSOR_NAME_SEPERATOR, SENSOR_NAME_SEPERATOR);
            break;
            
        case SENSOR_NAME_SEPERATOR:
            c = readTagChar(VALUE_TAG);
            if (m_nextState == VALUE_TAG) {
                m_nextChar= TAG_VALUE_START;
            }
            break;
            
        case VALUE_TAG:
            c = readTagChar(VALUE_START);
            break;
            
        case VALUE_START:
            c = '"';
            m_nextChar = m_currentInput->getValue();
            if ((NULL == m_nextChar) || ('\0' == *m_nextChar)) {
                m_nextState = VALUE_END;
            } else {
                m_nextState = VALUE;
            }
            break;
            
        case VALUE:
            c = readValueChar(VALUE_END);
            break;
            
        case VALUE_END:
            c = '"';
            // decide how the data set should be set up, with just the value, with
            // a conversion, or with the user defined scale
            if (m_currentInput->getCalibration() != NULL) {
                m_nextChar = TAG_CALIBRATION_START;
                m_nextState = CALIBRATION_TAG;
            } else {
                if (m_currentInput->getConversion() != NULL) {
                    // use conversion
                    m_nextState = CONVERSION_TAG;
                    m_nextChar = TAG_CONVERSION_START;
                } else if (m_currentInput->getScaleHigh() != m_currentInput->getScaleLow()) {
                    // use user defined scale
                    m_nextState = RAW_LOW_TAG;
                    m_nextChar = TAG_RAW_LOW_START;
                } else {
                    // just the value is needed
                    m_nextState = DATA_SET_END;
                }
            }
            break;
            
        case CALIBRATION_TAG:
            c = readTagChar(CALIBRATION_START);
            break;
            
        case CALIBRATION_START:
            c = '"';
            m_nextChar = m_currentInput->getCalibration();
            if ((NULL == m_nextChar) || ('\0' == *m_nextChar)) {
                m_nextState = CALIBRATION_END;
            } else {
                m_nextState = CALIBRATION;
            }
            break;
            
        case CALIBRATION:
            c = readValueChar(CALIBRATION_END);
            break;
            
        case CALIBRATION_END:
            c = '"';
            if (m_currentInput->getConversion() != NULL) {
                // use conversion
                m_nextState = CONVERSION_TAG;
                m_nextChar = TAG_CONVERSION_START;
            } else if (m_currentInput->getScaleHigh() != m_currentInput->getScaleLow()) {
                // use user defined scale
                m_nextState = RAW_LOW_TAG;
                m_nextChar = TAG_RAW_LOW_START;
            } else {
                // just the value is needed
                m_nextState = DATA_SET_END;
            }
            break;

        case CONVERSION_TAG:
            c = readTagChar(CONVERSION_START);
            break;
            
        case CONVERSION_START:
            c = '"';
            m_nextChar = m_currentInput->getConversion();
            if ((NULL == m_nextChar) || ('\0' == *m_nextChar)) {
                m_nextState = CONVERSION_END;
            } else {
                m_nextState = CONVERSION;
            }
            break;
            
        case CONVERSION:
            c = readValueChar(CONVERSION_END);
            break;
            
        case CONVERSION_END:
            c = '"';
            m_nextState = DATA_SET_END;
            break;
            
        case RAW_LOW_TAG:
            c = readTagChar(RAW_LOW_START);
            break;
            
        case RAW_LOW_START:
            c = '"';
            m_nextChar = m_currentInput->getRawLow();
            if ((NULL == m_nextChar) || ('\0' == *m_nextChar)) {
                m_nextState = RAW_LOW_END;
            } else {
                m_nextState = RAW_LOW;
            }
            break;
            
        case RAW_LOW:
            c = readValueChar(RAW_LOW_END);
            break;
            
        case RAW_LOW_END:
            c = '"';
            m_nextState = RAW_HIGH_TAG;
            m_nextChar = TAG_RAW_HIGH_START;
            break;
            
        case RAW_HIGH_TAG:
            c = readTagChar(RAW_HIGH_START);
            break;
            
        case RAW_HIGH_START:
            c = '"';
            m_nextChar = m_currentInput->getRawHigh();
            if ((NULL == m_nextChar) || ('\0' == *m_nextChar)) {
                m_nextState = RAW_HIGH_END;
            } else {
                m_nextState = RAW_HIGH;
            }
            break;
            
        case RAW_HIGH:
            c = readValueChar(RAW_HIGH_END);
            break;
            
        case RAW_HIGH_END:
            c = '"';
            m_nextState = SCALE_LOW_TAG;
            m_nextChar = TAG_SCALE_LOW_START;
            break;
            
        case SCALE_LOW_TAG:
            c = readTagChar(SCALE_LOW_START);
            break;
            
        case SCALE_LOW_START:
            c = '"';
            m_nextChar = m_currentInput->getScaleLow();
            if ((NULL == m_nextChar) || ('\0' == *m_nextChar)) {
                m_nextState = SCALE_LOW_END;
            } else {
                m_nextState = SCALE_LOW;
            }
            break;
            
        case SCALE_LOW:
            c = readValueChar(SCALE_LOW_END);
            break;
            
        case SCALE_LOW_END:
            c = '"';
            m_nextState = SCALE_HIGH_TAG;
            m_nextChar = TAG_SCALE_HIGH_START;
            break;
            
        case SCALE_HIGH_TAG:
            c = readTagChar(SCALE_HIGH_START);
            break;
            
        case SCALE_HIGH_START:
            c = '"';
            m_nextChar = m_currentInput->getScaleHigh();
            if ((NULL == m_nextChar) || ('\0' == *m_nextChar)) {
                m_nextState = SCALE_HIGH_END;
            } else {
                m_nextState = SCALE_HIGH;
            }
            break;
            
        case SCALE_HIGH:
            c = readValueChar(SCALE_HIGH_END);
            break;
            
        case SCALE_HIGH_END:
            c = '"';
            m_nextState = DATA_SET_END;
            break;
            
        case DATA_SET_END:
            c = '}';
            m_currentInput = m_currentInput->getNext();
            if (NULL != m_currentInput) {
                m_nextState = NEXT_INPUT;
            } else {
                m_nextState = INPUTS_END;
            }
            break;
            
        case NEXT_INPUT:
            c = ',';
            m_nextChar = m_currentInput->getName();
            m_nextState = SENSOR_NAME_START;
            break;
            
        case INPUTS_END:
            c = '}';
            m_nextState = END;
            break;
            
        case END:
        default:
            c = '\0';
    }
    
    return c;
}