-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
105 lines (87 loc) · 3.17 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
/**
* ambient2pwsweather
* * Pull weather data from an AmbientWeather.net Station and push to a PWSweather.com Station
*
* * Requires AmbientWeather.net API Key and PWSweather.com StationID and Password
* * Currently only supports 1 device from AmbientWeather
*/
process.env['NODE_CONFIG_DIR'] = __dirname + '/config/';
const got = require('got'),
pkg = require('./package.json'),
config = require('config'),
_ = require('lodash'),
moment = require('moment'),
logger = require('eazy-logger').Logger({
useLevelPrefixes: true,
level : process.env.LOG_LEVEL || config.log_level
}),
AmbientAPI = require('ambient-weather-api'),
appKey = config.ambient.app_key || process.env.AMBIENT_APP_KEY,
apiKey = config.ambient.api_key || process.env.AMBIENT_API_KEY,
pwsStation = config.pwsweather.station_id || process.env.PWS_STATION_ID,
pwsPassword = config.pwsweather.password || process.env.PWS_PASSWORD;
logger.info('------- ambient2weather v' + pkg.version, '--------');
logger.debug('Ambient Application Key:', appKey);
logger.debug('Ambient API Key:', apiKey);
logger.debug('PWS Station ID:', pwsStation);
logger.debug('PWS Password:', pwsPassword);
if (!appKey || !apiKey || !pwsStation || !pwsPassword) {
logger.error('Insufficient configuration data. Missing API Key, App Key, Station ID or PWS Password.');
process.exit();
}
// instantiate the API
const api = new AmbientAPI({
apiKey : apiKey,
applicationKey : appKey
});
// On connect, we subscribe (this will also re-subscribe when the connection fails and reconnects)
api.on('connect', () => {
logger.debug('Connected to Ambient Weather Realtime API')
api.subscribe(apiKey);
});
api.on('subscribed', data => {
logger.debug('Subscribed to device:', data.devices[0].info.name);
logger.debug(data.devices[0].lastData.date + ' - ' + data.devices[0].info.name + ' current outdoor temperature is: ' + data.devices[0].lastData.tempf + '°F');
});
var params = {};
// On data, package it up for PWSweather.com and send it over
api.on('data', data => {
logger.debug(data.date + ' - ' + data.device.info.name + ' current outdoor temperature is: ' + data.tempf + '°F');
delete data.device;
delete data.macAddress;
// logger.debug('data', data);
params = _.merge(data, {
ID : pwsStation,
PASSWORD : pwsPassword,
dateutc : moment(data.date).format('YYYY-MM-DD HH:mm:ss'),
baromin : data.baromrelin,
dewptf : data.dewPoint,
humidity : data.humidityin,
rainin : data.hourlyrainin,
UV : data.uv,
softwaretype: 'ambient2pwsweather-' + pkg.version,
action : 'updateraw'
});
delete params.date;
delete params.baromrelin;
delete params.dewPoint;
delete params.uv;
delete params.baromabsin;
delete params.feelslike;
delete params.maxdailygust;
delete params.totalrainin;
delete params.hourlyrainin;
// logger.debug('params', params);
got(config.pws_base_url, {query: params}).then(response => {
if (response.statusCode === 200) {
logger.debug('Success', response.statusMessage);
} else {
logger.error('Failed', response.statusMessage);
}
}).catch(reason => {
logger.error('PWSweather request failed', reason);
});
});
// Let's start the connection
logger.debug('Connecting to AmbientWeather.net...');
api.connect();