Skip to content

Commit 2448488

Browse files
author
wamisnet
committed
アナリティクス追加
1 parent a61066d commit 2448488

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

cores/esp32/nefry/Nefry.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@ BootMode
2929

3030
Adafruit_NeoPixel _NefryLEDNeo;
3131
Adafruit_DotStar _NefryLEDStar;
32+
bool connectAnFlg = false;
3233
//main
3334

3435
void Nefry_lib::nefry_init() {
3536
/* Display設定 */
37+
connectAnFlg = false;
3638
delay(10);
3739
NefryDisplay.begin();//logo表示
3840
if (boardId == 3) {
@@ -96,6 +98,9 @@ void Nefry_lib::nefry_loop() {
9698
if (_nefryWifiWait > 1000) {//WiFiに接続する間隔を10秒ごとに修正
9799
_nefryWifiWait = 0;
98100
NefryWiFi.run();
101+
if (connectAnFlg != true) {
102+
connectAnFlg = setAnalyticsData("connect");
103+
}
99104
}
100105
}
101106
}
@@ -459,4 +464,81 @@ bool Nefry_lib::getDisplayStatusEnabled() {
459464
return _displayStatusFlg;
460465
}
461466

467+
bool Nefry_lib::setAnalyticsData(String action) {
468+
bool state = false;
469+
if (WiFi.status() == WL_CONNECTED) {
470+
WiFiClient client;
471+
const int httpPort = 80;
472+
if (!client.connect("google-analytics.com", httpPort)) {
473+
//Serial.println("connection failed");
474+
return state;
475+
}
476+
477+
// We now create a URI for the request
478+
String url = "/collect?v=1&t=event&tid=UA-78080011-2&cid=NefryBT&ec=";
479+
url += "NefryBT";
480+
switch (boardId) {
481+
case 1:
482+
url += "r1";
483+
break;
484+
case 2:
485+
url += "r2";
486+
break;
487+
case 3:
488+
url += "r3";
489+
break;
490+
default:
491+
url += "error";
492+
break;
493+
}
494+
url += "&el=v";
495+
url += getVersion();
496+
url += "&ea=";
497+
url += action;
498+
499+
500+
//Serial.print("Requesting URL: ");
501+
//Serial.println(url);
502+
503+
// This will send the request to the server
504+
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
505+
"Host: google-analytics.com\r\n" +
506+
"Connection: close\r\n\r\n");
507+
unsigned long timeout = millis();
508+
while (client.available() == 0) {
509+
if (millis() - timeout > 5000) {
510+
//Serial.println(">>> Client Timeout !");
511+
client.stop();
512+
return state;
513+
}
514+
}
515+
516+
// Read all the lines of the reply from server and print them to Serial
517+
while (client.available()) {
518+
size_t len = client.available();
519+
if (len > 0) {
520+
String headerLine = client.readStringUntil('\n');
521+
headerLine.trim(); // remove \r
522+
//Serial.print(headerLine);
523+
log_d("[HTTP-Client][handleHeaderResponse] RX: '%s'", headerLine.c_str());
524+
525+
if (headerLine.startsWith("HTTP/1.")) {
526+
int _returnCode = headerLine.substring(9, headerLine.indexOf(' ', 9)).toInt();
527+
//Serial.printf("[HTTP] GET... code: %d\n", _returnCode);
528+
if (_returnCode == 200) {
529+
state = true;
530+
break;
531+
}
532+
else {
533+
state = false;
534+
break;
535+
}
536+
}
537+
}
538+
}
539+
client.stop();
540+
}
541+
return state;
542+
}
543+
462544
Nefry_lib Nefry;

cores/esp32/nefry/Nefry.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include "NefryDisplay.h"
1313
#include "NefryWeb.h"
1414
#include "NefryWebServer.h"
15+
#include "./inc/WiFi/src/WiFiClient.h"
16+
1517

1618
// Offset: W R G B
1719
#define NEO_RGB ((0 << 6) | (0 << 4) | (1 << 2) | (2))
@@ -160,6 +162,8 @@ class Nefry_lib
160162
_swPushingflg = false,
161163
_wifiEnableFlg = true,/* Wi-Fiの有効無効化 */
162164
_displayStatusFlg = true;/* ディスプレイの状態表示の有効無効化 */
165+
166+
bool setAnalyticsData(String action);
163167

164168
int
165169
_bootMode = -1, /* Boot状態を管理 -1:初期化中 0:起動中 1:通常起動 2:書き込みモード */

0 commit comments

Comments
 (0)