-
Notifications
You must be signed in to change notification settings - Fork 2
/
Arduino_Github_Printer.ino
363 lines (298 loc) · 10.2 KB
/
Arduino_Github_Printer.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
This code connects to a Wifi network and
prints out the comments and issues
of a certain repository from github.com.
It reads github's api as a string and parse it using
the Json library.
Circuit:
Arduino zero
WiFi 101 shield attached
*/
#include<WiFiSSLClient.h>
#include <SPI.h>
#include <WiFi101.h>
#include <ArduinoJson.h>
#include "Adafruit_Thermal.h"
#include "HeaderNew.h"
#include "HeaderClosed.h"
#include "HeaderComment.h"
#include "footer.h"
#include "div3.h"
#include "div4.h"
Adafruit_Thermal printer(&Serial1);
#define TX_PIN 6 // Arduino transmit
#define RX_PIN 5 // Arduino receive
const char* ssid = "My-Network";
const char* password = "My-Password";
const char* host = "api.github.com"; //this address is given by Github itself
const int httpsPort = 443;
WiFiSSLClient client;
String Old_id;
String IssueCommentEvent = "IssueCommentEvent";
String IssueEvent = "IssuesEvent";
String closed = "closed";
String line = "";
String line2 = "";
int counter = 0;
int a = 0;
void setup() { ///////////////////////////////////////////CONNECTING TO INTERNTET/////////////////////////////////////
pinMode(7, OUTPUT); digitalWrite(7, LOW);
Serial1.begin(9600);
Serial.begin(9600);
// while (!Serial);
Serial.println();
Serial.print("connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// Use WiFiClientSecure class to create TLS connection
Serial.print("connecting to ");
Serial.println(host);
if (!client.connect(host, httpsPort)) {
Serial.println("connection failed");
return;
}}
void httpRequest() { ////////////////////////////////////// HTTP CONNECTION //////////////////////////////////////////
if (!client.connect(host, httpsPort)) { //Connect to github
// Serial.println("connection failed");
return;
}
String url = "/orgs/casajasmina/events?page=1&per_page=1"; // /orgs/myRepository/events
Serial.print("requesting URL: "); // " ?page=1&per_page=1 " add this to get events
Serial.println(url); // one by one avoiding the filling of SRAM memory
client.print(String("GET ") + url + " HTTP/1.1\r\n" + //send an HTTP request
"Host: " + host + "\r\n" +
"User-Agent: BuildFailureDetectorESP8266\r\n\r\n");
// "Connection: close\r\n\r\n");
Serial.println("request sent");
}
void loop() { ///////////////////////////////////////// LOOP ///////////////////////////////////////
int q = 0;
DynamicJsonBuffer jsonBuffer; //Define the space in the memory for the Json parsing as dynamic since you don't know its dimension
httpRequest();
String line = ""; // line is the string where the github response will be saved
while (client.connected()) {
line = client.readStringUntil('\n');
if (line == "\r") {
Serial.println("headers received");
break;
}
}
line = client.readStringUntil('\n');
JsonArray& root = jsonBuffer.parseArray(line);
if (!root.success()) {
Serial.println("parseArray() failed");
}
String id = root[0]["id"]; //Compare id to not print old events
String type = root[0]["type"];
if (id.equals(Old_id)) {
Serial.println("There are no new events");
}
else {
printer.begin(200);
if (type.equals(IssueCommentEvent)) {
IssueComment(root);
Serial.println("closing connection");
Old_id = id;
}
else if (type.equals(IssueEvent)) {
NewIssue(root);
Serial.println("closing connection");
Old_id = id;
}
printer.sleep(); // Tell printer to sleep
delay(3000L); // Sleep for 3 seconds
printer.wake(); // MUST wake() before printing again, even if reset
printer.setDefault(); // Restore printer to defaults
}
line.remove(0);
client.flush();
client.stop();
delay(60 * 1000); //github allows you to make only 60 request per hour
}
void IssueComment(JsonArray & root1) { //////////////////////////////////////// COMMENT-EVENT ////////////////////////////////////////
String repo = root1[0]["repo"]["name"];
String title = root1[0]["payload"]["issue"]["title"];
String login = root1[0]["actor"]["login"];
String body = root1[0]["payload"]["comment"]["body"];
String data = root1[0]["payload"]["issue"]["created_at"];
printer.setLineHeight(30);
PrinterWrap(title);
printer.printBitmap(HeaderComment_width, HeaderComment_height, HeaderComment_data);
PrinterWrapM(body);
printer.printBitmap(div4_width, div4_height, div4_data);
printer.setSize('S');
printer.justify('C');
printer.boldOn();
printer.println(repo);
printer.boldOff();
printer.println(login);
printer.println(data.substring(0, 10));
String label = root1[0]["payload"]["issue"]["labels"][0]["name"];
if (label.equals("")) {
printer.printBitmap(footer_width, footer_height, footer_data);
}
else {
printer.printBitmap(div3_width, div3_height, div3_data);
while (true) {
String label2 = root1[0]["payload"]["issue"]["labels"][counter]["name"];
if (label2.equals("")) {
break;
}
printer.inverseOn();
printer.println(" " + label2 + " ");
printer.inverseOff();
counter++;
}
}
printer.println("");
}
void NewIssue(JsonArray & root1) { ////////////////////////////////////////// NEW ISSUE //////////////////////////////////////
String action = root1[0]["payload"]["action"];
printer.setLineHeight(30);
String repo = root1[0]["repo"]["name"];
String title = root1[0]["payload"]["issue"]["title"];
String login = root1[0]["actor"]["login"];
String body = root1[0]["payload"]["issue"]["body"];
String data = root1[0]["payload"]["issue"]["created_at"];
String data2 = root1[0]["payload"]["issue"]["closed_at"];
PrinterWrap(title);
if (action.equals("closed")) {
printer.printBitmap(HeaderClosed_width, HeaderClosed_height, HeaderClosed_data);
}
else {
printer.printBitmap(HeaderNew_width, HeaderNew_height, HeaderNew_data);
printer.setLineHeight(30);
if (!body.equals("")) {
PrinterWrapM(body);
printer.printBitmap(div4_width, div4_height, div4_data);
}
}
printer.setSize('S');
printer.justify('C');
printer.boldOn();
printer.println(repo);
printer.boldOff();
printer.println(login);
if (action.equals("closed")) {
printer.println(data2.substring(0, 10));}
else{
printer.println(data.substring(0, 10));}
String label = root1[0]["payload"]["issue"]["labels"][0]["name"];
if (label.equals(" ")) {
printer.printBitmap(footer_width, footer_height, footer_data);
}
else {
printer.printBitmap(div3_width, div3_height, div3_data);
printer.inverseOn();
printer.println(" " + label + " ");
printer.inverseOff();
}
printer.println("");
printer.println("");
printer.println("");
printer.println("");
}
void PrinterWrap(String StringTwo) { //////////This method is used to get the right words layout using L size//////////
int spacezero = 0;
int lastspace = StringTwo.lastIndexOf(' ');
int nextspace = 0;
int firstspace = StringTwo.indexOf(' ');
for (int b = 0; b <= StringTwo.length(); b++) {
nextspace = StringTwo.indexOf(' ', firstspace + b );
if (nextspace - spacezero > 14) {
int previous = StringTwo.indexOf(' ', firstspace + (b - 1) );
if (spacezero == 0) {
printer.setSize('L');
printer.justify('L');
printer.println(StringTwo.substring(spacezero, previous));
}
if (spacezero != 0) {
printer.setSize('L');
printer.justify('L');
printer.println(StringTwo.substring(spacezero + 1, previous));
}
spacezero = previous;
}
if (nextspace - spacezero == 15) {
if (spacezero == 0) {
printer.setSize('L');
printer.justify('L');
printer.println(StringTwo.substring(spacezero, nextspace));
}
if (spacezero != 0) {
printer.setSize('L');
printer.justify('L');
printer.println(StringTwo.substring(spacezero + 1, nextspace));
}
spacezero = nextspace;
}
if (b == StringTwo.length()) {
if (StringTwo.charAt(spacezero) == ' ') {
printer.setSize('L');
printer.justify('L');
printer.println(StringTwo.substring(spacezero + 1));
}
else {
printer.setSize('L');
printer.justify('L');
printer.println(StringTwo.substring(spacezero));
}
}
}
}
void PrinterWrapM(String StringTwo) { //////////This method is used to get the right words layout using M size//////////
int spacezero = 0;
int lastspace = StringTwo.lastIndexOf(' ');
int nextspace = 0;
int firstspace = StringTwo.indexOf(' ');
for (int b = 0; b <= StringTwo.length(); b++) {
nextspace = StringTwo.indexOf(' ', firstspace + b );
if (nextspace - spacezero > 30) {
int previous = StringTwo.indexOf(' ', firstspace + (b - 1) );
if (spacezero == 0) {
printer.setSize('M');
printer.justify('L');
printer.println(StringTwo.substring(spacezero, previous));
}
if (spacezero != 0) {
printer.setSize('M');
printer.justify('L');
printer.println(StringTwo.substring(spacezero + 1, previous));
}
spacezero = previous;
}
if (nextspace - spacezero == 30) {
if (spacezero == 0) {
printer.setSize('M');
printer.justify('L');
printer.println(StringTwo.substring(spacezero, nextspace));
}
if (spacezero != 0) {
printer.setSize('M');
printer.justify('L');
printer.println(StringTwo.substring(spacezero + 1, nextspace));
}
spacezero = nextspace;
}
if (b == StringTwo.length()) {
if (StringTwo.charAt(spacezero) == ' ') {
printer.setSize('M');
printer.justify('L');
printer.println(StringTwo.substring(spacezero + 1));
}
else {
printer.setSize('M');
printer.justify('L');
printer.println(StringTwo.substring(spacezero));
}
}
}
}