Skip to content

Commit

Permalink
Add EID to the input JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
keeramis committed Nov 19, 2024
1 parent 91c8d20 commit 4c926fa
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions src/cmd/esim.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand All @@ -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();

Expand All @@ -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 = {
Expand All @@ -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) {
Expand Down Expand Up @@ -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`);
}

Expand Down Expand Up @@ -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));
}
};

0 comments on commit 4c926fa

Please sign in to comment.