diff --git a/acs.coffee b/acs.coffee index 4c85dd8..2168feb 100644 --- a/acs.coffee +++ b/acs.coffee @@ -26,11 +26,34 @@ db.open( (err, db) -> ) -updateDevice = (deviceId, actions) -> - return if not actions? +updateDevice = (deviceId, actions, callback) -> + if not actions? + callback() if callback? + return + + updates = {} if actions.parameterValues? - params = common.arrayToHash(actions.parameterValues) - devicesCollection.update({'_id' : deviceId}, {'$set' : params}, {upsert: true}) + for p in actions.parameterValues + if common.endsWith(p[0], '.') + updates["#{p[0]}_value"] = p[1] + else + updates["#{p[0]}._value"] = p[1] + + if actions.parameterNames? + for p in actions.parameterNames + if common.endsWith(p[0], '.') + updates["#{p[0]}_writable"] = p[1] + updates["#{p[0]}_object"] = true + else + updates["#{p[0]}._writable"] = p[1] + + if Object.keys(updates).length > 0 + devicesCollection.update({'_id' : deviceId}, {'$set' : updates}, {upsert: true, safe: true}, (err) -> + callback(err) if callback? + ) + else + callback() if callback? + runTask = (sessionId, deviceId, task, reqParams, response) -> resParams = {} @@ -115,7 +138,7 @@ else util.log("#{deviceId}: Added init task #{task._id}") ) - devicesCollection.update({'_id' : deviceId}, {'$set' : common.arrayToHash(reqParams.informParameterValues)}, {upsert: true, safe:true}, (err, modified) -> + updateDevice(deviceId, {'parameterValues' : reqParams.informParameterValues}, (err) -> tr069.response(reqParams.sessionId, response, resParams, cookies) ) ) diff --git a/common.coffee b/common.coffee index 093d3b5..2ad4b22 100644 --- a/common.coffee +++ b/common.coffee @@ -16,3 +16,7 @@ exports.getDeviceId = (deviceIdStruct) -> return "#{escape(deviceIdStruct['OUI'])}-#{escape(deviceIdStruct['SerialNumber'])}" +exports.extend = (obj, mixin) -> + obj[name] = method for name, method of mixin + obj + diff --git a/tasks.coffee b/tasks.coffee index e6e90da..a6a212f 100644 --- a/tasks.coffee +++ b/tasks.coffee @@ -2,24 +2,23 @@ common = require './common' BATCH_SIZE = 16 this.init = (task, request, response) -> + ret = {} if not task.subtask? task.subtask = {'name' : 'getParameterNames', 'parameterPath' : '', 'nextLevel' : false} if task.subtask.name == 'getParameterNames' - ret = this.getParameterNames(task.subtask, request, response) - if ret? and ret.parameterNames? # task finished + common.extend(ret, this.getParameterNames(task.subtask, request, response)) + if ret.parameterNames? # task finished parameterNames = [] for p in ret.parameterNames if not common.endsWith(p[0], '.') parameterNames.push(p[0]) task.subtask = {'name' : 'getParameterValues', 'parameterNames' : parameterNames} - else - return if task.subtask.name == 'getParameterValues' - return this.getParameterValues(task.subtask, request, response) + common.extend(ret, this.getParameterValues(task.subtask, request, response)) - throw 'Unspecified error' + ret this.getParameterNames = (task, request, response) -> if request.getParameterNamesResponse?