This repository has been archived by the owner on Oct 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathelevator-01-state.js
100 lines (86 loc) · 2.55 KB
/
elevator-01-state.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright (c) Microsoft. All rights reserved.
/*global log*/
/*global updateState*/
/*global updateProperty*/
/*jslint node: true*/
"use strict";
var floors = 15;
// Default state
var state = {
online: true,
floor: 1,
vibration: 10.0,
vibration_unit: "mm",
temperature: 75.0,
temperature_unit: "F",
moving: true
};
// Default properties
var properties = {};
/**
* Restore the global state using data from the previous iteration.
*
* @param previousState device state from the previous iteration
* @param previousProperties device properties from the previous iteration
*/
function restoreSimulation(previousState, previousProperties) {
// If the previous state is null, force a default state
if (previousState) {
state = previousState;
} else {
log("Using default state");
}
if (previousProperties) {
properties = previousProperties;
} else {
log("Using default properties");
}
}
/**
* Simple formula generating a random value around the average
* in between min and max
*/
function vary(avg, percentage, min, max) {
var value = avg * (1 + ((percentage / 100) * (2 * Math.random() - 1)));
value = Math.max(value, min);
value = Math.min(value, max);
return value;
}
function varyfloor(current, min, max) {
if (current === min) {
return current + 1;
}
if (current === max) {
return current - 1;
}
if (Math.random() < 0.5) {
return current - 1;
}
return current + 1;
}
/**
* Entry point function called by the simulation engine.
* Returns updated simulation state.
* Device property updates must call updateProperties() to persist.
*
* @param context The context contains current time, device model and id
* @param previousState The device state since the last iteration
* @param previousProperties The device properties since the last iteration
*/
/*jslint unparam: true*/
function main(context, previousState, previousProperties) {
// Restore the global device properties and the global state before
// generating the new telemetry, so that the telemetry can apply changes
// using the previous function state.
restoreSimulation(previousState, previousProperties);
if (state.moving) {
state.floor = varyfloor(state.floor, 1, floors);
// 10 +/- 5%, Min 0, Max 20
state.vibration = vary(10, 5, 0, 20);
} else {
state.vibration = 0;
}
// 75 +/- 1%, Min 25, Max 100
state.temperature = vary(75, 1, 25, 100);
updateState(state);
}