Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
keeramis committed Nov 26, 2024
1 parent fde02e9 commit ecfafb0
Showing 1 changed file with 119 additions and 180 deletions.
299 changes: 119 additions & 180 deletions src/cmd/esim.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ module.exports = class eSimCommands extends CLICommandBase {
: 'No devices found.';
throw new Error(errorMessage);
}
await this.doProvision(devices[0]);
const device = devices[0];
const resp = await this.doProvision(device);
await this._changeLed(device, resp.success ? PROVISIONING_SUCCESS : PROVISIONING_FAILURE);
this._addToJson(this.outputJson, resp);
}

async bulkProvisionCommand(args) {
Expand All @@ -56,9 +59,12 @@ module.exports = class eSimCommands extends CLICommandBase {
const devices = await this.serial.findDevices();
for (const device of devices) {
if (!provisionedDevices.has(device.deviceId)) {
provisionedDevices.add(device.deviceId);
console.log(`Device ${device.deviceId} connected`);
await this.doProvision(device, { verbose: true });
const deviceId = device.deviceId;
provisionedDevices.add(deviceId);
console.log(`Device ${deviceId} connected`);
const resp = await this.doProvision(device, { verbose: true });
await this._changeLed(device, resp.success ? PROVISIONING_SUCCESS : PROVISIONING_FAILURE);
this._addToJson(this.outputJson, resp);
}
}
}, 1000);
Expand All @@ -68,200 +74,133 @@ module.exports = class eSimCommands extends CLICommandBase {

async doProvision(device) {
let provisionOutputLogs = [];
const timestamp = new Date().toISOString();
const platform = platformForId(device.specs.productId).name;
const port = device.port;

provisionOutputLogs.push(`${os.EOL}Provisioning device ${device.deviceId} with platform ${platform}`);

// Flash firmware and wait for AT to work
const flashResp = await this._flashATPassThroughFirmware(device, platform);
provisionOutputLogs.push(...flashResp.output);
if (!flashResp.success) {
await this._changeLed(device, PROVISIONING_FAILURE);
this._addToJson(this.outputJson, {
EID: null,
let eid = null;
let timestamp = null;
let expectedProfilesArray = [];
let downloadedProfilesArray = [];
let success = false;

// Add the output logs to the output JSON file in one msg
const outputMsg = () => {
return {
esim_id: eid,
device_id: device.deviceId,
success: false,
expectedProfiles: expectedProfilesArray,
downloadedProfiles: downloadedProfilesArray,
success: success,
timestamp: timestamp,
output: provisionOutputLogs
});
return;
}
}
};

// Get the EID
const eidResp = await this._getEid(port);
provisionOutputLogs.push(...eidResp.output);
if (!eidResp.success) {
await this._changeLed(device, PROVISIONING_FAILURE);
this._addToJson(this.outputJson, {
EID: null,
device_id: device.deviceId,
success: false,
timestamp: timestamp,
output: provisionOutputLogs,
});
return;
}
const eid = eidResp.eid;
provisionOutputLogs.push(`EID: ${eid}`);

// Get the profiles for this EID and compare them against the list in the input JSON under the same EID
const matchingEsim = this.inputJsonData.provisioning_data.find(item => item.esim_id === eid);
const iccidFromJson = matchingEsim.profiles.map((profile) => profile.iccid);
const expectedProfilesArray = matchingEsim.profiles;

const profileCmdResp = await this._checkForExistingProfiles(port);
provisionOutputLogs.push(...profileCmdResp.output);
if (!profileCmdResp.success) {
await this._changeLed(device, PROVISIONING_FAILURE);
this._addToJson(this.outputJson, {
EID: eid,
device_id: device.deviceId,
expectedProfilesArray: expectedProfilesArray,
downloadedProfiles: [],
success: false,
timestamp: timestamp,
output: provisionOutputLogs
});
return;
}
try {
timestamp = new Date().toISOString();
const platform = platformForId(device.specs.productId).name;
const port = device.port;

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

console.log('[dbg] existingIccids: ', existingIccids);
console.log('[dbg] profilesListOnDevice: ', profilesListOnDevice);
// Flash firmware and wait for AT to work
const flashResp = await this._flashATPassThroughFirmware(device, platform);
provisionOutputLogs.push(...flashResp.output);
if (!flashResp.success) {
return outputMsg();
}

if (profilesListOnDevice.length > 0) {
// extract the iccids that belong to this EID
const matchingEsim = this.inputJsonData.provisioning_data.find(item => item.esim_id === eid);
if (!matchingEsim) {
provisionOutputLogs.push('No profiles found for the given EID in the input JSON');
this._addToJson(this.outputJson, {
esim_id: eid,
device_id: device.deviceId,
expectedProfilesArray: expectedProfilesArray,
downloadedProfiles: [],
success: false,
timestamp: timestamp,
output: provisionOutputLogs,
});
return;
// Get the EID
const eidResp = await this._getEid(port);
provisionOutputLogs.push(...eidResp.output);
if (!eidResp.success) {
return outputMsg();
}
eid = (eidResp.eid).trim();
provisionOutputLogs.push(`EID: ${eid}`);

// Get the profiles for this EID and compare them against the list in the input JSON under the same EID
const matchingEsim = this.inputJsonData.provisioning_data.find(item => item.esim_id === eid);
const iccidFromJson = matchingEsim.profiles.map((profile) => profile.iccid);
const equal = _.isEqual(_.sortBy(existingIccids), _.sortBy(iccidFromJson));
if (equal) {
this._changeLed(device, PROVISIONING_SUCCESS);
provisionOutputLogs.push('Profiles already provisioned correctly on the device for the given EID');
this._addToJson(this.outputJson, {
esim_id: eid,
device_id: device.deviceId,
expectedProfilesArray: expectedProfilesArray,
success: true,
timestamp: timestamp,
output: provisionOutputLogs,
});
return;
} else {
provisionOutputLogs.push('Profiles exist on the device but do not match the profiles in the input JSON');
await this._changeLed(device, PROVISIONING_FAILURE);
this._addToJson(this.outputJson, {
esim_id: eid,
device_id: device.deviceId,
success: false,
timestamp: timestamp,
output: provisionOutputLogs,
});
return;
expectedProfilesArray = matchingEsim.profiles;

const profileCmdResp = await this._checkForExistingProfiles(port);
provisionOutputLogs.push(...profileCmdResp.output);
if (!profileCmdResp.success) {
return outputMsg();
}
}

// Get profiles for this EID from the input JSON
const profileResp = this._getProfiles(eid);
provisionOutputLogs.push(...profileResp.output);
if (!profileResp.success) {
await this._changeLed(device, PROVISIONING_FAILURE);
this._addToJson(this.outputJson, {
esim_id: eid,
device_id: device.deviceId,
success: false,
timestamp: timestamp,
output: provisionOutputLogs
});
return;
}
const profilesListOnDevice = profileCmdResp.profilesList;
const existingIccids = profilesListOnDevice.map((line) => line.split('[')[1].split(',')[0].trim());

provisionOutputLogs.push(`${os.EOL}Provisioning the following profiles to EID ${eid}:`);
if (profilesListOnDevice.length > 0) {
// extract the iccids that belong to this EID
const matchingEsim = this.inputJsonData.provisioning_data.find(item => item.esim_id === eid);
if (!matchingEsim) {
provisionOutputLogs.push('No profiles found for the given EID in the input JSON');
return outputMsg();
}
const iccidFromJson = matchingEsim.profiles.map((profile) => profile.iccid);
const equal = _.isEqual(_.sortBy(existingIccids), _.sortBy(iccidFromJson));
if (equal) {
success = true;
provisionOutputLogs.push('Profiles already provisioned correctly on the device for the given EID');
return outputMsg();
} else {
provisionOutputLogs.push('Profiles exist on the device but do not match the profiles in the input JSON');
return outputMsg();
}
}

const profiles = profileResp.profiles;
profiles.forEach((profile, index) => {
const rspUrl = `1\$${profile.smdp}\$${profile.matching_id}`;
provisionOutputLogs.push(`\t${index + 1}. ${profile.provider} (${rspUrl})`);
});
// Get profiles for this EID from the input JSON
const profileResp = this._getProfiles(eid);
provisionOutputLogs.push(...profileResp.output);
if (!profileResp.success) {
return outputMsg();
}

// Download each profile and update the JSON output
await this._changeLed(device, PROVISIONING_PROGRESS);
provisionOutputLogs.push(`${os.EOL}Provisioning the following profiles to EID ${eid}:`);

const downloadResp = await this._doDownload(profiles, port);
const downloadedProfiles = downloadResp.downloadedProfiles;
const downloadedProfilesArray = downloadedProfiles.map((profile) => {
return {
status: profile.status,
iccid: profile.iccid,
provider: profile.provider,
duration: profile.duration
};
});
provisionOutputLogs.push(...downloadResp.output);

if (!downloadResp.success) {
await this._changeLed(device, PROVISIONING_FAILURE);
this._addToJson(this.outputJson, {
esim_id: eid,
device_id: device.deviceId,
expectedProfiles: expectedProfilesArray,
downloadedProfiles: downloadedProfilesArray,
success: false,
timestamp: timestamp,
output: provisionOutputLogs,
const profiles = profileResp.profiles;
profiles.forEach((profile, index) => {
const rspUrl = `1\$${profile.smdp}\$${profile.matching_id}`;
provisionOutputLogs.push(`\t${index + 1}. ${profile.provider} (${rspUrl})`);
});
return;
}
await this._changeLed(device, PROVISIONING_SUCCESS);

const profilesOnDeviceAfterDownload = await this._listProfiles(port);
const iccidsOnDeviceAfterDownload = profilesOnDeviceAfterDownload.map((line) => line.split('[')[1].split(',')[0].trim());
const equal = _.isEqual(_.sortBy(iccidsOnDeviceAfterDownload), _.sortBy(iccidFromJson));
if (!equal) {
provisionOutputLogs.push('Profiles did not match after download');
await this._changeLed(device, PROVISIONING_FAILURE);
this._addToJson(this.outputJson, {
esim_id: eid,
device_id: device.deviceId,
expectedProfiles: expectedProfilesArray,
downloadedProfiles: downloadedProfilesArray,
success: false,
timestamp: timestamp,
output: provisionOutputLogs,

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

const downloadResp = await this._doDownload(profiles, port);
const downloadedProfiles = downloadResp.downloadedProfiles;
downloadedProfilesArray = downloadedProfiles.map((profile) => {
return {
status: profile.status,
iccid: profile.iccid,
provider: profile.provider,
duration: profile.duration
};
});
return;
}
provisionOutputLogs.push(...downloadResp.output);

if (!downloadResp.success) {
return outputMsg();
}

// Update the JSON output with the downloaded profiles
// Success case
this._addToJson(this.outputJson, {
esim_id: eid,
device_id: device.deviceId,
expectedProfiles: expectedProfilesArray,
downloadedProfiles: downloadedProfilesArray,
success: true,
timestamp: timestamp,
output: provisionOutputLogs
});

console.log(`${os.EOL}Provisioning complete for EID ${eid}`);
provisionOutputLogs.push(`${os.EOL}Provisioning complete for EID ${eid}`);
const profilesOnDeviceAfterDownload = await this._listProfiles(port);
const iccidsOnDeviceAfterDownload = profilesOnDeviceAfterDownload.map((line) => line.split('[')[1].split(',')[0].trim());
const equal = _.isEqual(_.sortBy(iccidsOnDeviceAfterDownload), _.sortBy(iccidFromJson));
if (!equal) {
provisionOutputLogs.push('Profiles did not match after download');
return outputMsg();
}

// Update the JSON output with the downloaded profiles
// Success case
success = true;
console.log(`${os.EOL}Provisioning complete for EID ${eid}`);
provisionOutputLogs.push(`${os.EOL}Provisioning complete for EID ${eid}`);
return outputMsg();
} catch (error) {
provisionOutputLogs.push(`Error during provisioning: ${error.message}`);
return outputMsg();
}
}

_validateArgs(args) {
Expand Down

0 comments on commit ecfafb0

Please sign in to comment.