forked from openhab/openhab-cloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
devices.js
91 lines (86 loc) · 3.62 KB
/
devices.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
const UserDevice = require('../models/userdevice');
const logger = require('../logger');
const mongoose = require('mongoose');
const notificationSender = require('../notificationsender');
const Notification = require('../models/notification');
const system = require('../system');
const form = require('express-form');
exports.devicesget = function (req, res) {
UserDevice.find({ owner: req.user.id }, function (error, userDevices) {
let selectedDeviceId = "";
if (!userDevices || error) {
userDevices = [];
} else {
if (req.params.hasOwnProperty('id')) {
selectedDeviceId = req.params.id;
} else {
if (userDevices.length > 0)
selectedDeviceId = userDevices[0]._id;
else
selectedDeviceId = "";
}
}
let selectedDeviceArrayId = 0;
for (let i = 0; i < userDevices.length; i++) {
if (userDevices[i]._id == selectedDeviceId) {
selectedDeviceArrayId = i;
}
}
res.render('devices', {
userDevices: userDevices,
title: "Devices", user: req.user, selectedDeviceId: selectedDeviceId,
selectedDeviceArrayId: selectedDeviceArrayId,
baseUrl: system.getBaseURL(), appleLink: system.getAppleLink(), androidLink: system.getAndroidLink(),
errormessages: req.flash('error'), infomessages: req.flash('info')
});
});
}
exports.devicessendmessagevalidate = form(
form.field("messagetext", "Message text").trim().required()
);
exports.devicessendmessage = function (req, res) {
if (!req.form.isValid) {
req.user.openhab(function (error, openhab) {
res.redirect('/devices/');
});
} else {
logger.info("sending message to device " + req.params.id);
const sendMessageDeviceId = mongoose.Types.ObjectId(req.params.id);
const message = req.form.messagetext;
const newNotification = new Notification({
user: req.user.id,
message: message,
payload: {message : message}
});
newNotification.save(function (error) {
if (error) {
logger.error('Error saving notification: %s', error);
} else {
UserDevice.findOne({ owner: req.user.id, _id: sendMessageDeviceId }, function (error, sendMessageDevice) {
if (!error && sendMessageDevice) {
if (sendMessageDevice.fcmRegistration) {
notificationSender.sendFCMNotification(sendMessageDevice.fcmRegistration, newNotification);
} else if (sendMessageDevice.iosDeviceToken) {
notificationSender.sendAppleNotification(sendMessageDevice.iosDeviceToken, message);
}
req.flash('info', 'Your message was sent');
res.redirect('/devices/' + sendMessageDevice._id);
} else {
req.flash('error', 'There was an error processing your request');
res.redirect('/devices/' + sendMessageDevice._id);
}
});
}
});
}
}
exports.devicesdelete = function (req, res) {
logger.info("deleting device " + req.params.id);
const deleteId = mongoose.Types.ObjectId(req.params.id);
UserDevice.findOne({ owner: req.user.id, _id: deleteId }, function (error, userDevice) {
if (!error && userDevice) {
userDevice.remove();
}
res.redirect('/devices');
});
};