-
Notifications
You must be signed in to change notification settings - Fork 3
/
ecar.js
178 lines (156 loc) · 4.94 KB
/
ecar.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
* Simple eCar simulator
*
* Call: node ecar.js
*/
// import dependencies
const Servient = require('@node-wot/core').Servient
const HttpServer = require('@node-wot/binding-http').HttpServer
// create Servient add HTTP binding with port configuration
let servient = new Servient();
servient.addServer(new HttpServer({
// port: 8081 // (default 8080)
}));
// runtime data
let soc; // state of charge 0..100%
let driving; // is driving true/false
let status; // readyToCharge, charging, stopCharging
function driveCar() {
console.log("Driving...");
driving = true;
//status = "stopCharging";
setTimeout(function () {
// code after time
let decStep = 5.25;
soc -= 1;
if (soc <= 0) {
driving = false;
soc = 0.0;
console.log("Battery drained :-(");
} else {
// keep on driving ?
if (driving) {
driveCar();
}
}
console.log("Charging status decreased by " + decStep + " -> " + soc);
}, 250);
}
function stopCar() {
console.log("STOP driving!");
driving = false;
}
function chargeCar() {
driving = false;
let intId = setInterval(async function () {
console.log("Charging... (SOC): " + soc + "%");
if (soc == 100) {
soc = 100;
console.log("Battery fully charged :-)");
//complete charging and set stopCharging status
clearInterval(intId); // stop loop
}
if (soc <= 99) {
// charging step
let incStep = 4;
soc += incStep;
}
}, 2000);
}
function stopCharging() {
console.log("STOP charging!");
status = "readyToCharge";
}
servient.start().then((WoT) => {
WoT.produce({
title: "eCar",
description: "eCarThing",
properties: {
soc: {
type: "number",
description: "Current chargingStatus in % (0 ... 100%)",
observable: true,
readOnly: true,
minimum: 0.0,
maximum: 100.0
},
driving: {
type: "boolean",
description: "Is car driving around",
observable: true,
readOnly: true
},
status: {
type: "string",
description: "Current car status (readyToCharge, charging, stopCharging)",
"enum": ["readyToCharge", "charging", "stopCharging"],
observable: true
}
},
actions: {
startDriving: {
description: "Starting to drive"
},
stopDriving: {
description: "Stopping to drive"
},
startCharging: {
description: "Starting to charge"
},
stopCharging: {
description: "Stopping to charge"
}
}
}).then((thing) => {
console.log("Produced " + thing.getThingDescription().title);
// init property values
soc = 32;
driving = false;
status = "notReadyToCharge";
// set property handlers (using async-await)
thing.setPropertyReadHandler("soc", async () => soc);
thing.setPropertyReadHandler("driving", async () => driving);
thing.setPropertyReadHandler("status", async () => status);
// set action handlers (using async-await)
thing.setActionHandler("startDriving", async (params, options) => {
status = "stopCharging";
driveCar();
});
thing.setActionHandler("stopDriving", async (params, options) => {
status = "readyToCharge";
stopCar();
});
thing.setActionHandler("startCharging", async (params, options) => {
status = "charging";
chargeCar();
});
thing.setActionHandler("stopCharging", async (params, options) => {
status = "stopCharging";
stopCharging();
});
// expose the thing
thing.expose().then(() => {
console.info(thing.getThingDescription().title + " ready");
console.info("TD : " + JSON.stringify(thing.getThingDescription()));
thing.readProperty("status").then((c) => {
console.log("eCar status is " + c);
});
});
// after 2.5s change status
setTimeout(function () {
status = "readyToCharge";
thing.readProperty("status").then((c) => {
console.log("eCar status is " + c);
});
}, 2500);
// after 2.5s change status
setInterval(function () {
if(soc==100) {status = "stopCharging"}
}, 2000);
});
});
// turn off messages from core package
const debug = console.debug
console.debug = (package, ...args) => { }
const warn = console.warn
console.warn = (package, ...args) => { }