Skip to content

Commit 441998b

Browse files
committed
block creation working
1 parent c675850 commit 441998b

23 files changed

+101
-20
lines changed

src/AdafruitIO.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class AdafruitIO {
3232

3333
friend class AdafruitIO_Feed;
3434
friend class AdafruitIO_Dashboard;
35+
friend class AdafruitIO_Block;
3536

3637
public:
3738
AdafruitIO(const char *user, const char *key);

src/AdafruitIO_Dashboard.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ bool AdafruitIO_Dashboard::create()
5959
return status == 201;
6060
}
6161

62+
const char* AdafruitIO_Dashboard::user() {
63+
return _io->_username;
64+
}
65+
66+
AdafruitIO* AdafruitIO_Dashboard::io() {
67+
return _io;
68+
}
69+
6270
ToggleBlock* AdafruitIO_Dashboard::addToggleBlock(AdafruitIO_Feed *feed)
6371
{
6472
return new ToggleBlock(this, feed);

src/AdafruitIO_Dashboard.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ class AdafruitIO_Dashboard {
3535
AdafruitIO_Dashboard(AdafruitIO *io, const char *name);
3636
~AdafruitIO_Dashboard();
3737

38-
const char *name;
38+
const char* name;
39+
const char* user();
40+
41+
AdafruitIO* io();
3942

4043
bool exists();
4144
bool create();

src/blocks/AdafruitIO_Block.cpp

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
// All text above must be included in any redistribution.
1111
//
1212
#include "AdafruitIO_Block.h"
13+
#include "AdafruitIO.h"
14+
#include "AdafruitIO_Dashboard.h"
1315

1416
AdafruitIO_Block::AdafruitIO_Block(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f)
1517
{
@@ -25,7 +27,51 @@ String AdafruitIO_Block::properties()
2527
return props;
2628
}
2729

30+
const char* AdafruitIO_Block::type()
31+
{
32+
return _visual_type;
33+
}
34+
2835
bool AdafruitIO_Block::save()
2936
{
30-
Serial.println(properties());
37+
HttpClient *http = _dashboard->io()->_http;
38+
39+
String url = "/api/v2/";
40+
url += _dashboard->user();
41+
url += "/dashboards/";
42+
url += _dashboard->name;
43+
url += "/blocks";
44+
45+
String user = _dashboard->user();
46+
user.replace("_", "-");
47+
48+
String block_feeds = "[{\"group_id\": \"";
49+
block_feeds += user;
50+
block_feeds += "\",\"feed_id\":\"";
51+
block_feeds += _feed->name;
52+
block_feeds += "\"}]";
53+
54+
String body = "{\"visual_type\":\"";
55+
body += type();
56+
body += "\",\"properties\":";
57+
body += properties();
58+
body += "\,\"size_x\":\"";
59+
body += width;
60+
body += "\",\"size_y\":\"";
61+
body += height;
62+
body += "\",\"block_feeds\":";
63+
body += block_feeds;
64+
body += "}";
65+
66+
http->startRequest(url.c_str(), HTTP_METHOD_POST);
67+
http->sendHeader(HTTP_HEADER_CONTENT_TYPE, "application/json");
68+
http->sendHeader(HTTP_HEADER_CONTENT_LENGTH, body.length());
69+
http->sendHeader("X-AIO-Key", _dashboard->io()->_key);
70+
http->endRequest();
71+
http->write((const byte*)body.c_str(), body.length());
72+
73+
int status = http->responseStatusCode();
74+
http->responseBody(); // needs to be read even if not used
75+
76+
return status == 200;
3177
}

src/blocks/AdafruitIO_Block.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,18 @@ class AdafruitIO_Block {
2424
AdafruitIO_Block(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f);
2525
~AdafruitIO_Block();
2626

27+
int width = 2;
28+
int height = 2;
29+
2730
virtual String properties();
31+
virtual const char* type();
2832

2933
bool save();
3034

3135
private:
3236
AdafruitIO_Dashboard *_dashboard;
3337
AdafruitIO_Feed *_feed;
34-
const char *_visual_type = "block";
38+
const char *_visual_type;
3539

3640
};
3741

src/blocks/ChartBlock.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ ChartBlock::ChartBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f) : AdafruitIO
1818
yAxisLabel = "Y";
1919
yAxisMin = 0;
2020
yAxisMax = 100;
21+
_visual_type = "line_chart";
2122
}
2223

2324
ChartBlock::~ChartBlock(){}
@@ -27,13 +28,13 @@ String ChartBlock::properties()
2728

2829
String props = "{\"historyHours\":\"";
2930
props += historyHours;
30-
props += "\",\"xAxisLabel\":";
31+
props += "\",\"xAxisLabel\":\"";
3132
props += xAxisLabel;
32-
props += "\",\"yAxisLabel\":";
33+
props += "\",\"yAxisLabel\":\"";
3334
props += yAxisLabel;
34-
props += "\",\"yAxisMin\":";
35+
props += "\",\"yAxisMin\":\"";
3536
props += yAxisMin;
36-
props += "\",\"yAxisMax\":";
37+
props += "\",\"yAxisMax\":\"";
3738
props += yAxisMax;
3839
props += "\"}";
3940

src/blocks/ChartBlock.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class ChartBlock : public AdafruitIO_Block {
2020
ChartBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f);
2121
~ChartBlock();
2222

23+
const char* type() { return _visual_type; }
2324
int historyHours;
2425
const char *xAxisLabel;
2526
const char *yAxisLabel;
@@ -29,7 +30,7 @@ class ChartBlock : public AdafruitIO_Block {
2930
String properties();
3031

3132
private:
32-
const char *_visual_type = "line_chart";
33+
const char *_visual_type;
3334

3435
};
3536

src/blocks/ColorBlock.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class ColorBlock : public AdafruitIO_Block {
2020
ColorBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f) : AdafruitIO_Block(d, f) {}
2121
~ColorBlock() {}
2222

23+
const char* type() { return _visual_type; }
24+
2325
private:
2426
const char *_visual_type = "color_picker";
2527

src/blocks/GaugeBlock.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ GaugeBlock::GaugeBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f) : AdafruitIO
1717
max = 100;
1818
width = "thin";
1919
label = "Value";
20+
_visual_type = "gauge";
2021
}
2122

2223
GaugeBlock::~GaugeBlock(){}

src/blocks/GaugeBlock.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ class GaugeBlock : public AdafruitIO_Block {
2626
const char *label;
2727

2828
String properties();
29+
const char* type() { return _visual_type; }
2930

3031
private:
31-
const char *_visual_type = "gauge";
32+
const char *_visual_type;
3233

3334
};
3435

0 commit comments

Comments
 (0)