forked from lasthead0/yandex2mqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
118 lines (107 loc) · 3.74 KB
/
app.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
'use strict';
const fs = require('fs');
const path = require('path');
/* express and https */
const ejs = require('ejs');
const express = require('express');
const app = express();
const https = require('https');
/* parsers */
const cookieParser = require('cookie-parser');
/* error handler */
const errorHandler = require('errorhandler');
/* seesion and passport */
const session = require('express-session');
const passport = require('passport');
/* mqtt client for devices */
const mqtt = require('mqtt');
/* */
const config = require('./config');
const Device = require('./device');
app.engine('ejs', ejs.__express);
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, './views'));
app.use(express.static('views'));
app.use(cookieParser());
app.use(express.json({
extended: false,
}));
app.use(express.urlencoded({
extended: true,
}));
app.use(errorHandler());
app.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: false,
}));
/* passport */
app.use(passport.initialize());
app.use(passport.session());
/* passport auth */
require('./auth');
/* routers */
const {site: r_site, oauth2: r_oauth2, user: r_user, client: r_client} = require('./routes');
app.get('/', r_site.index);
app.get('/login', r_site.loginForm);
app.post('/login', r_site.login);
app.get('/logout', r_site.logout);
app.get('/account', r_site.account);
app.get('/dialog/authorize', r_oauth2.authorization);
app.post('/dialog/authorize/decision', r_oauth2.decision);
app.post('/oauth/token', r_oauth2.token);
app.get('/api/userinfo', r_user.info);
app.get('/api/clientinfo', r_client.info);
app.get('/provider/v1.0', r_user.ping);
app.get('/provider', r_user.ping);
app.get('/provider/v1.0/user/devices', r_user.devices);
app.post('/provider/v1.0/user/devices/query', r_user.query);
app.post('/provider/v1.0/user/devices/action', r_user.action);
app.post('/provider/v1.0/user/unlink', r_user.unlink);
/* create https server */
const privateKey = fs.readFileSync(config.https.privateKey, 'utf8');
const certificate = fs.readFileSync(config.https.certificate, 'utf8');
const credentials = {
key: privateKey,
cert: certificate,
};
const httpsServer = https.createServer(credentials, app);
httpsServer.listen(config.https.port);
/* cache devices from config to global */
global.devices = [];
if (config.devices) {
config.devices.forEach(opts => {
global.devices.push(new Device(opts));
});
}
// load yandex token & skill id from config
global.dialogs = {};
(config.dialogs)?global.dialogs = config.dialogs:console.error('No yandex dialogs authorization data loaded. Check your config.js')
/* create subscriptions array */
const subscriptions = [];
global.devices.forEach(device => {
device.data.custom_data.mqtt.forEach(mqtt => {
const {instance, state: topic} = mqtt;
if (instance != undefined && topic != undefined) {
subscriptions.push({deviceId: device.data.id, instance, topic});
}
});
});
/* Create MQTT client (variable) in global */
global.mqttClient = mqtt.connect(`mqtt://${config.mqtt.host}`, {
port: config.mqtt.port,
username: config.mqtt.user,
password: config.mqtt.password
}).on('connect', () => { /* on connect event handler */
mqttClient.subscribe(subscriptions.map(pair => pair.topic));
}).on('offline', () => { /* on offline event handler */
/* */
}).on('message', (topic, message) => { /* on get message event handler */
const subscription = subscriptions.find(sub => topic.toLowerCase() === sub.topic.toLowerCase());
if (subscription == undefined) return;
const {deviceId, instance} = subscription;
const ldevice = global.devices.find(d => d.data.id == deviceId);
ldevice.updateState(`${message}`, instance);
ldevice.updateYandexState();
});
module.exports = app;