-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMM-Lynheter.js
216 lines (167 loc) · 5.25 KB
/
MMM-Lynheter.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/* global Module */
/* Magic Mirror
* Module: HelloWorld
*
* By Michael Teeuw http://michaelteeuw.nl
* MIT Licensed.
*/
Module.register("MMM-Lynheter",{
news: [],
loaded: false,
defaults: {
refreshInterval: 1000 * 60 * 1,
lat: 59.8260275,
lon: 10.8133689,
distance: 15,
httpRequestURL: 'http://139.162.242.171:8442',
alertDistance: 5,
recentHours: 1
},
getStyles: function () {
return ['lynheter.css'];
},
start: function () {
Log.info("Starting module: " + this.name);
this.loaded = false;
this.sendSocketNotification("CONFIG", this.config);
},
sortChronologically: function(news) {
var me = this,
features = news.features;
function compare( feature1, feature2 ) {
var feature1Properties = feature1.properties;
var feature1Tweets = feature1Properties.tweets;
var feature1LatestTweet = me.minMax(feature1Tweets);
var feature2Properties = feature2.properties;
var feature2Tweets = feature2Properties.tweets;
var feature2LatestTweet = me.minMax(feature2Tweets);
if ( feature1LatestTweet.createdAt < feature2LatestTweet.createdAt ){
return 1;
}
if ( feature1LatestTweet.createdAt > feature2LatestTweet.createdAt ){
return -1;
}
return 0;
}
news.features = features.sort( compare );
return news;
},
minMax: function (items) {
return items.reduce((acc, val) => {
if (acc.createdAt > val.createdAt) {
return acc;
}
return val;
}, { createdAt: 0});
},
// Override dom generator.
getDom: function () {
var wrapper = document.createElement("div");
wrapper.className = 'lynheter-news'
if (!this.loaded) {
wrapper.innerHTML = this.translate("LOADING");
Log.log("#LOADING");
return wrapper;
}
if (!this.news) {
wrapper.innerHTML = "No data";
Log.log("#NODATA");
return wrapper;
}
var XHoursEarlier = new Date();
XHoursEarlier.setHours(XHoursEarlier.getHours() - this.config.recentHours);
var features = this.news.features;
//Log.log('Parsing ' + features.length + ' features');
var nearestFeaturesWrapper = document.createElement("div");
nearestFeaturesWrapper.className = 'features';
for (var i = 0; i < features.length; i++) {
var currentFeature = features[i];
var featureProperties = currentFeature.properties;
var featureWrapper = document.createElement("div");
featureWrapper.className = 'feature';
var distanceWrapper = document.createElement("div");
var distance = featureProperties.distance;
distanceWrapper.innerHTML = (new String(distance)).substring(0, 4) + ' km';
distanceWrapper.className = "distance";
featureWrapper.appendChild(distanceWrapper);
var recentFeature = false;
var featureTweets = featureProperties.tweets;
for (var j = 0; j < featureTweets.length; j++) {
var currentFeatureTweet = featureTweets[j];
var tweetWrapper = document.createElement("div");
tweetWrapper.className = 'tweet';
var tweeter = currentFeatureTweet.tweeter;
var time = new Date(currentFeatureTweet.createdAt)
var timeString = time.toLocaleString("no");
var timeAndTweet = timeString + ' meldt av ' + tweeter;
var timeAndTweetWrapper = document.createElement("div");
timeAndTweetWrapper.className = 'timeAndTweet';
timeAndTweetWrapper.innerHTML = timeAndTweet;
tweetWrapper.appendChild(timeAndTweetWrapper);
// Figure out if this is a recent tweet or not
var timeBetweenNowAndTweet = time - XHoursEarlier;
// todo debug and fix this
console.log(time.toLocaleString("no") + ' vs ' + XHoursEarlier.toLocaleString("no"));
console.log('time diff at ' + distance + ' is ' + ((timeBetweenNowAndTweet/1000)/60) + ' minutes ');
if (timeBetweenNowAndTweet > 0) {
recentFeature = true;
}
var content = currentFeatureTweet.content;
var contentWrapper = document.createElement("div");
contentWrapper.className = 'content';
contentWrapper.innerHTML = '<i>' + content + '</i>';
tweetWrapper.appendChild(contentWrapper);
featureWrapper.appendChild(tweetWrapper);
}
if (distance < this.config.alertDistance) {
if (recentFeature) {
featureWrapper.className = 'close-and-recent-feature';
}
else {
featureWrapper.className = 'close-feature';
}
}
else {
if (recentFeature) {
featureWrapper.className = 'far-and-recent-feature';
}
else {
featureWrapper.className = 'far-feature';
}
}
nearestFeaturesWrapper.appendChild(featureWrapper);
}
wrapper.appendChild(nearestFeaturesWrapper);
return wrapper;
},
// unload the results from uber services
processData: function(news) {
console.log('processData()');
if (!news) {
// Did not receive usable new data.
// Maybe this needs a better check?
Log.log("#No data");
return;
}
news = this.sortChronologically(news);
this.news = news;
this.loaded = true;
this.updateDom(3000);
},
getTemplateData: function () {
return this.config;
},
socketNotificationReceived: function(notification, payload) {
console.log('Got notification ' + notification);
if (notification === "STARTED") {
this.updateDom();
Log.log("#STARTED");
}
else if (notification === "DATA") {
// console.log('Got DATA');
this.loaded = true;
doc = JSON.parse(payload);
this.processData(doc);
}
}
});