Skip to content

Commit f0918a4

Browse files
committed
initial version
0 parents  commit f0918a4

16 files changed

+3797
-0
lines changed

.eslintrc.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"rules": {
3+
"indent": ["error", "tab"],
4+
"quotes": ["error", "double"],
5+
"max-len": ["error", 250],
6+
"curly": "error",
7+
"camelcase": ["error", {"properties": "never"}],
8+
"no-trailing-spaces": ["error"],
9+
"no-irregular-whitespace": ["error"]
10+
},
11+
"env": {
12+
"browser": true,
13+
"node": true,
14+
"es6": true
15+
}
16+
}

.github/custom_text.png

6.73 KB
Loading

.github/duration.png

7.22 KB
Loading

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
uploads
3+
npm-debug.log

.stylelintrc

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"extends": "stylelint-config-standard",
3+
"font-family-name-quotes": "double-where-recommended",
4+
"block-no-empty": false
5+
}

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## [1.0.0] - 2017-12-31
2+
3+
- Prints estimated travel time in minutes from origin to destination

Gruntfile.js

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
module.exports = function(grunt) {
2+
require("time-grunt")(grunt);
3+
grunt.initConfig({
4+
pkg: grunt.file.readJSON("package.json"),
5+
eslint: {
6+
options: {
7+
configFile: ".eslintrc.json",
8+
fix: true
9+
},
10+
target: ["*.js"]
11+
},
12+
stylelint: {
13+
simple: {
14+
options: {
15+
configFile: ".stylelintrc",
16+
fix: true
17+
},
18+
src: ["*.css"]
19+
}
20+
},
21+
jsonlint: {
22+
main: {
23+
src: ["package.json", "translations/*.json"],
24+
options: {
25+
reporter: "jshint"
26+
}
27+
}
28+
},
29+
markdownlint: {
30+
all: {
31+
options: {
32+
config: {
33+
"default": true,
34+
"line-length": false,
35+
"blanks-around-headers": false,
36+
"no-duplicate-header": false,
37+
"no-inline-html": false,
38+
"MD010": false,
39+
"MD001": false,
40+
"MD031": false,
41+
"MD040": false,
42+
"MD002": false,
43+
"MD029": false,
44+
"MD041": false,
45+
"MD032": false,
46+
"MD036": false,
47+
"MD037": false,
48+
"MD009": false,
49+
"MD018": false,
50+
"MD012": false,
51+
"MD026": false,
52+
"MD038": false
53+
}
54+
},
55+
src: ["README.md", "CHANGELOG.md", "LICENSE.txt"]
56+
}
57+
},
58+
yamllint: {
59+
all: [".travis.yml"]
60+
}
61+
});
62+
grunt.loadNpmTasks("grunt-eslint");
63+
//FIXME
64+
//grunt.loadNpmTasks("grunt-stylelint");
65+
grunt.loadNpmTasks("grunt-jsonlint");
66+
grunt.loadNpmTasks("grunt-yamllint");
67+
grunt.loadNpmTasks("grunt-markdownlint");
68+
grunt.registerTask("default", ["eslint", "jsonlint", "markdownlint", "yamllint"]);
69+
};

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2017 Mark Fodor
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

MMM-trafficmate.css

Whitespace-only changes.

MMM-trafficmate.js

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/* global Module */
2+
3+
/* Magic Mirror
4+
* Module: MMM-trafficmate
5+
*
6+
* By Mark Fodor https://github.com/markfodor
7+
* MIT Licensed.
8+
*/
9+
Module.register("MMM-trafficmate", {
10+
defaults: {
11+
defaultText: config.defaultText || 'Route duration: ',
12+
travelMode: config.travelMode || 'DRIVING',
13+
origin: config.origin,
14+
destination: config.destination,
15+
apiKey: config.apiKey,
16+
updateInterval: 6000,
17+
retryDelay: 5000
18+
},
19+
20+
start: function () {
21+
var self = this;
22+
this.reponse = null;
23+
24+
//Flag for check if module is loaded
25+
this.loaded = false;
26+
27+
//TODO apiKey check
28+
var googleScript = document.createElement("script");
29+
googleScript.src = "https://maps.googleapis.com/maps/api/js?key=" + self.config.apiKey;
30+
document.getElementsByTagName("body")[0].appendChild(googleScript);
31+
32+
// Schedule update timer.
33+
setInterval(function () {
34+
self.getData();
35+
self.updateDom();
36+
}, this.config.updateInterval);
37+
},
38+
39+
getData: function () {
40+
var self = this;
41+
42+
if (google) {
43+
var directionsService = new google.maps.DirectionsService;
44+
directionsService.route({
45+
destination: self.config.destination,
46+
origin: self.config.origin,
47+
travelMode: self.config.travelMode
48+
}, function (response, status) {
49+
if (status === "OK") {
50+
self.response = response;
51+
} else {
52+
Log.error("Directions request failed due to " + status);
53+
}
54+
});
55+
} else {
56+
Log.error("Cloud not load google service.");
57+
}
58+
},
59+
60+
/* scheduleUpdate()
61+
* Schedule next update.
62+
*
63+
* argument delay number - Milliseconds before next update.
64+
* If empty, this.config.updateInterval is used.
65+
*/
66+
scheduleUpdate: function (delay) {
67+
var nextLoad = this.config.updateInterval;
68+
if (typeof delay !== "undefined" && delay >= 0) {
69+
nextLoad = delay;
70+
}
71+
nextLoad = nextLoad;
72+
var self = this;
73+
setTimeout(function () {
74+
self.getData();
75+
}, nextLoad);
76+
},
77+
78+
getDom: function () {
79+
var duration = this.response ? this.response.routes[0].legs[0].duration.text : "Loading...";
80+
81+
// create element wrapper for show into the module
82+
var wrapper = document.createElement("div");
83+
wrapper.innerHTML = this.config.defaultText + duration;
84+
85+
// Data from helper
86+
if (this.dataNotification) {
87+
var wrapperDataNotification = document.createElement("div");
88+
// translations + datanotification
89+
wrapperDataNotification.innerHTML = this.translate("UPDATE") + ": " + this.dataNotification.date;
90+
91+
wrapper.appendChild(wrapperDataNotification);
92+
}
93+
return wrapper;
94+
},
95+
96+
getScripts: function () {
97+
return [];
98+
},
99+
100+
// Load translations files
101+
getTranslations: function () {
102+
//FIXME: This can be load a one file javascript definition
103+
return {
104+
en: "translations/en.json",
105+
es: "translations/es.json"
106+
};
107+
},
108+
109+
processData: function (data) {
110+
var self = this;
111+
if (this.loaded === false) {
112+
self.updateDom(self.config.animationSpeed);
113+
}
114+
this.loaded = true;
115+
116+
// the data if load
117+
// send notification to helper
118+
this.sendSocketNotification("MMM-trafficmate-NOTIFICATION", data);
119+
},
120+
121+
// socketNotificationReceived from helper
122+
socketNotificationReceived: function (notification, payload) {
123+
if (notification === "MMM-trafficmate-NOTIFICATION") {
124+
// set dataNotification
125+
this.dataNotification = payload;
126+
this.updateDom();
127+
}
128+
},
129+
});

README.md

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# MMM-trafficmate
2+
This is a traffic module for the [MagicMirror](https://github.com/MichMich/MagicMirror).
3+
It is able to print estimated travel time in minutes from origin to destination.
4+
5+
![Route duration](.github/duration.png)
6+
7+
## Used technolgies/tools
8+
* [Magic Mirror Module Template](https://github.com/roramirez/MagicMirror-Module-Template)
9+
* [Google Directions API](https://developers.google.com/maps/documentation/javascript/directions)
10+
11+
## Installation
12+
Clone this repository in your `modules` folder, and install dependencies:
13+
```bash
14+
cd ~/MagicMirror/modules # adapt directory if you are using a different one
15+
git clone https://github.com/markfodor/MMM-trafficmate.git
16+
cd MMM-trafficmate
17+
npm install
18+
```
19+
20+
Add the module to your `config/config.js` file.
21+
```js
22+
{
23+
module: 'MMM-trafficmate',
24+
position: 'top_right',
25+
config: {
26+
apiKey: '<YOUR_API_KEY>',
27+
origin: '<ROUTE_ORIGIN>',
28+
destination: '<ROUTE_DESTINATION>'
29+
}
30+
},
31+
```
32+
**The _apiKey_, _origin_ and _destination_ fields are mandatory**.
33+
34+
Optional config properties
35+
36+
| Name | Options | Default value |
37+
| --- | --- | --- |
38+
| defaultText | - | "Route duration: " |
39+
| travleMode | "DRIVING" / "WALKING" / "BICYCLING" / "TRANSIT" | "DRIVING" |
40+
41+
## Running tests
42+
Run the `test` npm script
43+
```
44+
npm test
45+
```
46+
47+
Current Tests:
48+
- [ESLint](http://eslint.org/) for linting the javascript
49+
- [stylelint](https://stylelint.io/) for linting the CSS with [stylelint-config-standard](https://github.com/stylelint/stylelint-config-standard) as its base
50+
- [jsonlint](https://github.com/zaach/jsonlint) for linting the translation files
51+
- [markdownlint](https://github.com/DavidAnson/markdownlint) for checking the markdown files (`README.md`, `CHANGELOG.md`, `LICENSE.txt`)
52+
- [js-yaml](https://github.com/nodeca/js-yaml) to lint the `.travis.yml` (run through [grunt-yamllint](https://github.com/geedew/grunt-yamllint))
53+
54+
If it is possible it will also automatically fix some issues.
55+
56+
## Contribution
57+
If you have any suggest, please let me know [by an issue](https://github.com/markfodor/MMM-trafficmate/issues/new).
58+
59+
## License
60+
[The MIT License (MIT)](LICENSE)

node_helper.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* Magic Mirror
2+
* Node Helper: MMM-trafficmate
3+
*
4+
* By Mark Fodor https://github.com/markfodor
5+
* MIT Licensed.
6+
*/
7+
8+
var NodeHelper = require("node_helper");
9+
10+
module.exports = NodeHelper.create({
11+
12+
// Override socketNotificationReceived method.
13+
14+
/* socketNotificationReceived(notification, payload)
15+
* This method is called when a socket notification arrives.
16+
*
17+
* argument notification string - The identifier of the noitication.
18+
* argument payload mixed - The payload of the notification.
19+
*/
20+
socketNotificationReceived: function(notification, payload) {
21+
if (notification === "MMM-trafficmate-NOTIFICATION") {
22+
console.log("Working notification system. Notification:", notification, "payload: ", payload);
23+
// Send notification
24+
this.sendNotificationTest(this.anotherFunction()); //Is possible send objects :)
25+
}
26+
},
27+
28+
// Example function send notification test
29+
sendNotificationTest: function(payload) {
30+
this.sendSocketNotification("MMM-trafficmate-NOTIFICATION", payload);
31+
},
32+
33+
// this you can create extra routes for your module
34+
extraRoutes: function() {
35+
var self = this;
36+
this.expressApp.get("/MMM-trafficmate/extra_route", function(req, res) {
37+
// call another function
38+
values = self.anotherFunction();
39+
res.send(values);
40+
});
41+
},
42+
43+
// Test another function
44+
anotherFunction: function() {
45+
return {date: new Date()};
46+
}
47+
});

0 commit comments

Comments
 (0)