-
Notifications
You must be signed in to change notification settings - Fork 8
/
server.js
executable file
·273 lines (242 loc) · 7.86 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/************************************************************************************************
* ORKA Server
* -----------
* Entry point of orka
* loads ui and opens sockets for client communication. Acts as a bridge between ui and the clients
*
*
***********************************************************************************************/
'use strict'
// const electron = require('electron')
const {app, BrowserWindow} = require('electron')
const io = require('socket.io')()
const {ipcMain} = require('electron')
const settings = require('./settings.js') // Persistent settings storage
const port = settings.getServerOptions().port || 8000
const piSocketManager = require('./pi-Communicator.js') // maps socket id into name and vice versa
let win = null
let contents = null
function init () {
win = new BrowserWindow({width: 800, height: 500, show: false})
win.loadURL(`file://${__dirname}/ui/index.html`)
win.webContents.openDevTools()
// win.maximize();
win.setFullScreen(true)
win.once('closed', () => {
win = null
})
// wait for electron to load the page
win.once('ready-to-show', () => {
win.show()
})
contents = win.webContents
}
(function () {
app.once('ready', init)
io.listen(port)
})()
app.on('window-all-closed', () => {
app.quit()
})
io.on('connection', function (socket) {
var clientName = socket.handshake.query.client_name // Client name should be passed during connection
if (clientName === undefined) {
socket.disconnect()
} else {
piSocketManager.addSocket(clientName, socket.id)
contents.send('setPiConnectionStatus', {
name: clientName,
connected: true
})
}
socket.on('disconnect', function () {
if (piSocketManager.getNameFromSocketId(socket.id) != null) {
contents.send('setPiConnectionStatus', {
name: piSocketManager.getNameFromSocketId(socket.id),
connected: false
})
}
piSocketManager.removeSocket(socket.id)
})
/************************************************************************************************
*
* The following are the events supported by Orka. These events should in sync with orka-IPC-bridge.js.
*
* <- denotes the events coming from client to Orka Server
* -> denotes the events going to the client from the Orka Server
* -- denotes the events which flows through the Orka UI and Orka Server
*
*
***********************************************************************************************/
/**
* Event <- stats; statistics received from client
* @type object
*/
socket.on('stats', function (data) {
var name = piSocketManager.getNameFromSocketId(socket.id) // convert socket id into name
if (!name) {
console.error('un-registered client is sending stats')
}
if (name != null) {
contents.send('stats', {
'name': name,
'data': data
})
} else {
socket.disconnect()
}
})
/**
* Event <- output; output of executed command from client. see TaskSchedular and Batch Execution
* @type object
*/
socket.on('output', function (data) {
contents.send('output', {
'name': piSocketManager.getNameFromSocketId(socket.id),
output: data
})
})
/**
* Event <- alert;generated by the client when the statistics threashold is surpassed
* @type string
*/
socket.on('alert', (data) => {
console.log(data)
contents.send('alert', {
name: piSocketManager.getNameFromSocketId(socket.id),
message: data
})
})
/**
* Event <- systemInfo; basic system information from the client. Obtained at the connecting phase
* @type Object
*/
socket.on('systemInfo', (data) => {
contents.send('systemInfo', {
name: piSocketManager.getNameFromSocketId(socket.id),
data
})
})
})
/**
* Event -> Command; the command sent to client and executed
* @type string
*/
ipcMain.on('Command', (event, name, command) => {
var socketId = piSocketManager.getSocketIdFromName(name)
if (socketId != null) {
io.sockets.connected[socketId].emit('Command', command)
}
})
ipcMain.on('piRemoved', function (event, item) {
var sock_id = piSocketManager.getSocketIdFromName(item)
var client_sock = io.sockets.connected[sock_id]
// remove from both memory and storage
piSocketManager.removeSocket(sock_id)
settings.removeClient(item)
if (client_sock != undefined)
client_sock.disconnect()
})
ipcMain.on('piAdded', function (event, args) {
// when added , try to connect the client
var param = {}
param['name'] = args.name
// client connection settings defines ip,name and port for the client to connect to
param['settings'] = settings.getClientConnectionSettings()
piSocketManager.connectToPi(args.ip, args.port, param)
settings.addClient(args.name, {
ip: args.ip,
port: args.port
})
})
ipcMain.on('listCreated', (event, listName, args) => {
settings.addList(listName, args)
})
ipcMain.on('listRemoved', (event, listName) => {
settings.removeList(listName)
})
ipcMain.on('clientAddedToList', (event, listName, clients) => {
settings.addClientsToList(listName, clients)
})
ipcMain.on('clientRemovedFromList', (event, listName, clients) => {
settings.removeClientFromList(listName, clients)
})
ipcMain.on('taskCreated', (event, taskName, args) => {
settings.addTask(taskName, args)
})
ipcMain.on('taskDeleted', (event, taskname) => {
settings.removeTask(taskname)
})
ipcMain.on('disconnect', (event, name) => {
var sock_id = piSocketManager.getSocketIdFromName(name)
var client_sock = io.sockets.connected[sock_id]
if (client_sock != undefined) {
client_sock.disconnect()
}
})
ipcMain.on('connect', (event, args) => {
var param = {}
param['name'] = args.name
param['settings'] = settings.getClientConnectionSettings()
piSocketManager.connectToPi(args.ip, args.port, param)
})
ipcMain.on('quit', () => {
var pi = piSocketManager.getAllSocketsName()
for (var name in pi) {
io.sockets.connected[piSocketManager.getSocketIdFromName(pi[name])].disconnect()
}
setTimeout(function () {
app.quit()
}, 100)
})
/**
* Event -- open-url; opens URL in a new electron window
* @type {BrowserWindow}
*/
ipcMain.on('open-url', (event, clientname, hostname) => {
let shell = new BrowserWindow({width: 600, height: 600, title: clientname})
// the url is passed as parameter to the html file, which load the URL into webview.
// bypass cross-origin-policy
shell.loadURL(`file://${__dirname}/ui/test.html?${hostname}`)
shell.on('closed', () => {
shell = null
})
})
ipcMain.on('minimize', () => win.minimize())
/**
* events -- *; to read settings from secondary storage
*/
ipcMain.on('client-info-settings', function (event) {
event.returnValue = settings.getClients() || {}
})
ipcMain.on('lists-info-settings', function (event) {
event.returnValue = settings.getAllLists() || {}
})
ipcMain.on('tasks-info-settings', function (event) {
event.returnValue = settings.getAllTasks() || {}
})
ipcMain.on('client-connection-settings', function (event) {
event.returnValue = settings.getClientConnectionSettings() || {}
})
ipcMain.on('notification-settings', function (event) {
event.returnValue = settings.getNotificationStatus() || {}
})
ipcMain.on('toggle-notification-status', function (event, type, state) {
settings.toggleNotificationStatus(type, state)
})
ipcMain.on('set-server-options', function (event, port) {
settings.setServerOptions(port)
})
ipcMain.on('get-server-options', function (event) {
event.returnValue = port
})
ipcMain.on('set-client-connection-settings', function (event, options) {
settings.setClientConnectionSettings(options)
})
ipcMain.on('set-flock-webhook', function (event, webhook) {
settings.setFlockWebHook(webhook)
})
ipcMain.on('reset-default-settings', function (event) {
settings.resetSettings()
contents.send('settings-restored-to-default')
})