mirror of
https://github.com/Hopiu/webapi-eca.git
synced 2026-03-16 22:10:31 +00:00
added rule info and examples
This commit is contained in:
parent
2e66a24409
commit
d32d442000
6 changed files with 57 additions and 42 deletions
|
|
@ -1,4 +1,3 @@
|
|||
|
||||
#
|
||||
# EmailYak EVENT POLLER
|
||||
# ---------------------
|
||||
|
|
@ -17,10 +16,36 @@ exports.newMail = ( pushEvent ) ->
|
|||
# Syntax: needle.request method, url, data, [options], callback
|
||||
#
|
||||
needle.request 'get', url, null, null, ( err, resp, body ) ->
|
||||
log 'Poll function executed'
|
||||
if err
|
||||
log 'Error in EmailYak EM newMail: ' + err.message
|
||||
else
|
||||
log body
|
||||
if resp.statusCode is 200
|
||||
if body.Emails.length > 0
|
||||
log "#{ body.Emails.length } mail events pushed into the system"
|
||||
pushEvent mail for mail in body.Emails
|
||||
|
||||
###
|
||||
This will emit events of the form:
|
||||
( Refer to http://docs.emailyak.com/get-new-email.html for more information. )
|
||||
|
||||
{
|
||||
"EmailID": "xquukd5z",
|
||||
"Received": "2014-04-19T11:27:11",
|
||||
"ToAddress": "test@mscliveweb.simpleyak.com",
|
||||
"ParsedData": [
|
||||
{
|
||||
"Data": "Best Regards\nTest User",
|
||||
"Part": 0,
|
||||
"Type": "Email"
|
||||
}
|
||||
],
|
||||
"FromName": "Test User",
|
||||
"ToAddressList": "test@mscliveweb.simpleyak.com",
|
||||
"FromAddress": "test.address@provider.com",
|
||||
"HtmlBody": "Best Regards\nTest User",
|
||||
"CcAddressList": "",
|
||||
"TextBody": "Best Regards\nTest User",
|
||||
"Subject": "test subject"
|
||||
}
|
||||
###
|
||||
|
|
@ -8,13 +8,14 @@ required module params:
|
|||
|
||||
params.apikey = "Cc8AX35d4B89ozzmn5bpm7k70HRon5rrfUxZvOwkVRj31/oBGHzVfQSRp5mEvlOgxyh7xi+tFSL66iAFo1W/sQ=="
|
||||
params.userGuid = "d19f0d08-bf73-4115-90a8-ac045ad4f225"
|
||||
params.queryGuid = "caff10dc-3bf8-402e-b1b8-c799a77c3e8c"
|
||||
params.queryGuid = "4f833315-7aa0-4fcd-b8d0-c65f6a6bafcf"
|
||||
|
||||
io = new importio params.userGuid, params.apikey, "query.import.io"
|
||||
|
||||
exports.queryData = ( pushEvent ) ->
|
||||
debug params.apikey
|
||||
debug params.queryGuid
|
||||
debug params.userGuid
|
||||
io = new importio params.userGuid, params.apikey, "query.import.io"
|
||||
|
||||
io.connect ( connected ) ->
|
||||
if not connected
|
||||
|
|
@ -22,10 +23,15 @@ exports.queryData = ( pushEvent ) ->
|
|||
else
|
||||
log "Connected!"
|
||||
data = []
|
||||
io.query "input": "input": "query", "connectorGuids": [ params.queryGuid ], ( finished, msg ) ->
|
||||
io.query "input": { "input": "query" }, "connectorGuids": [ params.queryGuid ], ( finished, msg ) ->
|
||||
log 'query returned'
|
||||
log msg
|
||||
if msg.type is "MESSAGE"
|
||||
log "Adding #{ msg.data.results.length } results"
|
||||
data = data.concat msg.data.results
|
||||
if finished
|
||||
log "Done"
|
||||
log JSON.stringify data
|
||||
log 'all work done'
|
||||
log io
|
||||
io = null
|
||||
|
|
|
|||
|
|
@ -6,56 +6,38 @@ This module requires user-specific parameters:
|
|||
- openweatherKey
|
||||
- tempThreshold
|
||||
- city
|
||||
- eventTime ( hh:mm of the day )
|
||||
###
|
||||
urlService = 'http://api.openweathermap.org/data/2.5/weather'
|
||||
lastEvent = new Date 0
|
||||
twentyFourHoursInms = 24 * 60 * 60 * 1000
|
||||
dayTimeInMin = 0
|
||||
|
||||
calcEventDayTimeInMin = ( et ) ->
|
||||
arrTime = et.split ':'
|
||||
hrs = parseInt arrTime[ 0 ]
|
||||
mins = parseInt arrTime[ 1 ]
|
||||
dayTimeInMin = hrs * 60 + mins
|
||||
if isNaN dayTimeInMin
|
||||
log 'Wrong temperature input! ' + et
|
||||
|
||||
try
|
||||
calcEventDayTimeInMin params.eventTime
|
||||
catch err
|
||||
log 'Unable to parse the eventTime parameter'
|
||||
|
||||
|
||||
###
|
||||
Fetches the temperature
|
||||
###
|
||||
getTemperature = ( cb ) ->
|
||||
getWeatherData = ( cb ) ->
|
||||
url = urlService + '?APPID=' + params.openweatherKey + '&q=' + params.city
|
||||
needle.request 'get', url, null, null, cb
|
||||
|
||||
###
|
||||
Pushes the current weather data into the system
|
||||
###
|
||||
exports.currentData = ( pushEvent ) ->
|
||||
getWeatherData ( err, resp, body ) ->
|
||||
if err or resp.statusCode isnt 200
|
||||
log JSON.stringify body
|
||||
else
|
||||
pushEvent body
|
||||
|
||||
###
|
||||
Emits one event per day if the temperature today raises above user defined threshold
|
||||
###
|
||||
exports.temperatureOverThreshold = ( pushEvent ) ->
|
||||
getTemperature ( err, resp, body ) ->
|
||||
timeNow = new Date()
|
||||
|
||||
getWeatherData ( err, resp, body ) ->
|
||||
if err or resp.statusCode isnt 200
|
||||
debug body
|
||||
log JSON.stringify body
|
||||
else
|
||||
#If temperature is above threshold
|
||||
if body.main.temp_max - 272.15 > params.tempThreshold and
|
||||
|
||||
# If last event was more than 24 hours ago
|
||||
timeNow - lastEvent > twentyFourHoursInms and
|
||||
|
||||
# If we are past the time the user wants to get the information
|
||||
timeNow.getHours() * 60 + timeNow.getMinutes() > dayTimeInMin
|
||||
|
||||
lastEvent = timeNow
|
||||
if body.main.temp_max - 272.15 > params.tempThreshold
|
||||
pushEvent
|
||||
threshold: params.tempThreshold
|
||||
measured: body.main.temp_max - 272.15
|
||||
content: "The temperature will be #{ body.main.temp_max - 272.15 } today!"
|
||||
|
||||
|
|
|
|||
|
|
@ -68,8 +68,9 @@ fOnLoad = () ->
|
|||
id: ruleName
|
||||
$.post( '/usercommand', data )
|
||||
.done ( data ) ->
|
||||
ts = ( new Date() ).toISOString()
|
||||
log = data.message.replace new RegExp("\n", 'g'), "<br>"
|
||||
$( '#log_col' ).html "<h3>#{ ruleName } Log:</h3>#{ log }"
|
||||
$( '#log_col' ).html "<h3>#{ ruleName } Log ( #{ ts } ):</h3>#{ log }"
|
||||
.fail fErrHandler 'Could not get rule log! '
|
||||
|
||||
window.addEventListener 'load', fOnLoad, true
|
||||
|
|
|
|||
|
|
@ -85,9 +85,10 @@
|
|||
})
|
||||
};
|
||||
return $.post('/usercommand', data).done(function(data) {
|
||||
var log;
|
||||
var log, ts;
|
||||
ts = (new Date()).toISOString();
|
||||
log = data.message.replace(new RegExp("\n", 'g'), "<br>");
|
||||
return $('#log_col').html("<h3>" + ruleName + " Log:</h3>" + log);
|
||||
return $('#log_col').html("<h3>" + ruleName + " Log ( " + ts + " ):</h3>" + log);
|
||||
}).fail(fErrHandler('Could not get rule log! '));
|
||||
});
|
||||
};
|
||||
|
|
|
|||
|
|
@ -25,10 +25,10 @@
|
|||
};
|
||||
|
||||
fCreateLink( 'Forge Event Poller',
|
||||
fRedirect( 'forge?page=forge_event_poller' )
|
||||
fRedirect( 'forge?page=forge_module&type=event_poller' )
|
||||
);
|
||||
fCreateLink( 'Forge Action Invoker',
|
||||
fRedirect( 'forge?page=forge_action_invoker' )
|
||||
fRedirect( 'forge?page=forge_module&type=action_invoker' )
|
||||
);
|
||||
fCreateLink( 'Forge Rule',
|
||||
fRedirect( 'forge?page=forge_rule' )
|
||||
|
|
|
|||
Loading…
Reference in a new issue