1 #include <XivelyDatastream.h>
2 // FIXME Only needed until readStringUntil is available in Stream
5 XivelyDatastream::XivelyDatastream(String& aId, int aType)
6 : _idType(DATASTREAM_STRING), _valueType(aType), _idString(aId)
10 XivelyDatastream::XivelyDatastream(char* aIdBuffer, int aIdBufferSize, int aType)
11 : _idType(DATASTREAM_BUFFER), _valueType(aType), _idString(), _valueString()
13 _idBuffer._buffer = aIdBuffer;
14 _idBuffer._bufferSize = aIdBufferSize;
17 XivelyDatastream::XivelyDatastream(char* aIdBuffer, int aIdBufferSize, int aType, char* aValueBuffer, int aValueBufferSize)
18 : _idType(DATASTREAM_BUFFER), _valueType(aType)
20 _idBuffer._buffer = aIdBuffer;
21 _idBuffer._bufferSize = aIdBufferSize;
22 _value._valueBuffer._buffer = aValueBuffer;
23 _value._valueBuffer._bufferSize = aValueBufferSize;
26 int XivelyDatastream::updateValue(Stream& aStream)
31 _value._valueInt = aStream.parseInt();
33 case DATASTREAM_FLOAT:
34 _value._valueFloat = aStream.parseFloat();
36 case DATASTREAM_BUFFER:
38 int len = aStream.readBytesUntil('\n', _value._valueBuffer._buffer, _value._valueBuffer._bufferSize);
39 _value._valueBuffer._buffer[len] = '\0';
42 case DATASTREAM_STRING:
43 // FIXME Change this to use readStringUntil once that's in the core
44 // FIMXE and remove the timedRead method in here then too
46 int c = timedRead(aStream);
47 while (c >= 0 && c != '\n')
49 _valueString += (char)c;
50 c = timedRead(aStream);
56 int XivelyDatastream::timedRead(Stream& aStream)
59 long _startMillis = millis();
63 } while(millis() - _startMillis < 10000UL);
64 return -1; // -1 indicates timeout
68 void XivelyDatastream::setInt(int aValue)
70 if (_valueType == DATASTREAM_INT)
72 _value._valueInt = aValue;
76 void XivelyDatastream::setFloat(float aValue)
78 if (_valueType == DATASTREAM_FLOAT)
80 _value._valueFloat = aValue;
84 void XivelyDatastream::setString(String& aValue)
86 if (_valueType == DATASTREAM_STRING)
88 _valueString = aValue;
92 void XivelyDatastream::setBuffer(const char* aBuffer)
94 if (_valueType == DATASTREAM_BUFFER)
96 strncpy(_value._valueBuffer._buffer, aBuffer, _value._valueBuffer._bufferSize);
100 int XivelyDatastream::getInt()
102 if (_valueType == DATASTREAM_INT)
104 return _value._valueInt;
112 float XivelyDatastream::getFloat()
114 if (_valueType == DATASTREAM_FLOAT)
116 return _value._valueFloat;
124 String& XivelyDatastream::getString()
129 char* XivelyDatastream::getBuffer()
131 if (_valueType == DATASTREAM_BUFFER)
133 return _value._valueBuffer._buffer;
141 size_t XivelyDatastream::printTo(Print& aPrint) const
144 count += aPrint.print("{ \"id\" : \"");
145 if (_idType == DATASTREAM_STRING)
147 count += aPrint.print(_idString);
151 count += aPrint.print(_idBuffer._buffer);
153 count += aPrint.print("\", \"current_value\" : \"");
156 case DATASTREAM_STRING:
157 count += aPrint.print(_valueString);
159 case DATASTREAM_BUFFER:
160 count += aPrint.print(_value._valueBuffer._buffer);
163 count += aPrint.print(_value._valueInt);
165 case DATASTREAM_FLOAT:
166 count += aPrint.print(_value._valueFloat);
169 count += aPrint.print("\" }");
174 DatastreamBufferInt::DatastreamBufferInt(char* aId, int aIdLength, int aValue)
175 : _id(aId), _idLength(aIdLength), _value(aValue)
178 size_t DatastreamBufferInt::printTo(Print& aPrint) const
181 count += aPrint.print("\"id\" : \"");
182 count += aPrint.print(_id);
183 count += aPrint.print("\", \"current_value\" : \"");
184 count += aPrint.print(_value);
185 count += aPrint.print("\"");
189 DatastreamStringString::DatastreamStringString(String aId, String aValue)
190 : _id(aId), _value(aValue)
193 size_t DatastreamStringString::printTo(Print& aPrint) const
195 // Output the datastream in JSON
197 count += aPrint.print("\"id\" : \"");
198 count += aPrint.print(_id);
199 count += aPrint.print("\", \"current_value\" : \"");
200 count += aPrint.print(_value);
201 count += aPrint.print("\"");