Skip to content

Commit

Permalink
Merge pull request #743 from telefonicaid/task/use_apikey_find_device
Browse files Browse the repository at this point in the history
Task/use apikey find device
  • Loading branch information
fgalan authored Sep 11, 2023
2 parents e9c37c7 + a46175d commit 8977036
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
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

0 comments on commit 8977036

Please sign in to comment.