Skip to content

Commit e4c409b

Browse files
committed
refactoring/cleaning
1 parent 2544e67 commit e4c409b

File tree

12 files changed

+285
-247
lines changed

12 files changed

+285
-247
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ $ node app.js
2525

2626
### Global Plugins configuration:
2727

28-
- ```plugin_name```: step type
28+
- ```type```: step type
2929
- ```enabled```: enabled step
3030
- ```duration```: duration of step
3131

3232
### Datetime:
3333

3434
```json
3535
{
36-
"plugin_name": "datetime",
36+
"type": "datetime",
3737
"enabled": true,
3838
"duration": 5000,
3939
"format": {
@@ -50,7 +50,7 @@ $ node app.js
5050

5151
```json
5252
{
53-
"plugin_name": "weather",
53+
"type": "weather",
5454
"enabled": true,
5555
"duration": 5000,
5656
"city": "Paris",
@@ -67,7 +67,7 @@ $ node app.js
6767

6868
```json
6969
{
70-
"plugin_name": "lastfm-last-played-track",
70+
"type": "lastfm-last-played-track",
7171
"enabled": true,
7272
"duration": 5000,
7373
"username": "mhor_"
@@ -80,7 +80,7 @@ $ node app.js
8080

8181
```json
8282
{
83-
"plugin_name": "lastfm-current-track",
83+
"type": "lastfm-current-track",
8484
"enabled": true,
8585
"duration": 5000,
8686
"username": "mhor_"

config/default.json.dist

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,36 @@
1111
"key": null
1212
},
1313
"steps": [{
14-
"plugin_name": "lastfm-current-track",
14+
"type": "lastfm-current-track",
1515
"enabled": true,
1616
"duration": 5000,
1717
"username": "mhor_"
1818
}, {
19-
"plugin_name": "lastfm-last-played-track",
19+
"type": "lastfm-last-played-track",
2020
"enabled": true,
2121
"duration": 5000,
2222
"username": "mhor_"
2323
}, {
24-
"plugin_name": "weather",
24+
"type": "weather",
2525
"enabled": true,
2626
"duration": 5000,
2727
"city": "Paris",
2828
"lat": 48.8534100,
2929
"long": 2.3488000
3030
}, {
31-
"plugin_name": "weather",
31+
"type": "weather",
3232
"enabled": true,
3333
"duration": 5000,
3434
"city": "Mitry Mory",
3535
"lat": 48.9833,
3636
"long": 2.6167
3737
}, {
38-
"plugin_name": "datetime",
38+
"type": "datetime",
3939
"enabled": true,
4040
"duration": 5000,
4141
"format": {
42-
"date": "LLLL",
43-
"time": "llll"
42+
"date": "ll",
43+
"time": "LT"
4444
}
4545
}]
4646
}

src/arduino-station.js

Lines changed: 103 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,117 @@
1-
var config = require("config");
1+
const config = require("config");
22

3-
var Forecast = require("forecast");
4-
var LastFmNode = require("lastfm").LastFmNode;
3+
const Forecast = require("forecast");
4+
const LastFmNode = require("lastfm").LastFmNode;
55

6-
var Datetime = require('./datetime');
7-
var LastFmCurrentTrack = require('./lastfm-current-track');
8-
var LastFmLastPlayedTrack = require('./lastfm-last-played-track');
9-
var Weather = require('./weather');
6+
const Datetime = require('./step/datetime');
7+
const LastFmCurrentTrack = require('./step/lastfm-current-track');
8+
const LastFmLastPlayedTrack = require('./step/lastfm-last-played-track');
9+
const Weather = require('./step/weather');
1010

11-
function ArduinoStation(board, lcd) {
12-
this.sequences = [];
13-
this.loop = true;
14-
this.board = board;
15-
this.lcd = lcd;
16-
this.lastfm = null;
17-
this.forecast = null;
18-
this.LastFmNode = null;
19-
}
20-
21-
ArduinoStation.prototype.init = function() {
22-
this.forecast = new Forecast({
23-
service: "darksky",
24-
key: config.get("darksky.key"),
25-
units: "celcius",
26-
cache: true,
27-
ttl: {
28-
minutes: 60
29-
}
30-
});
31-
32-
this.lastfm = new LastFmNode({
33-
api_key: config.get("lastfm.key"),
34-
secret: null,
35-
useragent: null
36-
});
37-
38-
var self = this;
39-
config.get('steps').forEach(function(step) {
40-
switch(step.plugin_name) {
41-
case 'datetime':
42-
var datetime = new Datetime(step.duration, step.enabled);
43-
datetime.init();
44-
self.sequences.push(datetime);
45-
break;
46-
case 'lastfm-last-played-track':
47-
var lastFmLastPlayedTrack = new LastFmLastPlayedTrack(self.lastfm, step.username, step.duration, step.enabled);
48-
lastFmLastPlayedTrack.init();
49-
self.sequences.push(lastFmLastPlayedTrack);
50-
break;
51-
case 'lastfm-current-track':
52-
var lastFmCurrentTrack = new LastFmCurrentTrack(self.lastfm, step.username, step.duration, step.enabled);
53-
lastFmCurrentTrack.init();
54-
self.sequences.push(lastFmCurrentTrack);
55-
break;
56-
case 'weather':
57-
var weather = new Weather(self.forecast, step.lat, step.long, step.city, step.duration, step.enabled);
58-
weather.init();
59-
self.sequences.push(weather);
60-
break;
61-
}
62-
});
63-
}
11+
class ArduinoStation {
12+
constructor(board, lcd) {
13+
this.sequences = [];
14+
this.loop = true;
15+
this.board = board;
16+
this.lcd = lcd;
17+
this.lastfm = null;
18+
this.forecast = null;
19+
this.LastFmNode = null;
20+
}
6421

65-
ArduinoStation.prototype.execute = function(step) {
22+
init() {
23+
this.forecast = new Forecast({
24+
service: "darksky",
25+
key: config.get("darksky.key"),
26+
units: "celcius",
27+
cache: true,
28+
ttl: {
29+
minutes: 60
30+
}
31+
});
6632

67-
var name = this.sequences[step].type;
68-
var enabled = this.sequences[step].isEnabled();
69-
var duration = this.sequences[step].duration;
33+
this.lastfm = new LastFmNode({
34+
api_key: config.get("lastfm.key"),
35+
secret: null,
36+
useragent: null
37+
});
7038

71-
if (enabled === true) {
72-
this.board.info("APP", "Run method " + name + " for " + duration + " milliseconds");
73-
this.sequences[step].execute(this.board, this.lcd);
74-
} else {
75-
duration = 0;
39+
const self = this;
40+
config.get('steps').forEach((step) => {
41+
switch(step.type) {
42+
case 'datetime':
43+
const datetime = new Datetime(
44+
step.format.date,
45+
step.format.time,
46+
step.duration,
47+
step.enabled
48+
);
49+
datetime.init();
50+
self.sequences.push(datetime);
51+
break;
52+
case 'lastfm-last-played-track':
53+
const lastFmLastPlayedTrack = new LastFmLastPlayedTrack(
54+
self.lastfm,
55+
step.username,
56+
step.duration,
57+
step.enabled
58+
);
59+
lastFmLastPlayedTrack.init();
60+
self.sequences.push(lastFmLastPlayedTrack);
61+
break;
62+
case 'lastfm-current-track':
63+
const lastFmCurrentTrack = new LastFmCurrentTrack(
64+
self.lastfm,
65+
step.username,
66+
step.duration,
67+
step.enabled
68+
);
69+
lastFmCurrentTrack.init();
70+
self.sequences.push(lastFmCurrentTrack);
71+
break;
72+
case 'weather':
73+
const weather = new Weather(
74+
self.forecast,
75+
step.lat,
76+
step.long,
77+
step.city,
78+
step.duration,
79+
step.enabled
80+
);
81+
weather.init();
82+
self.sequences.push(weather);
83+
break;
84+
}
85+
});
7686
}
7787

78-
step++;
79-
if (step === this.sequences.length) {
80-
if (this.loop) {
81-
step = 0;
88+
execute(step) {
89+
const name = this.sequences[step].type;
90+
const enabled = this.sequences[step].isEnabled();
91+
var duration = this.sequences[step].duration;
92+
93+
if (enabled === true) {
94+
this.board.info("APP", "Run method " + name + " for " + duration + " milliseconds");
95+
this.sequences[step].execute(this.board, this.lcd);
8296
} else {
83-
// We"re done!
84-
process.exit(0);
97+
duration = 0;
8598
}
86-
}
8799

88-
this.board.wait(
89-
duration,
90-
(function() {this.execute(step);}).bind(this)
91-
);
100+
step++;
101+
if (step === this.sequences.length) {
102+
if (this.loop) {
103+
step = 0;
104+
} else {
105+
// We"re done!
106+
process.exit(0);
107+
}
108+
}
109+
110+
this.board.wait(
111+
duration,
112+
(() => { this.execute(step); }).bind(this)
113+
);
114+
}
92115
}
93116

94117
module.exports = ArduinoStation;

src/datetime.js

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/lastfm-current-track.js

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)