Skip to content

Commit

Permalink
Refactor contdd...
Browse files Browse the repository at this point in the history
  • Loading branch information
keeramis committed Nov 26, 2024
1 parent ecfafb0 commit c32bf28
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/cmd/esim.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const SerialCommand = require('./serial');
const FlashCommand = require('./flash');
const path = require('path');
const _ = require('lodash');
const { log } = require('console');

// TODO: Get these from exports
const PROVISIONING_PROGRESS = 1;
Expand All @@ -18,7 +19,6 @@ const PROVISIONING_FAILURE = 3;
const CTRL_REQUEST_APP_CUSTOM = 10;
const GET_AT_COMMAND_STATUS = 4;


module.exports = class eSimCommands extends CLICommandBase {
constructor() { // TODO: Bring ui class
super();
Expand Down Expand Up @@ -106,6 +106,7 @@ module.exports = class eSimCommands extends CLICommandBase {
if (!flashResp.success) {
return outputMsg();
}
provisionOutputLogs.push(`${os.EOL}Firmware flashed successfully`);

// Get the EID
const eidResp = await this._getEid(port);
Expand All @@ -120,6 +121,11 @@ module.exports = class eSimCommands extends CLICommandBase {
const matchingEsim = this.inputJsonData.provisioning_data.find(item => item.esim_id === eid);
const iccidFromJson = matchingEsim.profiles.map((profile) => profile.iccid);
expectedProfilesArray = matchingEsim.profiles;

if (!matchingEsim || iccidFromJson?.length === 0 || expectedProfilesArray?.length === 0) {
provisionOutputLogs.push('No profiles found for the given EID in the input JSON');
return outputMsg();
}

const profileCmdResp = await this._checkForExistingProfiles(port);
provisionOutputLogs.push(...profileCmdResp.output);
Expand All @@ -129,6 +135,8 @@ module.exports = class eSimCommands extends CLICommandBase {

const profilesListOnDevice = profileCmdResp.profilesList;
const existingIccids = profilesListOnDevice.map((line) => line.split('[')[1].split(',')[0].trim());
provisionOutputLogs.push(`${os.EOL}profilesListOnDevice: ${profilesListOnDevice}`);
provisionOutputLogs.push(`${os.EOL}existingIccids: ${existingIccids}`);

if (profilesListOnDevice.length > 0) {
// extract the iccids that belong to this EID
Expand Down Expand Up @@ -166,7 +174,8 @@ module.exports = class eSimCommands extends CLICommandBase {

// Download each profile and update the JSON output
await this._changeLed(device, PROVISIONING_PROGRESS);


provisionOutputLogs.push(`${os.EOL}Downloading profiles...`);
const downloadResp = await this._doDownload(profiles, port);
const downloadedProfiles = downloadResp.downloadedProfiles;
downloadedProfilesArray = downloadedProfiles.map((profile) => {
Expand All @@ -180,6 +189,7 @@ module.exports = class eSimCommands extends CLICommandBase {
provisionOutputLogs.push(...downloadResp.output);

if (!downloadResp.success) {
provisionOutputLogs.push('Profile download failed');
return outputMsg();
}

Expand Down Expand Up @@ -257,6 +267,7 @@ module.exports = class eSimCommands extends CLICommandBase {
// Flash the binary
const flashCmdInstance = new FlashCommand();

logAndPush(`${os.EOL}Flashing firmware...`);
await flashCmdInstance.flashLocal({
files: [device.deviceId, fwPath],
applicationOnly: true,
Expand All @@ -277,7 +288,7 @@ module.exports = class eSimCommands extends CLICommandBase {
while (Date.now() - start < timeout && !atOkReceived) {
try {
const resp = await usbDevice.sendControlRequest(CTRL_REQUEST_APP_CUSTOM, JSON.stringify(GET_AT_COMMAND_STATUS));
console.log('[dbg] resp: ', resp);
// console.log('[dbg] resp: ', resp);
if (resp?.result === 0 && resp.data?.[0] === '1') {
logAndPush('AT-OK received');
atOkReceived = true;
Expand Down Expand Up @@ -397,6 +408,7 @@ module.exports = class eSimCommands extends CLICommandBase {
}
});
};
logAndPush(`${os.EOL}Getting profiles for EID ${eid}...`);
const eidBlock = this.inputJsonData.provisioning_data.find((block) => block.esim_id === eid);

if (!eidBlock || !eidBlock.profiles || eidBlock.profiles.length === 0) {
Expand Down

0 comments on commit c32bf28

Please sign in to comment.