Do not store available custom commands in device doc, find them from config. No longer having to reinitialize the device after adding a custom command.

This commit is contained in:
Zaid Abdulla committed 2013-09-13 13:02:44 +03:00
1 parent 86ff19a6e2
commit 9537bfad0f
4 files changed
+26 -14

No files matched your search

-1
View File
@@ -107,7 +107,6 @@ updateDevice = (currentRequest, actions, callback) ->
commandName = p[0]
updates["_customCommands.#{commandName}._value"] = p[1]
updates["_customCommands.#{commandName}._timestamp"] = now
updates["_customCommands.#{commandName}._commands"] = p[2] if p[2]?
if Object.keys(updates).length > 0 or Object.keys(deletes).length > 0
db.devicesCollection.update({'_id' : currentRequest.deviceId}, {'$set' : updates, '$unset' : deletes}, {}, (err, count) ->
@@ -1,5 +1,7 @@
config = require './config'
common = require './common'
exports.execute = (deviceId, command, callback) ->
args = command.split(/\s+/)
f = args.shift()
@@ -9,9 +11,17 @@ exports.execute = (deviceId, command, callback) ->
file[c](deviceId, args.join(' '), callback)
exports.getCommands = (filename) ->
exports.getFileCommands = (filename) ->
commands = []
f = require("./config/custom_commands/#{filename}")
for k,v of f
commands.push(k)
return commands
exports.getDeviceCustomCommands = (deviceId) ->
commands = {}
for k,v of config.CUSTOM_COMMANDS
if eval(v).test(deviceId)
commands[k] = exports.getFileCommands(k)
return commands
+11 -8
View File
@@ -4,6 +4,7 @@ db = require './db'
mongoQuery = require './mongo-query'
query = require './query'
log = require('util').log
customCommands = require './custom-commands'
getPresets = (callback) ->
@@ -65,6 +66,7 @@ matchObject = (object, param) ->
exports.assertPresets = (deviceId, presetsHash, callback) ->
getPresets((presets, objects) ->
deviceCustomCommands = customCommands.getDeviceCustomCommands(deviceId)
# only fetch relevant params
projection = {_id : 1}
for p in presets
@@ -76,8 +78,9 @@ exports.assertPresets = (deviceId, presetsHash, callback) ->
when 'value', 'age'
projection[c.name] = 1
when 'custom_command_value', 'custom_command_age'
# TODO only get individual commands
projection['_customCommands'] = 1
commandfile = c.command.split(' ', 1)
if commandfile of deviceCustomCommands
projection["_customCommands.#{c.command.split(' ', 1)}"] = 1
when 'firmware'
projection['InternetGatewayDevice.DeviceInfo.SoftwareVersion'] = 1
when 'add_tag', 'delete_tag'
@@ -125,16 +128,16 @@ exports.assertPresets = (deviceId, presetsHash, callback) ->
expiry = Math.min(expiry, c.age - timeDiff)
when 'custom_command_value'
[filename, commandName] = c.command.split(' ', 2)
command = common.getParamValueFromPath(device, "_customCommands.#{filename}")
continue if not command? or commandName not in command._commands
dst = common.matchType(command._value, c.value)
if command._value != dst
continue if not deviceCustomCommands[filename]? or commandName not in deviceCustomCommands[filename]
currentValue = try common.getParamValueFromPath(device, "_customCommands.#{filename}")._value catch then undefined
dst = common.matchType(currentValue, c.value)
if currentValue != dst
taskList.push({device : deviceId, name : 'customCommand', command : c.command})
when 'custom_command_age'
[filename, commandName] = c.command.split(' ', 2)
continue if not deviceCustomCommands[filename]? or commandName not in deviceCustomCommands[filename]
command = common.getParamValueFromPath(device, "_customCommands.#{filename}")
continue if not command? or commandName not in command._commands
timeDiff = (now - command._timestamp) / 1000
timeDiff = try (now - command._timestamp) / 1000 catch then 9999999
if (c.age - timeDiff < config.PRESETS_TIME_PADDING)
expiry = Math.min(expiry, c.age)
taskList.push({device : deviceId, name : 'customCommand', command : c.command})
+4 -4
View File
@@ -2,7 +2,7 @@ config = require './config'
common = require './common'
db = require './db'
mongodb = require 'mongodb'
customCommands = require './customCommands'
customCommands = require './custom-commands'
BATCH_SIZE = 16
exports.STATUS_QUEUED = STATUS_QUEUED = 0
@@ -15,11 +15,11 @@ exports.STATUS_FINISHED = STATUS_FINISHED = 4
initCustomCommands = (deviceId, callback) ->
updates = {customCommands : []}
counter = 0
for k,v of config.CUSTOM_COMMANDS
if eval(v).test(deviceId)
for k,v of customCommands.getDeviceCustomCommands(deviceId)
if 'init' in v
++counter
customCommands.execute(deviceId, "#{k} init", (err, value) ->
updates.customCommands.push([k, value, customCommands.getCommands(k)])
updates.customCommands.push([k, value])
callback(updates) if --counter <= 0
)
callback(updates) if counter <= 0