diff --git a/acs.coffee b/acs.coffee index b9930a6..3dfd7d8 100644 --- a/acs.coffee +++ b/acs.coffee @@ -5,7 +5,7 @@ http = require 'http' https = require 'https' tr069 = require './tr-069' tasks = require './tasks' -sanitize = require('./sanitize').sanitize +normalize = require('./normalize').normalize db = require './db' presets = require './presets' mongodb = require 'mongodb' @@ -45,18 +45,21 @@ updateDevice = (currentRequest, actions, callback) -> now = new Date(Date.now()) updates = {} + deletes = {} if actions.inform updates['_last_inform'] = now if actions.parameterValues? for p in actions.parameterValues - v = sanitize(p[0], p[1]) + origValue = p[1] + v = normalize(p[0], origValue) + path = if common.endsWith(p[0], '.') then p[0] else "#{p[0]}." - if v is undefined - updates["#{path}_value"] = p[1] + if v == origValue + deletes["#{path}_orig"] = 1 else - updates["#{path}_value"] = v - updates["#{path}_orig"] = p[1] + updates["#{path}_orig"] = origValue + updates["#{path}_value"] = v updates["#{path}_timestamp"] = now if actions.parameterNames? @@ -68,7 +71,7 @@ updateDevice = (currentRequest, actions, callback) -> updates["#{path}_timestamp"] = now if Object.keys(updates).length > 0 - db.devicesCollection.update({'_id' : currentRequest.deviceId}, {'$set' : updates}, {safe: true}, (err, count) -> + db.devicesCollection.update({'_id' : currentRequest.deviceId}, {'$set' : updates, '$unset' : deletes}, {safe: true}, (err, count) -> if (err) callback(err) if callback? return diff --git a/normalize.coffee b/normalize.coffee new file mode 100644 index 0000000..1032160 --- /dev/null +++ b/normalize.coffee @@ -0,0 +1,39 @@ +common = require './common' + + +normalizers = {} + + +FLOAT_REGEX = /^([\-\+]?[0-9\.]+)/ +normalizers.float = (input) -> + res = FLOAT_REGEX.exec(input) + if res != null + parseFloat(res[1]) + else + null + + +exports.normalize = (path, value) -> + if path of parameters + return normalizers[parameters[path].type](value) + else + return value + + +parameters = { + 'InternetGatewayDevice.WiMAX.Status.RSSI' : { + type : 'float', + }, + 'InternetGatewayDevice.WiMAX.Status.CINR1' : { + type : 'float', + }, + 'InternetGatewayDevice.WiMAX.Status.CINR2' : { + type : 'float', + }, + 'InternetGatewayDevice.X_MTK_WiMAX_Param.CINR_level' : { + type : 'float', + }, + 'InternetGatewayDevice.X_MTK_WiMAX_Param.RSSI_level' : { + type : 'float', + }, +} \ No newline at end of file diff --git a/sanitize.coffee b/sanitize.coffee deleted file mode 100644 index 57e2fb4..0000000 --- a/sanitize.coffee +++ /dev/null @@ -1,23 +0,0 @@ -FLOAT_REGEX = /^([\-\+]?[0-9\.]+)/ - -extractFloat = (numStr) -> - res = FLOAT_REGEX.exec(numStr) - if res != null - parseFloat(res[1]) - else - null - -exports.sanitize = (path, value) -> - switch path - when 'InternetGatewayDevice.WiMAX.Status.RSSI' - extractFloat(value) - when 'InternetGatewayDevice.WiMAX.Status.CINR1' - extractFloat(value) - when 'InternetGatewayDevice.WiMAX.Status.CINR2' - extractFloat(value) - when 'InternetGatewayDevice.X_MTK_WiMAX_Param.CINR_level' # Seowon devices - extractFloat(value) - when 'InternetGatewayDevice.X_MTK_WiMAX_Param.RSSI_level' # Seowon devices - extractFloat(value) - else - undefined