Skip to content

Commit e3905ed

Browse files
authored
Merge pull request particle-iot#60 from AntonPuko/deviceClean
Device clean
2 parents 2ed25aa + 8c17f12 commit e3905ed

File tree

3 files changed

+44
-67
lines changed

3 files changed

+44
-67
lines changed

src/controllers/DevicesController.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class DevicesController extends Controller {
8282
return this.ok({ result: varValue });
8383
} catch (error) {
8484
const errorMessage = error.message;
85-
if (errorMessage.indexOf('Variable not found') >= 0) {
85+
if (errorMessage.match('Variable not found')) {
8686
throw new HttpError('Variable not found', 404);
8787
}
8888
throw error;
@@ -108,19 +108,21 @@ class DevicesController extends Controller {
108108
}
109109
// 2 flash device with known app
110110
if (postBody.app_id) {
111-
await this._deviceRepository.flashKnownApp(
111+
const flashStatus = await this._deviceRepository.flashKnownApp(
112112
deviceID,
113113
this.user.id,
114114
postBody.app_id,
115115
);
116116

117-
return this.ok({ id: deviceID, status: 'Update started' });
117+
return this.ok({ id: deviceID, status: flashStatus });
118118
}
119119

120120
const file = this.request.files.file[0];
121121
if (file && file.originalname.endsWith('.bin')) {
122-
await this._deviceRepository.flashBinary(deviceID, file);
123-
return this.ok({ id: deviceID, status: 'Update started' });
122+
const flashStatus = await this._deviceRepository
123+
.flashBinary(deviceID, file);
124+
125+
return this.ok({ id: deviceID, status: flashStatus });
124126
}
125127

126128
throw new HttpError('Did not update device');

src/repository/DeviceRepository.js

Lines changed: 21 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -84,28 +84,25 @@ class DeviceRepository {
8484
deviceID,
8585
userID,
8686
);
87+
8788
if (!attributes) {
8889
throw new HttpError('No device found', 404);
8990
}
9091

9192
const device = this._deviceServer.getDevice(attributes.deviceID);
92-
// TODO: Not sure if this should actually be the core ID that gets sent
93-
// but that's what the old source code does :/
94-
const response = device
95-
? await device.onApiMessage(
96-
attributes.deviceID,
97-
{ cmd: 'Ping' },
98-
)
93+
94+
const pingResponse = device
95+
? device.ping()
9996
: {
10097
connected: false,
10198
lastPing: null,
10299
};
103100

104101
return {
105102
...attributes,
106-
connected: response.connected,
103+
connected: pingResponse.connected,
107104
lastFlashedAppName: null,
108-
lastHeard: response.lastPing,
105+
lastHeard: pingResponse.lastPing,
109106
};
110107
};
111108

@@ -117,10 +114,7 @@ class DeviceRepository {
117114

118115
const [attributes, description] = await Promise.all([
119116
this._deviceAttributeRepository.getById(deviceID, userID),
120-
device.onApiMessage(
121-
deviceID,
122-
{ cmd: 'Describe' },
123-
),
117+
device.getDescription(),
124118
]);
125119

126120
if (!attributes) {
@@ -130,10 +124,10 @@ class DeviceRepository {
130124
return ({
131125
...attributes,
132126
connected: true,
133-
functions: description.f,
127+
functions: description.state.f,
134128
lastFlashedAppName: null,
135129
lastHeard: new Date(),
136-
variables: description.v,
130+
variables: description.state.v,
137131
});
138132
};
139133

@@ -142,23 +136,19 @@ class DeviceRepository {
142136
await this._deviceAttributeRepository.getAll(userID);
143137
const devicePromises = devicesAttributes.map(async attributes => {
144138
const device = this._deviceServer.getDevice(attributes.deviceID);
145-
// TODO: Not sure if this should actually be the core ID that gets sent
146-
// but that's what the old source code does :/
147-
const response = device
148-
? await device.onApiMessage(
149-
attributes.deviceID,
150-
{ cmd: 'Ping' },
151-
)
139+
140+
const pingResponse = device
141+
? device.ping()
152142
: {
153143
connected: false,
154144
lastPing: null,
155145
};
156146

157147
return {
158148
...attributes,
159-
connected: response.connected,
149+
connected: pingResponse.connected,
160150
lastFlashedAppName: null,
161-
lastHeard: response.lastPing,
151+
lastHeard: pingResponse.lastPing,
162152
};
163153
});
164154

@@ -179,16 +169,11 @@ class DeviceRepository {
179169
if (!device) {
180170
throw new HttpError('Could not get device for ID', 404);
181171
}
182-
const result = await device.onApiMessage(
183-
deviceID,
184-
{ cmd: 'CallFn', name: functionName, args: functionArguments },
185-
);
186172

187-
if (result.error) {
188-
throw result.error;
189-
}
190-
191-
return result.result;
173+
return await device.callFunction(
174+
functionName,
175+
functionArguments,
176+
);
192177
};
193178

194179
getVariableValue = async (
@@ -204,16 +189,8 @@ class DeviceRepository {
204189
if (!device) {
205190
throw new HttpError('Could not get device for ID', 404);
206191
}
207-
const result = await device.onApiMessage(
208-
deviceID,
209-
{ cmd: 'GetVar', name: varName },
210-
);
211-
212-
if (result.error) {
213-
throw result.error;
214-
}
215192

216-
return result;
193+
return await device.getVariableValue(varName);
217194
};
218195

219196
flashBinary = async (
@@ -225,16 +202,7 @@ class DeviceRepository {
225202
throw new HttpError('Could not get device for ID', 404);
226203
}
227204

228-
const result = await device.onApiMessage(
229-
deviceID,
230-
{ cmd: 'UFlash', args: { data: file.buffer } },
231-
);
232-
233-
if (result.error) {
234-
throw result.error;
235-
}
236-
237-
return result.result;
205+
return await device.flash(file.buffer);
238206
};
239207

240208
flashKnownApp = async (
@@ -257,16 +225,7 @@ class DeviceRepository {
257225
throw new HttpError('Could not get device for ID', 404);
258226
}
259227

260-
const result = await device.onApiMessage(
261-
deviceID,
262-
{ cmd: 'UFlash', args: { data: knownFirmware } },
263-
);
264-
265-
if (result.error) {
266-
throw result.error;
267-
}
268-
269-
return result.result;
228+
return await device.flash(knownFirmware);
270229
};
271230

272231
provision = async (

test/setup/SparkCoreMock.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@ class SparkCoreMock {
44
onApiMessage() {
55
return true;
66
}
7+
8+
getVariableValue = (): Object => 0;
9+
10+
getDescription = (): Object => ({
11+
firmware_version: '0.6.0',
12+
product_id: '6',
13+
state: {
14+
f: null,
15+
v: null,
16+
},
17+
});
18+
19+
ping = (): Object => ({
20+
connected: false,
21+
lastPing: new Date(),
22+
});
723
}
824

925
export default SparkCoreMock;

0 commit comments

Comments
 (0)