-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMM-gpsd.js
175 lines (153 loc) · 4.29 KB
/
MMM-gpsd.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
/* Magic Mirror
* Module: MMM-gpsd
*
* MIT Licensed.
*/
Module.register("MMM-gpsd", {
// Default module config.
defaults: {
header: "GPS Details",
units: config.units,
timeFormat: 12,
port: 2947,
hostname: "localhost",
debug: false,
showDevice: false,
showMode: false,
showSats: true
},
start: function () {
Log.log("Starting module: " + this.name);
// Add custom filters
this.addFilters();
this.gpsDeviceData = null;
this.gpsSkyData = null;
this.setGpsdConnection();
},
socketNotificationReceived: function (notification, payload) {
if (notification === "GPSD_DEVICE_DATA") {
// Handle no movement
if (!payload.speed) {
payload.speed = 0;
}
this.gpsDeviceData = { ...this.gpsData, ...payload };
this.sendNotification("GPS_DATA", this.gpsDeviceData);
this.updateDom();
}
if (notification === "GPSD_SKY_DATA") {
this.gpsSkyData = { ...this.gpsSkyData, ...payload };
this.updateDom();
}
},
setGpsdConnection: function () {
this.sendSocketNotification("GPSD_CONNECT", {
port: this.config.port,
hostname: this.config.hostname
});
},
getTemplate: function () {
return "MMM-gpsd.njk";
},
getTemplateData: function () {
return {
config: this.config,
gps: this.gpsDeviceData,
sky: this.gpsSkyData
};
},
getHeader: function () {
return this.config.header;
},
addFilters() {
this.nunjucksEnvironment().addFilter(
"roundValue",
function (value) {
return Math.round(value);
}.bind(this)
);
this.nunjucksEnvironment().addFilter(
"roundCoord",
function (value) {
return value.toFixed(5);
}.bind(this)
);
this.nunjucksEnvironment().addFilter(
"gpsMode",
function (mode) {
if (mode === 0) {
return "Not Active";
} else if (mode === 1) {
return "Acquiring Fix";
} else if (mode === 2) {
return "2D Fix";
} else if (mode === 3) {
return "3D Fix";
}
}.bind(this)
);
this.nunjucksEnvironment().addFilter(
"speed",
function (speed) {
if (this.config.units === "metric") {
speed = Math.round(speed * 3.6); // mps to kph
speed += " kph";
} else if (this.config.units === "imperial") {
speed = Math.round(speed * 2.23693629); // mps to mph
speed += " mph";
} else {
speed += " mps";
}
return speed;
}.bind(this)
);
this.nunjucksEnvironment().addFilter(
"formatTime",
function (date) {
date = moment(date);
if (this.config.timeFormat !== 24) {
return date.format("h:mm:ss a");
} else {
return date.format("h:mm:ss");
}
}.bind(this)
);
this.nunjucksEnvironment().addFilter(
"direction",
function (direction) {
if (direction > 11.25 && direction <= 33.75) {
return "NNE";
} else if (direction > 33.75 && direction <= 56.25) {
return "NE";
} else if (direction > 56.25 && direction <= 78.75) {
return "ENE";
} else if (direction > 78.75 && direction <= 101.25) {
return "E";
} else if (direction > 101.25 && direction <= 123.75) {
return "ESE";
} else if (direction > 123.75 && direction <= 146.25) {
return "SE";
} else if (direction > 146.25 && direction <= 168.75) {
return "SSE";
} else if (direction > 168.75 && direction <= 191.25) {
return "S";
} else if (direction > 191.25 && direction <= 213.75) {
return "SSW";
} else if (direction > 213.75 && direction <= 236.25) {
return "SW";
} else if (direction > 236.25 && direction <= 258.75) {
return "WSW";
} else if (direction > 258.75 && direction <= 281.25) {
return "W";
} else if (direction > 281.25 && direction <= 303.75) {
return "WNW";
} else if (direction > 303.75 && direction <= 326.25) {
return "NW";
} else if (direction > 326.25 && direction <= 348.75) {
return "NNW";
} else {
return "N";
}
}.bind(this)
);
}
});