-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathpws-historical.js
148 lines (137 loc) · 5.28 KB
/
pws-historical.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
module.exports = function(RED) {
function weatherPWSHistoricalDataNode( n ) {
RED.nodes.createNode(this,n );
var node = this;
var StationId = n.stationid;
var units = n.units;
var precision = n.precision;
var name = n.name;
var range = n.range;
var date = n.date;
var pwsConfigNode;
var apiKey;
const axios = require('axios');
// Retrieve the config node
pwsConfigNode = RED.nodes.getNode(n.apikey);
apiKey = pwsConfigNode.credentials.apikey;
if (!units) {
units = 'm';
}
if (!precision) {
precision = 'i';
}
function datevalidation( v ) {
var datestr =""+v // cast from number to string
if (!/^\d{8}/.test(datestr)) { return false; }
var year = datestr.substring(0,4);
var month = datestr.substring(4,6);
var day = datestr.substring(6,8);
// Date.parse will return NaN if this isn't a valid date
var time = Date.parse(year+"-"+month+"-"+day);
return !isNaN(time);
}
function dateparser( msg ) {
// Date validation and reformatting
var teststr;
if( typeof msg.twcparams.date !== 'undefined' ) {
// The pws date to query was passed in as a msg key value pair
// override any value specified in the edit panel
var time = new Date(msg.twcparams.date);
if( !isNaN(time) ) {
// msg.date contains a timestamp
// Grab the date 2020-01-01 from the ISO format
teststr = time.toISOString().substr(0,10);
} else {
// cast from number to string in case it was a number
teststr = ""+msg.twcparams.date;
}
} else {
// Use the date provided in the edit panel
teststr = date ;
}
var datestr;
// reformat 2020-01-01 or 2020/01/01 to 20200101 required yyyymmdd format
datestr = teststr.replace( /(\d{4})[\-/](\d{1,2})[\-/](\d{1,2})/, '$1$2$3' );
if( !datestr || !datevalidation(datestr) ) {
msg.payload = "Error: No or incorrect date format provided. yyyymmdd required.";
node.send(msg);
return null;
}
return datestr;
}
node.on('input', function (msg) {
msg.twcparams = msg.twcparams || {};
var datestr = dateparser( msg );
if( !datestr ) {
return;
}
msg.twcparams.date = datestr ;
if( typeof msg.twcparams.range == 'undefined' ) {
msg.twcparams.range = range;
}
if( typeof msg.twcparams.units == 'undefined' ) {
msg.twcparams.units = units; // take the default or the node setting
} else if( "emhEMH".indexOf(msg.twcparams.units) >= 0 ) {
// passed in param is valid, override default or node setting
msg.twcparams.units = msg.twcparams.units.toLowerCase();
} else {
msg.twcparams.units = units; // take the default or the node setting
}
if( typeof msg.twcparams.precision == 'undefined' ) {
msg.twcparams.precision = precision;
} else if( "idID".indexOf(msg.twcparams.precision) >= 0 ) {
// passed in param is valid, override default or node setting
msg.twcparams.precision = msg.twcparams.precision.toLowerCase();
} else {
msg.twcparams.precision = precision;
}
if ( msg.twcparams.precision == 'd') {
var numericPrecision = '&numericPrecision=decimal';
} else {
var numericPrecision = '';
}
var curStationId = StationId;
if( typeof msg.twcparams.StationID != 'undefined' ) {
curStationId = msg.twcparams.StationID.toUpperCase();
}
if( !curStationId ) {
// No StationID is set. Abort with error
msg.payload = "Error: No StationID provided.";
node.send(msg);
} else {
msg.twcparams.StationID = curStationId;
(async () => {
try {
const response = await axios.get('https://api.weather.com/v2/pws/history/'+ msg.twcparams.range + '?stationId='+ curStationId +'&format=json&date='+datestr+'&units='+msg.twcparams.units+'&apiKey='+apiKey+numericPrecision);
//console.log(response.data)
msg.payload = response.data;
node.send(msg);
} catch (error) {
//console.log(error.response.data);
//console.log(error.response.status);
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.log(error.response.data);
console.log(error.response.status);
console.log(error.response.headers);
node.warn(error.response.data);
} else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console.log(error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.log('Error', error.message);
}
console.log(error.config);
console.log(error.toJSON());
node.send(msg);
}
})();
}
});
}
RED.nodes.registerType("pws-historical",weatherPWSHistoricalDataNode);
}