-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
243 lines (194 loc) · 5.96 KB
/
index.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var cylon = require('cylon');
var request = require('request');
var config = require('./config');
var moment = require('moment-timezone');
app.use(express.static('public'));
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
http.listen(3000, function(){
console.log('Geographic Environment Observer online at http://localhost:3000');
});
var wunderground_api_key = config.wundergroundKey;
var pinoccio_api_key = config.pinoccioKey;
var timezone = null;
var lightSun = 'https://api.pinocc.io/v1/4/1/command/pixels.sun?token=' + pinoccio_api_key;
var lightMoon = 'https://api.pinocc.io/v1/4/1/command/pixels.moon?token=' + pinoccio_api_key;
var lightning = 'https://api.pinocc.io/v1/4/1/command/pixels.lightning?token=' + pinoccio_api_key;
cylon.robot({
connections: {
edison: { adaptor: 'intel-iot' }
},
devices: {
rainPin: { driver: 'direct-pin', pin: 8 },
cloudPin: { driver: 'direct-pin', pin: 9 }
}
}).on('ready', function(my) {
console.log('cylon ready');
io.on('connection', function(socket){
socket.on('rain', function (msg) {
calculateRain(msg);
});
socket.on('sunrise', function (msg) {
calculateSunrise(msg);
});
socket.on('sunset', function (msg) {
calculateSunset(msg);
});
socket.on('clouds', function (msg) {
calculateClouds(msg);
});
function calculateRain(data) {
console.log('Rain data is ', data);
socket.emit('rain', data);
if(data === true) {
//start rain
console.log('Start ☂');
my.rainPin.digitalWrite(1);
setTimeout(function(){
//stop rain
my.rainPin.digitalWrite(0);
console.log('Stop ☂');
}, 3000);
} else {
my.rainPin.digitalWrite(0);
console.log('No ☂');
}
}
function calculateSunrise(data) {
socket.emit('sunrise', data);
console.log('☀');
request(lightSun, function(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
});
}
function calculateSunset(data) {
socket.emit('sunset', data);
console.log('☾');
request(lightMoon, function(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
});
}
function calculateClouds(data) {
socket.emit('clouds', data);
if(data === true) {
console.log('☁');
my.cloudPin.digitalWrite(1);
} else {
console.log('No ☁');
my.cloudPin.digitalWrite(0);
}
}
socket.on('daytime', function (msg) {
calculateDaytime(msg);
});
function calculateDaytime(data){
if(data == true) {
socket.emit('daytime', true);
console.log('☀');
request(lightSun, function(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
});
} else {
socket.emit('daytime', false);
console.log('☾');
request(lightMoon, function(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
});
}
}
socket.on('city event', function (result) {
console.log('Getting data for', result.cityName);
var lat = result.lat;
var lon = result.lon;
timezone = result.tz;
get_weather(lat, lon);
get_astronomy(lat, lon);
setInterval(function () {
console.log('An hour has passed. Reobserving the weather...');
get_weather(lat, lon);
get_astronomy(lat, lon);
}, 1000 * 60 * 60);
});
function get_weather(lat, lon) {
var req_url = 'http://api.wunderground.com/api/' + wunderground_api_key + '/conditions/q/' + lat + ',' + lon + '.json';
console.log(req_url);
request(req_url, function(error, response, body) {
if (!error && response.statusCode == 200) {
var JSON_body = JSON.parse(body);
if(JSON_body.response.hasOwnProperty('error')) {
if(JSON_body.response.error.type === 'querynotfound')
{
console.log('Could not find this city');
}
} else {
process_weather(JSON_body.current_observation);
}
} else {
console.log('Error occured');
}
});
}
function process_weather(observation) {
var clouds = [ 'Mostly Cloudy', 'Partly Cloudy', 'Overcast', 'Shallow Fog', 'Partial Fog', 'Scattered Clouds' ];
var rain = [ 'Drizzle', 'Light Drizzle', 'Heavy Drizzle', 'Rain', 'Light Rain', 'Heavy Rain', 'Mist', 'Light Mist', 'Heavy Mist', 'Rain Mist', 'Light Rain Mist', 'Heavy Rain Mist', 'Rain Showers', 'Light Rain Showers', 'Heavy Rain Showers'];
if (clouds.indexOf(observation.weather) != -1) {
calculateClouds(true);
} else {
calculateClouds(false);
}
if (rain.indexOf(observation.weather) != -1 ) {
calculateRain(true);
calculateClouds(true);
} else {
calculateRain(false);
}
}
function get_astronomy(lat, lon) {
var req_url = 'http://api.wunderground.com/api/' + wunderground_api_key + '/astronomy/q/' + lat + ',' + lon + '.json';
request(req_url, function(error, response, body) {
if (!error && response.statusCode == 200) {
var JSON_body = JSON.parse(body);
if(JSON_body.response.hasOwnProperty('error')) {
console.log('Could not find this city');
} else {
process_astronomy(JSON_body);
}
}
});
}
function process_astronomy(observation) {
var hour = new Date().getHours();
var sunrise = observation.sun_phase.sunrise.hour;
var sunset = observation.sun_phase.sunset.hour;
console.log('Locating the sun in the sky...');
//console.log(moment.tz.zone(timezone).offset(moment().unix()) / 60);
if(hour >= sunrise && hour <= sunset) {
calculateDaytime(true);
} else {
calculateDaytime(false);
}
if (hour === sunrise) {
calculateDaytime(true);
calculateSunrise(true);
}
if (hour === sunset) {
calculateSunset(true);
calculateDaytime(false);
}
}
});
});
cylon.start();