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