Skip to content

Commit

Permalink
improved version check - #2190
Browse files Browse the repository at this point in the history
  • Loading branch information
proddy committed Nov 9, 2024
1 parent 367260b commit b00f5dd
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 59 deletions.
2 changes: 1 addition & 1 deletion interface/src/api/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const alovaInstance = createAlova({
});

export const alovaInstanceGH = createAlova({
baseURL: 'https://api.github.com/repos/emsesp/EMS-ESP32/releases',
baseURL: process.env.NODE_ENV === 'development' ? '/gh' : 'https://api.github.com/repos/emsesp/EMS-ESP32/releases',
statesHook: ReactHook,
requestAdapter: xhrRequestAdapter()
});
33 changes: 12 additions & 21 deletions interface/src/app/settings/Version.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from 'react';
import { useEffect, useState } from 'react';
import { toast } from 'react-toastify';

import CancelIcon from '@mui/icons-material/Cancel';
Expand Down Expand Up @@ -36,7 +36,7 @@ const Version = () => {
const [upgradeAvailable, setUpgradeAvailable] = useState<boolean>(false);

const { send: sendCheckUpgrade } = useRequest(
(version: string) => callAction({ action: 'checkUpgrade', param: version }),
(versions: string) => callAction({ action: 'checkUpgrade', param: versions }),
{
immediate: false
}
Expand All @@ -54,27 +54,18 @@ const Version = () => {
}
);

// called immediately to get the latest version, on page load
const { data: latestVersion } = useRequest(getStableVersion, {
// uncomment next 2 lines for testing, uses https://github.com/emsesp/EMS-ESP32/releases/download/v3.6.5/EMS-ESP-3_6_5-ESP32-16MB+.bin
// immediate: false,
// initialData: '3.6.5'
}).onSuccess((event) => {
if (!useDev) {
void sendCheckUpgrade(event.data);
}
});
// called immediately to get the latest versions on page load
const { data: latestVersion } = useRequest(getStableVersion);
const { data: latestDevVersion } = useRequest(getDevVersion);

// called immediately to get the latest version, on page load, then check for upgrade (works for both dev and stable)
const { data: latestDevVersion } = useRequest(getDevVersion, {
// uncomment next 2 lines for testing, uses https://github.com/emsesp/EMS-ESP32/releases/download/latest/EMS-ESP-3_7_0-dev_31-ESP32-16MB+.bin
// immediate: false,
// initialData: '3.7.0-dev.32'
}).onSuccess((event) => {
if (useDev) {
void sendCheckUpgrade(event.data);
useEffect(() => {
if (latestVersion && latestDevVersion) {
console.log("Latest versions, stable: " + latestVersion + " dev: " + latestDevVersion);
sendCheckUpgrade(latestDevVersion + "," + latestVersion).catch((error: Error) => {
console.error("Failed to check upgrade:", error);
});
}
});
}, [latestVersion, latestDevVersion]);

const STABLE_URL = 'https://github.com/emsesp/EMS-ESP32/releases/download/';
const STABLE_RELNOTES_URL =
Expand Down
3 changes: 2 additions & 1 deletion interface/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export default defineConfig(({ command, mode }) => {
changeOrigin: true,
secure: false
},
'/rest': 'http://localhost:3080'
'/rest': 'http://localhost:3080',
'/gh': 'http://localhost:3080' // mock for GitHub API
}
}
};
Expand Down
41 changes: 30 additions & 11 deletions mock-api/rest_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,21 @@ const router = AutoRouter({

const REST_ENDPOINT_ROOT = '/rest/';
const API_ENDPOINT_ROOT = '/api/';
const GH_ENDPOINT_ROOT = '/gh/'; // for mock GitHub API for version checking

// HTTP HEADERS for msgpack
const headers = {
'Access-Control-Allow-Origin': '*',
'Content-type': 'application/msgpack'
};

// Versions - all without the 'v'
const THIS_VERSION = '3.7.0';
// const THIS_VERSION = '3.6.4'; // for testing
const LATEST_STABLE_VERSION = '3.7.0';
const LATEST_DEV_VERSION = '3.7.1-dev.4';
const VERSION_IS_UPGRADEABLE = true;

// GLOBAL VARIABLES
let countWifiScanPoll = 0; // wifi network scan
let countHardwarePoll = 0; // for during an upload
Expand Down Expand Up @@ -199,16 +207,18 @@ function custom_support() {
function check_upgrade(version: string) {
let data = {};
if (version) {
console.log('check upgrade from version', version);
const dev_version = version.split(',')[0];
const stable_version = version.split(',')[1];
console.log("latest dev version: " + dev_version + ", latest stable version: " + stable_version);
console.log('Version upgrade check from version ' + THIS_VERSION + ', upgradable: ' + VERSION_IS_UPGRADEABLE);
data = {
emsesp_version: VERSION,
// upgradeable: true
upgradeable: false
emsesp_version: THIS_VERSION,
upgradeable: VERSION_IS_UPGRADEABLE
};
} else {
console.log('requesting ems-esp version');
console.log('requesting ems-esp version ('+THIS_VERSION+')');
data = {
emsesp_version: VERSION
emsesp_version: THIS_VERSION
};
}
return data;
Expand Down Expand Up @@ -468,11 +478,8 @@ const VERIFY_AUTHORIZATION_ENDPOINT = REST_ENDPOINT_ROOT + 'verifyAuthorization'
const SIGN_IN_ENDPOINT = REST_ENDPOINT_ROOT + 'signIn';
const GENERATE_TOKEN_ENDPOINT = REST_ENDPOINT_ROOT + 'generateToken';

const VERSION = '3.7.0';
// const VERSION = '3.6.4';

let system_status = {
emsesp_version: VERSION,
emsesp_version: THIS_VERSION,
bus_status: 0,
// status: 2,
uptime: 77186,
Expand Down Expand Up @@ -573,7 +580,7 @@ const EMSESP_SYSTEM_INFO_ENDPOINT = API_ENDPOINT_ROOT + 'system/info';

const emsesp_info = {
System: {
version: VERSION,
version: THIS_VERSION,
uptime: '001+06:40:34.018',
'uptime (seconds)': 110434,
freemem: 131,
Expand Down Expand Up @@ -4878,6 +4885,18 @@ router
return status(404); // not found
});

// Mock GitHub API

router
.get(GH_ENDPOINT_ROOT + '/tags/latest', () => {
console.log('returning latest development version: ' + LATEST_DEV_VERSION);
return { name: 'v'+LATEST_DEV_VERSION };
})
.get(GH_ENDPOINT_ROOT + '/latest', () => {
console.log('returning latest stable version: ' + LATEST_STABLE_VERSION);
return { name: 'v'+LATEST_STABLE_VERSION };
});

export default router;

// use this with cloudflare workers instead
Expand Down
32 changes: 16 additions & 16 deletions src/test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1014,22 +1014,22 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const
// request.url("/api");
// EMSESP::webAPIService.webAPIService(&request, doc.as<JsonVariant>());

char data2[] = "{\"action\":\"customSupport\", \"param\":\"hello\"}";
deserializeJson(doc, data2);
request.url("/rest/action");
EMSESP::webStatusService.action(&request, doc.as<JsonVariant>());

char data3[] = "{\"action\":\"export\", \"param\":\"schedule\"}";
deserializeJson(doc, data3);
request.url("/rest/action");
EMSESP::webStatusService.action(&request, doc.as<JsonVariant>());

char data4[] = "{\"action\":\"export\", \"param\":\"allvalues\"}";
deserializeJson(doc, data4);
request.url("/rest/action");
EMSESP::webStatusService.action(&request, doc.as<JsonVariant>());

char data5[] = "{\"action\":\"checkUpgrade\", \"param\":\"3.7.0-dev.99\"}";
// char data2[] = "{\"action\":\"customSupport\", \"param\":\"hello\"}";
// deserializeJson(doc, data2);
// request.url("/rest/action");
// EMSESP::webStatusService.action(&request, doc.as<JsonVariant>());

// char data3[] = "{\"action\":\"export\", \"param\":\"schedule\"}";
// deserializeJson(doc, data3);
// request.url("/rest/action");
// EMSESP::webStatusService.action(&request, doc.as<JsonVariant>());

// char data4[] = "{\"action\":\"export\", \"param\":\"allvalues\"}";
// deserializeJson(doc, data4);
// request.url("/rest/action");
// EMSESP::webStatusService.action(&request, doc.as<JsonVariant>());

char data5[] = "{\"action\":\"checkUpgrade\", \"param\":\"3.7.1-dev.99,3.7.0\"}";
deserializeJson(doc, data5);
request.url("/rest/action");
EMSESP::webStatusService.action(&request, doc.as<JsonVariant>());
Expand Down
27 changes: 18 additions & 9 deletions src/web/WebStatusService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,18 +203,27 @@ void WebStatusService::action(AsyncWebServerRequest * request, JsonVariant json)
}

// action = checkUpgrade
bool WebStatusService::checkUpgrade(JsonObject root, std::string & latest_version) {
root["emsesp_version"] = EMSESP_APP_VERSION;
bool WebStatusService::checkUpgrade(JsonObject root, std::string & versions) {
root["emsesp_version"] = EMSESP_APP_VERSION; // always send back current version

if (!latest_version.empty()) {
#if defined(EMSESP_DEBUG)
emsesp::EMSESP::logger().debug("Checking for upgrade: %s < %s", EMSESP_APP_VERSION, latest_version.c_str());
#endif
// versions holds the latest development version and stable version in one string, comma separated
std::string latest_dev_version = versions.substr(0, versions.find(','));
std::string latest_stable_version = versions.substr(versions.find(',') + 1);

version::Semver200_version settings_version(EMSESP_APP_VERSION);
version::Semver200_version this_version(latest_version);
if (!versions.empty()) {
version::Semver200_version this_version(EMSESP_APP_VERSION); // current version
bool using_dev_version = !this_version.prerelease().empty();
version::Semver200_version latest_version(using_dev_version ? latest_dev_version : latest_stable_version); // latest version

root["upgradeable"] = (this_version > settings_version);
root["upgradeable"] = (latest_version > this_version);

#if defined(EMSESP_DEBUG)
emsesp::EMSESP::logger().debug("Checking for version upgrade. This version=%s, latest dev=%s, latest stable=%s. Upgradeable=%s",
EMSESP_APP_VERSION,
latest_dev_version.c_str(),
latest_stable_version.c_str(),
root["upgradeable"].as<bool>() ? "true" : "false");
#endif
}

return true; // always ok
Expand Down

0 comments on commit b00f5dd

Please sign in to comment.