1+ #if defined(ESP8266)
2+ #include < ESP8266WiFi.h>
3+ #include < Updater.h>
4+ #include < ESP8266HTTPClient.h>
5+ #else
6+ #include < WiFi.h>
7+ #include < Update.h>
8+ #include < HTTPClient.h>
9+ #endif // ESP8266
10+ #include < cJSON.h>
11+ #include < string>
12+ #include " leafminer.h"
13+ #include " autoupdate.h"
14+ #include " model/configuration.h"
15+ #include " utils/platform.h"
16+ #include " utils/log.h"
17+
18+ const std::string AUTOUPDATE_URL = " https://raw.githubusercontent.com/matteocrippa/leafminer/feature/v0.0.5/version.json" ; // "https://raw.githubusercontent.com/matteocrippa/leafminer/main/version.json";
19+ const char TAG_AUTOUPDATE[] = " autoupdate" ;
20+
21+ #if defined(ESP8266_D)
22+ std::string DEVICE = " esp8266" ;
23+ #elif defined(GEEKMAGICCLOCK_SMALLTV)
24+ std::string DEVICE = " geekmagic-smalltv" ;
25+ #elif defined(ESP32_WROOM)
26+ std::string DEVICE = " esp32" ;
27+ #elif defined(ESP32_S2)
28+ std::string DEVICE = " esp32-s2" ;
29+ #elif defined(ESP32_S3)
30+ std::string DEVICE = " esp32-s3" ;
31+ #elif defined(LILYGO_T_S3)
32+ std::string DEVICE = " lilygo-t-display-s3" ;
33+ #else
34+ std::string DEVICE = " unknown" ;
35+ #endif
36+
37+ extern Configuration configuration;
38+
39+ void autoupdate ()
40+ {
41+ l_info (TAG_AUTOUPDATE, " Connecting to %s..." , configuration.wifi_ssid .c_str ());
42+ while (WiFi.waitForConnectResult () != WL_CONNECTED)
43+ {
44+ WiFi.begin (configuration.wifi_ssid .c_str (), configuration.wifi_password .c_str ());
45+ delay (800 );
46+ }
47+
48+ HTTPClient http;
49+ http.begin (AUTOUPDATE_URL.c_str ());
50+ int httpCode = http.GET ();
51+ if (httpCode == HTTP_CODE_OK)
52+ {
53+ std::string payload = http.getString ().c_str ();
54+ l_debug (TAG_AUTOUPDATE, " payload: %s" , payload.c_str ());
55+
56+ cJSON *json = cJSON_Parse (payload.c_str ());
57+ cJSON *versionItem = cJSON_GetObjectItem (json, " current" );
58+
59+ // Check if the "version" field exists and is a string
60+ if (versionItem != NULL && cJSON_IsString (versionItem))
61+ {
62+ std::string version = cJSON_GetStringValue (versionItem);
63+ // Now you can safely use the 'version' string
64+ l_debug (TAG_AUTOUPDATE, " Version: %s" , version.c_str ());
65+ if (strcmp (version.c_str (), _VERSION) == 0 )
66+ {
67+ l_debug (TAG_AUTOUPDATE, " No Updates, Version: %s" , version.c_str ());
68+ return ;
69+ }
70+ else
71+ {
72+ l_debug (TAG_AUTOUPDATE, " New Version: %s" , version.c_str ());
73+ cJSON *url = cJSON_GetObjectItemCaseSensitive (json, " link" );
74+ cJSON *device = cJSON_GetObjectItemCaseSensitive (json, " devices" );
75+
76+ // Check if the device is supported
77+ bool isDeviceSupported = false ;
78+ for (int i = 0 ; i < cJSON_GetArraySize (device); i++)
79+ {
80+ std::string deviceItem = cJSON_GetStringValue (cJSON_GetArrayItem (device, i));
81+ if (deviceItem == DEVICE)
82+ {
83+ isDeviceSupported = true ;
84+ break ;
85+ }
86+ }
87+
88+ if (!isDeviceSupported)
89+ {
90+ l_error (TAG_AUTOUPDATE, " Device not supported: %s" , DEVICE.c_str ());
91+ return ;
92+ }
93+ else
94+ {
95+ l_debug (TAG_AUTOUPDATE, " Device supported: %s" , DEVICE.c_str ());
96+
97+ // Replace placeholders in the URL with actual values
98+ std::string downloadUrl = url->valuestring ;
99+ size_t versionPos = downloadUrl.find (" {{version}}" );
100+ if (versionPos != std::string::npos)
101+ {
102+ downloadUrl.replace (versionPos, strlen (" {{version}}" ), version);
103+ }
104+
105+ size_t devicePos = downloadUrl.find (" {{device}}" );
106+ if (devicePos != std::string::npos)
107+ {
108+ downloadUrl.replace (devicePos, strlen (" {{device}}" ), DEVICE);
109+ }
110+
111+ l_debug (TAG_AUTOUPDATE, " Downloading: %s" , downloadUrl.c_str ());
112+
113+ http.begin (downloadUrl.c_str ());
114+ http.setFollowRedirects (HTTPC_FORCE_FOLLOW_REDIRECTS);
115+
116+ int httpCode = http.GET ();
117+ if (httpCode >= 200 && httpCode <= 302 )
118+ {
119+ l_debug (TAG_AUTOUPDATE, " Downloaded: %d" , http.getSize ());
120+ if (Update.begin (http.getSize ()))
121+ {
122+ l_debug (TAG_AUTOUPDATE, " Begin Update" );
123+ size_t written = Update.writeStream (http.getStream ());
124+ if (written == http.getSize ())
125+ {
126+ l_debug (TAG_AUTOUPDATE, " Written: %d" , written);
127+ if (Update.end ())
128+ {
129+ l_debug (TAG_AUTOUPDATE, " Update Success: %d" , written);
130+ ESP.restart ();
131+ }
132+ else
133+ {
134+ l_error (TAG_AUTOUPDATE, " Update Failed" );
135+ }
136+ }
137+ else
138+ {
139+ l_error (TAG_AUTOUPDATE, " Written: %d" , written);
140+ }
141+ }
142+ else
143+ {
144+ l_error (TAG_AUTOUPDATE, " Begin Update Failed" );
145+ }
146+ }
147+ else
148+ {
149+ l_error (TAG_AUTOUPDATE, " httpCode: %d" , httpCode);
150+ }
151+ }
152+ }
153+ }
154+ else
155+ {
156+ l_error (TAG_AUTOUPDATE, " Invalid or missing 'version' field in JSON" );
157+ return ;
158+ }
159+ }
160+ else
161+ {
162+ l_error (TAG_AUTOUPDATE, " httpCode: %d" , httpCode);
163+ }
164+ }
0 commit comments