Skip to content

Commit

Permalink
integration of releasae channels
Browse files Browse the repository at this point in the history
  • Loading branch information
ohAnd committed Feb 11, 2024
1 parent 2887456 commit 2affb02
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 36 deletions.
File renamed without changes.
101 changes: 90 additions & 11 deletions include/index_html.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,14 @@ const char INDEX_HTML[] PROGMEM = R"=====(
</div>
</div>
<hr>
<div style="display: grid;align-items: center;justify-content: center;width:100%;">
<div onclick="changeReleaseChannel(0)" id="relChanStable" class="updateChannel selected"
style="border-radius: 5px 0px 0px 5px;">stable</div>
<div onclick="changeReleaseChannel(1)" id="relChanSnapshot" class="updateChannel"
style="border-radius: 0px 5px 5px 0px;position:relative;top:-1.25em;left:50%;">snapshot</div>
<i style="font-size:x-small;">switch update channels (stable/ latest snapshot)</i>
</div>
<hr>
<div style="text-align: center;">
<!-- <input id="btnUpdateStart" class="btn" type="submit" name="doUpdate" value="Update starten"> -->
<b onclick="" id="btnUpdateStart" class="form-button btn">start update</b>
Expand Down Expand Up @@ -338,6 +346,8 @@ const char INDEX_HTML[] PROGMEM = R"=====(
let timerRemainingProgess = 0;
const waitTime = 31000;
let remainingTime = waitTime;
let timerInfoUpdate = 0;
let cacheInfoData = {};
$(document).ready(function () {
Expand All @@ -352,7 +362,7 @@ const char INDEX_HTML[] PROGMEM = R"=====(
getDataValues();
}, 5000);
window.setInterval(function () {
timerInfoUpdate = window.setInterval(function () {
getInfoValues();
}, 7500);
Expand Down Expand Up @@ -509,6 +519,9 @@ const char INDEX_HTML[] PROGMEM = R"=====(
$('#firmware').html("fw version: " + data.firmware.version);
if (data.firmware.selectedUpdateChannel == 0) { $("#relChanStable").addClass("selected"); $("#relChanSnapshot").removeClass("selected"); }
else { $("#relChanStable").removeClass("selected"); $("#relChanSnapshot").addClass("selected"); }
return true;
}
Expand Down Expand Up @@ -548,7 +561,7 @@ const char INDEX_HTML[] PROGMEM = R"=====(
$('#dtuHostIp').val(dtuData.dtuHostIp);
$('#dtuSsid').val(dtuData.dtuSsid);
$('#dtuPassword').val(dtuData.dtuPassword);
}
$('.passcheck').click(function () {
Expand Down Expand Up @@ -609,9 +622,9 @@ const char INDEX_HTML[] PROGMEM = R"=====(
}
function changeDtuData() {
var dtuHostIpSend= $('#dtuHostIp').val();
var dtuSsidSend= $('#dtuSsid').val();
var dtuPasswordSend= $('#dtuPassword').val();
var dtuHostIpSend = $('#dtuHostIp').val();
var dtuSsidSend = $('#dtuSsid').val();
var dtuPasswordSend = $('#dtuPassword').val();
var data = {};
data["dtuHostIpSend"] = dtuHostIpSend;
data["dtuSsidSend"] = dtuSsidSend;
Expand Down Expand Up @@ -647,13 +660,77 @@ const char INDEX_HTML[] PROGMEM = R"=====(
console.log("got from server - strResult.dtuSsid: " + strResult.dtuSsid + " - cmp with: " + dtuSsidSend);
console.log("got from server - strResult.dtuPassword: " + strResult.dtuHostIp + " - cmp with: " + dtuPasswordSend);
if(strResult.dtuHostIp == dtuHostIpSend && strResult.dtuSsid == dtuSsidSend && strResult.dtuPassword == dtuPasswordSend) {
if (strResult.dtuHostIp == dtuHostIpSend && strResult.dtuSsid == dtuSsidSend && strResult.dtuPassword == dtuPasswordSend) {
console.log("check saved data - OK");
alert("dtu Settings change\n__________________________________\n\nYour settings were successfully changed.\n\nPlease reboot!");
alert("dtu Settings change\n__________________________________\n\nYour settings were successfully changed.\n\nClient connection will be reconnected to the new IP.");
} else {
alert("dtu Settings change\n__________________________________\n\nSome error occured! Checking data from gateway are not as excpeted after sending to save.\n\nPlease try again!");
alert("dtu Settings change\n__________________________________\n\nSome error occured! Checking data from gateway are not as excpeted after sending to save.\n\nPlease try again!");
}

//$('#btnSaveDtuSettings').css('opacity', '0.3');
//$('#btnSaveDtuSettings').attr('onclick', "")
hide('#changeSettings');
return;
}
function changeReleaseChannel(channel) {
if (cacheInfoData.firmware.selectedUpdateChannel == channel) return;
cacheInfoData.firmware.versionServer = "reloading";
cacheInfoData.firmware.versiondateServer = "reloading";
cacheInfoData.firmware.selectedUpdateChannel = channel;
cacheInfoData.firmware.updateAvailable = 0;
getVersionData(cacheInfoData);
refreshInfo(cacheInfoData);
clearInterval(timerInfoUpdate);
timerInfoUpdate = window.setInterval(function () {
getInfoValues();
}, 7500);
var data = {};
data["releaseChannel"] = channel;
console.log("send to server: releaseChannel: " + channel);
const urlEncodedDataPairs = [];
// Turn the data object into an array of URL-encoded key/value pairs.
for (const [name, value] of Object.entries(data)) {
urlEncodedDataPairs.push(
`${encodeURIComponent(name)}=${encodeURIComponent(value)}`,
);
console.log("push: " + name);
}
// Combine the pairs into a single string and replace all %-encoded spaces to
// the '+' character; matches the behavior of browser form submissions.
const urlEncodedData = urlEncodedDataPairs.join("&").replace(/%20/g, "+");
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("POST", "/updateOTASettings", false); // false for synchronous request
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
// Finally, send our data.
xmlHttp.send(urlEncodedData);
try {
strResult = JSON.parse(xmlHttp.responseText);
console.log("got from server: " + strResult);
console.log("got from server - strResult.dtuHostIp: " + strResult.dtuHostIp + " - cmp with: " + dtuHostIpSend);
if (strResult.dtuHostIp == dtuHostIpSend && strResult.dtuSsid == dtuSsidSend && strResult.dtuPassword == dtuPasswordSend) {
console.log("check saved data - OK");
alert("dtu Settings change\n__________________________________\n\nYour settings were successfully changed.\n\nClient connection will be reconnected to the new IP.");
} else {
alert("dtu Settings change\n__________________________________\n\nSome error occured! Checking data from gateway are not as excpeted after sending to save.\n\nPlease try again!");
}
} catch (error) {
console.log("error at request change release channel: " + error);
}
//$('#btnSaveDtuSettings').css('opacity', '0.3');
//$('#btnSaveDtuSettings').attr('onclick', "")
Expand All @@ -672,10 +749,12 @@ const char INDEX_HTML[] PROGMEM = R"=====(
}
function getVersionData(data) {
if (data.firmware.selectedUpdateChannel == 1) { $('#firmwareVersionServer').html(data.firmware.versionServer); $('#builddateVersionServer').html(data.firmware.versiondateServer); }
else { $('#firmwareVersionServer').html(data.firmware.versionServerRelease); $('#builddateVersionServer').html(data.firmware.versiondateServerRelease); }
$('#firmwareVersion').html(data.firmware.version);
$('#builddateVersion').html(data.firmware.versiondate);
$('#firmwareVersionServer').html(data.firmware.versionServer);
$('#builddateVersionServer').html(data.firmware.versiondateServer);
if (data.firmware.updateAvailable == 1) {
$('#updateState').html("new update available");
$('#btnUpdateStart').css('opacity', '1.0');
Expand Down
25 changes: 21 additions & 4 deletions include/style_css.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ body {
color: #2196f3;
}

hr {
/* display: block;
height: 1px; */
/* border: 0; */
border-top: 1px solid #2196f3;
/* margin: 1em 0;
padding: 0; */
}

/* reload bar at the top */
.bar {
display: flex;
Expand Down Expand Up @@ -250,15 +259,16 @@ body {
font-size: 3.5vmin;
}

.selected {
background-color: #2196f3;
color: #FFF;
}

.popupHeader>.popupHeaderTabs>.selected {
border-left: 1px solid #2196f3;
border-top: 1px solid #2196f3;
border-right: 1px solid #2196f3;
border-bottom: 0px;
/* background-image: linear-gradient(#2196f3, #fff); */
background-color: #2196f3;
color: #FFF;

}

.popup>.popupContent {
Expand Down Expand Up @@ -599,6 +609,13 @@ td {
background: #3498db;
}

.updateChannel {
border: 1px solid #3498db;
text-align: center;
cursor: pointer;
width: 50%;
}

.passcheck {
cursor: pointer;
}
Expand Down
6 changes: 3 additions & 3 deletions include/version.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#define VERSION "1.0.0011"
#define BUILDTIME "11.02.2024 - 16:31:24"
#define BUILDTIMESTAMP "1707665484"
#define VERSION "1.1.3"
#define BUILDTIME "11.02.2024 - 21:27:23"
#define BUILDTIMESTAMP "1707683243"
8 changes: 4 additions & 4 deletions include/version.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"version": "1.0.0011",
"versiondate": "11.02.2024 - 16:31:24",
"linksnapshot": "https://github.com/ohAnd/dtuGateway/releases/download/snapshot/dtuGateway_snapshot_1.0.0011.bin",
"link": "https://github.com/ohAnd/dtuGateway//releases/latest/download/dtuGateway_release_1.0.0011.bin"
"version": "1.1.3",
"versiondate": "11.02.2024 - 21:27:23",
"linksnapshot": "https://github.com/ohAnd/dtuGateway/releases/download/snapshot/dtuGateway_snapshot_1.1.3.bin",
"link": "https://github.com/ohAnd/dtuGateway//releases/latest/download/dtuGateway_release_1.1.3.bin"
}
78 changes: 64 additions & 14 deletions src/dtuGateway.ino
Original file line number Diff line number Diff line change
Expand Up @@ -486,15 +486,39 @@ void handleUpdateDtuSettings()

server.send(200, "application/json", JSON);

delay(2000); // give time for the json response
delay(2000); // give time for the json response

// stopping connection to DTU and set right state - to force reconnect with new data
client.stop();
globalControls.dtuConnectState = DTU_STATE_OFFLINE;

Serial.println("handleUpdateDtuSettings - send JSON: " + String(JSON));
}

void handleUpdateOTASettings()
{
String releaseChannel = server.arg("releaseChannel"); // retrieve message from webserver
Serial.println("\nhandleUpdateOTASettings - got releaseChannel: " + releaseChannel);

userConfig.selectedUpdateChannel = releaseChannel.toInt();

saveConfigToEEPROM();
delay(500);

String JSON = "{";
JSON = JSON + "\"releaseChannel\": \"" + userConfig.selectedUpdateChannel + "\"";
JSON = JSON + "}";

server.send(200, "application/json", JSON);

// delay(2000); // give time for the json response

// trigger new update info with changed release channel
getUpdateInfo();

Serial.println("handleUpdateDtuSettings - send JSON: " + String(JSON));
}

// webserver port 80
void initializeWebServer()
{
Expand All @@ -512,6 +536,7 @@ void initializeWebServer()

server.on("/updateWifiSettings", handleUpdateWifiSettings);
server.on("/updateDtuSettings", handleUpdateDtuSettings);
server.on("/updateOTASettings", handleUpdateOTASettings);

// api GETs
server.on("/api/data", handleDataJson);
Expand All @@ -528,20 +553,24 @@ void initializeWebServer()
// ---> /updateRequest
void handleUpdateRequest()
{
String urlToBin = "";
if (userConfig.selectedUpdateChannel == 0) urlToBin = updateURLRelease;
else urlToBin = updateURL;

BearSSL::WiFiClientSecure updateclient;
updateclient.setInsecure();

int doubleSlashPos = updateURL.indexOf("//");
int firstSlashPos = updateURL.indexOf('/', doubleSlashPos + 2);
String host = updateURL.substring(doubleSlashPos + 2, firstSlashPos);
String url = updateURL.substring(firstSlashPos);
int doubleSlashPos = urlToBin.indexOf("//");
int firstSlashPos = urlToBin.indexOf('/', doubleSlashPos + 2);
String host = urlToBin.substring(doubleSlashPos + 2, firstSlashPos);
String url = urlToBin.substring(firstSlashPos);

Serial.print("connecting to ");
Serial.println(host);
Serial.print("with url: ");
Serial.println(url);

if (updateURL == "" || updateAvailable != true)
if (urlToBin == "" || updateAvailable != true)
{
Serial.println("[update] no url given or no update available");
return;
Expand All @@ -551,7 +580,7 @@ void handleUpdateRequest()
server.send(200, "application/json", "{\"update\": \"in_progress\"}");

Serial.println("[update] Update requested");
Serial.println("[update] try download from " + updateURL);
Serial.println("[update] try download from " + urlToBin);

// wait to seconds to load css on client side
Serial.println("[update] starting update");
Expand All @@ -564,7 +593,7 @@ void handleUpdateRequest()

// // ESPhttpUpdate.rebootOnUpdate(false); // remove automatic update
ESPhttpUpdate.setFollowRedirects(HTTPC_FORCE_FOLLOW_REDIRECTS);
t_httpUpdate_return ret = ESPhttpUpdate.update(updateclient, updateURL);
t_httpUpdate_return ret = ESPhttpUpdate.update(updateclient, urlToBin);

switch (ret)
{
Expand All @@ -584,14 +613,24 @@ void handleUpdateRequest()
// get the info about update from remote
boolean getUpdateInfo()
{
String versionUrl = "";
std::unique_ptr<BearSSL::WiFiClientSecure> secClient(new BearSSL::WiFiClientSecure);
secClient->setInsecure();

if (userConfig.selectedUpdateChannel == 0)
{
versionUrl = updateInfoWebPathRelease;
}
else
{
versionUrl = updateInfoWebPath;
}

// create an HTTPClient instance
HTTPClient https;

// Initializing an HTTPS communication using the secure client
if (https.begin(*secClient, updateInfoWebPath))
if (https.begin(*secClient, versionUrl))
{ // HTTPS
https.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS); // Enable automatic following of redirects
int httpCode = https.GET();
Expand Down Expand Up @@ -622,10 +661,21 @@ boolean getUpdateInfo()
}
else
{
versionServer = String(doc["version"]);
versiondateServer = String(doc["versiondate"]);
updateURL = String(doc["link"]);
updateAvailable = checkVersion(String(VERSION), versionServer);
if (userConfig.selectedUpdateChannel == 0)
{
versionServerRelease = String(doc["version"]);
versiondateServerRelease = String(doc["versiondate"]);
updateURLRelease = String(doc["link"]);
updateAvailable = checkVersion(String(VERSION), versionServerRelease);
}
else
{
versionServer = String(doc["version"]);
versiondateServer = String(doc["versiondate"]);
updateURL = String(doc["link"]);
updateAvailable = checkVersion(String(VERSION), versionServer);
}

server.sendHeader("Connection", "close");
server.send(200, "application/json", "{\"updateRequest\": \"done\"}");
}
Expand Down

0 comments on commit 2affb02

Please sign in to comment.