Support for adding/deleting tags in presets

This commit is contained in:
Zaid Abdulla committed 2013-06-01 19:40:36 +03:00
1 parent 913388ab34
commit 91e9e47a65
1 file changed
+33 -3
+33 -3
View File
@@ -3,6 +3,7 @@ common = require './common'
db = require './db' db = require './db'
mongoQuery = require './mongo-query' mongoQuery = require './mongo-query'
query = require './query' query = require './query'
log = require('util').log
getPresets = (callback) -> getPresets = (callback) ->
@@ -54,6 +55,10 @@ exports.assertPresets = (deviceId, presetsHash, callback) ->
projection[c.name] = 1 projection[c.name] = 1
when 'firmware' when 'firmware'
projection['InternetGatewayDevice.DeviceInfo.SoftwareVersion'] = 1 projection['InternetGatewayDevice.DeviceInfo.SoftwareVersion'] = 1
when 'add_tag', 'delete_tag'
projection['_tags'] = 1
when 'add_object', 'delete_object'
projection[c.name] = 1
else else
throw new Error('Unknown configuration type') throw new Error('Unknown configuration type')
@@ -69,27 +74,44 @@ exports.assertPresets = (deviceId, presetsHash, callback) ->
expiry = config.PRESETS_CACHE_DURATION expiry = config.PRESETS_CACHE_DURATION
getParameterValues = [] getParameterValues = []
setParameterValues = [] setParameterValues = []
add_tags = []
delete_tags = []
for c in configurations for c in configurations
param = common.getParamValueFromPath(device, c.name) param = if c.name? then common.getParamValueFromPath(device, c.name) else undefined
continue if not param? # ignore parameters that don't exist
switch c.type switch c.type
when 'value' when 'value'
continue if not param? # ignore parameters that don't exist
dst = common.matchType(param._value, c.value) dst = common.matchType(param._value, c.value)
if param._value != dst if param._value != dst
setParameterValues.push([c.name, dst, param._type]) setParameterValues.push([c.name, dst, param._type])
when 'age' when 'age'
continue if not param? # ignore parameters that don't exist
timeDiff = (now - param._timestamp) / 1000 timeDiff = (now - param._timestamp) / 1000
if (c.age - timeDiff < config.PRESETS_TIME_PADDING) if (c.age - timeDiff < config.PRESETS_TIME_PADDING)
expiry = Math.min(expiry, c.age) expiry = Math.min(expiry, c.age)
getParameterValues.push(c.name) getParameterValues.push(c.name)
else else
expiry = Math.min(expiry, c.age - timeDiff) expiry = Math.min(expiry, c.age - timeDiff)
when 'add_tag'
add_tags.push(c.tag) if not device['_tags']? or c.tag not in device['_tags']
when 'delete_tag'
delete_tags.push(c.tag) if device['_tags']? and c.tag in device['_tags']
else else
throw new Error('Unknown configuration type') throw new Error('Unknown configuration type')
if add_tags.length + delete_tags.length > 0
log("#{deviceId}: Updating tags")
if delete_tags.length > 0
db.devicesCollection.update({'_id' : deviceId}, {'$pull' : {'_tags' : {'$in' : delete_tags}}}, {safe : false})
if add_tags.length > 0
db.devicesCollection.update({'_id' : deviceId}, {'$addToSet' : {'_tags' : {'$each' : add_tags}}}, {safe : false})
if getParameterValues.length if getParameterValues.length
taskList.push {device : deviceId, name : 'getParameterValues', parameterNames: getParameterValues, timestamp : new Date()} taskList.push {device : deviceId, name : 'getParameterValues', parameterNames: getParameterValues, timestamp : new Date()}
if setParameterValues.length if setParameterValues.length
taskList.push {device : deviceId, name : 'setParameterValues', parameterValues: setParameterValues, timestamp : new Date()} taskList.push {device : deviceId, name : 'setParameterValues', parameterValues: setParameterValues, timestamp : new Date()}
@@ -110,7 +132,15 @@ accumulateConfigurations = (presets) ->
configurations = {} configurations = {}
for p in presets for p in presets
for c in p.configurations for c in p.configurations
configurationHash = if c.name? then "#{c.type}_#{c.name}" else c.type configurationHash = switch c.type
when 'add_tag', 'delete_tag'
"tag_#{c.tag}"
when 'add_object', 'delete_object'
"object_#{c.object}_#{c.object}"
when 'firmware'
'firmware'
else
"#{c.type}_#{c.name}"
if not maxWeights[configurationHash]? or p.weight > maxWeights[configurationHash] if not maxWeights[configurationHash]? or p.weight > maxWeights[configurationHash]
configurations[configurationHash] = c configurations[configurationHash] = c