]> git.piffa.net Git - arduino/blob - books/pdummies/Libraries/xively/XivelyDatastream.cpp
kit 5
[arduino] / books / pdummies / Libraries / xively / XivelyDatastream.cpp
1 #include <XivelyDatastream.h>
2 // FIXME Only needed until readStringUntil is available in Stream
3 #include <Arduino.h>
4
5 XivelyDatastream::XivelyDatastream(String& aId, int aType)
6   : _idType(DATASTREAM_STRING), _valueType(aType), _idString(aId)
7 {
8 }
9
10 XivelyDatastream::XivelyDatastream(char* aIdBuffer, int aIdBufferSize, int aType)
11   : _idType(DATASTREAM_BUFFER), _valueType(aType), _idString(), _valueString()
12 {
13   _idBuffer._buffer = aIdBuffer;
14   _idBuffer._bufferSize = aIdBufferSize;
15 }
16
17 XivelyDatastream::XivelyDatastream(char* aIdBuffer, int aIdBufferSize, int aType, char* aValueBuffer, int aValueBufferSize)
18   : _idType(DATASTREAM_BUFFER), _valueType(aType)
19 {
20   _idBuffer._buffer = aIdBuffer;
21   _idBuffer._bufferSize = aIdBufferSize;
22   _value._valueBuffer._buffer = aValueBuffer;
23   _value._valueBuffer._bufferSize = aValueBufferSize;
24 }
25
26 int XivelyDatastream::updateValue(Stream& aStream)
27 {
28   switch (_valueType)
29   {
30   case DATASTREAM_INT:
31     _value._valueInt = aStream.parseInt();
32     break;
33   case DATASTREAM_FLOAT:
34     _value._valueFloat = aStream.parseFloat();
35     break;
36   case DATASTREAM_BUFFER:
37     {
38       int len = aStream.readBytesUntil('\n', _value._valueBuffer._buffer, _value._valueBuffer._bufferSize);
39       _value._valueBuffer._buffer[len] = '\0';
40     }
41     break;
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
45     _valueString = "";
46     int c = timedRead(aStream);
47     while (c >= 0 && c != '\n')
48     {
49       _valueString += (char)c;
50       c = timedRead(aStream);
51     }
52     break;
53   };
54 }
55
56 int XivelyDatastream::timedRead(Stream& aStream)
57 {
58   int c;
59   long _startMillis = millis();
60   do {
61     c = aStream.read();
62     if (c >= 0) return c;
63   } while(millis() - _startMillis < 10000UL);
64   return -1;     // -1 indicates timeout
65 }
66
67
68 void XivelyDatastream::setInt(int aValue)
69 {
70   if (_valueType == DATASTREAM_INT)
71   {
72     _value._valueInt = aValue;
73   }
74 }
75
76 void XivelyDatastream::setFloat(float aValue)
77 {
78   if (_valueType == DATASTREAM_FLOAT)
79   {
80     _value._valueFloat = aValue;
81   }
82 }
83
84 void XivelyDatastream::setString(String& aValue)
85 {
86   if (_valueType == DATASTREAM_STRING)
87   {
88     _valueString = aValue;
89   }
90 }
91
92 void XivelyDatastream::setBuffer(const char* aBuffer)
93 {
94   if (_valueType == DATASTREAM_BUFFER)
95   {
96     strncpy(_value._valueBuffer._buffer, aBuffer, _value._valueBuffer._bufferSize);
97   }
98 }
99
100 int XivelyDatastream::getInt()
101 {
102   if (_valueType == DATASTREAM_INT)
103   {
104     return _value._valueInt;
105   }
106   else
107   {
108     return 0;
109   }
110 }
111
112 float XivelyDatastream::getFloat()
113 {
114   if (_valueType == DATASTREAM_FLOAT)
115   {
116     return _value._valueFloat;
117   }
118   else
119   {
120     return 0.0;
121   }
122 }
123
124 String& XivelyDatastream::getString()
125 {
126   return _valueString;
127 }
128
129 char* XivelyDatastream::getBuffer()
130 {
131   if (_valueType == DATASTREAM_BUFFER)
132   {
133     return _value._valueBuffer._buffer;
134   }
135   else
136   {
137     return NULL;
138   }
139 }
140
141 size_t XivelyDatastream::printTo(Print& aPrint) const
142 {
143   size_t count =0;
144   count += aPrint.print("{ \"id\" : \"");
145   if (_idType == DATASTREAM_STRING)
146   {
147     count += aPrint.print(_idString);
148   }
149   else
150   {
151     count += aPrint.print(_idBuffer._buffer);
152   }
153   count += aPrint.print("\", \"current_value\" : \"");
154   switch (_valueType)
155   {
156   case DATASTREAM_STRING:
157     count += aPrint.print(_valueString);
158     break;
159   case DATASTREAM_BUFFER:
160     count += aPrint.print(_value._valueBuffer._buffer);
161     break;
162   case DATASTREAM_INT:
163     count += aPrint.print(_value._valueInt);
164     break;
165   case DATASTREAM_FLOAT:
166     count += aPrint.print(_value._valueFloat);
167     break;
168   };
169   count += aPrint.print("\" }");
170   return count;
171 }
172
173 #if 0
174 DatastreamBufferInt::DatastreamBufferInt(char* aId, int aIdLength, int aValue)
175   : _id(aId), _idLength(aIdLength), _value(aValue)
176 {};
177
178 size_t DatastreamBufferInt::printTo(Print& aPrint) const
179 {
180   size_t count =0;
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("\"");
186   return count;
187 }
188
189 DatastreamStringString::DatastreamStringString(String aId, String aValue)
190   : _id(aId), _value(aValue)
191 {};
192
193 size_t DatastreamStringString::printTo(Print& aPrint) const
194 {
195   // Output the datastream in JSON
196   size_t count =0;
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("\"");
202   return count;
203 }
204 #endif