mirror of
https://github.com/Hopiu/webapi-eca.git
synced 2026-03-16 22:10:31 +00:00
Good night until tomorrow
This commit is contained in:
parent
9fe1c37881
commit
0b582244e3
31 changed files with 586 additions and 457 deletions
|
|
@ -16,6 +16,8 @@ db = require './persistence'
|
|||
dynmod = require './dynamic-modules'
|
||||
# - [Encryption](encryption.html)
|
||||
encryption = require './encryption'
|
||||
# - [Request Handler](request-handler.html)
|
||||
rh = require './request-handler'
|
||||
|
||||
# - Node.js Modules: [fs](http://nodejs.org/api/fs.html),
|
||||
# [path](http://nodejs.org/api/path.html) and
|
||||
|
|
@ -63,12 +65,13 @@ exports.addRuleListener = ( eh ) =>
|
|||
oRule = JSON.parse strRule
|
||||
db.resetLog userName, oRule.id
|
||||
eventInfo = ''
|
||||
if oRule.event_start
|
||||
eventInfo = "Starting at #{ new Date( oRule.event_start ) }, Interval set to #{ oRule.event_interval } minutes"
|
||||
if oRule.eventstart
|
||||
eventInfo = "Starting at #{ new Date( oRule.eventstart ) },
|
||||
Interval set to #{ oRule.eventinterval } minutes"
|
||||
db.appendLog userName, oRule.id, "INIT", "Rule '#{ oRule.id }' initialized. #{ eventInfo }"
|
||||
|
||||
eventEmitter.emit 'rule',
|
||||
event: 'init'
|
||||
intevent: 'init'
|
||||
user: userName
|
||||
rule: oRule
|
||||
catch err
|
||||
|
|
@ -226,12 +229,12 @@ storeRule = ( user, oBody, callback ) =>
|
|||
# This is how a rule is stored in the database
|
||||
rule =
|
||||
id: oBody.id
|
||||
event: oBody.event
|
||||
event_start: oBody.event_start
|
||||
event_interval: oBody.event_interval
|
||||
eventname: oBody.eventname
|
||||
eventstart: oBody.eventstart
|
||||
eventinterval: oBody.eventinterval
|
||||
conditions: oBody.conditions
|
||||
actions: oBody.actions
|
||||
if oBody.event_start
|
||||
if oBody.eventstart
|
||||
rule.timestamp = (new Date()).toISOString()
|
||||
strRule = JSON.stringify rule
|
||||
# store the rule
|
||||
|
|
@ -241,35 +244,35 @@ storeRule = ( user, oBody, callback ) =>
|
|||
# activate the rule
|
||||
db.activateRule rule.id, user.username
|
||||
# if event module parameters were send, store them
|
||||
if oBody.event_params
|
||||
epModId = rule.event.split( ' -> ' )[ 0 ]
|
||||
db.eventPollers.storeUserParams epModId, user.username, JSON.stringify oBody.event_params
|
||||
oFuncArgs = oBody.event_functions
|
||||
if oBody.eventparams
|
||||
epModId = rule.eventname.split( ' -> ' )[ 0 ]
|
||||
db.eventPollers.storeUserParams epModId, user.username, JSON.stringify oBody.eventparams
|
||||
oFuncArgs = oBody.eventfunctions
|
||||
# if event function arguments were send, store them
|
||||
for id, args of oFuncArgs
|
||||
arr = id.split ' -> '
|
||||
db.eventPollers.storeUserArguments user.username, rule.id, arr[ 0 ], arr[ 1 ], JSON.stringify args
|
||||
|
||||
# if action module params were send, store them
|
||||
oParams = oBody.action_params
|
||||
oParams = oBody.actionparams
|
||||
for id, params of oParams
|
||||
db.actionInvokers.storeUserParams id, user.username, JSON.stringify params
|
||||
oFuncArgs = oBody.action_functions
|
||||
oFuncArgs = oBody.actionfunctions
|
||||
# if action function arguments were send, store them
|
||||
for id, args of oFuncArgs
|
||||
arr = id.split ' -> '
|
||||
db.actionInvokers.storeUserArguments user.username, rule.id, arr[ 0 ], arr[ 1 ], JSON.stringify args
|
||||
|
||||
eventInfo = ''
|
||||
if rule.event_start
|
||||
eventInfo = "Starting at #{ new Date( rule.event_start ) }, Interval set to #{ rule.event_interval } minutes"
|
||||
if rule.eventstart
|
||||
eventInfo = "Starting at #{ new Date( rule.eventstart ) }, Interval set to #{ rule.eventinterval } minutes"
|
||||
# Initialize the rule log
|
||||
db.resetLog user.username, rule.id
|
||||
db.appendLog user.username, rule.id, "INIT", "Rule '#{ rule.id }' initialized. #{ eventInfo }"
|
||||
|
||||
# Inform everbody about the new rule
|
||||
eventEmitter.emit 'rule',
|
||||
event: 'new'
|
||||
intevent: 'new'
|
||||
user: user.username
|
||||
rule: rule
|
||||
callback
|
||||
|
|
@ -414,7 +417,7 @@ commandFunctions =
|
|||
# - conditions
|
||||
# - actions
|
||||
forge_rule: ( user, oBody, callback ) ->
|
||||
answ = hasRequiredParams [ 'id', 'event', 'conditions', 'actions' ], oBody
|
||||
answ = hasRequiredParams [ 'id', 'eventname', 'conditions', 'actions' ], oBody
|
||||
if answ.code isnt 200
|
||||
callback answ
|
||||
else
|
||||
|
|
@ -436,7 +439,7 @@ commandFunctions =
|
|||
else
|
||||
db.deleteRule oBody.id
|
||||
eventEmitter.emit 'rule',
|
||||
event: 'del'
|
||||
intevent: 'del'
|
||||
user: user.username
|
||||
rule: null
|
||||
ruleId: oBody.id
|
||||
|
|
@ -451,7 +454,7 @@ commandFunctions =
|
|||
if answ.code isnt 200
|
||||
callback answ
|
||||
else
|
||||
db.getUserWebhooks user.username, ( err, hooks ) =>
|
||||
db.getUserWebhookIDs user.username, ( err, hooks ) =>
|
||||
if hooks.indexOf( oBody.hookname ) > -1
|
||||
answ.code = 409
|
||||
answ.message = 'Webhook already existing: ' + oBody.hookname
|
||||
|
|
@ -468,15 +471,14 @@ commandFunctions =
|
|||
hookid
|
||||
hookid = genHookID arrHooks
|
||||
db.createWebhook user.username, oBody.hookname, hookid
|
||||
rh.activateWebhook hookid, oBody.hookname
|
||||
callback
|
||||
code: 200
|
||||
message: JSON.stringify
|
||||
hookid: hookid
|
||||
|
||||
|
||||
|
||||
get_all_webhooks: ( user, oBody, callback ) ->
|
||||
db.getUserWebhooks user.username, ( err, data ) ->
|
||||
db.getAllUserWebhooks user.username, ( err, data ) ->
|
||||
if err
|
||||
callback
|
||||
code: 400
|
||||
|
|
@ -486,24 +488,14 @@ commandFunctions =
|
|||
code: 200
|
||||
message: JSON.stringify data
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
delete_webhook: ( user, oBody, callback ) ->
|
||||
answ = hasRequiredParams [ 'hookname' ], oBody
|
||||
answ = hasRequiredParams [ 'hookid' ], oBody
|
||||
if answ.code isnt 200
|
||||
callback answ
|
||||
else
|
||||
db.getUserWebhooks user.username, ( err, hooks ) =>
|
||||
if hooks.indexOf( oBody.hookname ) is -1
|
||||
answ.code = 409
|
||||
answ.message = 'Webhook does not exist: ' + oBody.hookname
|
||||
callback answ
|
||||
else
|
||||
db.deleteUserWebhook user.username, oBody.hookname
|
||||
callback
|
||||
code: 200
|
||||
message: 'OK!'
|
||||
rh.deactivateWebhook oBody.hookid
|
||||
db.deleteWebhook user.username, oBody.hookid
|
||||
callback
|
||||
code: 200
|
||||
message: 'OK!'
|
||||
|
||||
|
|
@ -36,7 +36,6 @@ Initializes the dynamic module handler.
|
|||
###
|
||||
exports = module.exports = ( args ) =>
|
||||
@log = args.logger
|
||||
db args
|
||||
module.exports
|
||||
|
||||
logFunction = ( uId, rId, mId ) ->
|
||||
|
|
@ -95,15 +94,12 @@ exports.compileString = ( src, userId, oRule, modId, lang, modType, dbMod, cb )
|
|||
fPushEvent = ( userId, oRule, modType ) ->
|
||||
( obj ) ->
|
||||
timestamp = ( new Date() ).toISOString()
|
||||
rand = ( Math.floor Math.random() * 10e9 ).toString( 16 ).toUpperCase()
|
||||
if modType is 'eventpoller'
|
||||
db.pushEvent
|
||||
event: oRule.event + '_created:' + oRule.timestamp
|
||||
eventid: "#{ userId }_#{ oRule.event }_UTC|#{ timestamp }_#{ rand }"
|
||||
eventname: oRule.eventname + '_created:' + oRule.timestamp
|
||||
body: obj
|
||||
|
||||
else
|
||||
obj.eventid = "#{ userId }_#{ oRule.event }_UTC|#{ timestamp }_#{ rand }"
|
||||
db.pushEvent obj
|
||||
|
||||
fTryToLoadModule = ( userId, oRule, modId, src, modType, dbMod, params, cb ) =>
|
||||
|
|
|
|||
|
|
@ -57,7 +57,6 @@ Initializes the Engine and starts polling the event queue for new events.
|
|||
exports = module.exports = ( args ) =>
|
||||
if not isRunning
|
||||
@log = args.logger
|
||||
db args
|
||||
dynmod args
|
||||
setTimeout exports.startEngine, 10 # Very important, this forks a token for the poll task
|
||||
module.exports
|
||||
|
|
@ -88,18 +87,18 @@ are basically CRUD on rules.
|
|||
@param {Object} evt
|
||||
###
|
||||
exports.internalEvent = ( evt ) =>
|
||||
if not listUserRules[evt.user] and evt.event isnt 'del'
|
||||
if not listUserRules[evt.user] and evt.intevent isnt 'del'
|
||||
listUserRules[evt.user] = {}
|
||||
|
||||
|
||||
oUser = listUserRules[evt.user]
|
||||
oRule = evt.rule
|
||||
if evt.event is 'new' or ( evt.event is 'init' and not oUser[oRule.id] )
|
||||
if evt.intevent is 'new' or ( evt.intevent is 'init' and not oUser[oRule.id] )
|
||||
oUser[oRule.id] =
|
||||
rule: oRule
|
||||
actions: {}
|
||||
updateActionModules oRule.id
|
||||
|
||||
if evt.event is 'del' and oUser
|
||||
if evt.intevent is 'del' and oUser
|
||||
delete oUser[evt.ruleId]
|
||||
|
||||
# If a user is empty after all the updates above, we remove her from the list
|
||||
|
|
@ -273,14 +272,18 @@ processEvent = ( evt ) =>
|
|||
else
|
||||
fSearchAndInvokeAction node[arrPath[depth]], arrPath, funcName, evt, depth + 1
|
||||
|
||||
@log.info 'EN | processing event: ' + evt.event + '(' + evt.eventid + ')'
|
||||
@log.info 'EN | processing event: ' + evt.eventname
|
||||
for userName, oUser of listUserRules
|
||||
|
||||
for ruleName, oMyRule of oUser
|
||||
ruleEvent = oMyRule.rule.event
|
||||
|
||||
ruleEvent = oMyRule.rule.eventname
|
||||
if oMyRule.rule.timestamp
|
||||
ruleEvent += '_created:' + oMyRule.rule.timestamp
|
||||
if evt.event is ruleEvent and validConditions evt, oMyRule.rule, userName, ruleName
|
||||
@log.info 'EN | EVENT FIRED: ' + evt.event + '(' + evt.eventid + ') for rule ' + ruleName
|
||||
if evt.eventname is ruleEvent and validConditions evt, oMyRule.rule, userName, ruleName
|
||||
|
||||
@log.info 'EN | EVENT FIRED: ' + evt.eventname + ' for rule ' + ruleName
|
||||
|
||||
for action in oMyRule.rule.actions
|
||||
arr = action.split ' -> '
|
||||
fSearchAndInvokeAction listUserRules, [ userName, ruleName, 'actions', arr[0]], arr[1], evt, 0
|
||||
|
|
|
|||
|
|
@ -65,19 +65,19 @@ process.on 'message', ( msg ) ->
|
|||
# Let's split the event string to find module and function in an array
|
||||
|
||||
# A initialization notification or a new rule
|
||||
if msg.event is 'new' or msg.event is 'init'
|
||||
if msg.intevent is 'new' or msg.intevent is 'init'
|
||||
fLoadModule msg
|
||||
# We fetch the module also if the rule was updated
|
||||
|
||||
# A rule was deleted
|
||||
if msg.event is 'del'
|
||||
if msg.intevent is 'del'
|
||||
delete listUserModules[msg.user][msg.ruleId]
|
||||
if JSON.stringify( listUserModules[msg.user] ) is "{}"
|
||||
delete listUserModules[msg.user]
|
||||
|
||||
# Loads a module if required
|
||||
fLoadModule = ( msg ) ->
|
||||
arrName = msg.rule.event.split ' -> '
|
||||
arrName = msg.rule.eventname.split ' -> '
|
||||
fAnonymous = () ->
|
||||
db.eventPollers.getModule msg.user, arrName[ 0 ], ( err, obj ) ->
|
||||
if not obj
|
||||
|
|
@ -103,15 +103,15 @@ fLoadModule = ( msg ) ->
|
|||
oUser = listUserModules[msg.user]
|
||||
# We open up a new object for the rule it
|
||||
oUser[msg.rule.id] =
|
||||
id: msg.rule.event
|
||||
id: msg.rule.eventname
|
||||
timestamp: msg.rule.timestamp
|
||||
pollfunc: arrName[1]
|
||||
funcArgs: result.funcArgs
|
||||
event_interval: msg.rule.event_interval * 60 * 1000
|
||||
eventinterval: msg.rule.eventinterval * 60 * 1000
|
||||
module: result.module
|
||||
logger: result.logger
|
||||
|
||||
start = new Date msg.rule.event_start
|
||||
start = new Date msg.rule.eventstart
|
||||
nd = new Date()
|
||||
now = new Date()
|
||||
if start < nd
|
||||
|
|
@ -129,10 +129,10 @@ fLoadModule = ( msg ) ->
|
|||
log.info "EP | New event module '#{ arrName[0] }' loaded for user #{ msg.user },
|
||||
in rule #{ msg.rule.id }, registered at UTC|#{ msg.rule.timestamp },
|
||||
starting at UTC|#{ start.toISOString() } ( which is in #{ ( nd - now ) / 1000 / 60 } minutes )
|
||||
and polling every #{ msg.rule.event_interval } minutes"
|
||||
and polling every #{ msg.rule.eventinterval } minutes"
|
||||
setTimeout fCheckAndRun( msg.user, msg.rule.id, msg.rule.timestamp ), nd - now
|
||||
|
||||
if msg.event is 'new' or
|
||||
if msg.intevent is 'new' or
|
||||
not listUserModules[msg.user] or
|
||||
not listUserModules[msg.user][msg.rule.id]
|
||||
fAnonymous()
|
||||
|
|
@ -147,7 +147,7 @@ fCheckAndRun = ( userId, ruleId, timestamp ) ->
|
|||
if listUserModules[userId][ruleId].timestamp is timestamp
|
||||
oRule = listUserModules[userId][ruleId]
|
||||
fCallFunction userId, ruleId, oRule
|
||||
setTimeout fCheckAndRun( userId, ruleId, timestamp ), oRule.event_interval
|
||||
setTimeout fCheckAndRun( userId, ruleId, timestamp ), oRule.eventinterval
|
||||
else
|
||||
log.info "EP | We found a newer polling interval and discontinue this one which
|
||||
was created at UTC|#{ timestamp }"
|
||||
|
|
|
|||
|
|
@ -71,8 +71,6 @@ initRouting = ( port ) =>
|
|||
|
||||
# POST Requests
|
||||
|
||||
# - **`POST` to _"/event"_:** Events coming from remote systems are passed to the engine
|
||||
app.post '/event', requestHandler.handleEvent
|
||||
# - **`POST` to _"/login"_:** Credentials will be verified
|
||||
app.post '/login', requestHandler.handleLogin
|
||||
# - **`POST` to _"/logout"_:** User will be logged out
|
||||
|
|
@ -81,8 +79,10 @@ initRouting = ( port ) =>
|
|||
app.post '/usercommand', requestHandler.handleUserCommand
|
||||
# - **`POST` to _"/admincommand"_:** Admin requests are only possible for admins
|
||||
app.post '/admincommand', requestHandler.handleAdminCommand
|
||||
# - **`POST` to _"/event/*"_:** event posting, mainly a webhook for the webpage
|
||||
app.post '/event', requestHandler.handleEvent
|
||||
# - **`POST` to _"/webhooks/*"_:** Webhooks retrieve remote events
|
||||
app.post '/webhooks', requestHandler.handleWebhooks
|
||||
app.post '/webhooks/*', requestHandler.handleWebhooks
|
||||
# - **`POST` to _"/measurements/*"_:** We also want to record measurements
|
||||
app.post '/measurements', requestHandler.handleMeasurements
|
||||
|
||||
|
|
|
|||
|
|
@ -118,7 +118,7 @@ Push an event into the event queue.
|
|||
###
|
||||
exports.pushEvent = ( oEvent ) =>
|
||||
if oEvent
|
||||
@log.info "DB | Event pushed into the queue: '#{ oEvent.eventid }'"
|
||||
@log.info "DB | Event pushed into the queue: '#{ oEvent.eventname }'"
|
||||
@db.rpush 'event_queue', JSON.stringify oEvent
|
||||
else
|
||||
@log.warn 'DB | Why would you give me an empty event...'
|
||||
|
|
@ -802,38 +802,67 @@ Creates and stores a webhook.
|
|||
@param {String} hookname
|
||||
###
|
||||
exports.createWebhook = ( userId, hookname, hookid ) =>
|
||||
@db.sadd "user:#{ userId }:webhooks", hookname,
|
||||
replyHandler "sadd 'user:#{ userId }:webhooks' -> '#{ hookname }'"
|
||||
@db.sadd "webhooks", hookid, replyHandler "sadd 'webhooks' -> '#{ hookid }'"
|
||||
@db.sadd "user:#{ userId }:webhooks", hookid,
|
||||
replyHandler "sadd 'user:#{ userId }:webhooks' -> '#{ hookid }'"
|
||||
@db.set "webhook:#{ hookid }", hookname,
|
||||
replyHandler "set webhook:#{ hookid } -> #{ hookname }"
|
||||
|
||||
###
|
||||
Gets all the users webhooks.
|
||||
Returns a webhook name.
|
||||
|
||||
@public getWebhooks( *userId* )
|
||||
@public getWebhookName( *hookid* )
|
||||
@param {String} hookid
|
||||
###
|
||||
exports.getWebhookName = ( hookid, cb ) =>
|
||||
@db.get "webhook:#{ hookid }", cb
|
||||
|
||||
###
|
||||
Returns all the user's webhooks by ID.
|
||||
|
||||
@public getUserWebhookIDs( *userId* )
|
||||
@param {String} userId
|
||||
###
|
||||
exports.getUserWebhooks = ( userId, cb ) =>
|
||||
exports.getUserWebhookIDs = ( userId, cb ) =>
|
||||
@db.smembers "user:#{ userId }:webhooks", cb
|
||||
|
||||
###
|
||||
Gets all the users webhooks.
|
||||
Gets all the user's webhooks with names.
|
||||
|
||||
@public getWebhooks( *userId* )
|
||||
@public getAllUserWebhooks( *userId* )
|
||||
@param {String} userId
|
||||
###
|
||||
exports.getAllUserWebhooks = ( userId, cb ) =>
|
||||
getSetRecords "user:#{ userId }:webhooks", exports.getWebhookName, cb
|
||||
|
||||
###
|
||||
Returns all webhook IDs.
|
||||
|
||||
@public getAllWebhookIDs()
|
||||
###
|
||||
exports.getAllWebhookIDs = ( cb ) =>
|
||||
@db.smembers "webhooks", cb
|
||||
|
||||
###
|
||||
Deletes a webhook.
|
||||
Returns all webhooks with names.
|
||||
|
||||
@public deleteWebhook( *userId, hookname* )
|
||||
@param {String} userId
|
||||
@param {String} hookname
|
||||
@public getAllWebhooks()
|
||||
###
|
||||
exports.deleteUserWebhook = ( userId, hookname ) =>
|
||||
@db.srem "user:#{ userId }:webhooks", hookname,
|
||||
replyHandler "srem 'user:#{ userId }:webhooks' -> '#{ hookname }'"
|
||||
exports.getAllWebhooks = ( cb ) =>
|
||||
getSetRecords "webhooks", exports.getWebhookName, cb
|
||||
|
||||
###
|
||||
Delete a webhook.
|
||||
|
||||
@public deleteWebhook( *userId, hookid* )
|
||||
@param {String} userId
|
||||
@param {String} hookid
|
||||
###
|
||||
exports.deleteWebhook = ( userId, hookid ) =>
|
||||
@db.srem "webhooks", hookid, replyHandler "srem 'webhooks' -> '#{ hookid }'"
|
||||
@db.srem "user:#{ userId }:webhooks", hookid,
|
||||
replyHandler "srem 'user:#{ userId }:webhooks' -> '#{ hookid }'"
|
||||
@db.del "webhook:#{ hookid }", replyHandler "del webhook:#{ hookid }"
|
||||
|
||||
###
|
||||
Shuts down the db link.
|
||||
|
|
|
|||
|
|
@ -80,8 +80,6 @@ exports = module.exports = ( args ) =>
|
|||
data.code = 401
|
||||
data.message = 'Missing parameter for this command'
|
||||
cb null, data
|
||||
|
||||
db args
|
||||
|
||||
# Load the standard users from the user config file
|
||||
users = JSON.parse fs.readFileSync pathUsers, 'utf8'
|
||||
|
|
@ -89,18 +87,20 @@ exports = module.exports = ( args ) =>
|
|||
oUser.username = username
|
||||
db.storeUser oUser
|
||||
fStoreUser user, oUser for user, oUser of users
|
||||
|
||||
|
||||
@allowedHooks = {}
|
||||
db.getAllWebhooks ( err, oHooks ) =>
|
||||
if oHooks
|
||||
console
|
||||
@log.info "RH | Initializing #{ Object.keys( oHooks ).length } Webhooks"
|
||||
@allowedHooks = oHooks
|
||||
module.exports
|
||||
|
||||
|
||||
###
|
||||
Handles possible events that were posted to this server and pushes them into the
|
||||
event queue.
|
||||
|
||||
*Requires
|
||||
the [request](http://nodejs.org/api/http.html#http_class_http_clientrequest)
|
||||
and [response](http://nodejs.org/api/http.html#http_class_http_serverresponse)
|
||||
objects.*
|
||||
|
||||
@public handleEvent( *req, resp* )
|
||||
###
|
||||
exports.handleEvent = ( req, resp ) ->
|
||||
|
|
@ -109,30 +109,19 @@ exports.handleEvent = ( req, resp ) ->
|
|||
body += data
|
||||
|
||||
req.on 'end', ->
|
||||
#if req.session and req.session.user
|
||||
console.log typeof body
|
||||
console.log body
|
||||
try
|
||||
obj = JSON.parse body
|
||||
catch err
|
||||
resp.send 400, 'Badly formed event!'
|
||||
# If required event properties are present we process the event #
|
||||
console.log obj
|
||||
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 }_UTC|#{ timestamp }_#{ rand }"
|
||||
if obj and obj.eventname and not err
|
||||
answ =
|
||||
code: 200
|
||||
message: "Thank you for the event: #{ obj.eventid }"
|
||||
message: "Thank you for the event: #{ obj.eventname }"
|
||||
resp.send answ.code, answ
|
||||
db.pushEvent obj
|
||||
else
|
||||
resp.send 400, 'Your event was missing important parameters!'
|
||||
# else
|
||||
# resp.send 401, 'Please login!'
|
||||
|
||||
|
||||
|
||||
###
|
||||
Associates the user object with the session if login is successful.
|
||||
|
|
@ -365,31 +354,23 @@ exports.handleAdminCommand = ( req, resp ) =>
|
|||
resp.send 401, 'You need to be logged in as admin!'
|
||||
|
||||
|
||||
indexEvent = ( event, body, resp ) ->
|
||||
if typeof body is 'string'
|
||||
indexEvent = ( eventname, body, resp ) ->
|
||||
if typeof body is 'string'
|
||||
try
|
||||
body = JSON.parse body
|
||||
catch err
|
||||
try
|
||||
obj = qs.parse body
|
||||
body = qs.parse body
|
||||
catch err
|
||||
try
|
||||
obj = JSON.parse body
|
||||
catch err
|
||||
resp.send 400, 'Badly formed event!'
|
||||
return
|
||||
else
|
||||
obj = body
|
||||
timestamp = ( new Date() ).toISOString()
|
||||
rand = ( Math.floor Math.random() * 10e9 ).toString( 16 ).toUpperCase()
|
||||
obj.event = event
|
||||
obj.eventid = "#{ obj.event }_UTC|#{ timestamp }_#{ rand }"
|
||||
db.pushEvent obj
|
||||
resp.send 200, "Thank you for the event: #{ obj.eventid }"
|
||||
obj
|
||||
resp.send 400, 'Badly formed event!'
|
||||
return
|
||||
obj =
|
||||
eventname: eventname
|
||||
body: body
|
||||
db.pushEvent obj
|
||||
msg = "Thank you for the event: '#{ eventname }'"
|
||||
obj
|
||||
|
||||
###
|
||||
Handles webhook posts
|
||||
###
|
||||
exports.handleWebhooks = ( req, resp ) =>
|
||||
console.log 'RH | IMPLEMENT WEBHOOKS'
|
||||
|
||||
###
|
||||
Handles measurement posts
|
||||
|
|
@ -406,23 +387,29 @@ exports.handleMeasurements = ( req, resp ) =>
|
|||
fPath = path.resolve __dirname, '..', 'webpages', 'public', 'data', 'histochart.json'
|
||||
fs.writeFile fPath, JSON.stringify( JSON.parse( body ), undefined, 2 ), 'utf8'
|
||||
|
||||
# Activate a webhook. the body will be JSON parsed, the name of the webhook will
|
||||
# be the event name given to the event object, a timestamp will be added
|
||||
activateWebHook = ( app, name ) =>
|
||||
@log.info "HL | Webhook activated for #{ name }"
|
||||
app.post "/webhooks/#{ name }", ( req, resp ) ->
|
||||
###
|
||||
Handles webhook posts
|
||||
###
|
||||
exports.handleWebhooks = ( req, resp ) =>
|
||||
hookid = req.url.substring( 10 ).split( '/' )[ 0 ]
|
||||
hookname = @allowedHooks[ hookid ]
|
||||
if hookname
|
||||
body = ''
|
||||
req.on 'data', ( data ) ->
|
||||
body += data
|
||||
req.on 'end', () ->
|
||||
obj = indexEvent hookname, body, resp
|
||||
else
|
||||
resp.send 404, "Webhook not existing!"
|
||||
|
||||
req.on 'end', ->
|
||||
indexEvent name, body, resp
|
||||
|
||||
# Remove a webhook
|
||||
removeWebHook = ( app, hookid ) =>
|
||||
@log.info "HL | Removing Webhook for #{ hookid }"
|
||||
isFound = false
|
||||
for oRoute, i in app.routes.post
|
||||
if oRoute.path is "/webhooks/#{ name }"
|
||||
app.routes.post.splice i, 1
|
||||
isFound = true
|
||||
# Activate a webhook. the body will be JSON parsed, the name of the webhook will
|
||||
# be the event name given to the event object, a timestamp will be added
|
||||
exports.activateWebhook = ( hookid, name ) =>
|
||||
@log.info "HL | Webhook '#{ hookid }' activated"
|
||||
@allowedHooks[ hookid ] = name
|
||||
|
||||
# Deactivate a webhook
|
||||
exports.deactivateWebhook = ( hookid ) =>
|
||||
@log.info "HL | Webhook '#{ hookid }' deactivated"
|
||||
delete @allowedHooks[ hookid ]
|
||||
|
|
|
|||
|
|
@ -9,9 +9,5 @@
|
|||
"file-level": "info",
|
||||
"file-path": "logs/server.log"
|
||||
},
|
||||
"webhooks": [
|
||||
"github",
|
||||
"uptimestatistics"
|
||||
],
|
||||
"keygen-passphrase": "[TODO this has to come from prompt when server is started!]"
|
||||
}
|
||||
|
|
@ -10,7 +10,7 @@ Components Manager
|
|||
*/
|
||||
|
||||
(function() {
|
||||
var commandFunctions, db, dynmod, encryption, eventEmitter, events, exports, forgeModule, fs, getModuleParams, getModuleUserArguments, getModuleUserParams, getModules, hasRequiredParams, path, storeModule, storeRule;
|
||||
var commandFunctions, db, dynmod, encryption, eventEmitter, events, exports, forgeModule, fs, getModuleParams, getModuleUserArguments, getModuleUserParams, getModules, hasRequiredParams, path, rh, storeModule, storeRule;
|
||||
|
||||
db = require('./persistence');
|
||||
|
||||
|
|
@ -18,6 +18,8 @@ Components Manager
|
|||
|
||||
encryption = require('./encryption');
|
||||
|
||||
rh = require('./request-handler');
|
||||
|
||||
fs = require('fs');
|
||||
|
||||
path = require('path');
|
||||
|
|
@ -67,12 +69,12 @@ Components Manager
|
|||
oRule = JSON.parse(strRule);
|
||||
db.resetLog(userName, oRule.id);
|
||||
eventInfo = '';
|
||||
if (oRule.event_start) {
|
||||
eventInfo = "Starting at " + (new Date(oRule.event_start)) + ", Interval set to " + oRule.event_interval + " minutes";
|
||||
if (oRule.eventstart) {
|
||||
eventInfo = "Starting at " + (new Date(oRule.eventstart)) + ", Interval set to " + oRule.eventinterval + " minutes";
|
||||
}
|
||||
db.appendLog(userName, oRule.id, "INIT", "Rule '" + oRule.id + "' initialized. " + eventInfo);
|
||||
return eventEmitter.emit('rule', {
|
||||
event: 'init',
|
||||
intevent: 'init',
|
||||
user: userName,
|
||||
rule: oRule
|
||||
});
|
||||
|
|
@ -319,48 +321,48 @@ Components Manager
|
|||
var args, arr, epModId, eventInfo, id, oFuncArgs, oParams, params, rule, strRule;
|
||||
rule = {
|
||||
id: oBody.id,
|
||||
event: oBody.event,
|
||||
event_start: oBody.event_start,
|
||||
event_interval: oBody.event_interval,
|
||||
eventname: oBody.eventname,
|
||||
eventstart: oBody.eventstart,
|
||||
eventinterval: oBody.eventinterval,
|
||||
conditions: oBody.conditions,
|
||||
actions: oBody.actions
|
||||
};
|
||||
if (oBody.event_start) {
|
||||
if (oBody.eventstart) {
|
||||
rule.timestamp = (new Date()).toISOString();
|
||||
}
|
||||
strRule = JSON.stringify(rule);
|
||||
db.storeRule(rule.id, strRule);
|
||||
db.linkRule(rule.id, user.username);
|
||||
db.activateRule(rule.id, user.username);
|
||||
if (oBody.event_params) {
|
||||
epModId = rule.event.split(' -> ')[0];
|
||||
db.eventPollers.storeUserParams(epModId, user.username, JSON.stringify(oBody.event_params));
|
||||
if (oBody.eventparams) {
|
||||
epModId = rule.eventname.split(' -> ')[0];
|
||||
db.eventPollers.storeUserParams(epModId, user.username, JSON.stringify(oBody.eventparams));
|
||||
}
|
||||
oFuncArgs = oBody.event_functions;
|
||||
oFuncArgs = oBody.eventfunctions;
|
||||
for (id in oFuncArgs) {
|
||||
args = oFuncArgs[id];
|
||||
arr = id.split(' -> ');
|
||||
db.eventPollers.storeUserArguments(user.username, rule.id, arr[0], arr[1], JSON.stringify(args));
|
||||
}
|
||||
oParams = oBody.action_params;
|
||||
oParams = oBody.actionparams;
|
||||
for (id in oParams) {
|
||||
params = oParams[id];
|
||||
db.actionInvokers.storeUserParams(id, user.username, JSON.stringify(params));
|
||||
}
|
||||
oFuncArgs = oBody.action_functions;
|
||||
oFuncArgs = oBody.actionfunctions;
|
||||
for (id in oFuncArgs) {
|
||||
args = oFuncArgs[id];
|
||||
arr = id.split(' -> ');
|
||||
db.actionInvokers.storeUserArguments(user.username, rule.id, arr[0], arr[1], JSON.stringify(args));
|
||||
}
|
||||
eventInfo = '';
|
||||
if (rule.event_start) {
|
||||
eventInfo = "Starting at " + (new Date(rule.event_start)) + ", Interval set to " + rule.event_interval + " minutes";
|
||||
if (rule.eventstart) {
|
||||
eventInfo = "Starting at " + (new Date(rule.eventstart)) + ", Interval set to " + rule.eventinterval + " minutes";
|
||||
}
|
||||
db.resetLog(user.username, rule.id);
|
||||
db.appendLog(user.username, rule.id, "INIT", "Rule '" + rule.id + "' initialized. " + eventInfo);
|
||||
eventEmitter.emit('rule', {
|
||||
event: 'new',
|
||||
intevent: 'new',
|
||||
user: user.username,
|
||||
rule: rule
|
||||
});
|
||||
|
|
@ -522,7 +524,7 @@ Components Manager
|
|||
},
|
||||
forge_rule: function(user, oBody, callback) {
|
||||
var answ;
|
||||
answ = hasRequiredParams(['id', 'event', 'conditions', 'actions'], oBody);
|
||||
answ = hasRequiredParams(['id', 'eventname', 'conditions', 'actions'], oBody);
|
||||
if (answ.code !== 200) {
|
||||
return callback(answ);
|
||||
} else {
|
||||
|
|
@ -551,7 +553,7 @@ Components Manager
|
|||
} else {
|
||||
db.deleteRule(oBody.id);
|
||||
eventEmitter.emit('rule', {
|
||||
event: 'del',
|
||||
intevent: 'del',
|
||||
user: user.username,
|
||||
rule: null,
|
||||
ruleId: oBody.id
|
||||
|
|
@ -568,7 +570,7 @@ Components Manager
|
|||
if (answ.code !== 200) {
|
||||
return callback(answ);
|
||||
} else {
|
||||
return db.getUserWebhooks(user.username, (function(_this) {
|
||||
return db.getUserWebhookIDs(user.username, (function(_this) {
|
||||
return function(err, hooks) {
|
||||
if (hooks.indexOf(oBody.hookname) > -1) {
|
||||
answ.code = 409;
|
||||
|
|
@ -591,6 +593,7 @@ Components Manager
|
|||
};
|
||||
hookid = genHookID(arrHooks);
|
||||
db.createWebhook(user.username, oBody.hookname, hookid);
|
||||
rh.activateWebhook(hookid, oBody.hookname);
|
||||
return callback({
|
||||
code: 200,
|
||||
message: JSON.stringify({
|
||||
|
|
@ -604,7 +607,7 @@ Components Manager
|
|||
}
|
||||
},
|
||||
get_all_webhooks: function(user, oBody, callback) {
|
||||
return db.getUserWebhooks(user.username, function(err, data) {
|
||||
return db.getAllUserWebhooks(user.username, function(err, data) {
|
||||
if (err) {
|
||||
return callback({
|
||||
code: 400,
|
||||
|
|
@ -620,25 +623,16 @@ Components Manager
|
|||
},
|
||||
delete_webhook: function(user, oBody, callback) {
|
||||
var answ;
|
||||
answ = hasRequiredParams(['hookname'], oBody);
|
||||
answ = hasRequiredParams(['hookid'], oBody);
|
||||
if (answ.code !== 200) {
|
||||
return callback(answ);
|
||||
} else {
|
||||
return db.getUserWebhooks(user.username, (function(_this) {
|
||||
return function(err, hooks) {
|
||||
if (hooks.indexOf(oBody.hookname) === -1) {
|
||||
answ.code = 409;
|
||||
answ.message = 'Webhook does not exist: ' + oBody.hookname;
|
||||
return callback(answ);
|
||||
} else {
|
||||
db.deleteUserWebhook(user.username, oBody.hookname);
|
||||
return callback({
|
||||
code: 200,
|
||||
message: 'OK!'
|
||||
});
|
||||
}
|
||||
};
|
||||
})(this));
|
||||
rh.deactivateWebhook(oBody.hookid);
|
||||
db.deleteWebhook(user.username, oBody.hookid);
|
||||
return callback({
|
||||
code: 200,
|
||||
message: 'OK!'
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -39,7 +39,6 @@ Dynamic Modules
|
|||
exports = module.exports = (function(_this) {
|
||||
return function(args) {
|
||||
_this.log = args.logger;
|
||||
db(args);
|
||||
return module.exports;
|
||||
};
|
||||
})(this);
|
||||
|
|
@ -120,17 +119,14 @@ Dynamic Modules
|
|||
|
||||
fPushEvent = function(userId, oRule, modType) {
|
||||
return function(obj) {
|
||||
var rand, timestamp;
|
||||
var timestamp;
|
||||
timestamp = (new Date()).toISOString();
|
||||
rand = (Math.floor(Math.random() * 10e9)).toString(16).toUpperCase();
|
||||
if (modType === 'eventpoller') {
|
||||
return db.pushEvent({
|
||||
event: oRule.event + '_created:' + oRule.timestamp,
|
||||
eventid: "" + userId + "_" + oRule.event + "_UTC|" + timestamp + "_" + rand,
|
||||
eventname: oRule.eventname + '_created:' + oRule.timestamp,
|
||||
body: obj
|
||||
});
|
||||
} else {
|
||||
obj.eventid = "" + userId + "_" + oRule.event + "_UTC|" + timestamp + "_" + rand;
|
||||
return db.pushEvent(obj);
|
||||
}
|
||||
};
|
||||
|
|
|
|||
15
js/engine.js
15
js/engine.js
|
|
@ -57,7 +57,6 @@ Engine
|
|||
return function(args) {
|
||||
if (!isRunning) {
|
||||
_this.log = args.logger;
|
||||
db(args);
|
||||
dynmod(args);
|
||||
setTimeout(exports.startEngine, 10);
|
||||
return module.exports;
|
||||
|
|
@ -96,19 +95,19 @@ Engine
|
|||
exports.internalEvent = (function(_this) {
|
||||
return function(evt) {
|
||||
var oRule, oUser;
|
||||
if (!listUserRules[evt.user] && evt.event !== 'del') {
|
||||
if (!listUserRules[evt.user] && evt.intevent !== 'del') {
|
||||
listUserRules[evt.user] = {};
|
||||
}
|
||||
oUser = listUserRules[evt.user];
|
||||
oRule = evt.rule;
|
||||
if (evt.event === 'new' || (evt.event === 'init' && !oUser[oRule.id])) {
|
||||
if (evt.intevent === 'new' || (evt.intevent === 'init' && !oUser[oRule.id])) {
|
||||
oUser[oRule.id] = {
|
||||
rule: oRule,
|
||||
actions: {}
|
||||
};
|
||||
updateActionModules(oRule.id);
|
||||
}
|
||||
if (evt.event === 'del' && oUser) {
|
||||
if (evt.intevent === 'del' && oUser) {
|
||||
delete oUser[evt.ruleId];
|
||||
}
|
||||
if (JSON.stringify(oUser) === "{}") {
|
||||
|
|
@ -347,7 +346,7 @@ Engine
|
|||
return fSearchAndInvokeAction(node[arrPath[depth]], arrPath, funcName, evt, depth + 1);
|
||||
}
|
||||
};
|
||||
_this.log.info('EN | processing event: ' + evt.event + '(' + evt.eventid + ')');
|
||||
_this.log.info('EN | processing event: ' + evt.eventname);
|
||||
_results = [];
|
||||
for (userName in listUserRules) {
|
||||
oUser = listUserRules[userName];
|
||||
|
|
@ -356,12 +355,12 @@ Engine
|
|||
_results1 = [];
|
||||
for (ruleName in oUser) {
|
||||
oMyRule = oUser[ruleName];
|
||||
ruleEvent = oMyRule.rule.event;
|
||||
ruleEvent = oMyRule.rule.eventname;
|
||||
if (oMyRule.rule.timestamp) {
|
||||
ruleEvent += '_created:' + oMyRule.rule.timestamp;
|
||||
}
|
||||
if (evt.event === ruleEvent && validConditions(evt, oMyRule.rule, userName, ruleName)) {
|
||||
this.log.info('EN | EVENT FIRED: ' + evt.event + '(' + evt.eventid + ') for rule ' + ruleName);
|
||||
if (evt.eventname === ruleEvent && validConditions(evt, oMyRule.rule, userName, ruleName)) {
|
||||
this.log.info('EN | EVENT FIRED: ' + evt.eventname + ' for rule ' + ruleName);
|
||||
_results1.push((function() {
|
||||
var _i, _len, _ref, _results2;
|
||||
_ref = oMyRule.rule.actions;
|
||||
|
|
|
|||
|
|
@ -71,10 +71,10 @@ Dynamic Modules
|
|||
|
||||
process.on('message', function(msg) {
|
||||
log.info("EP | Got info about new rule: " + msg.event);
|
||||
if (msg.event === 'new' || msg.event === 'init') {
|
||||
if (msg.intevent === 'new' || msg.intevent === 'init') {
|
||||
fLoadModule(msg);
|
||||
}
|
||||
if (msg.event === 'del') {
|
||||
if (msg.intevent === 'del') {
|
||||
delete listUserModules[msg.user][msg.ruleId];
|
||||
if (JSON.stringify(listUserModules[msg.user]) === "{}") {
|
||||
return delete listUserModules[msg.user];
|
||||
|
|
@ -84,7 +84,7 @@ Dynamic Modules
|
|||
|
||||
fLoadModule = function(msg) {
|
||||
var arrName, fAnonymous;
|
||||
arrName = msg.rule.event.split(' -> ');
|
||||
arrName = msg.rule.eventname.split(' -> ');
|
||||
fAnonymous = function() {
|
||||
return db.eventPollers.getModule(msg.user, arrName[0], function(err, obj) {
|
||||
if (!obj) {
|
||||
|
|
@ -100,15 +100,15 @@ Dynamic Modules
|
|||
}
|
||||
oUser = listUserModules[msg.user];
|
||||
oUser[msg.rule.id] = {
|
||||
id: msg.rule.event,
|
||||
id: msg.rule.eventname,
|
||||
timestamp: msg.rule.timestamp,
|
||||
pollfunc: arrName[1],
|
||||
funcArgs: result.funcArgs,
|
||||
event_interval: msg.rule.event_interval * 60 * 1000,
|
||||
eventinterval: msg.rule.eventinterval * 60 * 1000,
|
||||
module: result.module,
|
||||
logger: result.logger
|
||||
};
|
||||
start = new Date(msg.rule.event_start);
|
||||
start = new Date(msg.rule.eventstart);
|
||||
nd = new Date();
|
||||
now = new Date();
|
||||
if (start < nd) {
|
||||
|
|
@ -122,13 +122,13 @@ Dynamic Modules
|
|||
} else {
|
||||
nd = start;
|
||||
}
|
||||
log.info("EP | New event module '" + arrName[0] + "' loaded for user " + msg.user + ", in rule " + msg.rule.id + ", registered at UTC|" + msg.rule.timestamp + ", starting at UTC|" + (start.toISOString()) + " ( which is in " + ((nd - now) / 1000 / 60) + " minutes ) 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 + ", registered at UTC|" + msg.rule.timestamp + ", starting at UTC|" + (start.toISOString()) + " ( which is in " + ((nd - now) / 1000 / 60) + " minutes ) and polling every " + msg.rule.eventinterval + " minutes");
|
||||
return setTimeout(fCheckAndRun(msg.user, msg.rule.id, msg.rule.timestamp), nd - now);
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
if (msg.event === 'new' || !listUserModules[msg.user] || !listUserModules[msg.user][msg.rule.id]) {
|
||||
if (msg.intevent === 'new' || !listUserModules[msg.user] || !listUserModules[msg.user][msg.rule.id]) {
|
||||
return fAnonymous();
|
||||
}
|
||||
};
|
||||
|
|
@ -141,7 +141,7 @@ Dynamic Modules
|
|||
if (listUserModules[userId][ruleId].timestamp === timestamp) {
|
||||
oRule = listUserModules[userId][ruleId];
|
||||
fCallFunction(userId, ruleId, oRule);
|
||||
return setTimeout(fCheckAndRun(userId, ruleId, timestamp), oRule.event_interval);
|
||||
return setTimeout(fCheckAndRun(userId, ruleId, timestamp), oRule.eventinterval);
|
||||
} else {
|
||||
return log.info("EP | We found a newer polling interval and discontinue this one which was created at UTC|" + timestamp);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -64,12 +64,12 @@ HTTP Listener
|
|||
app.use('/', express["static"](path.resolve(__dirname, '..', 'webpages', 'public')));
|
||||
app.get('/admin', requestHandler.handleAdmin);
|
||||
app.get('/forge', requestHandler.handleForge);
|
||||
app.post('/event', requestHandler.handleEvent);
|
||||
app.post('/login', requestHandler.handleLogin);
|
||||
app.post('/logout', requestHandler.handleLogout);
|
||||
app.post('/usercommand', requestHandler.handleUserCommand);
|
||||
app.post('/admincommand', requestHandler.handleAdminCommand);
|
||||
app.post('/webhooks', requestHandler.handleWebhooks);
|
||||
app.post('/event', requestHandler.handleEvent);
|
||||
app.post('/webhooks/*', requestHandler.handleWebhooks);
|
||||
app.post('/measurements', requestHandler.handleMeasurements);
|
||||
server = app.listen(parseInt(port) || 8111);
|
||||
server.on('listening', function() {
|
||||
|
|
|
|||
|
|
@ -159,7 +159,7 @@ Persistence
|
|||
exports.pushEvent = (function(_this) {
|
||||
return function(oEvent) {
|
||||
if (oEvent) {
|
||||
_this.log.info("DB | Event pushed into the queue: '" + oEvent.eventid + "'");
|
||||
_this.log.info("DB | Event pushed into the queue: '" + oEvent.eventname + "'");
|
||||
return _this.db.rpush('event_queue', JSON.stringify(oEvent));
|
||||
} else {
|
||||
return _this.log.warn('DB | Why would you give me an empty event...');
|
||||
|
|
@ -1045,20 +1045,35 @@ Persistence
|
|||
|
||||
exports.createWebhook = (function(_this) {
|
||||
return function(userId, hookname, hookid) {
|
||||
_this.db.sadd("user:" + userId + ":webhooks", hookname, replyHandler("sadd 'user:" + userId + ":webhooks' -> '" + hookname + "'"));
|
||||
return _this.db.sadd("webhooks", hookid, replyHandler("sadd 'webhooks' -> '" + hookid + "'"));
|
||||
_this.db.sadd("webhooks", hookid, replyHandler("sadd 'webhooks' -> '" + hookid + "'"));
|
||||
_this.db.sadd("user:" + userId + ":webhooks", hookid, replyHandler("sadd 'user:" + userId + ":webhooks' -> '" + hookid + "'"));
|
||||
return _this.db.set("webhook:" + hookid, hookname, replyHandler("set webhook:" + hookid + " -> " + hookname));
|
||||
};
|
||||
})(this);
|
||||
|
||||
|
||||
/*
|
||||
Gets all the users webhooks.
|
||||
Returns a webhook name.
|
||||
|
||||
@public getWebhooks( *userId* )
|
||||
@public getWebhookName( *hookid* )
|
||||
@param {String} hookid
|
||||
*/
|
||||
|
||||
exports.getWebhookName = (function(_this) {
|
||||
return function(hookid, cb) {
|
||||
return _this.db.get("webhook:" + hookid, cb);
|
||||
};
|
||||
})(this);
|
||||
|
||||
|
||||
/*
|
||||
Returns all the user's webhooks by ID.
|
||||
|
||||
@public getUserWebhookIDs( *userId* )
|
||||
@param {String} userId
|
||||
*/
|
||||
|
||||
exports.getUserWebhooks = (function(_this) {
|
||||
exports.getUserWebhookIDs = (function(_this) {
|
||||
return function(userId, cb) {
|
||||
return _this.db.smembers("user:" + userId + ":webhooks", cb);
|
||||
};
|
||||
|
|
@ -1066,12 +1081,25 @@ Persistence
|
|||
|
||||
|
||||
/*
|
||||
Gets all the users webhooks.
|
||||
Gets all the user's webhooks with names.
|
||||
|
||||
@public getWebhooks( *userId* )
|
||||
@public getAllUserWebhooks( *userId* )
|
||||
@param {String} userId
|
||||
*/
|
||||
|
||||
exports.getAllUserWebhooks = (function(_this) {
|
||||
return function(userId, cb) {
|
||||
return getSetRecords("user:" + userId + ":webhooks", exports.getWebhookName, cb);
|
||||
};
|
||||
})(this);
|
||||
|
||||
|
||||
/*
|
||||
Returns all webhook IDs.
|
||||
|
||||
@public getAllWebhookIDs()
|
||||
*/
|
||||
|
||||
exports.getAllWebhookIDs = (function(_this) {
|
||||
return function(cb) {
|
||||
return _this.db.smembers("webhooks", cb);
|
||||
|
|
@ -1080,16 +1108,31 @@ Persistence
|
|||
|
||||
|
||||
/*
|
||||
Deletes a webhook.
|
||||
Returns all webhooks with names.
|
||||
|
||||
@public deleteWebhook( *userId, hookname* )
|
||||
@param {String} userId
|
||||
@param {String} hookname
|
||||
@public getAllWebhooks()
|
||||
*/
|
||||
|
||||
exports.deleteUserWebhook = (function(_this) {
|
||||
return function(userId, hookname) {
|
||||
return _this.db.srem("user:" + userId + ":webhooks", hookname, replyHandler("srem 'user:" + userId + ":webhooks' -> '" + hookname + "'"));
|
||||
exports.getAllWebhooks = (function(_this) {
|
||||
return function(cb) {
|
||||
return getSetRecords("webhooks", exports.getWebhookName, cb);
|
||||
};
|
||||
})(this);
|
||||
|
||||
|
||||
/*
|
||||
Delete a webhook.
|
||||
|
||||
@public deleteWebhook( *userId, hookid* )
|
||||
@param {String} userId
|
||||
@param {String} hookid
|
||||
*/
|
||||
|
||||
exports.deleteWebhook = (function(_this) {
|
||||
return function(userId, hookid) {
|
||||
_this.db.srem("webhooks", hookid, replyHandler("srem 'webhooks' -> '" + hookid + "'"));
|
||||
_this.db.srem("user:" + userId + ":webhooks", hookid, replyHandler("srem 'user:" + userId + ":webhooks' -> '" + hookid + "'"));
|
||||
return _this.db.del("webhook:" + hookid, replyHandler("del webhook:" + hookid));
|
||||
};
|
||||
})(this);
|
||||
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ Request Handler
|
|||
*/
|
||||
|
||||
(function() {
|
||||
var activateWebHook, crypto, db, dirHandlers, exports, fs, getHandlerPath, getRemoteScripts, getScript, getTemplate, indexEvent, mustache, path, pathUsers, qs, removeWebHook, renderPage;
|
||||
var crypto, db, dirHandlers, exports, fs, getHandlerPath, getRemoteScripts, getScript, getTemplate, indexEvent, mustache, path, pathUsers, qs, renderPage;
|
||||
|
||||
db = require('./persistence');
|
||||
|
||||
|
|
@ -92,7 +92,6 @@ Request Handler
|
|||
return cb(null, data);
|
||||
}
|
||||
};
|
||||
db(args);
|
||||
users = JSON.parse(fs.readFileSync(pathUsers, 'utf8'));
|
||||
fStoreUser = function(username, oUser) {
|
||||
oUser.username = username;
|
||||
|
|
@ -102,6 +101,14 @@ Request Handler
|
|||
oUser = users[user];
|
||||
fStoreUser(user, oUser);
|
||||
}
|
||||
_this.allowedHooks = {};
|
||||
db.getAllWebhooks(function(err, oHooks) {
|
||||
if (oHooks) {
|
||||
console;
|
||||
_this.log.info("RH | Initializing " + (Object.keys(oHooks).length) + " Webhooks");
|
||||
return _this.allowedHooks = oHooks;
|
||||
}
|
||||
});
|
||||
return module.exports;
|
||||
};
|
||||
})(this);
|
||||
|
|
@ -110,12 +117,6 @@ Request Handler
|
|||
/*
|
||||
Handles possible events that were posted to this server and pushes them into the
|
||||
event queue.
|
||||
|
||||
*Requires
|
||||
the [request](http://nodejs.org/api/http.html#http_class_http_clientrequest)
|
||||
and [response](http://nodejs.org/api/http.html#http_class_http_serverresponse)
|
||||
objects.*
|
||||
|
||||
@public handleEvent( *req, resp* )
|
||||
*/
|
||||
|
||||
|
|
@ -126,23 +127,17 @@ Request Handler
|
|||
return body += data;
|
||||
});
|
||||
return req.on('end', function() {
|
||||
var answ, err, obj, rand, timestamp;
|
||||
console.log(typeof body);
|
||||
console.log(body);
|
||||
var answ, err, obj;
|
||||
try {
|
||||
obj = JSON.parse(body);
|
||||
} catch (_error) {
|
||||
err = _error;
|
||||
resp.send(400, 'Badly formed event!');
|
||||
}
|
||||
console.log(obj);
|
||||
if (obj && obj.event && !err) {
|
||||
timestamp = (new Date()).toISOString();
|
||||
rand = (Math.floor(Math.random() * 10e9)).toString(16).toUpperCase();
|
||||
obj.eventid = "" + obj.event + "_UTC|" + timestamp + "_" + rand;
|
||||
if (obj && obj.eventname && !err) {
|
||||
answ = {
|
||||
code: 200,
|
||||
message: "Thank you for the event: " + obj.eventid
|
||||
message: "Thank you for the event: " + obj.eventname
|
||||
};
|
||||
resp.send(answ.code, answ);
|
||||
return db.pushEvent(obj);
|
||||
|
|
@ -438,45 +433,32 @@ Request Handler
|
|||
};
|
||||
})(this);
|
||||
|
||||
indexEvent = function(event, body, resp) {
|
||||
var err, obj, rand, timestamp;
|
||||
indexEvent = function(eventname, body, resp) {
|
||||
var err, msg, obj;
|
||||
if (typeof body === 'string') {
|
||||
try {
|
||||
obj = qs.parse(body);
|
||||
body = JSON.parse(body);
|
||||
} catch (_error) {
|
||||
err = _error;
|
||||
try {
|
||||
obj = JSON.parse(body);
|
||||
body = qs.parse(body);
|
||||
} catch (_error) {
|
||||
err = _error;
|
||||
resp.send(400, 'Badly formed event!');
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
obj = body;
|
||||
}
|
||||
timestamp = (new Date()).toISOString();
|
||||
rand = (Math.floor(Math.random() * 10e9)).toString(16).toUpperCase();
|
||||
obj.event = event;
|
||||
obj.eventid = "" + obj.event + "_UTC|" + timestamp + "_" + rand;
|
||||
obj = {
|
||||
eventname: eventname,
|
||||
body: body
|
||||
};
|
||||
db.pushEvent(obj);
|
||||
resp.send(200, "Thank you for the event: " + obj.eventid);
|
||||
msg = "Thank you for the event: '" + eventname + "'";
|
||||
return obj;
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
Handles webhook posts
|
||||
*/
|
||||
|
||||
exports.handleWebhooks = (function(_this) {
|
||||
return function(req, resp) {
|
||||
return console.log('RH | IMPLEMENT WEBHOOKS');
|
||||
};
|
||||
})(this);
|
||||
|
||||
|
||||
/*
|
||||
Handles measurement posts
|
||||
*/
|
||||
|
|
@ -499,39 +481,42 @@ Request Handler
|
|||
};
|
||||
})(this);
|
||||
|
||||
activateWebHook = (function(_this) {
|
||||
return function(app, name) {
|
||||
_this.log.info("HL | Webhook activated for " + name);
|
||||
return app.post("/webhooks/" + name, function(req, resp) {
|
||||
var body;
|
||||
|
||||
/*
|
||||
Handles webhook posts
|
||||
*/
|
||||
|
||||
exports.handleWebhooks = (function(_this) {
|
||||
return function(req, resp) {
|
||||
var body, hookid, hookname;
|
||||
hookid = req.url.substring(10).split('/')[0];
|
||||
hookname = _this.allowedHooks[hookid];
|
||||
if (hookname) {
|
||||
body = '';
|
||||
req.on('data', function(data) {
|
||||
return body += data;
|
||||
});
|
||||
return req.on('end', function() {
|
||||
return indexEvent(name, body, resp);
|
||||
var obj;
|
||||
return obj = indexEvent(hookname, body, resp);
|
||||
});
|
||||
});
|
||||
} else {
|
||||
return resp.send(404, "Webhook not existing!");
|
||||
}
|
||||
};
|
||||
})(this);
|
||||
|
||||
removeWebHook = (function(_this) {
|
||||
return function(app, hookid) {
|
||||
var i, isFound, oRoute, _i, _len, _ref, _results;
|
||||
_this.log.info("HL | Removing Webhook for " + hookid);
|
||||
isFound = false;
|
||||
_ref = app.routes.post;
|
||||
_results = [];
|
||||
for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) {
|
||||
oRoute = _ref[i];
|
||||
if (oRoute.path === ("/webhooks/" + name)) {
|
||||
app.routes.post.splice(i, 1);
|
||||
_results.push(isFound = true);
|
||||
} else {
|
||||
_results.push(void 0);
|
||||
}
|
||||
}
|
||||
return _results;
|
||||
exports.activateWebhook = (function(_this) {
|
||||
return function(hookid, name) {
|
||||
_this.log.info("HL | Webhook '" + hookid + "' activated");
|
||||
return _this.allowedHooks[hookid] = name;
|
||||
};
|
||||
})(this);
|
||||
|
||||
exports.deactivateWebhook = (function(_this) {
|
||||
return function(hookid) {
|
||||
_this.log.info("HL | Webhook '" + hookid + "' deactivated");
|
||||
return delete _this.allowedHooks[hookid];
|
||||
};
|
||||
})(this);
|
||||
|
||||
|
|
|
|||
|
|
@ -55,7 +55,7 @@
|
|||
},
|
||||
"events": {
|
||||
"eventOne":{
|
||||
"event": "test_1",
|
||||
"eventname": "test_1",
|
||||
"body": {
|
||||
"property": "test_1",
|
||||
"nestedProperty": {
|
||||
|
|
@ -64,10 +64,10 @@
|
|||
}
|
||||
},
|
||||
"eventTwo":{
|
||||
"event": "test_2"
|
||||
"eventname": "test_2"
|
||||
},
|
||||
"eventReal":{
|
||||
"event": "epOne -> newMail",
|
||||
"eventname": "epOne -> newMail",
|
||||
"body": {
|
||||
"property": "test_1",
|
||||
"nestedProperty": {
|
||||
|
|
@ -79,8 +79,8 @@
|
|||
"rules": {
|
||||
"ruleOne": {
|
||||
"id": "ruleReal",
|
||||
"event": "test_1",
|
||||
"event_interval": 1,
|
||||
"eventname": "test_1",
|
||||
"eventinterval": 1,
|
||||
"conditions":
|
||||
[
|
||||
{
|
||||
|
|
@ -91,7 +91,7 @@
|
|||
}
|
||||
],
|
||||
"actions": ["aiOne -> printToLog"],
|
||||
"action_functions": {
|
||||
"actionfunctions": {
|
||||
"aiOne -> printToLog": [
|
||||
{
|
||||
"argument": "evt",
|
||||
|
|
@ -103,15 +103,15 @@
|
|||
},
|
||||
"ruleTwo": {
|
||||
"id": "ruleRealTwo",
|
||||
"event": "test_2",
|
||||
"event_interval": 1,
|
||||
"eventname": "test_2",
|
||||
"eventinterval": 1,
|
||||
"conditions": [],
|
||||
"actions": ["aiTwo -> otherEvent"]
|
||||
},
|
||||
"ruleThree": {
|
||||
"id": "ruleRealThree",
|
||||
"event": "epOne -> newMail",
|
||||
"event_interval": 1,
|
||||
"eventname": "epOne -> newMail",
|
||||
"eventinterval": 1,
|
||||
"conditions": [],
|
||||
"actions": ["aiThree -> printUserParamToLog"]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -112,14 +112,14 @@ exports.testListener = ( test ) =>
|
|||
|
||||
cm.addRuleListener ( evt ) =>
|
||||
strEvt = JSON.stringify evt.rule
|
||||
if evt.event is 'init'
|
||||
if evt.intevent is 'init'
|
||||
if strEvt is strRuleOne or strEvt is strRuleTwo
|
||||
test.ok true, 'Dummy true to fill expected tests!'
|
||||
|
||||
if strEvt is strRuleThree
|
||||
test.ok false, 'Init Rule found test rule number two??'
|
||||
|
||||
if evt.event is 'new'
|
||||
if evt.intevent is 'new'
|
||||
if strEvt is strRuleOne or strEvt is strRuleTwo
|
||||
test.ok false, 'New Rule got test rule number one??'
|
||||
|
||||
|
|
@ -215,7 +215,6 @@ exports.ruleForge =
|
|||
|
||||
fWaitForPersistence = () ->
|
||||
evt = objects.events.eventReal
|
||||
evt.eventid = 'event_testid'
|
||||
db.pushEvent evt
|
||||
|
||||
fWaitAgain = () ->
|
||||
|
|
|
|||
|
|
@ -42,12 +42,12 @@ exports.tearDown = ( cb ) ->
|
|||
db.deleteUser oUser.username
|
||||
|
||||
engine.internalEvent
|
||||
event: 'del'
|
||||
intevent: 'del'
|
||||
user: oUser.username
|
||||
rule: oRuleOne
|
||||
|
||||
engine.internalEvent
|
||||
event: 'del'
|
||||
intevent: 'del'
|
||||
user: oUser.username
|
||||
rule: oRuleTwo
|
||||
engine.shutDown()
|
||||
|
|
@ -67,7 +67,7 @@ exports.ruleEvents =
|
|||
test.strictEqual listRules[oUser.username], undefined, 'Initial user object exists!?'
|
||||
|
||||
engine.internalEvent
|
||||
event: 'new'
|
||||
intevent: 'new'
|
||||
user: oUser.username
|
||||
rule: oRuleOne
|
||||
|
||||
|
|
@ -79,7 +79,7 @@ exports.ruleEvents =
|
|||
|
||||
|
||||
engine.internalEvent
|
||||
event: 'new'
|
||||
intevent: 'new'
|
||||
user: oUser.username
|
||||
rule: oRuleTwo
|
||||
|
||||
|
|
@ -90,7 +90,7 @@ exports.ruleEvents =
|
|||
test.ok listRules[oUser.username][oRuleTwo.id].actions[mod], 'Missing action!'
|
||||
|
||||
engine.internalEvent
|
||||
event: 'del'
|
||||
intevent: 'del'
|
||||
user: oUser.username
|
||||
rule: null
|
||||
ruleId: oRuleTwo.id
|
||||
|
|
@ -100,7 +100,7 @@ exports.ruleEvents =
|
|||
test.ok listRules[oUser.username][oRuleOne.id].actions[mod], 'Missing action!'
|
||||
|
||||
engine.internalEvent
|
||||
event: 'del'
|
||||
intevent: 'del'
|
||||
user: oUser.username
|
||||
rule: null
|
||||
ruleId: oRuleOne.id
|
||||
|
|
|
|||
|
|
@ -133,7 +133,6 @@ exports.events =
|
|||
semaphore = 2
|
||||
fPopEvent = () ->
|
||||
fCb = ( err, obj ) ->
|
||||
oEvt.eventid = obj.eventid # because the event id is generated by the system
|
||||
test.deepEqual obj, oEvt, 'Caught event is not what we expected'
|
||||
if --semaphore is 0
|
||||
test.done()
|
||||
|
|
@ -189,7 +188,7 @@ exports.testLoginOrPage = ( test ) ->
|
|||
resp = createResponse ( code, msg ) ->
|
||||
|
||||
# After being logged in we should get the expected page
|
||||
test.ok msg.indexOf( 'document.title = \'Event Forge!\'' ) > 0, 'Didn\' get forge page?'
|
||||
test.ok msg.indexOf( 'document.title = \'Push Events!\'' ) > 0, 'Didn\' get forge page?'
|
||||
req = createLoggedInRequest()
|
||||
req.query =
|
||||
page: 'wrongpage'
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ fFindKeyStringPair = ( obj ) ->
|
|||
|
||||
fOnLoad = () ->
|
||||
document.title = 'Push Events!'
|
||||
$( '#pagetitle' ).text 'Push your own custom event directly into the engine!'
|
||||
$( '#pagetitle' ).text 'Push your own event directly into the engine!'
|
||||
|
||||
editor = ace.edit "editor"
|
||||
editor.setTheme "ace/theme/monokai"
|
||||
|
|
@ -48,13 +48,13 @@ fOnLoad = () ->
|
|||
|
||||
try
|
||||
obj = JSON.parse editor.getValue() # try to parse, throw an error if JSON not valid
|
||||
if obj.event and typeof obj.event is 'string' and obj.event isnt ''
|
||||
if obj.eventname and typeof obj.eventname is 'string' and obj.eventname isnt ''
|
||||
sel = ''
|
||||
if obj.body and typeof obj.body is 'object'
|
||||
oSelector = fFindKeyStringPair obj.body
|
||||
if oSelector
|
||||
sel = "&selkey=#{ oSelector.key }&selval=#{ oSelector.val }"
|
||||
url = 'forge?page=forge_rule&eventtype=custom&eventname=' + obj.event + sel
|
||||
url = 'forge?page=forge_rule&eventtype=custom&eventname=' + obj.eventname + sel
|
||||
window.open url, '_blank'
|
||||
else
|
||||
$( '#info' ).text 'Please provide a valid eventname'
|
||||
|
|
|
|||
|
|
@ -9,13 +9,13 @@ if oParams.id
|
|||
oParams.id = decodeURIComponent oParams.id
|
||||
|
||||
strPublicKey = ''
|
||||
fPlaceAndPaintInterval = () ->
|
||||
$( '#event_start' ).html 'Start Time:
|
||||
<input id="input_start" type="text" />
|
||||
<b>"hh:mm"</b>, default = 12:00'
|
||||
$( '#event_interval' ).html 'Interval:
|
||||
<input id="input_interval" type="text" />
|
||||
<b>"days hours:minutes"</b>, default = 10 minutes'
|
||||
# fPlaceAndPaintInterval = () ->
|
||||
# $( '#event_start' ).html 'Start Time:
|
||||
# <input id="input_start" type="text" />
|
||||
# <b>"hh:mm"</b>, default = 12:00'
|
||||
# $( '#event_interval' ).html 'Interval:
|
||||
# <input id="input_interval" type="text" />
|
||||
# <b>"days hours:minutes"</b>, default = 10 minutes'
|
||||
|
||||
fDisplayError = ( msg ) ->
|
||||
window.scrollTo 0, 0
|
||||
|
|
@ -55,19 +55,22 @@ fOnLoad = () ->
|
|||
editor.setTheme "ace/theme/monokai"
|
||||
editor.getSession().setMode "ace/mode/json"
|
||||
editor.setShowPrintMargin false
|
||||
# editor.session.setUseSoftTabs false
|
||||
|
||||
|
||||
$( '#input_id' ).focus()
|
||||
|
||||
# EVENT
|
||||
|
||||
# If the user is coming from an event he wants a rule to be setup for him
|
||||
switch oParams.eventtype
|
||||
when 'custom'
|
||||
$( '#input_id' ).val "My '#{ oParams.eventname }' Rule"
|
||||
$( '#select_event_type' ).val 'Custom Event'
|
||||
inpEvt = $( '<input>' ).attr( 'type', 'text').attr 'id', 'input_eventname'
|
||||
inpEvt = $( '<input>' ).attr( 'type', 'text' )
|
||||
.attr( 'style', 'font-size:1em' ).attr 'id', 'input_eventname'
|
||||
inpEvt.val oParams.eventname
|
||||
$( '#event_parameters' ).append inpEvt
|
||||
$( '#event_parameters' ).append $( '<h4>' ).text( 'Event Name : ' ).append inpEvt
|
||||
inpEvt.focus()
|
||||
editor.setValue "[\n\n]"
|
||||
|
||||
when 'webhook'
|
||||
console.log 'webhook'
|
||||
|
|
@ -82,8 +85,9 @@ fOnLoad = () ->
|
|||
|
||||
# The user wants to act on a custom event
|
||||
when 'Custom Event'
|
||||
inpEvt = $( '<input>' ).attr( 'type', 'text').attr 'id', 'input_eventname'
|
||||
$( '#event_parameters' ).append inpEvt
|
||||
inpEvt = $( '<input>' ).attr( 'type', 'text' )
|
||||
.attr( 'style', 'font-size:1em' ).attr 'id', 'input_eventname'
|
||||
$( '#event_parameters' ).append $( '<h4>' ).text( 'Event Name : ' ).append inpEvt
|
||||
|
||||
# The user wants a webhook as event producer
|
||||
when 'Webhook'
|
||||
|
|
@ -120,7 +124,7 @@ fOnLoad = () ->
|
|||
catch err
|
||||
console.error 'ERROR: non-object received from server: ' + data.message
|
||||
|
||||
fail: fFailedRequest 'Error fetching event poller'
|
||||
fail: fFailedRequest 'Error fetching Event Poller'
|
||||
|
||||
$( '#select_event' ).change () ->
|
||||
evtFunc = $( this ).val()
|
||||
|
|
@ -152,7 +156,7 @@ fOnLoad = () ->
|
|||
body: JSON.stringify
|
||||
id: arr[ 0 ]
|
||||
done: fAddEventParams arr[ 0 ]
|
||||
fail: fFailedRequest 'Error fetching event poller params'
|
||||
fail: fFailedRequest 'Error fetching Event Poller params'
|
||||
fFetchEventFunctionArgs arr
|
||||
|
||||
fFetchEventFunctionArgs = ( arrName ) ->
|
||||
|
|
@ -269,7 +273,7 @@ fOnLoad = () ->
|
|||
if arrEls.length is 0
|
||||
$( '#select_actions' ).append $( '<option>' ).text module + ' -> ' + act
|
||||
fAppendActions module, actions for module, actions of oAis
|
||||
.fail fFailedRequest 'Error fetching event poller'
|
||||
.fail fFailedRequest 'Error fetching Event Poller'
|
||||
|
||||
fAddSelectedAction = ( name ) ->
|
||||
arrName = name.split ' -> '
|
||||
|
|
@ -584,15 +588,15 @@ fOnLoad = () ->
|
|||
command: 'forge_rule'
|
||||
body: JSON.stringify
|
||||
id: $( '#input_id' ).val()
|
||||
event: eventId
|
||||
event_params: ep
|
||||
event_start: start
|
||||
event_interval: mins
|
||||
event_functions: evtFuncs
|
||||
eventname: eventId
|
||||
eventparams: ep
|
||||
eventstart: start
|
||||
eventinterval: mins
|
||||
eventfunctions: evtFuncs
|
||||
conditions: conds
|
||||
actions: acts
|
||||
action_params: ap
|
||||
action_functions: actFuncs
|
||||
actionparams: ap
|
||||
actionfunctions: actFuncs
|
||||
$.post( '/usercommand', obj )
|
||||
.done ( data ) ->
|
||||
$( '#info' ).text data.message
|
||||
|
|
@ -617,18 +621,18 @@ fOnLoad = () ->
|
|||
$( '#input_id' ).val oRule.id
|
||||
|
||||
# Event
|
||||
$( '#select_event' ).val oRule.event
|
||||
$( '#select_event' ).val oRule.eventname
|
||||
if $( '#select_event' ).val() isnt ''
|
||||
fFetchEventParams oRule.event
|
||||
fFetchEventParams oRule.eventname
|
||||
fPlaceAndPaintInterval()
|
||||
|
||||
$( '#input_event' ).val oRule.event
|
||||
d = new Date oRule.event_start
|
||||
$( '#input_event' ).val oRule.eventname
|
||||
d = new Date oRule.eventstart
|
||||
mins = d.getMinutes()
|
||||
if mins.toString().length is 1
|
||||
mins = '0' + mins
|
||||
$( '#input_start' ).val d.getHours() + ':' + mins
|
||||
$( '#input_interval' ).val oRule.event_interval
|
||||
$( '#input_interval' ).val oRule.eventinterval
|
||||
|
||||
# Conditions
|
||||
editor.setValue JSON.stringify oRule.conditions, undefined, 2
|
||||
|
|
|
|||
|
|
@ -8,17 +8,22 @@ for param in arrParams
|
|||
if oParams.id
|
||||
oParams.id = decodeURIComponent oParams.id
|
||||
|
||||
hostUrl = [ location.protocol, '//', location.host ].join ''
|
||||
|
||||
|
||||
fDisplayError = ( msg ) ->
|
||||
window.scrollTo 0, 0
|
||||
$( '#info' ).text "Error: #{ msg }"
|
||||
$( '#info' ).attr 'class', 'error'
|
||||
|
||||
|
||||
fIssueRequest = ( args ) ->
|
||||
$( '#info' ).text ''
|
||||
$.post( '/usercommand', args.body )
|
||||
.done args.done
|
||||
.fail args.fail
|
||||
|
||||
|
||||
fFailedRequest = ( msg ) ->
|
||||
( err ) ->
|
||||
if err.status is 401
|
||||
|
|
@ -26,29 +31,36 @@ fFailedRequest = ( msg ) ->
|
|||
else
|
||||
fDisplayError msg
|
||||
|
||||
fUpdateWebhookList = ( data ) ->
|
||||
|
||||
fUpdateWebhookList = () ->
|
||||
fIssueRequest
|
||||
body:
|
||||
command: 'get_all_webhooks'
|
||||
done: fProcessWebhookList
|
||||
fail: fFailedRequest 'Unable to get Webhook list'
|
||||
|
||||
|
||||
fProcessWebhookList = ( data ) ->
|
||||
$( '#table_webhooks tr' ).remove()
|
||||
oHooks = JSON.parse data.message
|
||||
console.log oHooks
|
||||
for modName of oHooks
|
||||
tr = $ '<tr>'
|
||||
inp = $( '<div>' ).text modName
|
||||
console.log hostUrl
|
||||
for hookid, hookname of oHooks
|
||||
tr = $( '<tr>' )
|
||||
tdName = $( '<div>' ).text hookname
|
||||
tdUrl = $( '<input>' ).attr( 'style', 'width:600px' ).val "#{ hostUrl }/webhooks/#{ hookid }"
|
||||
img = $( '<img>' ).attr( 'class', 'del' )
|
||||
.attr( 'title', 'Delete Module' ).attr 'src', 'red_cross_small.png'
|
||||
tr.append( $( '<td>' ).append img )
|
||||
tr.append( $( '<td>' ).append inp )
|
||||
tr.append( $( '<td>' ).attr( 'style', 'padding-left:10px' ).append tdName )
|
||||
tr.append( $( '<td>' ).attr( 'style', 'padding-left:10px' ).append tdUrl )
|
||||
$( '#table_webhooks' ).append tr
|
||||
|
||||
|
||||
fOnLoad = () ->
|
||||
|
||||
document.title = 'Create Webhooks!'
|
||||
# Load existing rules
|
||||
|
||||
fIssueRequest
|
||||
body:
|
||||
command: 'get_all_webhooks'
|
||||
done: fUpdateWebhookList
|
||||
fail: fFailedRequest 'Unable to post get_all_webhooks request'
|
||||
# Load existing Webhooks
|
||||
fUpdateWebhookList()
|
||||
|
||||
# Register button action
|
||||
$( '#but_submit' ).click ->
|
||||
|
|
@ -56,7 +68,7 @@ fOnLoad = () ->
|
|||
|
||||
hookname = $( '#inp_hookname' ).val()
|
||||
if hookname is ''
|
||||
fDisplayError 'Please provide a hookname!'
|
||||
fDisplayError 'Please provide an Event Name for your new Webhook!'
|
||||
|
||||
else
|
||||
$( '#display_hookurl *' ).remove()
|
||||
|
|
@ -67,23 +79,44 @@ fOnLoad = () ->
|
|||
hookname: hookname
|
||||
done: ( data ) ->
|
||||
oAnsw = JSON.parse data.message
|
||||
url = [ location.protocol, '//', location.host ].join ''
|
||||
|
||||
h3 = $( '<h3>' ).text "This is the Webhook Url you should use:"
|
||||
inp = $('<input>').attr( 'type', 'text' ).attr( 'style', 'font-size:1em' )
|
||||
.val "#{ url }/webhooks/#{ oAnsw.hookid }"
|
||||
h3.append inp
|
||||
$( '#display_hookurl' ).append h3
|
||||
b = $( '<b>' ).text "This is the Webhook Url you will use for your Event : "
|
||||
$( '#display_hookurl' ).append b
|
||||
$( '#display_hookurl' ).append $('<br>')
|
||||
inp = $('<input>').attr( 'type', 'text' ).attr( 'style', 'width:600px' )
|
||||
.val "#{ hostUrl }/webhooks/#{ oAnsw.hookid }"
|
||||
$( '#display_hookurl' ).append inp
|
||||
$( '#display_hookurl' ).append $('<br>')
|
||||
|
||||
div = $('<div>').html "Push your location via <a href=\"#{ url }/mobile/index.html?hookid=#{ oAnsw.hookid }\">this page</a>"
|
||||
div = $( '<div>' )
|
||||
div.append $( '<br>' )
|
||||
div.append $( '<div>' ).html "1. Try it out and push your location to your new webhook
|
||||
via <a target=\"_blank\" href=\"#{ hostUrl }/mobile.html?hookid=#{ oAnsw.hookid }\">this page</a>."
|
||||
div.append $( '<br>' )
|
||||
div.append $( '<div>' ).html "2. Then you should setup <a target=\"_blank\"
|
||||
href=\"forge?page=forge_rule&eventtype=webhook&hookname=#{ hookname }\">a Rule for this Event!</a>"
|
||||
$( '#display_hookurl' ).append div
|
||||
|
||||
fail: ( err ) ->
|
||||
if err.status is 409
|
||||
fFailedRequest( 'Webhook Event Name already existing!' ) err
|
||||
else
|
||||
fFailedRequest( 'Unable to create Webhook! ' + err.message ) err
|
||||
|
||||
# <h3>This is the Webhook Url you should use:</h3>
|
||||
# push your location via this page: mobile?hookid=
|
||||
|
||||
$( '#table_webhooks' ).on 'click', 'img', () ->
|
||||
if confirm "Do you really want to delete this webhook?"
|
||||
url = $( 'input', $( this ).closest( 'tr' ) ).val()
|
||||
arrUrl = url.split '/'
|
||||
fIssueRequest
|
||||
body:
|
||||
command: 'delete_webhook'
|
||||
body: JSON.stringify
|
||||
hookid: arrUrl[ arrUrl.length - 1 ]
|
||||
done: ( data ) ->
|
||||
$( '#info' ).text data.message
|
||||
$( '#info' ).attr 'class', 'success'
|
||||
fUpdateWebhookList()
|
||||
fail: ( err ) ->
|
||||
fFailedRequest( 'Unable to delete Webhook!' ) err
|
||||
fUpdateWebhookList()
|
||||
|
||||
window.addEventListener 'load', fOnLoad, true
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@
|
|||
fOnLoad = function() {
|
||||
var editor;
|
||||
document.title = 'Push Events!';
|
||||
$('#pagetitle').text('Push your own custom event directly into the engine!');
|
||||
$('#pagetitle').text('Push your own event directly into the engine!');
|
||||
editor = ace.edit("editor");
|
||||
editor.setTheme("ace/theme/monokai");
|
||||
editor.getSession().setMode("ace/mode/json");
|
||||
|
|
@ -65,7 +65,7 @@
|
|||
var err, oSelector, obj, sel, url;
|
||||
try {
|
||||
obj = JSON.parse(editor.getValue());
|
||||
if (obj.event && typeof obj.event === 'string' && obj.event !== '') {
|
||||
if (obj.eventname && typeof obj.eventname === 'string' && obj.eventname !== '') {
|
||||
sel = '';
|
||||
if (obj.body && typeof obj.body === 'object') {
|
||||
oSelector = fFindKeyStringPair(obj.body);
|
||||
|
|
@ -73,7 +73,7 @@
|
|||
sel = "&selkey=" + oSelector.key + "&selval=" + oSelector.val;
|
||||
}
|
||||
}
|
||||
url = 'forge?page=forge_rule&eventtype=custom&eventname=' + obj.event + sel;
|
||||
url = 'forge?page=forge_rule&eventtype=custom&eventname=' + obj.eventname + sel;
|
||||
return window.open(url, '_blank');
|
||||
} else {
|
||||
$('#info').text('Please provide a valid eventname');
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
// Generated by CoffeeScript 1.7.1
|
||||
(function() {
|
||||
var arrKV, arrParams, fDisplayError, fFailedRequest, fIssueRequest, fOnLoad, fPlaceAndPaintInterval, oParams, param, strPublicKey, _i, _len,
|
||||
var arrKV, arrParams, fDisplayError, fFailedRequest, fIssueRequest, fOnLoad, oParams, param, strPublicKey, _i, _len,
|
||||
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
|
||||
|
||||
arrParams = window.location.search.substring(1).split('&');
|
||||
|
|
@ -19,11 +19,6 @@
|
|||
|
||||
strPublicKey = '';
|
||||
|
||||
fPlaceAndPaintInterval = function() {
|
||||
$('#event_start').html('Start Time: <input id="input_start" type="text" /> <b>"hh:mm"</b>, default = 12:00');
|
||||
return $('#event_interval').html('Interval: <input id="input_interval" type="text" /> <b>"days hours:minutes"</b>, default = 10 minutes');
|
||||
};
|
||||
|
||||
fDisplayError = function(msg) {
|
||||
window.scrollTo(0, 0);
|
||||
$('#info').text("Error: " + msg);
|
||||
|
|
@ -68,12 +63,16 @@
|
|||
editor.setTheme("ace/theme/monokai");
|
||||
editor.getSession().setMode("ace/mode/json");
|
||||
editor.setShowPrintMargin(false);
|
||||
$('#input_id').focus();
|
||||
switch (oParams.eventtype) {
|
||||
case 'custom':
|
||||
$('#input_id').val("My '" + oParams.eventname + "' Rule");
|
||||
$('#select_event_type').val('Custom Event');
|
||||
inpEvt = $('<input>').attr('type', 'text').attr('id', 'input_eventname');
|
||||
inpEvt = $('<input>').attr('type', 'text').attr('style', 'font-size:1em').attr('id', 'input_eventname');
|
||||
inpEvt.val(oParams.eventname);
|
||||
$('#event_parameters').append(inpEvt);
|
||||
$('#event_parameters').append($('<h4>').text('Event Name : ').append(inpEvt));
|
||||
inpEvt.focus();
|
||||
editor.setValue("[\n\n]");
|
||||
break;
|
||||
case 'webhook':
|
||||
console.log('webhook');
|
||||
|
|
@ -85,8 +84,8 @@
|
|||
$('#event_parameters *').remove();
|
||||
switch ($(this).val()) {
|
||||
case 'Custom Event':
|
||||
inpEvt = $('<input>').attr('type', 'text').attr('id', 'input_eventname');
|
||||
return $('#event_parameters').append(inpEvt);
|
||||
inpEvt = $('<input>').attr('type', 'text').attr('style', 'font-size:1em').attr('id', 'input_eventname');
|
||||
return $('#event_parameters').append($('<h4>').text('Event Name : ').append(inpEvt));
|
||||
case 'Webhook':
|
||||
return fIssueRequest({
|
||||
body: {
|
||||
|
|
@ -146,7 +145,7 @@
|
|||
return console.error('ERROR: non-object received from server: ' + data.message);
|
||||
}
|
||||
},
|
||||
fail: fFailedRequest('Error fetching event poller')
|
||||
fail: fFailedRequest('Error fetching Event Poller')
|
||||
});
|
||||
}
|
||||
});
|
||||
|
|
@ -186,7 +185,7 @@
|
|||
})
|
||||
},
|
||||
done: fAddEventParams(arr[0]),
|
||||
fail: fFailedRequest('Error fetching event poller params')
|
||||
fail: fFailedRequest('Error fetching Event Poller params')
|
||||
});
|
||||
return fFetchEventFunctionArgs(arr);
|
||||
}
|
||||
|
|
@ -354,7 +353,7 @@
|
|||
_results.push(fAppendActions(module, actions));
|
||||
}
|
||||
return _results;
|
||||
}).fail(fFailedRequest('Error fetching event poller'));
|
||||
}).fail(fFailedRequest('Error fetching Event Poller'));
|
||||
fAddSelectedAction = function(name) {
|
||||
var arrEls, arrName, div, fDelayed, img, subdiv, table, td, tr, _ref;
|
||||
arrName = name.split(' -> ');
|
||||
|
|
@ -727,15 +726,15 @@
|
|||
command: 'forge_rule',
|
||||
body: JSON.stringify({
|
||||
id: $('#input_id').val(),
|
||||
event: eventId,
|
||||
event_params: ep,
|
||||
event_start: start,
|
||||
event_interval: mins,
|
||||
event_functions: evtFuncs,
|
||||
eventname: eventId,
|
||||
eventparams: ep,
|
||||
eventstart: start,
|
||||
eventinterval: mins,
|
||||
eventfunctions: evtFuncs,
|
||||
conditions: conds,
|
||||
actions: acts,
|
||||
action_params: ap,
|
||||
action_functions: actFuncs
|
||||
actionparams: ap,
|
||||
actionfunctions: actFuncs
|
||||
})
|
||||
};
|
||||
return $.post('/usercommand', obj).done(function(data) {
|
||||
|
|
@ -763,19 +762,19 @@
|
|||
oRule = JSON.parse(data.message);
|
||||
if (oRule) {
|
||||
$('#input_id').val(oRule.id);
|
||||
$('#select_event').val(oRule.event);
|
||||
$('#select_event').val(oRule.eventname);
|
||||
if ($('#select_event').val() !== '') {
|
||||
fFetchEventParams(oRule.event);
|
||||
fFetchEventParams(oRule.eventname);
|
||||
fPlaceAndPaintInterval();
|
||||
}
|
||||
$('#input_event').val(oRule.event);
|
||||
d = new Date(oRule.event_start);
|
||||
$('#input_event').val(oRule.eventname);
|
||||
d = new Date(oRule.eventstart);
|
||||
mins = d.getMinutes();
|
||||
if (mins.toString().length === 1) {
|
||||
mins = '0' + mins;
|
||||
}
|
||||
$('#input_start').val(d.getHours() + ':' + mins);
|
||||
$('#input_interval').val(oRule.event_interval);
|
||||
$('#input_interval').val(oRule.eventinterval);
|
||||
editor.setValue(JSON.stringify(oRule.conditions, void 0, 2));
|
||||
_ref = oRule.actions;
|
||||
_results = [];
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
// Generated by CoffeeScript 1.7.1
|
||||
(function() {
|
||||
var arrKV, arrParams, fDisplayError, fFailedRequest, fIssueRequest, fOnLoad, fUpdateWebhookList, oParams, param, _i, _len;
|
||||
var arrKV, arrParams, fDisplayError, fFailedRequest, fIssueRequest, fOnLoad, fProcessWebhookList, fUpdateWebhookList, hostUrl, oParams, param, _i, _len;
|
||||
|
||||
arrParams = window.location.search.substring(1).split('&');
|
||||
|
||||
|
|
@ -16,6 +16,8 @@
|
|||
oParams.id = decodeURIComponent(oParams.id);
|
||||
}
|
||||
|
||||
hostUrl = [location.protocol, '//', location.host].join('');
|
||||
|
||||
fDisplayError = function(msg) {
|
||||
window.scrollTo(0, 0);
|
||||
$('#info').text("Error: " + msg);
|
||||
|
|
@ -37,18 +39,31 @@
|
|||
};
|
||||
};
|
||||
|
||||
fUpdateWebhookList = function(data) {
|
||||
var img, inp, modName, oHooks, tr, _results;
|
||||
fUpdateWebhookList = function() {
|
||||
return fIssueRequest({
|
||||
body: {
|
||||
command: 'get_all_webhooks'
|
||||
},
|
||||
done: fProcessWebhookList,
|
||||
fail: fFailedRequest('Unable to get Webhook list')
|
||||
});
|
||||
};
|
||||
|
||||
fProcessWebhookList = function(data) {
|
||||
var hookid, hookname, img, oHooks, tdName, tdUrl, tr, _results;
|
||||
$('#table_webhooks tr').remove();
|
||||
oHooks = JSON.parse(data.message);
|
||||
console.log(oHooks);
|
||||
console.log(hostUrl);
|
||||
_results = [];
|
||||
for (modName in oHooks) {
|
||||
for (hookid in oHooks) {
|
||||
hookname = oHooks[hookid];
|
||||
tr = $('<tr>');
|
||||
inp = $('<div>').text(modName);
|
||||
tdName = $('<div>').text(hookname);
|
||||
tdUrl = $('<input>').attr('style', 'width:600px').val("" + hostUrl + "/webhooks/" + hookid);
|
||||
img = $('<img>').attr('class', 'del').attr('title', 'Delete Module').attr('src', 'red_cross_small.png');
|
||||
tr.append($('<td>').append(img));
|
||||
tr.append($('<td>').append(inp));
|
||||
tr.append($('<td>').attr('style', 'padding-left:10px').append(tdName));
|
||||
tr.append($('<td>').attr('style', 'padding-left:10px').append(tdUrl));
|
||||
_results.push($('#table_webhooks').append(tr));
|
||||
}
|
||||
return _results;
|
||||
|
|
@ -56,19 +71,13 @@
|
|||
|
||||
fOnLoad = function() {
|
||||
document.title = 'Create Webhooks!';
|
||||
fIssueRequest({
|
||||
body: {
|
||||
command: 'get_all_webhooks'
|
||||
},
|
||||
done: fUpdateWebhookList,
|
||||
fail: fFailedRequest('Unable to post get_all_webhooks request')
|
||||
});
|
||||
return $('#but_submit').click(function() {
|
||||
fUpdateWebhookList();
|
||||
$('#but_submit').click(function() {
|
||||
var hookname;
|
||||
$('#info').text('');
|
||||
hookname = $('#inp_hookname').val();
|
||||
if (hookname === '') {
|
||||
return fDisplayError('Please provide a hookname!');
|
||||
return fDisplayError('Please provide an Event Name for your new Webhook!');
|
||||
} else {
|
||||
$('#display_hookurl *').remove();
|
||||
return fIssueRequest({
|
||||
|
|
@ -79,14 +88,19 @@
|
|||
})
|
||||
},
|
||||
done: function(data) {
|
||||
var div, h3, inp, oAnsw, url;
|
||||
var b, div, inp, oAnsw;
|
||||
oAnsw = JSON.parse(data.message);
|
||||
url = [location.protocol, '//', location.host].join('');
|
||||
h3 = $('<h3>').text("This is the Webhook Url you should use:");
|
||||
inp = $('<input>').attr('type', 'text').attr('style', 'font-size:1em').val("" + url + "/webhooks/" + oAnsw.hookid);
|
||||
h3.append(inp);
|
||||
$('#display_hookurl').append(h3);
|
||||
div = $('<div>').html("Push your location via <a href=\"" + url + "/mobile/index.html?hookid=" + oAnsw.hookid + "\">this page</a>");
|
||||
b = $('<b>').text("This is the Webhook Url you will use for your Event : ");
|
||||
$('#display_hookurl').append(b);
|
||||
$('#display_hookurl').append($('<br>'));
|
||||
inp = $('<input>').attr('type', 'text').attr('style', 'width:600px').val("" + hostUrl + "/webhooks/" + oAnsw.hookid);
|
||||
$('#display_hookurl').append(inp);
|
||||
$('#display_hookurl').append($('<br>'));
|
||||
div = $('<div>');
|
||||
div.append($('<br>'));
|
||||
div.append($('<div>').html("1. Try it out and push your location to your new webhook via <a target=\"_blank\" href=\"" + hostUrl + "/mobile.html?hookid=" + oAnsw.hookid + "\">this page</a>."));
|
||||
div.append($('<br>'));
|
||||
div.append($('<div>').html("2. Then you should setup <a target=\"_blank\" href=\"forge?page=forge_rule&eventtype=webhook&hookname=" + hookname + "\">a Rule for this Event!</a>"));
|
||||
return $('#display_hookurl').append(div);
|
||||
},
|
||||
fail: function(err) {
|
||||
|
|
@ -99,6 +113,30 @@
|
|||
});
|
||||
}
|
||||
});
|
||||
return $('#table_webhooks').on('click', 'img', function() {
|
||||
var arrUrl, url;
|
||||
if (confirm("Do you really want to delete this webhook?")) {
|
||||
url = $('input', $(this).closest('tr')).val();
|
||||
arrUrl = url.split('/');
|
||||
return fIssueRequest({
|
||||
body: {
|
||||
command: 'delete_webhook',
|
||||
body: JSON.stringify({
|
||||
hookid: arrUrl[arrUrl.length - 1]
|
||||
})
|
||||
},
|
||||
done: function(data) {
|
||||
$('#info').text(data.message);
|
||||
$('#info').attr('class', 'success');
|
||||
return fUpdateWebhookList();
|
||||
},
|
||||
fail: function(err) {
|
||||
fFailedRequest('Unable to delete Webhook!')(err);
|
||||
return fUpdateWebhookList();
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
window.addEventListener('load', fOnLoad, true);
|
||||
|
|
|
|||
|
|
@ -1,14 +1,15 @@
|
|||
|
||||
<div style="width:100%;float:left;">
|
||||
<div style="width:100%;float:left">
|
||||
|
||||
<div id="editor">
|
||||
{
|
||||
"event": "mail",
|
||||
"eventname": "button-click",
|
||||
"body": {
|
||||
"subject": "hello"
|
||||
"subject": "hello world"
|
||||
}
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
<button id="but_prepare">Prepare a Rule for this Event</button>
|
||||
<button id="but_prepare" style="width:200px">Prepare a Rule for this Event</button>
|
||||
<br>
|
||||
<button id="but_submit">Push Event into System</button>
|
||||
<button id="but_submit" style="width:200px">Push Event into System</button>
|
||||
|
|
|
|||
|
|
@ -1,19 +1,23 @@
|
|||
<h2>Rule Name: <input id="input_id" type="text" /></h2>
|
||||
<h2>Rule Name : <input id="input_id" type="text" value="My new Rule"></h2>
|
||||
|
||||
<h2>EVENT</h2>
|
||||
<b>Event Type:</b>
|
||||
<select id="select_event_type">
|
||||
<option></option>
|
||||
<option>Custom Event</option>
|
||||
<option>Webhook</option>
|
||||
<option>Event Poller</option>
|
||||
</select>
|
||||
<div id="event_type">
|
||||
<h4>Event Type :
|
||||
<select id="select_event_type">
|
||||
<option></option>
|
||||
<option>Custom Event</option>
|
||||
<option>Webhook</option>
|
||||
<option>Event Poller</option>
|
||||
</select>
|
||||
</h4>
|
||||
</div>
|
||||
<div id="event_parameters"></div>
|
||||
|
||||
<h2>CONDITIONS</h2>
|
||||
Refer to <a target="_blank" href="https://github.com/harthur/js-select#selectors">js-select selectors</a> for valid selectors! ( only the first matching element will be returned )
|
||||
<table>
|
||||
<tr><td>
|
||||
<h2>CONDITIONS</h2>
|
||||
<div id="conditions">
|
||||
Refer to <a target="_blank" href="https://github.com/harthur/js-select#selectors">js-select selectors</a> for valid selectors!
|
||||
<div id="editor_conditions">
|
||||
[
|
||||
{
|
||||
|
|
@ -24,6 +28,7 @@ Refer to <a target="_blank" href="https://github.com/harthur/js-select#selectors
|
|||
}
|
||||
]
|
||||
</div>
|
||||
</div>
|
||||
</td></tr>
|
||||
<tr><td>
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
<!DOCTYPE HTML>
|
||||
<html>
|
||||
<head>
|
||||
<title></title>
|
||||
<title>WebAPI ECA Engine</title>
|
||||
<link href='http://fonts.googleapis.com/css?family=Roboto:300' rel='stylesheet' type='text/css'>
|
||||
<link href='http://fonts.googleapis.com/css?family=Nunito' rel='stylesheet' type='text/css'>
|
||||
<link rel="stylesheet" type="text/css" href="style.css">
|
||||
|
|
@ -24,24 +24,27 @@
|
|||
menubar.append(link);
|
||||
};
|
||||
|
||||
fCreateLink( 'Forge Event Poller',
|
||||
fRedirect( 'forge?page=forge_module&type=event_poller' )
|
||||
);
|
||||
fCreateLink( 'Forge Action Invoker',
|
||||
fRedirect( 'forge?page=forge_module&type=action_invoker' )
|
||||
);
|
||||
fCreateLink( 'Forge Rule',
|
||||
fRedirect( 'forge?page=forge_rule' )
|
||||
);
|
||||
fCreateLink( 'Invoke Event',
|
||||
fCreateLink( 'Push Event',
|
||||
fRedirect( 'forge?page=forge_event' )
|
||||
);
|
||||
fCreateLink( 'Edit Modules',
|
||||
fRedirect( 'forge?page=edit_modules' )
|
||||
fCreateLink( 'Create Rule',
|
||||
fRedirect( 'forge?page=forge_rule' )
|
||||
);
|
||||
fCreateLink( 'Create Webhooks',
|
||||
fRedirect( 'forge?page=forge_webhook' )
|
||||
);
|
||||
fCreateLink( 'Edit Rules',
|
||||
fRedirect( 'forge?page=edit_rules' )
|
||||
);
|
||||
fCreateLink( 'Create EP',
|
||||
fRedirect( 'forge?page=forge_module&type=event_poller' )
|
||||
);
|
||||
fCreateLink( 'Create AI',
|
||||
fRedirect( 'forge?page=forge_module&type=action_invoker' )
|
||||
);
|
||||
fCreateLink( 'Edit EPs & AIs',
|
||||
fRedirect( 'forge?page=edit_modules' )
|
||||
);
|
||||
fCreateLink( 'Logout', function() {
|
||||
$.post( '/logout' ).done( fRedirect( document.URL ) );
|
||||
});
|
||||
|
|
|
|||
|
|
@ -2,9 +2,11 @@
|
|||
<head>
|
||||
<title>Mobile Page</title>
|
||||
<script src='//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js' type='text/javascript'></script>
|
||||
<link href='http://fonts.googleapis.com/css?family=Roboto:300' rel='stylesheet' type='text/css'>
|
||||
<link href='http://fonts.googleapis.com/css?family=Nunito' rel='stylesheet' type='text/css'>
|
||||
<link rel="stylesheet" type="text/css" href="style.css">
|
||||
</head>
|
||||
<body>
|
||||
<body id="mobile">
|
||||
<h1>Mobile Page</h1>
|
||||
<script>
|
||||
|
||||
|
|
@ -28,21 +30,30 @@
|
|||
|
||||
document.getElementById("mapholder").innerHTML="<img src='"+img_url+"'>";
|
||||
var fPostPosition = function() {
|
||||
var url = '../event';
|
||||
if( hookid ) url = '../webhooks/' + hookid
|
||||
$.post( url, JSON.stringify({
|
||||
event: 'geoposition',
|
||||
latitude: lat,
|
||||
longitude: lon
|
||||
}))
|
||||
.done(function(data) {
|
||||
$('#info').text("Sent event to engine: " + data.message);
|
||||
})
|
||||
.fail(function(err) {
|
||||
$('#info').text("Error: " + err);
|
||||
});
|
||||
// Post position every 5 mins
|
||||
setTimeout( fPostPosition, 5 * 60 * 1000 );
|
||||
if( !hookid ) $('#info').text("No Webhook defined!");
|
||||
else {
|
||||
$.post( '../webhooks/' + hookid, JSON.stringify({
|
||||
timestamp: (new Date()).toISOString(),
|
||||
datatype: 'geoposition',
|
||||
latitude: lat,
|
||||
longitude: lon
|
||||
}))
|
||||
.done( function( data ) {
|
||||
dat = JSON.parse( data );
|
||||
$( '#info' ).append( $( '<div>' ).text( dat.message ) );
|
||||
$( '#info' ).append( $( '<div>' ).text( '{' ) );
|
||||
for( prop in dat.evt ) {
|
||||
$( '#info' ).append( $( '<div>' ).html( " "
|
||||
+ prop + ': ' + JSON.stringify(dat.evt[ prop ]) ) );
|
||||
}
|
||||
$( '#info' ).append( $( '<div>' ).text( '}' ) );
|
||||
})
|
||||
.fail(function(err) {
|
||||
$('#info').text("Error: " + err.responseText);
|
||||
});
|
||||
// Post position every 5 mins
|
||||
setTimeout( fPostPosition, 5 * 60 * 1000 );
|
||||
}
|
||||
}
|
||||
fPostPosition()
|
||||
}
|
||||
|
|
@ -62,6 +73,7 @@
|
|||
</script>
|
||||
<div id="print"></div>
|
||||
<div id="mapholder"></div>
|
||||
<br>
|
||||
<div id="info"></div>
|
||||
</body>
|
||||
</html>
|
||||
|
|
@ -1,4 +0,0 @@
|
|||
|
||||
body {
|
||||
font-family: sans-serif, "Times New Roman", Georgia, Serif;
|
||||
}
|
||||
|
|
@ -6,6 +6,11 @@ body {
|
|||
background-color: #EEF;
|
||||
}
|
||||
|
||||
h4 {
|
||||
margin-top: 5px;
|
||||
margin-bottom: 5px;
|
||||
}
|
||||
|
||||
input[type=text],
|
||||
input[type=password] {
|
||||
-webkit-transition: all 0.30s ease-in-out;
|
||||
|
|
@ -26,6 +31,10 @@ input[type=password]:focus {
|
|||
border: 1px solid rgba(81, 203, 238, 1);
|
||||
}
|
||||
|
||||
#mobile {
|
||||
margin: 10px 20px 10px 20px;
|
||||
}
|
||||
|
||||
#menubar {
|
||||
/*font-size: 0.85em;*/
|
||||
font-weight: bold;
|
||||
|
|
@ -83,6 +92,17 @@ input[type=password]:focus {
|
|||
padding-bottom: 5px;
|
||||
}
|
||||
|
||||
#select_event_type {
|
||||
font-size: 1em;
|
||||
}
|
||||
|
||||
#event_type,
|
||||
#event_parameters,
|
||||
#conditions,
|
||||
#action_parameters {
|
||||
margin-left: 20px;
|
||||
}
|
||||
|
||||
#input_id {
|
||||
font-size: 1em;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue