-
Notifications
You must be signed in to change notification settings - Fork 690
/
remote.js
executable file
·164 lines (146 loc) · 4.21 KB
/
remote.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
/**
* smart-mirror remote by Evan Cohen
*/
const stream = require('stream')
const _ = require('lodash')
let remote = new stream.Writable()
remote.start = function () {
const express = require('express')
const app = express()
const fs = require('fs')
const getConfigSchema = require('./remote/config.schema.js')
let config = ""
let configDefault = ""
let configJSON = ""
let configPath = __dirname + "/config.json"
let configDefaultPath = __dirname + "/remote/.config.default.json"
function getFiles() {
configDefault = JSON.parse(fs.readFileSync(configDefaultPath, "utf8"))
if (fs.existsSync(configPath)) {
try {
config = JSON.parse(fs.readFileSync(configPath, "utf8")) //json'd config file
} catch (e) {
config = configDefault
}
} else {
config = configDefault
}
configDefault = JSON.parse(fs.readFileSync(configDefaultPath, "utf8"))
//TODO this is async, all of the remote should be async too
}
getFiles()
const server = require('http').createServer(app)
// Start the server
server.listen(config.remote.port)
// Use the remote directory and initilize socket connection
app.use(express.static(__dirname + '/remote'))
remote.io = require('socket.io')(server)
/**
* When the connection begins
*/
remote.io.on('connection', function (socket) {
socket.emit('connected')
// When the mirror recieves a remote command
socket.on('command', function (command) {
remote.emit('command', command)
})
socket.on('devtools', function (open) {
remote.emit('devtools', open)
})
socket.on('kiosk', function () {
remote.emit('kiosk')
})
socket.on('reload', function () {
remote.emit('reload')
})
socket.on('clickWakeUp', function () {
remote.emit('wakeUp')
})
socket.on('clickSleep', function () {
remote.emit('sleep')
})
socket.on('getAnnyAng', function () {
socket.emit('loadAnnyAng', (typeof config.general.language != 'undefined') ? config.general.language : 'en-US')
})
socket.on('saveConfig', function (data) { // used to save the form JSON
fs.writeFile(configPath, JSON.stringify(data, null, 2), "utf8", function (err) {
if (err) {
console.error(err)
} else {
checkForActiveChanged(config,data)
remote.emit('relaunch', data)
}
})
})
socket.on('getForm', function () {
getConfigSchema(config, function (configSchema) {
configSchema.form.sort(function (a, b) { return a.order - b.order })
configJSON = configSchema
socket.emit("json", { "configJSON": configJSON, "configDefault": configDefault, "config": config })
})
})
}) // end - connection
function checkForActiveChanged(oldConfig,newConfig){
// if the new plugin config, doesn't match the old
let cleanup=false
let ok = oldConfig.plugins
let nk = newConfig.plugins
// if the old and new language settings are the same , check the plugin settings
if(newConfig.general.language.trim().toLowerCase()==oldConfig.general.language.trim().toLowerCase()){
// if not the same content, something changed
if(!_.isEqual(ok, nk)){
// if same length
if(ok.length===nk.length){
// make hashs
let okh = {}
for (let e of ok){
okh[e.name]=e
}
// of both arrays
let nkh = {}
for(let f of nk){
nkh[f.name]=f
}
// compare all the items for active the same
for(let k of Object.keys(nkh)){
if(okh[k].active !== nkh[k].active){
// if one is different, done
cleanup=true;
break;
}
}
// still nothing changed
// some key name changed, don't really care about name
// check active for all those keys
if(cleanup == false){
for(let l of Object.keys(okh)){
if(okh[l].active !== nkh[l].active){
// if one is different, done
cleanup=true;
break;
}
}
}
}
else {
cleanup=true
}
}
}
else {
// languages different
cleanup=true;
}
if(cleanup){
let langfile=__dirname+"/app/locales/"+newConfig.general.language.trim().substring(0,2)+'c.json'
fs.unlinkSync(langfile)
}
}
/**
* When a remote disconnects
*/
remote.io.on('disconnect', function () {
remote.emit('disconnected')
})// end - disconnect
} // end - start
module.exports = remote