Skip to content
This repository was archived by the owner on Nov 16, 2023. It is now read-only.

Commit 73810c0

Browse files
authored
UDF for multiple motion sensors (#60)
* UDF for multiple motion sensors * Review suggestions from Stefan * var occupancyStatus
1 parent fd00abb commit 73810c0

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
// Sample code for detecting motion in an area with multiple motion sensors:
5+
// This code is designed to work with the existing sample code.
6+
// To test this end to end, make the following changes:
7+
// 1) In the provisionSample.yaml, change the "script" key to point to this file (multiplemotionsensors.js).
8+
// 2) In the provisionSample.yaml, add a couple more motion sensors. For example, the sample "sensors" node could look like this:
9+
// sensors:
10+
// - dataType: Motion
11+
// hardwareId: SAMPLE_SENSOR_MOTION_ONE
12+
// - dataType: CarbonDioxide
13+
// hardwareId: SAMPLE_SENSOR_CARBONDIOXIDE
14+
// - dataType: Motion
15+
// hardwareId: SAMPLE_SENSOR_MOTION_TWO
16+
// - dataType: Motion
17+
// hardwareId: SAMPLE_SENSOR_MOTION_THREE
18+
// 3) Add these additional motion sensors to the device-connectivity project's appSettings.json. For example:
19+
// "Sensors": [{
20+
// "DataType": "Motion",
21+
// "HardwareId": "SAMPLE_SENSOR_MOTION_ONE"
22+
// },{
23+
// "DataType": "CarbonDioxide",
24+
// "HardwareId": "SAMPLE_SENSOR_CARBONDIOXIDE"
25+
// },{
26+
// "DataType": "Motion",
27+
// "HardwareId": "SAMPLE_SENSOR_MOTION_TWO"
28+
// },{
29+
// "DataType": "Motion",
30+
// "HardwareId": "SAMPLE_SENSOR_MOTION_THREE"
31+
// }]
32+
//
33+
//
34+
35+
36+
var motionType = "Motion";
37+
var occupancyStatus = "AvailableAndFresh";
38+
39+
function process(telemetry, executionContext) {
40+
try {
41+
// Log SensorId and Message
42+
log(`Sensor ID: ${telemetry.SensorId}. `);
43+
log(`Sensor value: ${JSON.stringify(telemetry.Message)}.`);
44+
45+
// Get sensor metadata
46+
var sensor = getSensorMetadata(telemetry.SensorId);
47+
48+
// Retrieve the sensor reading
49+
var parseReading = JSON.parse(telemetry.Message);
50+
51+
// Set the sensor reading as the current value for the sensor.
52+
setSensorValue(telemetry.SensorId, sensor.DataType, parseReading.SensorValue);
53+
54+
// Get parent space
55+
var parentSpace = sensor.Space();
56+
57+
// Get all children sensors of the parent space
58+
var allSensors = parentSpace.ChildSensors();
59+
60+
// Get all motion sensors of the parent space
61+
var motionSensors = allSensors.filter(function(item){
62+
return item.DataType === motionType;
63+
});
64+
65+
// Is any of the motion sensors detecting motion?
66+
var motionDetected = motionSensors.find(function(element) {
67+
return element.Value().Value.toLowerCase() === "true";
68+
});
69+
70+
// Set OccupancyStatus for the space
71+
var occupied = "Room is occupied";
72+
var empty = "Room is empty!";
73+
74+
if (motionDetected != undefined)
75+
{
76+
// Motion is detected by at least one sensor
77+
log(`${occupied}. Motion Detected: ${motionDetected}. Sensors: ${motionSensors}.`);
78+
setSpaceValue(parentSpace.Id, occupancyStatus, occupied);
79+
}
80+
else {
81+
// No motion detected by any sensor
82+
log(`${empty}. Motion Not Detected: ${motionDetected}. Sensors: ${motionSensors}.`);
83+
setSpaceValue(parentSpace.Id, occupancyStatus, empty);
84+
// You could try creating a Logic App mail notification in case none of the motion sensors
85+
// detect anything. In that case, uncomment the following line:
86+
// parentSpace.Notify(JSON.stringify(empty));
87+
}
88+
}
89+
catch (error)
90+
{
91+
var errormsg = `An error has occurred processing the UDF Error: ${error.name} Message ${error.message}.`;
92+
log(errormsg);
93+
setSpaceValue(parentSpace.Id, occupancyStatus, errormsg);
94+
}
95+
}

0 commit comments

Comments
 (0)