-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
162 lines (133 loc) · 6.05 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
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
'use strict'
var PushOverMessenger = require('./lib/pushover.js')
var EmailMessenger = require('./lib/email.js')
var IftttMessenger = require('./lib/ifttt.js')
var PushcutMessenger = require('./lib/pushcut.js')
let Service, Characteristic, HomebridgeAPI
module.exports = (homebridge) => {
Service = homebridge.hap.Service
Characteristic = homebridge.hap.Characteristic
HomebridgeAPI = homebridge
homebridge.registerAccessory('homebridge-messenger', 'HomebridgeMessenger', HomebridgeMessenger)
}
class HomebridgeMessenger {
constructor (log, config) {
this.log = log
this.config = config
// Add main switch to Homebride
this.serviceMainSwitch = new Service.Switch(this.config.name, 0)
this.log("Added Main Switch : " + this.config.name);
// Initialize cache
this.cacheDirectory = HomebridgeAPI.user.persistPath();
this.storage = require('node-persist');
this.storage.initSync({dir:this.cacheDirectory, forgiveParseErrors: true});
// Get cache and validate if main switch is in cache
var cachedState = this.storage.getItemSync(this.config.name);
if((cachedState === undefined) || (cachedState === false)) { // If not in cache
this.isOn = false
this.serviceMainSwitch.setCharacteristic(Characteristic.On, false);
this.log("Main Switch status"+ " : " + false);
} else { // If in cache
this.isOn = true
this.serviceMainSwitch.setCharacteristic(Characteristic.On, true);
this.log("Main Switch status"+ " : " + true);
}
// Load configured messages
this.loadMessages();
}
loadMessages()
{
this.messages = this.config.messages || [];
this.serviceMessagesSwitches = [];
// Iterate through configured messages
for (let x = 0; x < this.messages.length; x++) {
// Add switch for each message
let serviceMessageSwitch = new Service.Switch(this.messages[x].name , x + 100)
this.log("Added " + this.messages[x].type.toLowerCase() + " : " + this.messages[x].name);
// Add event handler for each message
serviceMessageSwitch.getCharacteristic(Characteristic.On) .on('set', function(value, callback) {
if (value==true) { // If message switch status is On
if (this.isOn) { // If main switch status if On
var message;
switch(this.messages[x].type.toLowerCase()) {
// Message type is email
case "email":
message = new EmailMessenger(this.config.services.email.recipient,
this.config.services.email.smtpServer,
this.config.services.email.smtpPort,
this.config.services.email.smtpSecure,
this.config.services.email.smtpUsername,
this.config.services.email.smtpPassword,
this.messages[x].name,
this.messages[x].text,
this.messages[x].recipients)
break;
// Message type is pushover
case "pushover":
message = new PushOverMessenger(this.config.services.pushover.user,
this.config.services.pushover.token,
this.messages[x].name,
this.messages[x].text,
this.messages[x].priority,
this.messages[x].device,
this.messages[x].sound,
this.messages[x].url,
this.messages[x].urltitle)
break;
// Message type is ifttt
case "ifttt":
message = new IftttMessenger(this.config.services.ifttt.key,
this.messages[x].event,
this.messages[x].value1,
this.messages[x].value2,
this.messages[x].value3)
break;
// Message type is pushcut
case "pushcut":
message = new PushcutMessenger(this.config.services.pushcut.apikey,
this.messages[x].notification,
this.messages[x].title,
this.messages[x].text,
this.messages[x].input,
this.messages[x].actions)
break;
// Invalid message type
default:
throw new Error(this.messages[x].name + " : Invalid type value.");
break;
}
this.log(this.messages[x].name + " : Message sent to " + message.getRecipient())
message.sendMessage()
} else { // If main switch status if Off
this.log(this.messages[x].name + " : Message not sent. Master switch is off.")
}
// Configure message switch to be stateless : will be turned off after 100 ms.
setTimeout(function() {
serviceMessageSwitch.setCharacteristic(Characteristic.On, false);
}.bind(this), 100, this.time);
}
callback(null);
}.bind(this));
// Add message switch to array. Array will be loaded in getServices()
this.serviceMessagesSwitches.push(serviceMessageSwitch);
}
}
setOnCharacteristicHandler (value, callback) {
this.isOn = value
this.storage.setItemSync(this.config.name, value);
this.log("Main Switch status"+ " : " + value);
callback(null);
}
getServices () {
// Load configuration information for devices
const informationService = new Service.AccessoryInformation()
.setCharacteristic(Characteristic.Manufacturer, require('./package.json').name)
.setCharacteristic(Characteristic.SerialNumber, require('./package.json').name)
.setCharacteristic(Characteristic.Model, require('./package.json').name)
.setCharacteristic(Characteristic.FirmwareRevision, require('./package.json').version)
// Event handler for main switch
this.serviceMainSwitch.getCharacteristic(Characteristic.On).on('set', this.setOnCharacteristicHandler.bind(this))
// Send all switches to Homebridge to be added
return [informationService, this.serviceMainSwitch, ...this.serviceMessagesSwitches];
}
}