Small changes to read and weather route
This commit is contained in:
@@ -3,12 +3,14 @@
|
|||||||
## Description
|
## Description
|
||||||
This script is used by OpenSprinkler Unified Firmware to update the water level of the device. It also provides timezone information based on user location along with other local information (sunrise, sunset, daylights saving time, etc).
|
This script is used by OpenSprinkler Unified Firmware to update the water level of the device. It also provides timezone information based on user location along with other local information (sunrise, sunset, daylights saving time, etc).
|
||||||
|
|
||||||
The production version runs on Amazon Elastic Beanstalk (AWS EB) and therefore this package is tailored to be zipped and uploaded to AWS EB. The script is written in Python.
|
The production version runs on Amazon Elastic Beanstalk (AWS EB) and therefore this package is tailored to be zipped and uploaded to AWS EB. The script is written in Javascript for Node.JS.
|
||||||
|
|
||||||
## File Detail
|
## File Detail
|
||||||
**Requirements.txt** is used to define the required Python modules needed to run the script.
|
**server.js** is the primary file launching the API daemon.
|
||||||
|
|
||||||
**Application.py** parses the incoming URL and returns the appropriate values. The script defaults to URL format return however a 'format' parameter can be passed with the value 'json' in order to output JSON.
|
**routes/*.js** contains all the endpoints for the API service. Currently, only one exists for weather adjustment.
|
||||||
|
|
||||||
|
**models/*.js** contains all the database models used by the routes. Currently, only one exists to manage weather cache data.
|
||||||
|
|
||||||
## Privacy
|
## Privacy
|
||||||
|
|
||||||
|
|||||||
@@ -12,47 +12,6 @@
|
|||||||
time: /(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})([+-])(\d{2})(\d{2})/
|
time: /(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})([+-])(\d{2})(\d{2})/
|
||||||
};
|
};
|
||||||
|
|
||||||
// Generic HTTP request handler that parses the URL and uses the
|
|
||||||
// native Node.js http module to perform the request
|
|
||||||
function httpRequest( url, callback ) {
|
|
||||||
url = url.match( filters.url );
|
|
||||||
|
|
||||||
var options = {
|
|
||||||
host: url[1],
|
|
||||||
port: url[2] || 80,
|
|
||||||
path: url[3]
|
|
||||||
};
|
|
||||||
|
|
||||||
http.get( options, function( response ) {
|
|
||||||
var data = "";
|
|
||||||
|
|
||||||
// Reassemble the data as it comes in
|
|
||||||
response.on( "data", function( chunk ) {
|
|
||||||
data += chunk;
|
|
||||||
} );
|
|
||||||
|
|
||||||
// Once the data is completely received, return it to the callback
|
|
||||||
response.on( "end", function() {
|
|
||||||
callback( data );
|
|
||||||
} );
|
|
||||||
} ).on( "error", function() {
|
|
||||||
|
|
||||||
// If the HTTP request fails, return false
|
|
||||||
callback( false );
|
|
||||||
} );
|
|
||||||
}
|
|
||||||
|
|
||||||
// Converts IP string to integer
|
|
||||||
function ipToInt( ip ) {
|
|
||||||
ip = ip.split( "." );
|
|
||||||
return ( ( ( ( ( ( +ip[0] ) * 256 ) + ( +ip[1] ) ) * 256 ) + ( +ip[2] ) ) * 256 ) + ( +ip[3] );
|
|
||||||
}
|
|
||||||
|
|
||||||
// Resolves the Month / Day / Year of a Date object
|
|
||||||
Date.prototype.toUSDate = function(){
|
|
||||||
return ( this.getMonth() + 1 ) + "/" + this.getDate() + "/" + this.getFullYear();
|
|
||||||
};
|
|
||||||
|
|
||||||
// Takes a PWS or ICAO location and resolves the GPS coordinates
|
// Takes a PWS or ICAO location and resolves the GPS coordinates
|
||||||
function getPWSCoordinates( location, weatherUndergroundKey, callback ) {
|
function getPWSCoordinates( location, weatherUndergroundKey, callback ) {
|
||||||
var url = "http://api.wunderground.com/api/" + weatherUndergroundKey +
|
var url = "http://api.wunderground.com/api/" + weatherUndergroundKey +
|
||||||
@@ -96,23 +55,6 @@
|
|||||||
} );
|
} );
|
||||||
}
|
}
|
||||||
|
|
||||||
// Accepts a time string formatted in ISO-8601 and returns the timezone.
|
|
||||||
// The timezone output is formatted for OpenSprinkler Unified firmware.
|
|
||||||
function getTimezone( time ) {
|
|
||||||
|
|
||||||
// Match the provided time string against a regex for parsing
|
|
||||||
time = time.match( filters.time );
|
|
||||||
|
|
||||||
var hour = parseInt( time[7] + time[8] ),
|
|
||||||
minute = parseInt( time[9] );
|
|
||||||
|
|
||||||
// Convert the timezone into the OpenSprinkler encoded format
|
|
||||||
minute = ( minute / 15 >> 0 ) / 4;
|
|
||||||
hour = hour + ( hour >=0 ? minute : -minute );
|
|
||||||
|
|
||||||
return ( ( hour + 12 ) * 4 ) >> 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Retrieve weather data to complete the weather request
|
// Retrieve weather data to complete the weather request
|
||||||
function getWeatherData( location, callback ) {
|
function getWeatherData( location, callback ) {
|
||||||
|
|
||||||
@@ -249,21 +191,6 @@
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to return the sunrise and sunset times from the weather reply
|
|
||||||
function getSunData( weather ) {
|
|
||||||
|
|
||||||
// Sun times are parsed from string against a regex to identify the timezone
|
|
||||||
var sunrise = weather.observation.sunrise.match( filters.time ),
|
|
||||||
sunset = weather.observation.sunset.match( filters.time );
|
|
||||||
|
|
||||||
return [
|
|
||||||
|
|
||||||
// Values are converted to minutes from midnight for the controller
|
|
||||||
parseInt( sunrise[4] ) * 60 + parseInt( sunrise[5] ),
|
|
||||||
parseInt( sunset[4] ) * 60 + parseInt( sunset[5] )
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
// Checks if the weather data meets any of the restrictions set by OpenSprinkler.
|
// Checks if the weather data meets any of the restrictions set by OpenSprinkler.
|
||||||
// Restrictions prevent any watering from occurring and are similar to 0% watering level.
|
// Restrictions prevent any watering from occurring and are similar to 0% watering level.
|
||||||
//
|
//
|
||||||
@@ -410,5 +337,90 @@
|
|||||||
} );
|
} );
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
} )();
|
|
||||||
|
|
||||||
|
// Generic HTTP request handler that parses the URL and uses the
|
||||||
|
// native Node.js http module to perform the request
|
||||||
|
function httpRequest( url, callback ) {
|
||||||
|
url = url.match( filters.url );
|
||||||
|
|
||||||
|
var options = {
|
||||||
|
host: url[1],
|
||||||
|
port: url[2] || 80,
|
||||||
|
path: url[3]
|
||||||
|
};
|
||||||
|
|
||||||
|
http.get( options, function( response ) {
|
||||||
|
var data = "";
|
||||||
|
|
||||||
|
// Reassemble the data as it comes in
|
||||||
|
response.on( "data", function( chunk ) {
|
||||||
|
data += chunk;
|
||||||
|
} );
|
||||||
|
|
||||||
|
// Once the data is completely received, return it to the callback
|
||||||
|
response.on( "end", function() {
|
||||||
|
callback( data );
|
||||||
|
} );
|
||||||
|
} ).on( "error", function() {
|
||||||
|
|
||||||
|
// If the HTTP request fails, return false
|
||||||
|
callback( false );
|
||||||
|
} );
|
||||||
|
}
|
||||||
|
|
||||||
|
// Accepts a time string formatted in ISO-8601 and returns the timezone.
|
||||||
|
// The timezone output is formatted for OpenSprinkler Unified firmware.
|
||||||
|
function getTimezone( time ) {
|
||||||
|
|
||||||
|
// Match the provided time string against a regex for parsing
|
||||||
|
time = time.match( filters.time );
|
||||||
|
|
||||||
|
var hour = parseInt( time[7] + time[8] ),
|
||||||
|
minute = parseInt( time[9] );
|
||||||
|
|
||||||
|
// Convert the timezone into the OpenSprinkler encoded format
|
||||||
|
minute = ( minute / 15 >> 0 ) / 4;
|
||||||
|
hour = hour + ( hour >=0 ? minute : -minute );
|
||||||
|
|
||||||
|
return ( ( hour + 12 ) * 4 ) >> 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to return the sunrise and sunset times from the weather reply
|
||||||
|
function getSunData( weather ) {
|
||||||
|
|
||||||
|
// Sun times are parsed from string against a regex to identify the timezone
|
||||||
|
var sunrise = weather.observation.sunrise.match( filters.time ),
|
||||||
|
sunset = weather.observation.sunset.match( filters.time );
|
||||||
|
|
||||||
|
return [
|
||||||
|
|
||||||
|
// Values are converted to minutes from midnight for the controller
|
||||||
|
parseInt( sunrise[4] ) * 60 + parseInt( sunrise[5] ),
|
||||||
|
parseInt( sunset[4] ) * 60 + parseInt( sunset[5] )
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Converts IP string to integer
|
||||||
|
function ipToInt( ip ) {
|
||||||
|
ip = ip.split( "." );
|
||||||
|
return ( ( ( ( ( ( +ip[0] ) * 256 ) + ( +ip[1] ) ) * 256 ) + ( +ip[2] ) ) * 256 ) + ( +ip[3] );
|
||||||
|
}
|
||||||
|
|
||||||
|
function F2C( temp ) {
|
||||||
|
return ( temp - 32 ) * 5 / 9;
|
||||||
|
}
|
||||||
|
|
||||||
|
function mm2in( x ) {
|
||||||
|
return x * 0.03937008;
|
||||||
|
}
|
||||||
|
|
||||||
|
function ft2m( x ) {
|
||||||
|
return x * 0.3048;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Resolves the Month / Day / Year of a Date object
|
||||||
|
Date.prototype.toUSDate = function(){
|
||||||
|
return ( this.getMonth() + 1 ) + "/" + this.getDate() + "/" + this.getFullYear();
|
||||||
|
};
|
||||||
|
|
||||||
|
} )();
|
||||||
|
|||||||
Reference in New Issue
Block a user