mirror of
https://github.com/Hopiu/webapi-eca.git
synced 2026-03-16 22:10:31 +00:00
ISO time declarations added. Import.io example added
This commit is contained in:
parent
ca63157349
commit
bc999b6012
19 changed files with 106 additions and 75 deletions
|
|
@ -108,7 +108,7 @@ fLoadModule = ( msg ) ->
|
|||
|
||||
|
||||
log.info "EP | New event module '#{ arrName[0] }' loaded for user #{ msg.user },
|
||||
in rule #{ msg.rule.id }, starting at #{ ts.toISOString() }
|
||||
in rule #{ msg.rule.id }, starting at UTC|#{ ts.toISOString() }
|
||||
and polling every #{ msg.rule.event_interval } minutes"
|
||||
setTimeout fCheckAndRun( msg.user, msg.rule.id, ts ), 1000
|
||||
|
||||
|
|
@ -121,7 +121,7 @@ fPushEvent = ( userId, ruleId, oRule ) ->
|
|||
( obj ) ->
|
||||
db.pushEvent
|
||||
event: oRule.id
|
||||
eventid: "polled #{ oRule.id } #{ userId }_#{ ( new Date() ).toISOString() }"
|
||||
eventid: "polled #{ oRule.id } #{ userId }_UTC|#{ ( new Date() ).toISOString() }"
|
||||
payload: obj
|
||||
|
||||
fCheckAndRun = ( userId, ruleId, timestamp ) ->
|
||||
|
|
@ -137,7 +137,7 @@ fCheckAndRun = ( userId, ruleId, timestamp ) ->
|
|||
setTimeout fCheckAndRun( userId, ruleId, timestamp ), oRule.event_interval
|
||||
else
|
||||
log.info "EP | We found a newer polling interval and discontinue this one which
|
||||
was created at #{ timestamp.toISOString() }"
|
||||
was created at UTC|#{ timestamp.toISOString() }"
|
||||
|
||||
# We have to register the poll function in belows anonymous function
|
||||
# because we're fast iterating through the listUserModules and references will
|
||||
|
|
|
|||
|
|
@ -46,7 +46,7 @@ indexEvent = ( event, body, resp ) ->
|
|||
timestamp = ( new Date() ).toISOString()
|
||||
rand = ( Math.floor Math.random() * 10e9 ).toString( 16 ).toUpperCase()
|
||||
obj.event = event
|
||||
obj.eventid = "#{ obj.event }_#{ timestamp }_#{ rand }"
|
||||
obj.eventid = "#{ obj.event }_UTC|#{ timestamp }_#{ rand }"
|
||||
db.pushEvent obj
|
||||
resp.send 200, "Thank you for the event: #{ obj.eventid }"
|
||||
catch err
|
||||
|
|
|
|||
|
|
@ -386,7 +386,7 @@ Appends a log entry.
|
|||
###
|
||||
exports.appendLog = ( userId, ruleId, moduleId, message ) =>
|
||||
@db.append "#{ userId }:#{ ruleId }:log",
|
||||
"[#{ ( new Date() ).toISOString() }] {#{ moduleId }} #{ message }\n"
|
||||
"[UTC|#{ ( new Date() ).toISOString() }] {#{ moduleId }} #{ message }\n"
|
||||
|
||||
###
|
||||
Retrieves a log entry.
|
||||
|
|
|
|||
|
|
@ -79,7 +79,7 @@ exports.handleEvent = ( req, resp ) ->
|
|||
if obj and obj.event and not err
|
||||
timestamp = ( new Date() ).toISOString()
|
||||
rand = ( Math.floor Math.random() * 10e9 ).toString( 16 ).toUpperCase()
|
||||
obj.eventid = "#{ obj.event }_#{ timestamp }_#{ rand }"
|
||||
obj.eventid = "#{ obj.event }_UTC|#{ timestamp }_#{ rand }"
|
||||
answ =
|
||||
code: 200
|
||||
message: "Thank you for the event: #{ obj.eventid }"
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
#
|
||||
# Pushes an event into the system each time the function is polled
|
||||
#
|
||||
exports.push = ( pushEvent ) ->
|
||||
pushEvent
|
||||
exports.push = () ->
|
||||
exports.pushEvent
|
||||
content: "This is an event that will be sent again and again every ten seconds"
|
||||
|
|
@ -8,7 +8,7 @@
|
|||
|
||||
url = 'https://api.emailyak.com/v1/' + params.apikey + '/json/get/new/email/'
|
||||
|
||||
exports.newMail = ( pushEvent ) ->
|
||||
exports.newMail = () ->
|
||||
|
||||
# needlereq allows the user to make calls to API's
|
||||
# Refer to https://github.com/tomas/needle for more information
|
||||
|
|
@ -23,7 +23,7 @@ exports.newMail = ( pushEvent ) ->
|
|||
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
|
||||
exports.pushEvent mail for mail in body.Emails
|
||||
|
||||
###
|
||||
This will emit events of the form:
|
||||
|
|
|
|||
|
|
@ -4,46 +4,81 @@ required module params:
|
|||
|
||||
- apikey
|
||||
- userGuid
|
||||
- queryGuid
|
||||
###
|
||||
|
||||
params.apikey = "Cc8AX35d4B89ozzmn5bpm7k70HRon5rrfUxZvOwkVRj31/oBGHzVfQSRp5mEvlOgxyh7xi+tFSL66iAFo1W/sQ=="
|
||||
params.userGuid = "d19f0d08-bf73-4115-90a8-ac045ad4f225"
|
||||
params.queryGuid = "2a1d789a-4d24-4942-bdca-ffa0e9f99c85"
|
||||
params.queryGuid = "2a1d789a-4d24-4942-bdca-ffa0e9f99c85"
|
||||
# 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
|
||||
|
||||
tryToConnect = ( numAttempt, cb ) ->
|
||||
io.connect ( connected ) ->
|
||||
if not connected
|
||||
log "ERROR: Unable to connect"
|
||||
if connected
|
||||
cb true
|
||||
else
|
||||
log "Unable to connect, attempting again... ##{ numAttempt++ }"
|
||||
if numAttempt is 5
|
||||
cb false
|
||||
else
|
||||
tryToConnect numAttempt, cb
|
||||
|
||||
arrPages = [
|
||||
"http://www.meteoblue.com/en/switzerland/weather-basel"
|
||||
"http://www.meteoblue.com/en/switzerland/weather-z%C3%BCrich"
|
||||
"http://www.meteoblue.com/en/switzerland/weather-luzern"
|
||||
"http://www.meteoblue.com/en/switzerland/weather-liestal"
|
||||
"http://www.meteoblue.com/en/switzerland/weather-bern"
|
||||
"http://www.meteoblue.com/en/switzerland/weather-lugano"
|
||||
"http://www.meteoblue.com/en/switzerland/weather-sankt-gallen"
|
||||
]
|
||||
|
||||
getCityUrl = ( idCity ) ->
|
||||
id = parseInt( idCity ) || 0
|
||||
if id < 0 or id >= arrPages.length
|
||||
id = 0
|
||||
arrPages[ id ]
|
||||
|
||||
queryService = ( inputParams ) ->
|
||||
tryToConnect 0, ( connected ) ->
|
||||
if not connected
|
||||
log 'ERROR: Cannot execute query because connection failed!'
|
||||
else
|
||||
log "Connected!"
|
||||
data = []
|
||||
inp = { "webpage/url": "http://www.meteoblue.com/en/switzerland/weather-sankt-gallen" }
|
||||
io.query "input": inp, "connectorGuids": [ params.queryGuid ], ( finished, msg ) ->
|
||||
log 'query returned'
|
||||
log msg
|
||||
io.query inputParams, ( finished, 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
|
||||
io.query({
|
||||
"connectorGuids": [
|
||||
"2a1d789a-4d24-4942-bdca-ffa0e9f99c85"
|
||||
],
|
||||
"input": {
|
||||
"webpage/url": "http://www.meteoblue.com/en/switzerland/weather-sankt-gallen"
|
||||
}
|
||||
}, getCallbackFunction());
|
||||
exports.pushEvent data
|
||||
|
||||
exports.meteoblueWeekData = ( idCity ) ->
|
||||
params =
|
||||
input: "webpage/url": getCityUrl idCity
|
||||
connectorGuids: [ "2a1d789a-4d24-4942-bdca-ffa0e9f99c85" ]
|
||||
queryService params
|
||||
# [
|
||||
# {
|
||||
# wind: '9 mph',
|
||||
# day_identifier: 'Today',
|
||||
# day_name: 'Mon',
|
||||
# temp_max: '61 °F',
|
||||
# temp_min: '50 °F',
|
||||
# sunlight: '0 h',
|
||||
# rain: '0-2mm'
|
||||
# },
|
||||
# [...]
|
||||
# ]
|
||||
|
||||
exports.meteoblueCurrentData = ( idCity ) ->
|
||||
params =
|
||||
input: "webpage/url": getCityUrl idCity
|
||||
connectorGuids: [ "06394265-b4e1-4b48-be82-a9f2acb9040f" ]
|
||||
queryService params
|
||||
# [
|
||||
# {
|
||||
# current_time_wind_desc: '01:00 | Overcast',
|
||||
# current_temp: '53°F',
|
||||
# coordinates: '47.56°N 7.59°E 260m asl',
|
||||
# city: 'Basel-Stadt'
|
||||
# }
|
||||
# ]
|
||||
|
|
|
|||
|
|
@ -47,27 +47,27 @@ callService = ( args ) ->
|
|||
###
|
||||
Calls the user's unread content service.
|
||||
###
|
||||
exports.unreadContentInfo = ( pushEvent ) ->
|
||||
exports.unreadContentInfo = () ->
|
||||
callService
|
||||
service: '36'
|
||||
method: 'unreadcontent'
|
||||
callback: ( err, resp, body ) ->
|
||||
if not err and resp.statusCode is 200
|
||||
pushEvent oEntry for oEntry in body
|
||||
exports.pushEvent oEntry for oEntry in body
|
||||
else
|
||||
log 'Error: ' + body.error.message
|
||||
|
||||
###
|
||||
Fetches unread contents
|
||||
###
|
||||
exports.unreadContent = ( pushEvent ) ->
|
||||
exports.unreadContent = () ->
|
||||
exports.unreadContentInfo ( evt ) ->
|
||||
getContent
|
||||
contentId: evt.id
|
||||
contentServiceId: evt.serviceId
|
||||
callback: ( err, resp, body ) ->
|
||||
if not err and resp.statusCode is 200
|
||||
pushEvent
|
||||
exports.pushEvent
|
||||
id: body.id
|
||||
content: body.text
|
||||
object: body
|
||||
|
|
|
|||
|
|
@ -8,9 +8,9 @@ isRunning = false
|
|||
###
|
||||
This event is emitted if the system had a restart.
|
||||
###
|
||||
exports.hasRestarted = ( pushEvent ) ->
|
||||
exports.hasRestarted = () ->
|
||||
if not isRunning
|
||||
isRunning = true
|
||||
pushEvent
|
||||
exports.pushEvent
|
||||
content: "The system has been restarted at #{ ( new Date ).toISOString() }"
|
||||
|
||||
|
|
|
|||
|
|
@ -20,24 +20,24 @@ getWeatherData = ( cb ) ->
|
|||
###
|
||||
Pushes the current weather data into the system
|
||||
###
|
||||
exports.currentData = ( pushEvent ) ->
|
||||
exports.currentData = () ->
|
||||
getWeatherData ( err, resp, body ) ->
|
||||
if err or resp.statusCode isnt 200
|
||||
log JSON.stringify body
|
||||
else
|
||||
pushEvent body
|
||||
exports.pushEvent body
|
||||
|
||||
###
|
||||
Emits one event per day if the temperature today raises above user defined threshold
|
||||
###
|
||||
exports.temperatureOverThreshold = ( pushEvent ) ->
|
||||
exports.temperatureOverThreshold = () ->
|
||||
getWeatherData ( err, resp, body ) ->
|
||||
if err or resp.statusCode isnt 200
|
||||
log JSON.stringify body
|
||||
else
|
||||
#If temperature is above threshold
|
||||
if body.main.temp_max - 272.15 > params.tempThreshold
|
||||
pushEvent
|
||||
exports.pushEvent
|
||||
threshold: params.tempThreshold
|
||||
measured: body.main.temp_max - 272.15
|
||||
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ src = cs.compile code
|
|||
|
||||
sandbox =
|
||||
id: 'test.vm'
|
||||
params: params.userparams
|
||||
params: params
|
||||
needle: needle
|
||||
request: request
|
||||
cryptoJS: crypto
|
||||
|
|
@ -35,13 +35,12 @@ sandbox =
|
|||
debug: console.log
|
||||
exports: {}
|
||||
|
||||
sandbox.exports.pushEvent = ( obj ) ->
|
||||
console.log obj
|
||||
|
||||
vm.runInNewContext src, sandbox, sandbox.id
|
||||
|
||||
if process.argv[ 3 ] is 'ep'
|
||||
sandbox.exports[ process.argv[ 4 ] ] ( evt ) ->
|
||||
console.log evt
|
||||
else
|
||||
sandbox.exports[ process.argv[ 3 ] ] params.event
|
||||
sandbox.exports[ process.argv[ 3 ] ].apply null, [ "param1", "param2", "param3", "param4" ]
|
||||
|
||||
console.log "If no error happened until here it seems the script
|
||||
compiled and ran correctly! Congrats!"
|
||||
|
|
@ -38,7 +38,7 @@ compilation and running of module code
|
|||
|
||||
sandbox = {
|
||||
id: 'test.vm',
|
||||
params: params.userparams,
|
||||
params: params,
|
||||
needle: needle,
|
||||
request: request,
|
||||
cryptoJS: crypto,
|
||||
|
|
@ -48,15 +48,13 @@ compilation and running of module code
|
|||
exports: {}
|
||||
};
|
||||
|
||||
sandbox.exports.pushEvent = function(obj) {
|
||||
return console.log(obj);
|
||||
};
|
||||
|
||||
vm.runInNewContext(src, sandbox, sandbox.id);
|
||||
|
||||
if (process.argv[3] === 'ep') {
|
||||
sandbox.exports[process.argv[4]](function(evt) {
|
||||
return console.log(evt);
|
||||
});
|
||||
} else {
|
||||
sandbox.exports[process.argv[3]](params.event);
|
||||
}
|
||||
sandbox.exports[process.argv[3]].apply(null, ["param1", "param2", "param3", "param4"]);
|
||||
|
||||
console.log("If no error happened until here it seems the script compiled and ran correctly! Congrats!");
|
||||
|
||||
|
|
|
|||
|
|
@ -103,7 +103,7 @@ Dynamic Modules
|
|||
timestamp: ts
|
||||
};
|
||||
oUser[msg.rule.id].module.pushEvent = fPushEvent(msg.user, msg.rule.id, oUser[msg.rule.id]);
|
||||
log.info("EP | New event module '" + arrName[0] + "' loaded for user " + msg.user + ", in rule " + msg.rule.id + ", starting at " + (ts.toISOString()) + " and polling every " + msg.rule.event_interval + " minutes");
|
||||
log.info("EP | New event module '" + arrName[0] + "' loaded for user " + msg.user + ", in rule " + msg.rule.id + ", starting at UTC|" + (ts.toISOString()) + " and polling every " + msg.rule.event_interval + " minutes");
|
||||
return setTimeout(fCheckAndRun(msg.user, msg.rule.id, ts), 1000);
|
||||
});
|
||||
}
|
||||
|
|
@ -118,7 +118,7 @@ Dynamic Modules
|
|||
return function(obj) {
|
||||
return db.pushEvent({
|
||||
event: oRule.id,
|
||||
eventid: "polled " + oRule.id + " " + userId + "_" + ((new Date()).toISOString()),
|
||||
eventid: "polled " + oRule.id + " " + userId + "_UTC|" + ((new Date()).toISOString()),
|
||||
payload: obj
|
||||
});
|
||||
};
|
||||
|
|
@ -134,7 +134,7 @@ Dynamic Modules
|
|||
fCallFunction(userId, ruleId, oRule);
|
||||
return setTimeout(fCheckAndRun(userId, ruleId, timestamp), oRule.event_interval);
|
||||
} else {
|
||||
return log.info("EP | We found a newer polling interval and discontinue this one which was created at " + (timestamp.toISOString()));
|
||||
return log.info("EP | We found a newer polling interval and discontinue this one which was created at UTC|" + (timestamp.toISOString()));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -49,7 +49,7 @@ HTTP Listener
|
|||
timestamp = (new Date()).toISOString();
|
||||
rand = (Math.floor(Math.random() * 10e9)).toString(16).toUpperCase();
|
||||
obj.event = event;
|
||||
obj.eventid = "" + obj.event + "_" + timestamp + "_" + rand;
|
||||
obj.eventid = "" + obj.event + "_UTC|" + timestamp + "_" + rand;
|
||||
db.pushEvent(obj);
|
||||
return resp.send(200, "Thank you for the event: " + obj.eventid);
|
||||
} catch (_error) {
|
||||
|
|
|
|||
|
|
@ -507,7 +507,7 @@ Persistence
|
|||
|
||||
exports.appendLog = (function(_this) {
|
||||
return function(userId, ruleId, moduleId, message) {
|
||||
return _this.db.append("" + userId + ":" + ruleId + ":log", "[" + ((new Date()).toISOString()) + "] {" + moduleId + "} " + message + "\n");
|
||||
return _this.db.append("" + userId + ":" + ruleId + ":log", "[UTC|" + ((new Date()).toISOString()) + "] {" + moduleId + "} " + message + "\n");
|
||||
};
|
||||
})(this);
|
||||
|
||||
|
|
|
|||
|
|
@ -87,7 +87,7 @@ Request Handler
|
|||
if (obj && obj.event && !err) {
|
||||
timestamp = (new Date()).toISOString();
|
||||
rand = (Math.floor(Math.random() * 10e9)).toString(16).toUpperCase();
|
||||
obj.eventid = "" + obj.event + "_" + timestamp + "_" + rand;
|
||||
obj.eventid = "" + obj.event + "_UTC|" + timestamp + "_" + rand;
|
||||
answ = {
|
||||
code: 200,
|
||||
message: "Thank you for the event: " + obj.eventid
|
||||
|
|
|
|||
|
|
@ -70,7 +70,7 @@ fOnLoad = () ->
|
|||
.done ( data ) ->
|
||||
ts = ( new Date() ).toISOString()
|
||||
log = data.message.replace new RegExp("\n", 'g'), "<br>"
|
||||
$( '#log_col' ).html "<h3>#{ ruleName } Log ( #{ ts } ):</h3>#{ log }"
|
||||
$( '#log_col' ).html "<h3>#{ ruleName } Log:</h3> <i>( updated UTC|#{ ts } )</i><br/><br/>#{ log }"
|
||||
.fail fErrHandler 'Could not get rule log! '
|
||||
|
||||
window.addEventListener 'load', fOnLoad, true
|
||||
|
|
|
|||
|
|
@ -88,7 +88,7 @@
|
|||
var log, ts;
|
||||
ts = (new Date()).toISOString();
|
||||
log = data.message.replace(new RegExp("\n", 'g'), "<br>");
|
||||
return $('#log_col').html("<h3>" + ruleName + " Log ( " + ts + " ):</h3>" + log);
|
||||
return $('#log_col').html("<h3>" + ruleName + " Log:</h3> <i>( updated UTC|" + ts + " )</i><br/><br/>" + log);
|
||||
}).fail(fErrHandler('Could not get rule log! '));
|
||||
});
|
||||
};
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@
|
|||
|
||||
url = 'https://api.emailyak.com/v1/' + params.apikey + '/json/get/new/email/'
|
||||
|
||||
exports.newMail = ( pushEvent ) ->
|
||||
exports.newMail = () ->
|
||||
|
||||
# needlereq allows the user to make calls to API's
|
||||
# Refer to https://github.com/tomas/needle for more information
|
||||
|
|
@ -24,9 +24,8 @@ exports.newMail = ( pushEvent ) ->
|
|||
else
|
||||
if resp.statusCode is 200
|
||||
if body.Emails.length > 0
|
||||
log JSON.stringify body.Emails[0]
|
||||
log 'Poll function executed'
|
||||
pushEvent mail for mail in body.Emails
|
||||
log "#{ body.Emails.length } mail events pushed into the system"
|
||||
exports.pushEvent mail for mail in body.Emails
|
||||
|
||||
###
|
||||
This will emit events of the form:
|
||||
|
|
|
|||
Loading…
Reference in a new issue