Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task/use apikey find device #743

Merged
merged 9 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
- Fix: ensure device apikey in already provisioned device (iota-node-lib#1430)
- Fix: try to use apikey from measure/group to find, update and remove device in first attempt (iota-node-lib#1426)
- Fix: ensure device apikey in already provisioned device (iota-node-lib#1430, iota-node-lib#1435)
14 changes: 9 additions & 5 deletions lib/iotaUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ function createConfigurationNotification(results) {
return configurations;
}

function findOrCreate(deviceId, transport, group, callback) {
iotAgentLib.getDeviceSilently(deviceId, group.service, group.subservice, function (error, device) {
function findOrCreate(deviceId, transport, apikey, group, callback) {
iotAgentLib.getDeviceSilently(deviceId, apikey, group.service, group.subservice, function (error, device) {
if (!error && device) {
if (
(!('apikey' in device) || device.apikey === undefined) &&
Expand All @@ -151,11 +151,12 @@ function findOrCreate(deviceId, transport, group, callback) {
.getLogger()
.info(context, 'Update provisioned device %j with measure/group apikey %j', device, group.apikey);
device.apikey = group.apikey; // group apikey is the same of current measure apikey
iotAgentLib.updateRegister(device, function (error, device) {
iotAgentLib.updateDevice(device, function (error) {
callback(error, device, group);
});
} else {
callback(null, device, group);
}
callback(null, device, group);
} else if (error.name === 'DEVICE_NOT_FOUND') {
const newDevice = {
id: deviceId,
Expand Down Expand Up @@ -186,6 +187,9 @@ function findOrCreate(deviceId, transport, group, callback) {
}
// Check autoprovision flag in order to register or not device
if (group.autoprovision === undefined || group.autoprovision === true) {
config
.getLogger()
.debug(context, 'Registering autoprovision of Device %j for its conf %j', newDevice, group);
iotAgentLib.register(newDevice, function (error, device) {
callback(error, device, group);
});
Expand Down Expand Up @@ -237,7 +241,7 @@ function retrieveDevice(deviceId, apiKey, transport, callback) {
async.waterfall(
[
apply(iotAgentLib.getConfigurationSilently, config.getConfig().iota.defaultResource || '', apiKey),
apply(findOrCreate, deviceId, transport),
apply(findOrCreate, deviceId, transport, apiKey), // group.apikey and apikey are the same
apply(
iotAgentLib.mergeDeviceWithConfiguration,
['lazy', 'active', 'staticAttributes', 'commands', 'subscriptions'],
Expand Down
Loading