|
1 | | -var config = require("config"); |
| 1 | +const config = require("config"); |
2 | 2 |
|
3 | | -var Forecast = require("forecast"); |
4 | | -var LastFmNode = require("lastfm").LastFmNode; |
| 3 | +const Forecast = require("forecast"); |
| 4 | +const LastFmNode = require("lastfm").LastFmNode; |
5 | 5 |
|
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'); |
10 | 10 |
|
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 | + } |
64 | 21 |
|
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 | + }); |
66 | 32 |
|
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 | + }); |
70 | 38 |
|
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 | + }); |
76 | 86 | } |
77 | 87 |
|
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); |
82 | 96 | } else { |
83 | | - // We"re done! |
84 | | - process.exit(0); |
| 97 | + duration = 0; |
85 | 98 | } |
86 | | - } |
87 | 99 |
|
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 | + } |
92 | 115 | } |
93 | 116 |
|
94 | 117 | module.exports = ArduinoStation; |
0 commit comments