From 4c926fa40c2fc7aeb8c2133329ca50b67d25319c Mon Sep 17 00:00:00 2001 From: keeramis Date: Tue, 19 Nov 2024 12:52:26 -0800 Subject: [PATCH] Add EID to the input JSON --- src/cmd/esim.js | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/src/cmd/esim.js b/src/cmd/esim.js index f3ea9415f..1e239aa19 100644 --- a/src/cmd/esim.js +++ b/src/cmd/esim.js @@ -42,15 +42,15 @@ module.exports = class eSimCommands extends CLICommandBase { const eid = await this._getEid(port); console.log(`${os.EOL}EID: ${eid}`); - // await this._checkForExistingProfiles(port); + await this._checkForExistingProfiles(port); // Parse the JSON to get EID and profiles const input = fs.readFileSync(this.inputJson); - const profilesJson = JSON.parse(input); + const inputJsonData = JSON.parse(input); - // Work only with one device - const first = profilesJson.EIDs[0]; - const profiles = first.profiles; + // Get the profile list that matches the EID that is given by the field eid + const eidBlock = inputJsonData.EIDs.filter((block) => block.eid === eid); + const profiles = eidBlock[0]?.profiles; if (profiles.length === 0) { throw new Error('No profiles to provision in the input json'); } @@ -66,12 +66,14 @@ module.exports = class eSimCommands extends CLICommandBase { } // Download each profile + cnt = 0; for (const profile of profiles) { + cnt++; let iccid = null; const smdp = profile.smdp; const matchingId = profile.matching_id; const rspUrl = `1\$${smdp}\$${matchingId}`; - console.log(`${os.EOL}Downloading ${profile.provider} profile from ${rspUrl}`); + console.log(`${os.EOL}${cnt}. Downloading ${profile.provider} profile from ${rspUrl}`); const start = Date.now(); @@ -82,12 +84,12 @@ module.exports = class eSimCommands extends CLICommandBase { const output = res.stdout; if (output.includes('Profile successfully downloaded')) { - console.log(`${os.EOL}Profile successfully downloaded in ${timeTaken} sec`); + console.log(`${os.EOL}\tProfile successfully downloaded in ${timeTaken} sec`); const iccidLine = output.split('\n').find((line) => line.includes('Profile with ICCID')); // FIXME: Get the string after the "ICCID" string iccid = iccidLine.split(' ')[4]; } else { - console.log(`${os.EOL}Profile download failed`); + console.log(`${os.EOL}\tProfile download failed`); } const outputData = { @@ -98,8 +100,11 @@ module.exports = class eSimCommands extends CLICommandBase { output: output }; - await fs.writeJson(outputJson, outputData); + await this._addToJson(this.outputJson, outputData); + console.log(os.EOL); } + + console.log(`${os.EOL}Provisioning complete`); } _validateArgs(args) { @@ -150,10 +155,12 @@ module.exports = class eSimCommands extends CLICommandBase { // FIXME: Use the firmware that does not have initial logs and then remove this block console.log(`${os.EOL}Wait for the initial logs to clear up`); + console.log(`${os.EOL}--------------------------------------`); const monitor = await this.serial.monitorPort({ port, follow: false }); await utilities.delay(30000); await monitor.stop(); await utilities.delay(5000); + console.log(`${os.EOL}--------------------------------------`); console.log(`${os.EOL}Initial logs likely cleared`); } @@ -188,4 +195,11 @@ module.exports = class eSimCommands extends CLICommandBase { } console.log(`${os.EOL}No existing profiles found`); } + + async _addToJson(jsonFile, data) { + const existing = fs.readFileSync(jsonFile); + const existingJson = JSON.parse(existing); + existingJson.push(data); + fs.writeFileSync(jsonFile, JSON.stringify(existingJson, null, 4)); + } };