forked from tagyoureit/nodejs-poolController
-
Notifications
You must be signed in to change notification settings - Fork 0
/
outputSocketToConsoleExample.js
executable file
·77 lines (67 loc) · 3.3 KB
/
outputSocketToConsoleExample.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
/* nodejs-poolController. An application to control pool equipment.
* Copyright (C) 2016, 2017. Russell Goldin, tagyoureit. russ.goldin@gmail.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//This makes the module available to the app through BottleJS
module.exports = function(container) {
//load the configuration file
var configFile = container.settings.getConfig()
//and read the variables we put there
var level = configFile.outputSocketToConsoleExample.level
var protocol_http = configFile.poolController.http.enabled
var protocol_https = configFile.poolController.https.enabled
var serverURL;
var secureTransport;
//The following IF statement sets the varibles if the transport is either HTTP or HTTPS
if (protocol_https === 0) {
serverURL = 'http://localhost:' + bottle.container.settings.get('httpExpressPort') + '/'
secureTransport = false
} else {
serverURL = 'https://localhost:' + bottle.container.settings.get('httpsExpressPort') + '/'
secureTransport = true
}
//we listen to events with the socket client
var io = container.socketClient
var socket = io.connect(serverURL, {
secure: secureTransport,
reconnect: true,
rejectUnauthorized: false
});
//This is a listener for the time event. data is what is received.
socket.on('time', function(data) {
console.log('outputSocketToConsoleExample: The time was broadcast, and it was received. The time is: %s', JSON.stringify(data))
})
//The 'connect' function fires when the socket is connected
socket.on('connect', function(err) {
console.log('outputSocketToConsoleExample: Socket connected to socket @ %s (secure: %s)', serverURL, secureTransport)
})
//The 'error' function fires if there is an error connecting to the socket
socket.on('error', function(err) {
console.log('outputSocketToConsoleExample: Error connecting to socket @ %s (secure: %s)', serverURL, secureTransport)
})
socket.on('schedule', function(data){
console.log('outputSocketToConsoleExample: The schedules were broadcast, and it was received. The schedules are: %s', JSON.stringify(data, null, 4))
})
//This init can be this simple. It just lets us know the integration is running
function init() {
//to log through all the logger channels (formatting, to the Bootstrap debug, etc, use "container.logger")
//we are using our variable, level, to set the level of the logger here
container.logger[level]('outputSocketToConsoleExample Loaded.')
}
//This makes the init() function available to the app globally
return {
init: init
}
}