-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
104 lines (89 loc) · 2.51 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
const PLUGIN_ID = 'signalk-notify';
const PLUGIN_NAME = 'Signalk notify';
var unsubscribes = [];
module.exports = function(app) {
var plugin = {};
plugin.id = PLUGIN_ID;
plugin.name = PLUGIN_NAME;
plugin.description = 'A plugin to send notifications when an event occurs';
plugin.start = function(options, restartPlugin) {
app.debug('Plugin started');
plugin.options = options;
options.notifications.forEach(listen);
app.setProviderStatus('Running');
};
function listen(option) {
// console.log(JSON.stringify(option, null, 2));
let _notify = function(event) {
// console.log(JSON.stringify(event, null, 2));
if (option.method == 'LOG') {
console.log(new Date(), option.message);
} else if (option.method == 'DEBUG') {
app.debug(option.message);
} else if (option.method == 'NOTIFICATION') {
let value = {
method: ['sound'],
state: 'alert',
message: option.message
};
if (event.type == 'FALLING') {
value = null;
}
let notification = {
context: event.value.context,
updates: [{
$source: PLUGIN_ID,
timestamp: event.value.updates[0].timestamp,
values: [{
path: `notifications.${event.event}`,
value: value
}]
}]
};
app.handleMessage(PLUGIN_ID, notification);
app.debug(notification);
}
};
app.on(option.event, _notify);
unsubscribes.push(() => {
app.removeListener(option.event, _notify);
});
}
plugin.stop = function() {
// Here we put logic we need when the plugin stops
app.debug('Plugin stopped');
unsubscribes.forEach(f => f());
app.setProviderStatus('Stopped');
};
plugin.schema = {
title: PLUGIN_NAME,
type: 'object',
properties: {
notifications: {
type: 'array',
title: 'notifications',
items: {
type: 'object',
properties: {
event: {
type: 'string',
title: 'event'
},
message: {
type: 'string',
title: 'message'
},
method: {
type: 'string',
title: 'method',
enum: ['LOG', 'DEBUG', 'NOTIFICATION'],
enumNames: ['write to log', 'write to debug ', 'create notification'],
default: 'LOG'
}
}
}
}
}
};
return plugin;
};