mirror of
https://github.com/Hopiu/webapi-eca.git
synced 2026-03-16 22:10:31 +00:00
need to refactor rules storage in the next update to associate them to users
This commit is contained in:
parent
2289e89507
commit
f716a26544
4 changed files with 68 additions and 19 deletions
|
|
@ -99,8 +99,24 @@ fPushEvent = ( userId, oRule, modType ) ->
|
|||
eventname: oRule.eventname + '_created:' + oRule.timestamp
|
||||
body: obj
|
||||
|
||||
else
|
||||
db.pushEvent obj
|
||||
fSetVar = ( userId, ruleId, modId ) ->
|
||||
( field, data ) ->
|
||||
db.persistSetVar userId, ruleId, modId, field, JSON.stringify data
|
||||
|
||||
fGetVar = ( userId, ruleId, modId ) ->
|
||||
( field, cb ) ->
|
||||
fObectify = ( cb ) ->
|
||||
( err, str ) ->
|
||||
if err
|
||||
cb err
|
||||
else
|
||||
try
|
||||
cb null, JSON.parse str
|
||||
catch
|
||||
cb err
|
||||
|
||||
db.persistGetVar userId, ruleId, modId, field, fObectify cb
|
||||
|
||||
|
||||
fTryToLoadModule = ( userId, oRule, modId, src, modType, dbMod, params, cb ) =>
|
||||
if not params
|
||||
|
|
@ -126,6 +142,9 @@ fTryToLoadModule = ( userId, oRule, modId, src, modType, dbMod, params, cb ) =>
|
|||
exports: {}
|
||||
setTimeout: setTimeout # This one allows probably too much
|
||||
pushEvent: fPushEvent userId, oRule, modType
|
||||
# TODO garbage collect entries below if rule is deleted...
|
||||
setVar: fSetVar userId, oRule.id, modId
|
||||
getVar: fGetVar userId, oRule.id, modId
|
||||
|
||||
|
||||
#TODO child_process to run module!
|
||||
|
|
|
|||
|
|
@ -376,10 +376,39 @@ class IndexedModules
|
|||
###
|
||||
|
||||
|
||||
###
|
||||
Stores data for a module in a rule. This is used to allow persistance for moduöes in rules.
|
||||
|
||||
@public log( *userId, ruleId, moduleId, field, data* )
|
||||
@param {String} userId
|
||||
@param {String} ruleId
|
||||
@param {String} moduleId
|
||||
@param {String} field
|
||||
@param {String} data
|
||||
###
|
||||
exports.persistSetVar = ( userId, ruleId, moduleId, field, data ) =>
|
||||
@db.hmset "rulepersistence:#{ userId }:#{ ruleId }:#{ moduleId }", field, data,
|
||||
replyHandler "hmset 'rulepersistence:#{ userId }:#{ ruleId }:#{ moduleId }' -> #{ field } = [data]"
|
||||
|
||||
|
||||
###
|
||||
Gets data for a module in a rule.
|
||||
|
||||
@public log( *userId, ruleId, moduleId, field, cb* )
|
||||
@param {String} userId
|
||||
@param {String} ruleId
|
||||
@param {String} moduleId
|
||||
@param {String} field
|
||||
@param {function} cb
|
||||
###
|
||||
exports.persistGetVar = ( userId, ruleId, moduleId, field, cb ) =>
|
||||
@db.hget "rulepersistence:#{ userId }:#{ ruleId }:#{ moduleId }", field, cb
|
||||
|
||||
|
||||
###
|
||||
Appends a log entry.
|
||||
|
||||
@public log( *userId, ruleId, message* )
|
||||
@public log( *userId, ruleId, moduleId, message* )
|
||||
@param {String} userId
|
||||
@param {String} ruleId
|
||||
@param {String} message
|
||||
|
|
|
|||
|
|
@ -381,20 +381,20 @@ parsePushAndAnswerEvent = ( eventname, username, body, resp ) ->
|
|||
obj
|
||||
|
||||
|
||||
###
|
||||
Handles measurement posts
|
||||
###
|
||||
exports.handleMeasurements = ( req, resp ) =>
|
||||
body = ''
|
||||
req.on 'data', ( data ) ->
|
||||
body += data
|
||||
# ###
|
||||
# Handles measurement posts
|
||||
# ###
|
||||
# exports.handleMeasurements = ( req, resp ) =>
|
||||
# body = ''
|
||||
# req.on 'data', ( data ) ->
|
||||
# body += data
|
||||
|
||||
req.on 'end', ->
|
||||
obj = parsePushAndAnswerEvent name, null, body, resp
|
||||
if obj.eventname is 'uptimestatistics'
|
||||
# This is a hack to quickly allow storing of public accessible data
|
||||
fPath = path.resolve __dirname, '..', 'webpages', 'public', 'data', 'histochart.json'
|
||||
fs.writeFile fPath, JSON.stringify( obj, undefined, 2 ), 'utf8'
|
||||
# req.on 'end', ->
|
||||
# obj = parsePushAndAnswerEvent name, null, body, resp
|
||||
# if obj.eventname is 'uptimestatistics'
|
||||
# # This is a hack to quickly allow storing of public accessible data
|
||||
# fPath = path.resolve __dirname, '..', 'webpages', 'public', 'data', 'histochart.json'
|
||||
# fs.writeFile fPath, JSON.stringify( obj, undefined, 2 ), 'utf8'
|
||||
|
||||
###
|
||||
Handles webhook posts
|
||||
|
|
|
|||
|
|
@ -9,12 +9,13 @@ broadcast = ( msg ) ->
|
|||
json: true
|
||||
jar: true
|
||||
url: hook
|
||||
body: msg
|
||||
body:
|
||||
message: msg
|
||||
request options, ( err, resp, body ) ->
|
||||
if err or resp.statusCode isnt 200
|
||||
log "Error in pushing data to webhook '#{ url }'!"
|
||||
log "Error in pushing data to webhook '#{ hook }'!"
|
||||
else
|
||||
log "Successfully posted data to '#{ url }'"
|
||||
log "Successfully posted data to '#{ hook }'"
|
||||
|
||||
exports.message = ( msg ) ->
|
||||
if msg.webhook
|
||||
|
|
|
|||
Loading…
Reference in a new issue