-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
149 lines (127 loc) · 6.29 KB
/
main.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
var Protocol = require('azure-iot-device-mqtt').Mqtt;
var ModuleClient = require('azure-iot-device').ModuleClient;
var axios = require('axios');
var pjson = require('./package.json');
/*
client must be abe to implement
-----------------------------------------------------------------------
| type | name | desc |
-----------------------------------------------------------------------
| twin desired string | fileURI | download a json |
| direct command + params | restart(string) | send json to md |
| direct command + params | start(string) | start md engine |
| direct command + params | stop(string) | stop md engine |
-----------------------------------------------------------------------
(string) should be a URI inc ports within docker i.e. http://mockDeviceDe1:9000
*/
console.log('mock-devices-edge starting up - version' + pjson.version);
let latestState = {};
let reportPayload = {};
ModuleClient.fromEnvironment(Protocol, function (err, client) {
if (err) {
console.error('Could not create client from Environment: ' + err.toString());
process.exit(-1);
} else {
client.on('error', function (err) {
console.error(`Client fatal error: ${err.message}`);
process.exit(-1);
});
client.open(function (err) {
if (err) {
console.error('Client open error: ' + err.toString());
console.error(err.message);
process.exit(-1);
};
console.log('Starting client');
reportPayload = { "status": "Client started " + pjson.version }
client.onMethod('start', (request, response) => {
const methodName = request['methodName'];
const methodPayload = request['payload'];
if (methodName === 'start') {
const url = `${methodPayload}/api/devices/start`;
console.log('Server requested: ' + url);
axios.get(url)
.then((res) => {
response.send(200, 'Start success'), (err) => {
if (err) { console.log('Start response error:' + err.message); return; }
reportPayload = { "status": `[${new Date().toISOString()}] Start for ${methodPayload}` }
}
console.log('Start completed');
})
.catch((err) => {
console.error(`Start post error: ${err.message}`);
})
} else {
console.error('Start method received bad params');
}
});
client.onMethod('stop', (request, response) => {
const methodName = request['methodName'];
const methodPayload = request['payload'];
if (methodName === 'stop') {
const url = `${methodPayload}/api/devices/stop`;
console.log('Server requested: ' + url);
axios.get(url)
.then((res) => {
response.send(200, 'Stop success'), (err) => {
if (err) { console.log('Stop response error:' + err.message); return; }
reportPayload = { "status": `[${new Date().toISOString()}] Stop for ${methodPayload}` }
}
console.log('Stop completed');
})
.catch((err) => {
console.error(`Stop post error: ${err.message}`);
})
} else {
console.error('Stop method received bad params');
}
});
client.onMethod('restart', (request, response) => {
const methodName = request['methodName'];
const methodPayload = request['payload'];
if (methodName === 'restart') {
const url = `${methodPayload}/api/state`;
console.log('Server requested: ' + url);
axios.post(url, latestState)
.then((res) => {
response.send(200, 'Restart success'), (err) => {
if (err) { console.log('Restart response error:' + err.message); return; }
reportPayload = { "status": `[${new Date().toISOString()}] Restart for ${methodPayload}` }
}
console.log('Restart completed');
})
.catch((err) => {
console.error(`Restart post error: ${err.message}`);
})
} else {
console.error('Restart method received bad params');
}
});
client.getTwin(function (err, twin) {
if (err) {
console.error('could not get twin');
} else {
twin.on('properties.desired', function (delta) {
if (delta && delta['fileURI'] && delta['fileURI'] != '') {
console.log('fetching: ' + delta['fileURI']);
axios.get(delta['fileURI'])
.then((res) => {
latestState = res.data;
reportPayload = { "status": `[${new Date().toISOString()}] State file fetched` }
})
.catch((err) => {
console.error(`Fetch get error: ${err.message}`);
})
}
});
setInterval(() => {
twin.properties.reported.update(reportPayload, ((err) => {
if (err) { console.log('Report send error:' + err.message); return; }
reportPayload = {};
}))
}, 1000);
}
});
})
}
})