Skip to content

Commit ecfafb0

Browse files
committed
Refactor
1 parent fde02e9 commit ecfafb0

File tree

1 file changed

+119
-180
lines changed

1 file changed

+119
-180
lines changed

src/cmd/esim.js

Lines changed: 119 additions & 180 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ module.exports = class eSimCommands extends CLICommandBase {
4545
: 'No devices found.';
4646
throw new Error(errorMessage);
4747
}
48-
await this.doProvision(devices[0]);
48+
const device = devices[0];
49+
const resp = await this.doProvision(device);
50+
await this._changeLed(device, resp.success ? PROVISIONING_SUCCESS : PROVISIONING_FAILURE);
51+
this._addToJson(this.outputJson, resp);
4952
}
5053

5154
async bulkProvisionCommand(args) {
@@ -56,9 +59,12 @@ module.exports = class eSimCommands extends CLICommandBase {
5659
const devices = await this.serial.findDevices();
5760
for (const device of devices) {
5861
if (!provisionedDevices.has(device.deviceId)) {
59-
provisionedDevices.add(device.deviceId);
60-
console.log(`Device ${device.deviceId} connected`);
61-
await this.doProvision(device, { verbose: true });
62+
const deviceId = device.deviceId;
63+
provisionedDevices.add(deviceId);
64+
console.log(`Device ${deviceId} connected`);
65+
const resp = await this.doProvision(device, { verbose: true });
66+
await this._changeLed(device, resp.success ? PROVISIONING_SUCCESS : PROVISIONING_FAILURE);
67+
this._addToJson(this.outputJson, resp);
6268
}
6369
}
6470
}, 1000);
@@ -68,200 +74,133 @@ module.exports = class eSimCommands extends CLICommandBase {
6874

6975
async doProvision(device) {
7076
let provisionOutputLogs = [];
71-
const timestamp = new Date().toISOString();
72-
const platform = platformForId(device.specs.productId).name;
73-
const port = device.port;
74-
75-
provisionOutputLogs.push(`${os.EOL}Provisioning device ${device.deviceId} with platform ${platform}`);
76-
77-
// Flash firmware and wait for AT to work
78-
const flashResp = await this._flashATPassThroughFirmware(device, platform);
79-
provisionOutputLogs.push(...flashResp.output);
80-
if (!flashResp.success) {
81-
await this._changeLed(device, PROVISIONING_FAILURE);
82-
this._addToJson(this.outputJson, {
83-
EID: null,
77+
let eid = null;
78+
let timestamp = null;
79+
let expectedProfilesArray = [];
80+
let downloadedProfilesArray = [];
81+
let success = false;
82+
83+
// Add the output logs to the output JSON file in one msg
84+
const outputMsg = () => {
85+
return {
86+
esim_id: eid,
8487
device_id: device.deviceId,
85-
success: false,
88+
expectedProfiles: expectedProfilesArray,
89+
downloadedProfiles: downloadedProfilesArray,
90+
success: success,
8691
timestamp: timestamp,
8792
output: provisionOutputLogs
88-
});
89-
return;
90-
}
93+
}
94+
};
9195

92-
// Get the EID
93-
const eidResp = await this._getEid(port);
94-
provisionOutputLogs.push(...eidResp.output);
95-
if (!eidResp.success) {
96-
await this._changeLed(device, PROVISIONING_FAILURE);
97-
this._addToJson(this.outputJson, {
98-
EID: null,
99-
device_id: device.deviceId,
100-
success: false,
101-
timestamp: timestamp,
102-
output: provisionOutputLogs,
103-
});
104-
return;
105-
}
106-
const eid = eidResp.eid;
107-
provisionOutputLogs.push(`EID: ${eid}`);
108-
109-
// Get the profiles for this EID and compare them against the list in the input JSON under the same EID
110-
const matchingEsim = this.inputJsonData.provisioning_data.find(item => item.esim_id === eid);
111-
const iccidFromJson = matchingEsim.profiles.map((profile) => profile.iccid);
112-
const expectedProfilesArray = matchingEsim.profiles;
113-
114-
const profileCmdResp = await this._checkForExistingProfiles(port);
115-
provisionOutputLogs.push(...profileCmdResp.output);
116-
if (!profileCmdResp.success) {
117-
await this._changeLed(device, PROVISIONING_FAILURE);
118-
this._addToJson(this.outputJson, {
119-
EID: eid,
120-
device_id: device.deviceId,
121-
expectedProfilesArray: expectedProfilesArray,
122-
downloadedProfiles: [],
123-
success: false,
124-
timestamp: timestamp,
125-
output: provisionOutputLogs
126-
});
127-
return;
128-
}
96+
try {
97+
timestamp = new Date().toISOString();
98+
const platform = platformForId(device.specs.productId).name;
99+
const port = device.port;
129100

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

133-
console.log('[dbg] existingIccids: ', existingIccids);
134-
console.log('[dbg] profilesListOnDevice: ', profilesListOnDevice);
103+
// Flash firmware and wait for AT to work
104+
const flashResp = await this._flashATPassThroughFirmware(device, platform);
105+
provisionOutputLogs.push(...flashResp.output);
106+
if (!flashResp.success) {
107+
return outputMsg();
108+
}
135109

136-
if (profilesListOnDevice.length > 0) {
137-
// extract the iccids that belong to this EID
138-
const matchingEsim = this.inputJsonData.provisioning_data.find(item => item.esim_id === eid);
139-
if (!matchingEsim) {
140-
provisionOutputLogs.push('No profiles found for the given EID in the input JSON');
141-
this._addToJson(this.outputJson, {
142-
esim_id: eid,
143-
device_id: device.deviceId,
144-
expectedProfilesArray: expectedProfilesArray,
145-
downloadedProfiles: [],
146-
success: false,
147-
timestamp: timestamp,
148-
output: provisionOutputLogs,
149-
});
150-
return;
110+
// Get the EID
111+
const eidResp = await this._getEid(port);
112+
provisionOutputLogs.push(...eidResp.output);
113+
if (!eidResp.success) {
114+
return outputMsg();
151115
}
116+
eid = (eidResp.eid).trim();
117+
provisionOutputLogs.push(`EID: ${eid}`);
118+
119+
// Get the profiles for this EID and compare them against the list in the input JSON under the same EID
120+
const matchingEsim = this.inputJsonData.provisioning_data.find(item => item.esim_id === eid);
152121
const iccidFromJson = matchingEsim.profiles.map((profile) => profile.iccid);
153-
const equal = _.isEqual(_.sortBy(existingIccids), _.sortBy(iccidFromJson));
154-
if (equal) {
155-
this._changeLed(device, PROVISIONING_SUCCESS);
156-
provisionOutputLogs.push('Profiles already provisioned correctly on the device for the given EID');
157-
this._addToJson(this.outputJson, {
158-
esim_id: eid,
159-
device_id: device.deviceId,
160-
expectedProfilesArray: expectedProfilesArray,
161-
success: true,
162-
timestamp: timestamp,
163-
output: provisionOutputLogs,
164-
});
165-
return;
166-
} else {
167-
provisionOutputLogs.push('Profiles exist on the device but do not match the profiles in the input JSON');
168-
await this._changeLed(device, PROVISIONING_FAILURE);
169-
this._addToJson(this.outputJson, {
170-
esim_id: eid,
171-
device_id: device.deviceId,
172-
success: false,
173-
timestamp: timestamp,
174-
output: provisionOutputLogs,
175-
});
176-
return;
122+
expectedProfilesArray = matchingEsim.profiles;
123+
124+
const profileCmdResp = await this._checkForExistingProfiles(port);
125+
provisionOutputLogs.push(...profileCmdResp.output);
126+
if (!profileCmdResp.success) {
127+
return outputMsg();
177128
}
178-
}
179129

180-
// Get profiles for this EID from the input JSON
181-
const profileResp = this._getProfiles(eid);
182-
provisionOutputLogs.push(...profileResp.output);
183-
if (!profileResp.success) {
184-
await this._changeLed(device, PROVISIONING_FAILURE);
185-
this._addToJson(this.outputJson, {
186-
esim_id: eid,
187-
device_id: device.deviceId,
188-
success: false,
189-
timestamp: timestamp,
190-
output: provisionOutputLogs
191-
});
192-
return;
193-
}
130+
const profilesListOnDevice = profileCmdResp.profilesList;
131+
const existingIccids = profilesListOnDevice.map((line) => line.split('[')[1].split(',')[0].trim());
194132

195-
provisionOutputLogs.push(`${os.EOL}Provisioning the following profiles to EID ${eid}:`);
133+
if (profilesListOnDevice.length > 0) {
134+
// extract the iccids that belong to this EID
135+
const matchingEsim = this.inputJsonData.provisioning_data.find(item => item.esim_id === eid);
136+
if (!matchingEsim) {
137+
provisionOutputLogs.push('No profiles found for the given EID in the input JSON');
138+
return outputMsg();
139+
}
140+
const iccidFromJson = matchingEsim.profiles.map((profile) => profile.iccid);
141+
const equal = _.isEqual(_.sortBy(existingIccids), _.sortBy(iccidFromJson));
142+
if (equal) {
143+
success = true;
144+
provisionOutputLogs.push('Profiles already provisioned correctly on the device for the given EID');
145+
return outputMsg();
146+
} else {
147+
provisionOutputLogs.push('Profiles exist on the device but do not match the profiles in the input JSON');
148+
return outputMsg();
149+
}
150+
}
196151

197-
const profiles = profileResp.profiles;
198-
profiles.forEach((profile, index) => {
199-
const rspUrl = `1\$${profile.smdp}\$${profile.matching_id}`;
200-
provisionOutputLogs.push(`\t${index + 1}. ${profile.provider} (${rspUrl})`);
201-
});
152+
// Get profiles for this EID from the input JSON
153+
const profileResp = this._getProfiles(eid);
154+
provisionOutputLogs.push(...profileResp.output);
155+
if (!profileResp.success) {
156+
return outputMsg();
157+
}
202158

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

206-
const downloadResp = await this._doDownload(profiles, port);
207-
const downloadedProfiles = downloadResp.downloadedProfiles;
208-
const downloadedProfilesArray = downloadedProfiles.map((profile) => {
209-
return {
210-
status: profile.status,
211-
iccid: profile.iccid,
212-
provider: profile.provider,
213-
duration: profile.duration
214-
};
215-
});
216-
provisionOutputLogs.push(...downloadResp.output);
217-
218-
if (!downloadResp.success) {
219-
await this._changeLed(device, PROVISIONING_FAILURE);
220-
this._addToJson(this.outputJson, {
221-
esim_id: eid,
222-
device_id: device.deviceId,
223-
expectedProfiles: expectedProfilesArray,
224-
downloadedProfiles: downloadedProfilesArray,
225-
success: false,
226-
timestamp: timestamp,
227-
output: provisionOutputLogs,
161+
const profiles = profileResp.profiles;
162+
profiles.forEach((profile, index) => {
163+
const rspUrl = `1\$${profile.smdp}\$${profile.matching_id}`;
164+
provisionOutputLogs.push(`\t${index + 1}. ${profile.provider} (${rspUrl})`);
228165
});
229-
return;
230-
}
231-
await this._changeLed(device, PROVISIONING_SUCCESS);
232-
233-
const profilesOnDeviceAfterDownload = await this._listProfiles(port);
234-
const iccidsOnDeviceAfterDownload = profilesOnDeviceAfterDownload.map((line) => line.split('[')[1].split(',')[0].trim());
235-
const equal = _.isEqual(_.sortBy(iccidsOnDeviceAfterDownload), _.sortBy(iccidFromJson));
236-
if (!equal) {
237-
provisionOutputLogs.push('Profiles did not match after download');
238-
await this._changeLed(device, PROVISIONING_FAILURE);
239-
this._addToJson(this.outputJson, {
240-
esim_id: eid,
241-
device_id: device.deviceId,
242-
expectedProfiles: expectedProfilesArray,
243-
downloadedProfiles: downloadedProfilesArray,
244-
success: false,
245-
timestamp: timestamp,
246-
output: provisionOutputLogs,
166+
167+
// Download each profile and update the JSON output
168+
await this._changeLed(device, PROVISIONING_PROGRESS);
169+
170+
const downloadResp = await this._doDownload(profiles, port);
171+
const downloadedProfiles = downloadResp.downloadedProfiles;
172+
downloadedProfilesArray = downloadedProfiles.map((profile) => {
173+
return {
174+
status: profile.status,
175+
iccid: profile.iccid,
176+
provider: profile.provider,
177+
duration: profile.duration
178+
};
247179
});
248-
return;
249-
}
180+
provisionOutputLogs.push(...downloadResp.output);
181+
182+
if (!downloadResp.success) {
183+
return outputMsg();
184+
}
250185

251-
// Update the JSON output with the downloaded profiles
252-
// Success case
253-
this._addToJson(this.outputJson, {
254-
esim_id: eid,
255-
device_id: device.deviceId,
256-
expectedProfiles: expectedProfilesArray,
257-
downloadedProfiles: downloadedProfilesArray,
258-
success: true,
259-
timestamp: timestamp,
260-
output: provisionOutputLogs
261-
});
262-
263-
console.log(`${os.EOL}Provisioning complete for EID ${eid}`);
264-
provisionOutputLogs.push(`${os.EOL}Provisioning complete for EID ${eid}`);
186+
const profilesOnDeviceAfterDownload = await this._listProfiles(port);
187+
const iccidsOnDeviceAfterDownload = profilesOnDeviceAfterDownload.map((line) => line.split('[')[1].split(',')[0].trim());
188+
const equal = _.isEqual(_.sortBy(iccidsOnDeviceAfterDownload), _.sortBy(iccidFromJson));
189+
if (!equal) {
190+
provisionOutputLogs.push('Profiles did not match after download');
191+
return outputMsg();
192+
}
193+
194+
// Update the JSON output with the downloaded profiles
195+
// Success case
196+
success = true;
197+
console.log(`${os.EOL}Provisioning complete for EID ${eid}`);
198+
provisionOutputLogs.push(`${os.EOL}Provisioning complete for EID ${eid}`);
199+
return outputMsg();
200+
} catch (error) {
201+
provisionOutputLogs.push(`Error during provisioning: ${error.message}`);
202+
return outputMsg();
203+
}
265204
}
266205

267206
_validateArgs(args) {

0 commit comments

Comments
 (0)