From d25eda9a7d6bf89284b630b2d55cbb0a7e3a9432 Mon Sep 17 00:00:00 2001 From: Aurelien VALADE Date: Mon, 28 Nov 2022 16:26:42 +0100 Subject: [PATCH 1/2] Adding EtcherPro device serial number to the Settings modal Change-type: patch --- lib/gui/app/components/settings/settings.tsx | 11 +++- lib/gui/app/utils/etcher-pro-specific.ts | 67 ++++++++++++++++++++ 2 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 lib/gui/app/utils/etcher-pro-specific.ts diff --git a/lib/gui/app/components/settings/settings.tsx b/lib/gui/app/components/settings/settings.tsx index f072b44cd7..54d68a0b4e 100644 --- a/lib/gui/app/components/settings/settings.tsx +++ b/lib/gui/app/components/settings/settings.tsx @@ -25,6 +25,7 @@ import * as analytics from '../../modules/analytics'; import { open as openExternal } from '../../os/open-external/services/open-external'; import { Modal } from '../../styled-components'; import * as i18next from 'i18next'; +import { etcherProInfo } from '../../utils/etcher-pro-specific'; interface Setting { name: string; @@ -55,7 +56,7 @@ interface SettingsModalProps { toggleModal: (value: boolean) => void; } -const UUID = process.env.BALENA_DEVICE_UUID; +const EPInfo = etcherProInfo(); const InfoBox = (props: any) => ( @@ -117,10 +118,14 @@ export function SettingsModal({ toggleModal }: SettingsModalProps) { ); })} - {UUID !== undefined && ( + {EPInfo !== undefined && ( {i18next.t('settings.systemInformation')} - + {EPInfo.get_serial() === undefined ? ( + + ) : ( + + )} )} | undefined; + public uuid: string; + + constructor(supervisorAddr: string, supervisorKey: string) { + this.supervisorAddr = supervisorAddr; + this.supervisorKey = supervisorKey; + this.uuid = (process.env.BALENA_DEVICE_UUID ?? 'NO-UUID').substring(0, 7); + this.tags = undefined; + this.get_tags().then((tags) => (this.tags = tags)); + } + + async get_tags(): Promise> { + const result = await fetch( + this.supervisorAddr + '/v2/device/tags?apikey=' + this.supervisorKey, + ); + const parsed = await result.json(); + if (parsed['status'] === 'success') { + return Object.assign( + {}, + ...parsed['tags'].map((tag: any) => { + return { [tag.name]: tag['value'] }; + }), + ); + } else { + return await {}; + } + } + + public get_serial(): string | undefined { + if (this.tags) { + return this.tags['Serial']; + } else { + return undefined; + } + } +} + +export function etcherProInfo(): EtcherPro | undefined { + const BALENA_SUPERVISOR_ADDRESS = process.env.BALENA_SUPERVISOR_ADDRESS; + const BALENA_SUPERVISOR_API_KEY = process.env.BALENA_SUPERVISOR_API_KEY; + + if (BALENA_SUPERVISOR_ADDRESS && BALENA_SUPERVISOR_API_KEY) { + return new EtcherPro(BALENA_SUPERVISOR_ADDRESS, BALENA_SUPERVISOR_API_KEY); + } + return undefined; +} From 45f6ee667d1b2b72e66c6d726651edea79ac329a Mon Sep 17 00:00:00 2001 From: Aurelien VALADE Date: Thu, 12 Jan 2023 14:52:08 +0100 Subject: [PATCH 2/2] Cleaning-up EtcherPro specific code --- lib/gui/app/utils/etcher-pro-specific.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/gui/app/utils/etcher-pro-specific.ts b/lib/gui/app/utils/etcher-pro-specific.ts index afe6e4aab6..f59a2f326d 100644 --- a/lib/gui/app/utils/etcher-pro-specific.ts +++ b/lib/gui/app/utils/etcher-pro-specific.ts @@ -16,6 +16,12 @@ import { Dictionary } from 'lodash'; +type BalenaTag = { + id: number, + name: string, + value: string +} + export class EtcherPro { private supervisorAddr: string; private supervisorKey: string; @@ -38,12 +44,12 @@ export class EtcherPro { if (parsed['status'] === 'success') { return Object.assign( {}, - ...parsed['tags'].map((tag: any) => { - return { [tag.name]: tag['value'] }; + ...parsed['tags'].map((tag: BalenaTag) => { + return { [tag.name]: tag.value }; }), ); } else { - return await {}; + return {}; } }