forked from RedNax67/MMM-IPCam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMM-IPCam.js
93 lines (69 loc) · 2.19 KB
/
MMM-IPCam.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
Module.register("MMM-IPCam",{
// Default module config.
defaults: {
updateInterval : 10000, // 10 seconds
invertColors : false
},
getStyles : function () {
return ["IPCam.css"];
},
start: function() {
Log.info(this.config);
Log.info("Starting module: " + this.name);
this.Cam = "";
this.CamNum = "";
this.CamTitle = "";
this.getCam();
},
// Define required scripts.
getScripts: function() {
return ["moment.js"];
},
getCam: function() {
Log.info("IPcam: Getting image.");
this.sendSocketNotification("GET_CAM", {config: this.config});
},
socketNotificationReceived: function(notification, payload) {
Log.log(this.name + " received a socket notification: " + notification + " - Payload: " + payload);
if(notification === "CAM"){
Log.info('RECEIVED CAM MSG: '+ payload);
var b64encoded = btoa(payload.img);
this.Cam = "data:image/jpg;base64," + b64encoded;
if (payload.all != 'failed to get cam alias.') {
this.CamTitle = payload.all;
}
this.scheduleUpdate();
}
},
// Override dom generator.
getDom: function() {
var wrapper = document.createElement("div");
var title = document.createElement("div");
title.className = "bright small light";
title.innerHTML = this.CamTitle;
var ipcam = document.createElement("img");
ipcam.src = this.Cam;
if(this.config.invertColors){
ipcam.setAttribute("style", "-webkit-filter: invert(100%); max-width: 100%; max-height: 100%; height: 280px; ")
} else {
ipcam.setAttribute("style", "max-width: 100%; max-height: 100%; height: 280px; opacity:0.7; -moz-opacity:0.7; filter:alpha(opacity=70);")
}
wrapper.className = "img";
wrapper.appendChild(title);
wrapper.appendChild(ipcam);
return wrapper;
},
scheduleUpdate: function (delay) {
var self = this;
var nextLoad = this.config.updateInterval;
self.updateDom(2000);
if (typeof delay !== "undefined" && delay >= 0) {
nextLoad = delay;
}
var self = this;
clearTimeout(this.updateTimer);
this.updateTimer = setTimeout(function () {
self.getCam();
}, nextLoad);
}
});