зеркало из
1
0
Форкнуть 0

Merge pull request #843 from olivakar/ok-handler-twin

chore(azure-iot-device) : update the handler for twin
This commit is contained in:
olivakar 2020-07-09 11:07:36 -08:00 коммит произвёл GitHub
Родитель a30f326bef f298426500
Коммит 5712b44d12
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
1 изменённых файлов: 34 добавлений и 27 удалений

Просмотреть файл

@ -24,6 +24,7 @@ let intervalToken3;
const commandNameGetMaxMinReport1 = thermostat1ComponentName + commandComponentCommandNameSeparator + 'getMaxMinReport'; const commandNameGetMaxMinReport1 = thermostat1ComponentName + commandComponentCommandNameSeparator + 'getMaxMinReport';
const commandNameGetMaxMinReport2 = thermostat2ComponentName + commandComponentCommandNameSeparator + 'getMaxMinReport'; const commandNameGetMaxMinReport2 = thermostat2ComponentName + commandComponentCommandNameSeparator + 'getMaxMinReport';
const commandNameReboot = 'reboot'; const commandNameReboot = 'reboot';
const serialNumber = 'alwinexlepaho8329';
const commandHandler = async (request, response) => { const commandHandler = async (request, response) => {
helperLogCommandRequest(request); helperLogCommandRequest(request);
@ -97,34 +98,41 @@ const updateComponentReportedProperties = (deviceTwin, patch, componentName) =>
}); });
}; };
const helperAttachHandlerForDesiredPropertyPatches = (deviceTwin) => { const desiredPropertyPatchListener = (deviceTwin, componentNames) => {
deviceTwin.on('properties.desired', (delta) => { deviceTwin.on('properties.desired', (delta) => {
if (delta[thermostat1ComponentName] || delta[thermostat2ComponentName] || delta[deviceInfoComponentName]) { console.log('Received an update for device with value: ' + JSON.stringify(delta));
Object.entries(delta).forEach(([key, values]) => {
const version = delta.$version; const version = delta.$version;
const componentName = Object.keys(delta)[0]; if (!!(componentNames) && componentNames.includes(key)) { // then it is a component we are expecting
let logLine; const componentName = key;
if (!!(componentName)) { const patchForComponents = { [componentName]: {} };
logLine = 'Received an update for component: ' + componentName + ' with value: ' + JSON.stringify(delta); Object.entries(values).forEach(([propertyName, propertyValue]) => {
} else { if (propertyName !== '__t' && propertyName !== '$version') {
logLine = 'Received an update for root interface with value: ' + JSON.stringify(delta); console.log('Will update property: ' + propertyName + ' to value: ' + propertyValue + ' of component: ' + componentName);
const propertyContent = { value: propertyValue };
propertyContent.ac = 200;
propertyContent.ad = 'Successfully executed patch';
propertyContent.av = version;
patchForComponents[componentName][propertyName] = propertyContent;
}
});
updateComponentReportedProperties(deviceTwin, patchForComponents, componentName);
} }
console.log(logLine); else if (key !== '$version') { // individual property for root
const patch = { [componentName]: {} }; const patchForRoot = { };
Object.entries(delta[componentName]).forEach(([propertyName, propertyValue]) => { console.log('Will update property: ' + key + ' to value: ' + values + ' for root');
if (propertyName !== '__t') { const propertyContent = { value: values };
const propertyContent = { value: propertyValue }; propertyContent.ac = 200;
propertyContent.ac = 200; propertyContent.ad = 'Successfully executed patch';
propertyContent.ad = 'Successfully executed patch'; propertyContent.av = version;
propertyContent.av = version; patchForRoot[key] = propertyContent;
patch[componentName][propertyName] = propertyContent; updateComponentReportedProperties(deviceTwin, patchForRoot, null);
} }
}); });
updateComponentReportedProperties(deviceTwin, patch, componentName);
}
}); });
}; };
const helperAttachExitListener = async (deviceClient) => { const exitListener = async (deviceClient) => {
const standardInput = process.stdin; const standardInput = process.stdin;
standardInput.setEncoding('utf-8'); standardInput.setEncoding('utf-8');
console.log('Please enter q or Q to exit sample.'); console.log('Please enter q or Q to exit sample.');
@ -197,18 +205,17 @@ async function main() {
}, 6000); }, 6000);
// attach a standard input exit listener // attach a standard input exit listener
helperAttachExitListener(client); exitListener(client);
try { try {
resultTwin = await client.getTwin(); resultTwin = await client.getTwin();
const patchRoot = helperCreateReportedPropertiesPatch({ serialNumber: 'alwinexlepaho8329' }, null); // Only report readable propertiess
const patchRoot = helperCreateReportedPropertiesPatch({ serialNumber: {"value" : serialNumber, "ac" : 200, "av" : 1} }, null);
const patchThermostat1Info = helperCreateReportedPropertiesPatch({ const patchThermostat1Info = helperCreateReportedPropertiesPatch({
targetTemperature: { 'value': 56.78, 'ac': 200, 'ad': 'first report', 'av': 1 },
maxTempSinceLastReboot: 67.89, maxTempSinceLastReboot: 67.89,
}, thermostat1ComponentName); }, thermostat1ComponentName);
const patchThermostat2Info = helperCreateReportedPropertiesPatch({ const patchThermostat2Info = helperCreateReportedPropertiesPatch({
targetTemperature: { 'value': 35.67, 'ac': 200, 'ad': 'first report', 'av': 1 },
maxTempSinceLastReboot: 98.65, maxTempSinceLastReboot: 98.65,
}, thermostat2ComponentName); }, thermostat2ComponentName);
@ -228,7 +235,7 @@ async function main() {
updateComponentReportedProperties(resultTwin, patchThermostat1Info, thermostat1ComponentName); updateComponentReportedProperties(resultTwin, patchThermostat1Info, thermostat1ComponentName);
updateComponentReportedProperties(resultTwin, patchThermostat2Info, thermostat2ComponentName); updateComponentReportedProperties(resultTwin, patchThermostat2Info, thermostat2ComponentName);
updateComponentReportedProperties(resultTwin, patchDeviceInfo, deviceInfoComponentName); updateComponentReportedProperties(resultTwin, patchDeviceInfo, deviceInfoComponentName);
helperAttachHandlerForDesiredPropertyPatches(resultTwin); desiredPropertyPatchListener(resultTwin, [thermostat1ComponentName, thermostat2ComponentName, deviceInfoComponentName]);
} catch (err) { } catch (err) {
console.error('could not retrieve twin or report twin properties\n' + err.toString()); console.error('could not retrieve twin or report twin properties\n' + err.toString());
} }