improved version check - #2190

This commit is contained in:
proddy
2024-11-09 15:08:36 +01:00
parent 367260b143
commit b00f5dd8c0
6 changed files with 76 additions and 56 deletions

View File

@@ -57,7 +57,7 @@ export const alovaInstance = createAlova({
}); });
export const alovaInstanceGH = 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, statesHook: ReactHook,
requestAdapter: xhrRequestAdapter() requestAdapter: xhrRequestAdapter()
}); });

View File

@@ -1,4 +1,4 @@
import { useState } from 'react'; import { useEffect, useState } from 'react';
import { toast } from 'react-toastify'; import { toast } from 'react-toastify';
import CancelIcon from '@mui/icons-material/Cancel'; import CancelIcon from '@mui/icons-material/Cancel';
@@ -36,7 +36,7 @@ const Version = () => {
const [upgradeAvailable, setUpgradeAvailable] = useState<boolean>(false); const [upgradeAvailable, setUpgradeAvailable] = useState<boolean>(false);
const { send: sendCheckUpgrade } = useRequest( const { send: sendCheckUpgrade } = useRequest(
(version: string) => callAction({ action: 'checkUpgrade', param: version }), (versions: string) => callAction({ action: 'checkUpgrade', param: versions }),
{ {
immediate: false immediate: false
} }
@@ -54,27 +54,18 @@ const Version = () => {
} }
); );
// called immediately to get the latest version, on page load // called immediately to get the latest versions on page load
const { data: latestVersion } = useRequest(getStableVersion, { 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 const { data: latestDevVersion } = useRequest(getDevVersion);
// immediate: false,
// initialData: '3.6.5'
}).onSuccess((event) => {
if (!useDev) {
void sendCheckUpgrade(event.data);
}
});
// called immediately to get the latest version, on page load, then check for upgrade (works for both dev and stable) useEffect(() => {
const { data: latestDevVersion } = useRequest(getDevVersion, { if (latestVersion && latestDevVersion) {
// 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 console.log("Latest versions, stable: " + latestVersion + " dev: " + latestDevVersion);
// immediate: false, sendCheckUpgrade(latestDevVersion + "," + latestVersion).catch((error: Error) => {
// initialData: '3.7.0-dev.32' console.error("Failed to check upgrade:", error);
}).onSuccess((event) => {
if (useDev) {
void sendCheckUpgrade(event.data);
}
}); });
}
}, [latestVersion, latestDevVersion]);
const STABLE_URL = 'https://github.com/emsesp/EMS-ESP32/releases/download/'; const STABLE_URL = 'https://github.com/emsesp/EMS-ESP32/releases/download/';
const STABLE_RELNOTES_URL = const STABLE_RELNOTES_URL =

View File

@@ -20,7 +20,8 @@ export default defineConfig(({ command, mode }) => {
changeOrigin: true, changeOrigin: true,
secure: false secure: false
}, },
'/rest': 'http://localhost:3080' '/rest': 'http://localhost:3080',
'/gh': 'http://localhost:3080' // mock for GitHub API
} }
} }
}; };

View File

@@ -21,6 +21,7 @@ const router = AutoRouter({
const REST_ENDPOINT_ROOT = '/rest/'; const REST_ENDPOINT_ROOT = '/rest/';
const API_ENDPOINT_ROOT = '/api/'; const API_ENDPOINT_ROOT = '/api/';
const GH_ENDPOINT_ROOT = '/gh/'; // for mock GitHub API for version checking
// HTTP HEADERS for msgpack // HTTP HEADERS for msgpack
const headers = { const headers = {
@@ -28,6 +29,13 @@ const headers = {
'Content-type': 'application/msgpack' '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 // GLOBAL VARIABLES
let countWifiScanPoll = 0; // wifi network scan let countWifiScanPoll = 0; // wifi network scan
let countHardwarePoll = 0; // for during an upload let countHardwarePoll = 0; // for during an upload
@@ -199,16 +207,18 @@ function custom_support() {
function check_upgrade(version: string) { function check_upgrade(version: string) {
let data = {}; let data = {};
if (version) { 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 = { data = {
emsesp_version: VERSION, emsesp_version: THIS_VERSION,
// upgradeable: true upgradeable: VERSION_IS_UPGRADEABLE
upgradeable: false
}; };
} else { } else {
console.log('requesting ems-esp version'); console.log('requesting ems-esp version ('+THIS_VERSION+')');
data = { data = {
emsesp_version: VERSION emsesp_version: THIS_VERSION
}; };
} }
return data; return data;
@@ -468,11 +478,8 @@ const VERIFY_AUTHORIZATION_ENDPOINT = REST_ENDPOINT_ROOT + 'verifyAuthorization'
const SIGN_IN_ENDPOINT = REST_ENDPOINT_ROOT + 'signIn'; const SIGN_IN_ENDPOINT = REST_ENDPOINT_ROOT + 'signIn';
const GENERATE_TOKEN_ENDPOINT = REST_ENDPOINT_ROOT + 'generateToken'; const GENERATE_TOKEN_ENDPOINT = REST_ENDPOINT_ROOT + 'generateToken';
const VERSION = '3.7.0';
// const VERSION = '3.6.4';
let system_status = { let system_status = {
emsesp_version: VERSION, emsesp_version: THIS_VERSION,
bus_status: 0, bus_status: 0,
// status: 2, // status: 2,
uptime: 77186, uptime: 77186,
@@ -573,7 +580,7 @@ const EMSESP_SYSTEM_INFO_ENDPOINT = API_ENDPOINT_ROOT + 'system/info';
const emsesp_info = { const emsesp_info = {
System: { System: {
version: VERSION, version: THIS_VERSION,
uptime: '001+06:40:34.018', uptime: '001+06:40:34.018',
'uptime (seconds)': 110434, 'uptime (seconds)': 110434,
freemem: 131, freemem: 131,
@@ -4878,6 +4885,18 @@ router
return status(404); // not found 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; export default router;
// use this with cloudflare workers instead // use this with cloudflare workers instead

View File

@@ -1014,22 +1014,22 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const
// request.url("/api"); // request.url("/api");
// EMSESP::webAPIService.webAPIService(&request, doc.as<JsonVariant>()); // EMSESP::webAPIService.webAPIService(&request, doc.as<JsonVariant>());
char data2[] = "{\"action\":\"customSupport\", \"param\":\"hello\"}"; // char data2[] = "{\"action\":\"customSupport\", \"param\":\"hello\"}";
deserializeJson(doc, data2); // deserializeJson(doc, data2);
request.url("/rest/action"); // request.url("/rest/action");
EMSESP::webStatusService.action(&request, doc.as<JsonVariant>()); // EMSESP::webStatusService.action(&request, doc.as<JsonVariant>());
char data3[] = "{\"action\":\"export\", \"param\":\"schedule\"}"; // char data3[] = "{\"action\":\"export\", \"param\":\"schedule\"}";
deserializeJson(doc, data3); // deserializeJson(doc, data3);
request.url("/rest/action"); // request.url("/rest/action");
EMSESP::webStatusService.action(&request, doc.as<JsonVariant>()); // EMSESP::webStatusService.action(&request, doc.as<JsonVariant>());
char data4[] = "{\"action\":\"export\", \"param\":\"allvalues\"}"; // char data4[] = "{\"action\":\"export\", \"param\":\"allvalues\"}";
deserializeJson(doc, data4); // deserializeJson(doc, data4);
request.url("/rest/action"); // request.url("/rest/action");
EMSESP::webStatusService.action(&request, doc.as<JsonVariant>()); // EMSESP::webStatusService.action(&request, doc.as<JsonVariant>());
char data5[] = "{\"action\":\"checkUpgrade\", \"param\":\"3.7.0-dev.99\"}"; char data5[] = "{\"action\":\"checkUpgrade\", \"param\":\"3.7.1-dev.99,3.7.0\"}";
deserializeJson(doc, data5); deserializeJson(doc, data5);
request.url("/rest/action"); request.url("/rest/action");
EMSESP::webStatusService.action(&request, doc.as<JsonVariant>()); EMSESP::webStatusService.action(&request, doc.as<JsonVariant>());

View File

@@ -203,18 +203,27 @@ void WebStatusService::action(AsyncWebServerRequest * request, JsonVariant json)
} }
// action = checkUpgrade // action = checkUpgrade
bool WebStatusService::checkUpgrade(JsonObject root, std::string & latest_version) { bool WebStatusService::checkUpgrade(JsonObject root, std::string & versions) {
root["emsesp_version"] = EMSESP_APP_VERSION; root["emsesp_version"] = EMSESP_APP_VERSION; // always send back current version
// 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);
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"] = (latest_version > this_version);
if (!latest_version.empty()) {
#if defined(EMSESP_DEBUG) #if defined(EMSESP_DEBUG)
emsesp::EMSESP::logger().debug("Checking for upgrade: %s < %s", EMSESP_APP_VERSION, latest_version.c_str()); 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 #endif
version::Semver200_version settings_version(EMSESP_APP_VERSION);
version::Semver200_version this_version(latest_version);
root["upgradeable"] = (this_version > settings_version);
} }
return true; // always ok return true; // always ok