Initial implementation of custom commands feature (e.g. telnet).

This commit is contained in:
Zaid Abdulla committed 2013-09-10 15:54:09 +03:00
1 parent 3c7f554264
commit ff286080e8
5 files changed
+62 -1

No files matched your search

+7
View File
@@ -102,6 +102,13 @@ updateDevice = (currentRequest, actions, callback) ->
updates["#{path}_writable"] = p[1] if p[1]?
updates["#{path}_timestamp"] = now
if actions.customCommands?
for p in actions.customCommands
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) ->
if (err)
+2
View File
@@ -49,3 +49,5 @@ exports.ALIASES = aliases
# load authentication scripts
exports.auth = require('./config/auth')
exports.CUSTOM_COMMANDS = require('./config/custom_commands')
+3
View File
@@ -0,0 +1,3 @@
{
"seowon_frequencies" : "/^[0-9]+\\-SWU\\%2D9200\\-[0-9A-Z]+$/"
}
+17
View File
@@ -0,0 +1,17 @@
common = require './common'
exports.execute = (deviceId, command, callback) ->
args = command.split(/\s+/)
f = args.shift()
c = args.shift()
file = require "./config/custom_commands/#{f}"
file[c](deviceId, args.join(' '), callback)
exports.getCommands = (filename) ->
commands = []
f = require("./config/custom_commands/#{filename}")
for k,v of f
commands.push(k)
return commands
+33 -1
View File
@@ -2,6 +2,7 @@ config = require './config'
common = require './common'
db = require './db'
mongodb = require 'mongodb'
customCommands = require './customCommands'
BATCH_SIZE = 16
exports.STATUS_QUEUED = STATUS_QUEUED = 0
@@ -11,6 +12,19 @@ exports.STATUS_FAULT = STATUS_FAULT = 3
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)
++counter
customCommands.execute(deviceId, "#{k} init", (err, value) ->
updates.customCommands.push([k, value, customCommands.getCommands(k)])
callback(updates) if --counter <= 0
)
callback(updates) if counter <= 0
this.init = (task, methodResponse, callback) ->
if not task.subtask?
task.subtask = {device : task.device, name : 'getParameterNames', parameterPath : '', nextLevel : false}
@@ -42,7 +56,13 @@ this.init = (task, methodResponse, callback) ->
# Ignore GetParameterValues errors. A workaround for the crappy Seewon devices.
methodResponse = {parameterList : {}}
this.getParameterValues(task.subtask, methodResponse, (err, status, cwmpResponse, deviceUpdates) ->
callback(err, status, cwmpResponse, deviceUpdates)
if status is STATUS_FINISHED
initCustomCommands(task.device, (updates) ->
common.extend(deviceUpdates, updates)
callback(err, status, cwmpResponse, deviceUpdates)
)
else
callback(err, status, cwmpResponse, deviceUpdates)
)
else
throw Error('Unexpected subtask name')
@@ -335,5 +355,17 @@ this.download = (task, methodResponse, callback) ->
callback(null, STATUS_FINISHED)
this.customCommand = (task, methodResponse, callback) ->
# TODO implement timeout
customCommands.execute(task.device, task.command, (err, value) ->
if err?
task.fault = err
callback(null, STATUS_FAULT)
else
commandName = task.command.split(' ', 2)[0]
callback(null, STATUS_FINISHED, null, {customCommands : [[commandName, value]]})
)
exports.task = (task, methodResponse, callback) ->
this[task.name](task, methodResponse, callback)