From 9537bfad0f48a42d262bdd371319596c74e9dd6c Mon Sep 17 00:00:00 2001 From: Zaid Abdulla Date: Fri, 13 Sep 2013 13:02:44 +0300 Subject: [PATCH] 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. --- acs.coffee | 1 - ...mCommands.coffee => custom-commands.coffee | 12 +++++++++++- presets.coffee | 19 +++++++++++-------- tasks.coffee | 8 ++++---- 4 files changed, 26 insertions(+), 14 deletions(-) rename customCommands.coffee => custom-commands.coffee (58%) diff --git a/acs.coffee b/acs.coffee index 19ec12b..53d94ab 100644 --- a/acs.coffee +++ b/acs.coffee @@ -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) -> diff --git a/customCommands.coffee b/custom-commands.coffee similarity index 58% rename from customCommands.coffee rename to custom-commands.coffee index 6f7f3b3..314fa03 100644 --- a/customCommands.coffee +++ b/custom-commands.coffee @@ -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 diff --git a/presets.coffee b/presets.coffee index 9a58de3..9d62c0c 100644 --- a/presets.coffee +++ b/presets.coffee @@ -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}) diff --git a/tasks.coffee b/tasks.coffee index e833295..1fc7994 100644 --- a/tasks.coffee +++ b/tasks.coffee @@ -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