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
1416AdafruitIO_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+
2835bool 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}
0 commit comments