diff --git a/lib/cwmp.coffee b/lib/cwmp.coffee index e5ef607..27e101c 100644 --- a/lib/cwmp.coffee +++ b/lib/cwmp.coffee @@ -64,8 +64,7 @@ updateDevice = (currentRequest, actions, callback) -> return now = new Date(Date.now()) - updates = {} - deletes = {} + update = {'$set' : {}, '$unset' : {}} if actions.parameterValues? for p in actions.parameterValues @@ -74,36 +73,36 @@ updateDevice = (currentRequest, actions, callback) -> path = if common.endsWith(p[0], '.') then p[0] else "#{p[0]}." if v == origValue - deletes["#{path}_orig"] = 1 + update['$unset']["#{path}_orig"] = 1 else - updates["#{path}_orig"] = origValue - updates["#{path}_value"] = v - updates["#{path}_timestamp"] = now + update['$set']["#{path}_orig"] = origValue + update['$set']["#{path}_value"] = v + update['$set']["#{path}_timestamp"] = now if p[2]? - updates["#{path}_type"] = p[2] + update['$set']["#{path}_type"] = p[2] else - deletes["#{path}_type"] = 1 + update['$unset']["#{path}_type"] = 1 if actions.deletedObjects? for p in actions.deletedObjects - deletes[p] = 1 + update['$unset'][p] = 1 if actions.instanceName? for i in actions.instanceName - updates["#{i[0]}._name"] = i[1] + update['$set']["#{i[0]}._name"] = i[1] if actions.parameterNames? for p in actions.parameterNames if OBJECT_REGEX.test(p[0]) path = p[0] if INSTANCE_REGEX.test(p[0]) - updates["#{path}_instance"] = true + update['$set']["#{path}_instance"] = true else - updates["#{path}_object"] = true + update['$set']["#{path}_object"] = true else path = "#{p[0]}." - updates["#{path}_writable"] = p[1] if p[1]? - updates["#{path}_timestamp"] = now + update['$set']["#{path}_writable"] = p[1] if p[1]? + update['$set']["#{path}_timestamp"] = now # Add parameter (sans any dot suffix) if has an alias if (a = parameters.getAlias(pp = path.slice(0, -1)))? @@ -113,14 +112,17 @@ updateDevice = (currentRequest, actions, callback) -> if actions.customCommands? for p in actions.customCommands commandName = p[0] - updates["_customCommands.#{commandName}._value"] = p[1] - updates["_customCommands.#{commandName}._timestamp"] = now + update['$set']["_customCommands.#{commandName}._value"] = p[1] + update['$set']["_customCommands.#{commandName}._timestamp"] = now if actions.set? - common.extend(updates, actions.set) + common.extend(update['$set'], actions.set) - if Object.keys(updates).length > 0 or Object.keys(deletes).length > 0 - db.devicesCollection.update({'_id' : currentRequest.deviceId}, {'$set' : updates, '$unset' : deletes}, {}, (err, count) -> + for k of update + delete update[k] if Object.keys(update[k]).length == 0 + + if Object.keys(update).length + db.devicesCollection.update({'_id' : currentRequest.deviceId}, update, {}, (err, count) -> throw err if err # Clear aliases cache if there's a new aliased parameter that has not been included in aliases cache if newAliases? diff --git a/lib/presets.coffee b/lib/presets.coffee index 1440451..7b1a5a0 100644 --- a/lib/presets.coffee +++ b/lib/presets.coffee @@ -28,7 +28,11 @@ exports.getDevicePreset = (deviceId, presets, objects, aliases, callback) -> # only fetch relevant params projection = {_id : 1} for p in presets - p.precondition = query.expand(p.precondition, aliases) + if common.typeOf(p.precondition) is common.STRING_TYPE + p.precondition = query.expand(JSON.parse(p.precondition), aliases) + else + # Accept an object for backward compatiblity + p.precondition = query.expand(p.precondition ? {}, aliases) mongoQuery.projection(p.precondition, projection) for c in p.configurations diff --git a/package.json b/package.json index 271841c..cb38d97 100644 --- a/package.json +++ b/package.json @@ -20,10 +20,10 @@ "bin": "bin" }, "dependencies": { - "libxmljs": "~0.8.1", - "redis": "~0.10.0", - "hiredis": "~0.1.15", - "mongodb": "~1.3.23" + "libxmljs": "~0.9.0", + "redis": "~0.10.1", + "hiredis": "~0.1.16", + "mongodb": "~1.4.4" }, "devDependencies": { "coffee-script": "~1.7.1"