-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
54 lines (43 loc) · 1.47 KB
/
index.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
var express = require('express')
var app = express()
var http = require('http').Server(app)
var io = require('socket.io')(http)
var _ = require('lodash')
var WEBPORT = 3000
var devices = require('./config/devices')
var PeriodicActivity = require('./lib/periodic-activity')
var AnalogUpdater = require('./lib/analog-updater')
var setTemperature = require('./lib/set-temperature')
var tempSensor = new AnalogUpdater(0, function (newValue) {
io.emit('controlUpdate', {'temperature': newValue})
setTemperature('temperatura', newValue)
})
var intput_1 = new PeriodicActivity(devices['intput_1'], function (newValue) {
io.emit('controlUpdate', {'intput_1': !!newValue})
})
var intput_2 = new PeriodicActivity(devices['intput_2'], function (newValue) {
io.emit('controlUpdate', {'intput_2': !!newValue})
})
var intput_3 = new PeriodicActivity(devices['intput_3'], function (newValue) {
io.emit('controlUpdate', {'intput_3': !!newValue})
})
function getRootCallback (req, res) {
res.sendfile('index.html')
}
app.get('/', getRootCallback)
io.on('connection', function onConnection (socket) {
console.log('Dentro')
socket.on('controlHandler', function (data) {
_.forEach(data, function (val, key) {
console.log('Phone Action: ', val, key)
devices[key].write(parseState(val))
})
io.emit('controlUpdate', data)
})
})
function parseState (value) {
return value ? 1 : 0
}
http.listen(WEBPORT, function serverStart () {
console.log('http:my-ip.com/', WEBPORT)
})