-
Notifications
You must be signed in to change notification settings - Fork 4
/
MMM-AI.js
138 lines (131 loc) · 3.64 KB
/
MMM-AI.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
/* Magic Mirror
* Module: MMM-AI
*
* customized and implemented by ejay From eouia work on MMM-HTMLBOX and MMM-NotificationTrigger
*/
Module.register("MMM-AI",{
defaults: {
width: "100%",
height: "inherit",
refresh_interval_sec: 0,
content: `<img id="MY_ANIMATION" src="config/jarvis-standby2.gif"/>`, // this can be changed in config.js
//file: "sample.html",
useWebhook: false,
triggers:[
{
trigger: "SAMPLE_INCOMINIG_NOTIFICATION",
triggerSenderFilter: (sender) => {
return true
},
triggerPayloadFilter: (payload) => {
return true
},
fires: [
{
fire:"SAMPLE_OUTGOING_NOTIFICATION",
payload: (payload) => {
return payload
},
delay: 0,
exec: ""
},
],
},
]
},
start: function() {
this.timer = null
this.sendSocketNotification("INIT")
},
socketNotificationReceived: function(notification, payload) {
if (notification == "WEBHOOK" && this.config.useWebhook) {
this.notificationReceived(payload.notification, payload.payload, payload.sender)
}
if (notification == "EXEC_RESULT") {
this.sendNotification(payload.fire + "_RESULT", payload)
console.log("[NOTTRG] Execution Result: ", payload)
}
},
notificationReceived: function(notification, payload, sender) {
if (notification == "DOM_OBJECTS_CREATED") {
this.refresh()
}
var triggers = this.config.triggers
for(i in triggers) {
var trigger = triggers[i]
if (notification == trigger.trigger) {
var senderFilter = (trigger.triggerSenderFilter)
? trigger.triggerSenderFilter
: this.defaults.triggers[0].triggerSenderFilter
var payloadFilter = (trigger.triggerPayloadFilter)
? trigger.triggerPayloadFilter
: this.defaults.triggers[0].triggerPayloadFilter
if (senderFilter(sender) && payloadFilter(payload)) {
for(j in trigger.fires) {
var fire = trigger.fires[j]
var result = payload
if (typeof fire.payload == "function") {
result = fire.payload(payload)
} else if (fire.payload) {
result = fire.payload
}
if(fire.delay) {
setTimeout(()=>{
this.sendNotification(fire.fire, result)
if (fire.exec) {
this.sendSocketNotification("EXEC", {
trigger:trigger.trigger,
fire: fire.fire,
exec:fire.exec
})
}
}, fire.delay)
} else {
this.sendNotification(fire.fire, result)
if (fire.exec) {
this.sendSocketNotification("EXEC", {
trigger:trigger.trigger,
fire: fire.fire,
exec:fire.exec
})
}
}
}
}
}
}
},
refresh: function() {
if (this.config.file !== "") {
this.readFileTrick("/modules/MMM-AI/" + this.config.file)
}
this.updateDom()
if (this.config.refresh_interval_sec > 0) {
var self = this
this.timer = setTimeout(function(){
self.refresh()
}, this.config.refresh_interval_sec * 1000)
}
},
getDom: function() {
var wrapper = document.createElement("div")
wrapper.innerHTML = this.config.content
wrapper.className = "HTMLBX"
wrapper.style.width = this.config.width
wrapper.style.height = this.config.height
return wrapper
},
readFileTrick: function (url, callback) {
var xmlHttp = new XMLHttpRequest()
var self = this
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
console.log("EEE!")
self.config.content = xmlHttp.responseText
self.updateDom()
}
}
xmlHttp.open("GET", url, true)
xmlHttp.send(null)
}
})