diff --git a/coffee/dynamic-modules.coffee b/coffee/dynamic-modules.coffee index 9bffcae..72222fe 100644 --- a/coffee/dynamic-modules.coffee +++ b/coffee/dynamic-modules.coffee @@ -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! diff --git a/coffee/persistence.coffee b/coffee/persistence.coffee index 49380b1..49ca289 100644 --- a/coffee/persistence.coffee +++ b/coffee/persistence.coffee @@ -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 diff --git a/coffee/request-handler.coffee b/coffee/request-handler.coffee index 3f0d1b5..af811d7 100644 --- a/coffee/request-handler.coffee +++ b/coffee/request-handler.coffee @@ -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 diff --git a/examples/action-invokers/imbroadcaster.coffee b/examples/action-invokers/imbroadcaster.coffee index 88e2acc..312ba13 100644 --- a/examples/action-invokers/imbroadcaster.coffee +++ b/examples/action-invokers/imbroadcaster.coffee @@ -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