Skip to content

Commit 5af1bfa

Browse files
committed
Moved get/put into the new PachubeClient class, and reworked Datastream to be a single class rather than a class hierarchy. Still needs support for getting String-type datastreams
1 parent b1ccc81 commit 5af1bfa

File tree

5 files changed

+215
-110
lines changed

5 files changed

+215
-110
lines changed

Pachube.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11

22
#include <PachubeDatastream.h>
33
#include <PachubeFeed.h>
4+
#include <PachubeClient.h>
45

PachubeDatastream.cpp

Lines changed: 150 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,154 @@
11
#include <PachubeDatastream.h>
22

3+
Datastream::Datastream(String& aId, int aType)
4+
: _idType(DATASTREAM_STRING), _valueType(aType), _idString(aId)
5+
{
6+
}
7+
8+
Datastream::Datastream(char* aIdBuffer, int aIdBufferSize, int aType)
9+
: _idType(DATASTREAM_BUFFER), _valueType(aType), _idString(), _valueString()
10+
{
11+
_idBuffer._buffer = aIdBuffer;
12+
_idBuffer._bufferSize = aIdBufferSize;
13+
}
14+
15+
Datastream::Datastream(char* aIdBuffer, int aIdBufferSize, int aType, char* aValueBuffer, int aValueBufferSize)
16+
: _idType(DATASTREAM_BUFFER), _valueType(aType)
17+
{
18+
_idBuffer._buffer = aIdBuffer;
19+
_idBuffer._bufferSize = aIdBufferSize;
20+
_value._valueBuffer._buffer = aValueBuffer;
21+
_value._valueBuffer._bufferSize = aValueBufferSize;
22+
}
23+
24+
int Datastream::updateValue(Stream& aStream)
25+
{
26+
switch (_valueType)
27+
{
28+
case DATASTREAM_INT:
29+
_value._valueInt = aStream.parseInt();
30+
break;
31+
case DATASTREAM_FLOAT:
32+
_value._valueFloat = aStream.parseFloat();
33+
break;
34+
case DATASTREAM_BUFFER:
35+
{
36+
int len = aStream.readBytesUntil('\n', _value._valueBuffer._buffer, _value._valueBuffer._bufferSize);
37+
_value._valueBuffer._buffer[len] = '\0';
38+
}
39+
break;
40+
case DATASTREAM_STRING:
41+
// FIXME Implement String methods for Stream?
42+
break;
43+
};
44+
}
45+
46+
void Datastream::setInt(int aValue)
47+
{
48+
if (_valueType == DATASTREAM_INT)
49+
{
50+
_value._valueInt = aValue;
51+
}
52+
}
53+
54+
void Datastream::setFloat(float aValue)
55+
{
56+
if (_valueType == DATASTREAM_FLOAT)
57+
{
58+
_value._valueFloat = aValue;
59+
}
60+
}
61+
62+
void Datastream::setString(String& aValue)
63+
{
64+
if (_valueType == DATASTREAM_STRING)
65+
{
66+
_valueString = aValue;
67+
}
68+
}
69+
70+
void Datastream::setBuffer(const char* aBuffer)
71+
{
72+
if (_valueType == DATASTREAM_BUFFER)
73+
{
74+
strncpy(_value._valueBuffer._buffer, aBuffer, _value._valueBuffer._bufferSize);
75+
}
76+
}
77+
78+
int Datastream::getInt()
79+
{
80+
if (_valueType == DATASTREAM_INT)
81+
{
82+
return _value._valueInt;
83+
}
84+
else
85+
{
86+
return 0;
87+
}
88+
}
89+
90+
float Datastream::getFloat()
91+
{
92+
if (_valueType == DATASTREAM_FLOAT)
93+
{
94+
return _value._valueFloat;
95+
}
96+
else
97+
{
98+
return 0.0;
99+
}
100+
}
101+
102+
String& Datastream::getString()
103+
{
104+
return _valueString;
105+
}
106+
107+
char* Datastream::getBuffer()
108+
{
109+
if (_valueType == DATASTREAM_BUFFER)
110+
{
111+
return _value._valueBuffer._buffer;
112+
}
113+
else
114+
{
115+
return NULL;
116+
}
117+
}
118+
119+
size_t Datastream::printTo(Print& aPrint) const
120+
{
121+
size_t count =0;
122+
count += aPrint.print("{ \"id\" : \"");
123+
if (_idType == DATASTREAM_STRING)
124+
{
125+
count += aPrint.print(_idString);
126+
}
127+
else
128+
{
129+
count += aPrint.print(_idBuffer._buffer);
130+
}
131+
count += aPrint.print("\", \"current_value\" : \"");
132+
switch (_valueType)
133+
{
134+
case DATASTREAM_STRING:
135+
count += aPrint.print(_valueString);
136+
break;
137+
case DATASTREAM_BUFFER:
138+
count += aPrint.print(_value._valueBuffer._buffer);
139+
break;
140+
case DATASTREAM_INT:
141+
count += aPrint.print(_value._valueInt);
142+
break;
143+
case DATASTREAM_FLOAT:
144+
count += aPrint.print(_value._valueFloat);
145+
break;
146+
};
147+
count += aPrint.print("\" }");
148+
return count;
149+
}
150+
151+
#if 0
3152
DatastreamBufferInt::DatastreamBufferInt(char* aId, int aIdLength, int aValue)
4153
: _id(aId), _idLength(aIdLength), _value(aValue)
5154
{};
@@ -30,4 +179,4 @@ size_t DatastreamStringString::printTo(Print& aPrint) const
30179
count += aPrint.print("\"");
31180
return count;
32181
}
33-
182+
#endif

PachubeDatastream.h

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,50 @@
11
#ifndef PACHUBE_DATASTREAM_H
22
#define PACHUBE_DATASTREAM_H
33

4-
#include <Print.h>
4+
#include <Stream.h>
55
#include <Printable.h>
66

7+
#define DATASTREAM_STRING 0
8+
#define DATASTREAM_BUFFER 1
9+
#define DATASTREAM_INT 2
10+
#define DATASTREAM_FLOAT 3
11+
712
class Datastream : public Printable {
8-
public:
9-
enum DataType { eString, eInt, eFloat };
10-
virtual DataType getDataType() const =0;
11-
virtual void value(int aValue) =0;
12-
virtual void value(float aValue) =0;
13-
virtual void value(String aValue) =0;
14-
};
13+
friend class PachubeClient;
1514

16-
class DatastreamBufferInt : public Datastream {
15+
typedef struct {
16+
char* _buffer;
17+
int _bufferSize;
18+
} tBuffer;
1719
public:
18-
DatastreamBufferInt(char* aId, int aIdLength, int aValue);
19-
DatastreamBufferInt(char* aId, int aIdLength);
20-
virtual DataType getDataType() const { return eInt; };
21-
virtual size_t printTo(Print&) const;
22-
int value() const { return _value; };
23-
virtual void value(int aValue) { _value = aValue; };
24-
virtual void value(float aValue) { _value = aValue; };
25-
virtual void value(String aValue) { /* Can't do anything with a string */ };
2620

27-
char* _id;
28-
int _idLength;
29-
int _value;
30-
};
31-
32-
class DatastreamStringString : public Datastream {
33-
public:
34-
DatastreamStringString(String aId, String aValue);
35-
DatastreamStringString();
36-
virtual DataType getDataType() const { return eString; };
21+
Datastream(String& aId, int aType);
22+
Datastream(char* aIdBuffer, int aIdBufferLength, int aType);
23+
Datastream(char* aIdBuffer, int aIdBufferLength, int aType, char* aValueBuffer, int aValueBufferLength);
24+
int updateValue(Stream& aStream);
25+
void setInt(int aValue);
26+
void setFloat(float aValue);
27+
void setString(String& aValue);
28+
void setBuffer(const char* aValue);
29+
String& getString();
30+
int getInt();
31+
float getFloat();
32+
char* getBuffer();
3733
virtual size_t printTo(Print&) const;
38-
String value() const { return _value; };
39-
virtual void value(int aValue) { _value = ""; _value += aValue; };
40-
virtual void value(float aValue) { /* can't convert from a float */ };
41-
virtual void value(String aValue) { _value = aValue; };
42-
43-
String _id;
44-
String _value;
34+
protected:
35+
int idLength() { return (_idType == DATASTREAM_STRING ? _idString.length() : strlen(_idBuffer._buffer)); };
36+
char idChar(int idx) { return (_idType == DATASTREAM_STRING ? _idString[idx] : (idx > strlen(_idBuffer._buffer) ? '\0' : _idBuffer._buffer[idx])); };
37+
38+
int _idType;
39+
String _idString;
40+
tBuffer _idBuffer;
41+
int _valueType;
42+
String _valueString;
43+
union {
44+
tBuffer _valueBuffer;
45+
int _valueInt;
46+
float _valueFloat;
47+
} _value;
4548
};
4649

4750
#endif

PachubeFeed.cpp

Lines changed: 18 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,33 @@
11
#include <Pachube.h>
2-
#include <HttpClient.h>
3-
#include <CountingStream.h>
42

5-
PachubeFeed::PachubeFeed(char* aID, Client& aClient, Datastream* aDatastreams, int aDatastreamsCount)
6-
: /*_id(aID),*/ _client(aClient), _datastreams(aDatastreams), _datastreamsCount(aDatastreamsCount)
3+
PachubeFeed::PachubeFeed(unsigned long aID, Datastream* aDatastreams, int aDatastreamsCount)
4+
: _id(aID), _datastreams(aDatastreams), _datastreamsCount(aDatastreamsCount)
75
{
8-
strcpy(_id, aID);
6+
//strcpy(_id, aID);
97
}
108

11-
int PachubeFeed::put(const char* aApiKey)
9+
size_t PachubeFeed::printTo(Print& aPrint) const
1210
{
13-
HttpClient http(_client);
14-
char path[30];
15-
strcpy(path, "/v2/feeds/");
16-
strcat(path, _id);
17-
strcat(path, ".json");
18-
19-
http.beginRequest();
20-
int ret = http.put("api.pachube.com", path);
21-
if (ret == 0)
11+
int len = 0;
12+
len += aPrint.println("{");
13+
len += aPrint.println("\"version\":\"1.0.0\",");
14+
len += aPrint.println("\"datastreams\" : [");
15+
for (int j =0; j < _datastreamsCount; j++)
2216
{
23-
http.sendHeader("X-PachubeApiKey", aApiKey);
24-
25-
CountingStream countingStream; // Used to work out how long that data will be
26-
for (int i =kCalculateDataLength; i <= kSendData; i++)
17+
len += aPrint.print(_datastreams[j]);
18+
if (j == _datastreamsCount-1)
2719
{
28-
Print* s;
29-
int len =0;
30-
if (i == kCalculateDataLength)
31-
{
32-
s = &countingStream;
33-
}
34-
else
35-
{
36-
s = &http;
37-
}
38-
len += s->println("{");
39-
len += s->println("\"version\":\"1.0.0\",");
40-
len += s->println("\"datastreams\" : [");
41-
for (int j =0; j < _datastreamsCount; j++)
42-
{
43-
len += s->print("{ ");
44-
len += s->print(_datastreams[j]);
45-
len += s->print(" }");
46-
if (j == _datastreamsCount-1)
47-
{
48-
// Last time through
49-
len += s->println();
50-
}
51-
else
52-
{
53-
len += s->println(",");
54-
}
55-
}
56-
len += s->println("]");
57-
len += s->println("}");
58-
if (i == kCalculateDataLength)
59-
{
60-
// We now know how long the data will be...
61-
http.sendHeader("Content-Length", len);
62-
}
20+
// Last time through
21+
len += aPrint.println();
6322
}
64-
// Now we're done sending the request
65-
http.endRequest();
66-
67-
ret = http.responseStatusCode();
68-
if ((ret < 200) || (ret > 299))
23+
else
6924
{
70-
// It wasn't a successful response, ensure it's -ve so the error is easy to spot
71-
if (ret > 0)
72-
{
73-
ret = ret * -1;
74-
}
25+
len += aPrint.println(",");
7526
}
76-
http.stop();
7727
}
78-
return ret;
28+
len += aPrint.println("]");
29+
len += aPrint.println("}");
30+
return len;
7931
}
8032

8133

PachubeFeed.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@
44

55
#include <Client.h>
66
#include <PachubeDatastream.h>
7+
#include <Printable.h>
78

8-
class PachubeFeed
9+
class PachubeFeed : public Printable
910
{
1011
public:
11-
PachubeFeed(char* aID, Client& aClient, Datastream* aDatastreams, int aDatastreamsCount);
12+
PachubeFeed(unsigned long aID, Datastream* aDatastreams, int aDatastreamsCount);
1213

13-
int put(const char* aApiKey);
14+
virtual size_t printTo(Print&) const;
15+
unsigned long id() { return _id; };
16+
int size() { return _datastreamsCount; };
17+
Datastream& operator[] (unsigned i) { return _datastreams[i]; };
1418
protected:
15-
static const int kCalculateDataLength =0;
16-
static const int kSendData =1;
17-
18-
//unsigned long _id;
19+
unsigned long _id;
1920
// FIXME Need to pick right sizes, not have magic numbers here
20-
char _id[15];
21-
Client& _client;
21+
//char _id[15];
2222
Datastream* _datastreams;
2323
int _datastreamsCount;
2424
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy