-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
221 lines (205 loc) · 9.01 KB
/
server.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// DEFINE VAR -------------------------------------------------------------------------------------------------------- //
const serve = require('koa-static');
const Koa = require('koa');
const app = new Koa();
app.use(serve('dist'));
const server = require('http').createServer(app.callback())
const io = require('socket.io')(server);
const getSdk = require('balena-sdk');
const balena = getSdk({ apiUrl: process.env.BALENACLOUD_API_URL });
const jsonrpc = require('jsonrpc-lite');
const fetch = require("node-fetch");
var uuid = "empty";
var intervalid = null;
var myservices = [];
var mydevices = [];
// INIT BALENA CLOUD ------------------------------------------------------------------------------------------------- //
// auth to Balena cloud
balena.auth.isLoggedIn(function(error, isLoggedIn) {
if (error) throw error;
if (isLoggedIn) {
balena.auth.whoami(function(error, username) {
if (error) throw error;
console.log('Connected to Balena Cloud with username:', username);
});
} else {
console.log('Not Connected to Balena Cloud');
balena.auth.loginWithToken(process.env.BALENACLOUD_API_KEY, function(error) {
if (error) throw error;
balena.auth.whoami(function(error, username) {
if (error) throw error;
console.log('Connected to Balena Cloud with username:', username);
});
});
}
});
// CORE APP -----------------------------------------------------------------------------------------------------------//
io.on('connection', function (socket) {
console.log('socket.io connected');
socket.emit('device name', process.env.SNAPCAST_DEVICE_NAME);
socket.emit('application name', process.env.BALENA_APP_NAME);
balena.models.device.getAllByApplication(process.env.BALENA_APP_NAME).then(function(devices) {
mydevices = [];
devices.forEach(function(device) {
let mydevice = {id:device.id, name:device.device_name, uuid:device.uuid, status:device.status, is_online:device.is_online, ip_address:device.ip_address, };
mydevices.push(mydevice);
if (device.device_name == process.env.BALENA_DEVICE_NAME_AT_INIT) {
balena.models.device.getWithServiceDetails(device.id).then(function(device) {
myservices = [];
Object.keys(device.current_services).forEach(function(serviceName) {
let myservice = {name:serviceName, status:device.current_services[serviceName][0].status, id:device.current_services[serviceName][0].id, image_id:device.current_services[serviceName][0].image_id};
myservices.push(myservice);
});
socket.emit('services', myservices);
});
}
});
socket.emit('devices', mydevices);
});
socket.on('realtime', function(realtime) {
if (!realtime) {
socket.emit('notification', 'enable realtime');
intervalid = setInterval(function(){
balena.models.device.getAllByApplication(process.env.BALENA_APP_NAME).then(function(devices) {
devices.forEach(function(device) {
if (device.device_name == process.env.BALENA_DEVICE_NAME_AT_INIT) {
balena.models.device.getWithServiceDetails(device.id).then(function(device) {
myservices = [];
Object.keys(device.current_services).forEach(function(serviceName) {
let service = {name:serviceName, status:device.current_services[serviceName][0].status, id:device.current_services[serviceName][0].id, image_id:device.current_services[serviceName][0].image_id};
myservices.push(service);
});
socket.emit('services', myservices);
});
}
});
});
}, 2000);
} else {
if (intervalid) {
socket.emit('notification', 'disable realtime');
clearInterval(intervalid);
}
}
});
socket.on('reboot device', function(){
balena.models.device.getAllByApplication(process.env.BALENA_APP_NAME).then(function(devices) {
devices.forEach(function(device) {
if (device.device_name == process.env.BALENA_DEVICE_NAME_AT_INIT) {
let notification = 'reboot device : ' + device.device_name;
socket.emit('notification', notification);
balena.models.device.reboot(device.id);
}
});
});
});
socket.on('restart services', function(){
balena.models.device.getAllByApplication(process.env.BALENA_APP_NAME).then(function(devices) {
devices.forEach(function(device) {
if (device.device_name == process.env.BALENA_DEVICE_NAME_AT_INIT) {
let notification = 'restart all service on : ' + device.device_name;
socket.emit('notification', notification);
balena.models.device.restartApplication(device.id);
}
});
});
});
socket.on('reboot all devices', function(){
balena.models.application.getAll(function(error, applications) {
applications.forEach(function(application) {
if (application.app_name == process.env.BALENA_APP_NAME) {
let notification = 'reboot all devices for : ' + application.app_name;
socket.emit('notification', notification);
balena.models.application.reboot(application.id);
}
});
});
});
socket.on('restart all services', function(){
balena.models.application.getAll(function(error, applications) {
applications.forEach(function(application) {
if (application.app_name == process.env.BALENA_APP_NAME) {
let notification = 'restart all services for : ' + application.app_name;
socket.emit('notification', notification);
balena.models.application.restart(application.id);
}
});
});
});
socket.on('stop service', function(imageID){
balena.models.device.getAllByApplication(process.env.BALENA_APP_NAME).then(function(devices) {
devices.forEach(function(device) {
if (device.device_name == process.env.BALENA_DEVICE_NAME_AT_INIT) {
balena.models.device.stopService(device.id, imageID).then(function() {
balena.models.device.getWithServiceDetails(device.id).then(function(device) {
Object.keys(device.current_services).forEach(function(serviceName) {
if (device.current_services[serviceName][0].image_id == imageID) {
var notification = 'service ' + serviceName + ' is stopping on ' + process.env.BALENA_DEVICE_NAME_AT_INIT
socket.emit('notification', notification);
}
});
});
});
}
});
});
});
socket.on('start service', function(imageID){
balena.models.device.getAllByApplication(process.env.BALENA_APP_NAME).then(function(devices) {
devices.forEach(function(device) {
if (device.device_name == process.env.BALENA_DEVICE_NAME_AT_INIT) {
balena.models.device.startService(device.id, imageID).then(function() {
balena.models.device.getWithServiceDetails(device.id).then(function(device) {
Object.keys(device.current_services).forEach(function(serviceName) {
if (device.current_services[serviceName][0].image_id == imageID) {
var notification = 'service ' + serviceName + ' is starting on ' + process.env.BALENA_DEVICE_NAME_AT_INIT
socket.emit('notification', notification);
}
});
});
});
}
});
});
});
socket.on('restart service', function(imageID){
balena.models.device.getAllByApplication(process.env.BALENA_APP_NAME).then(function(devices) {
devices.forEach(function(device) {
if (device.device_name == process.env.BALENA_DEVICE_NAME_AT_INIT) {
balena.models.device.restartService(device.id, imageID).then(function() {
balena.models.device.getWithServiceDetails(device.id).then(function(device) {
Object.keys(device.current_services).forEach(function(serviceName) {
if (device.current_services[serviceName][0].image_id == imageID) {
var notification = 'service ' + serviceName + ' is restarting on ' + process.env.BALENA_DEVICE_NAME_AT_INIT
socket.emit('notification', notification);
}
});
});
});
}
});
});
});
socket.on('disconnect', function(){ console.log('socket.io disconnected') });
});
var myjson = jsonrpc.request('1', 'Client.GetStatus', {"id":"b8:27:eb:eb:59:68"});
fetch('http://192.168.100.226:1780/jsonrpc', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
body: JSON.stringify(myjson),
})
.then(response => response.json())
.then(function(response) {
console.log(response.result.client.config);
});
//balena.models.device.restartService(2910251, 2296310).then(function() {
// console.log('restart snapcast !')
//});
// HTTP SERVER --------------------------------------------------------------------------------------------------------//
// http server on 3000
server.listen(3000, function () {
console.log('Server listening on port 3000\n');
});