Cleaned up value normalization code

This commit is contained in:
Zaid Abdulla committed 2013-03-15 22:33:25 +03:00
1 parent 8f30fc7d09
commit 56fa1f03e4
3 files changed
+49 -30

No files matched your search

+10 -7
View File
@@ -5,7 +5,7 @@ http = require 'http'
https = require 'https' https = require 'https'
tr069 = require './tr-069' tr069 = require './tr-069'
tasks = require './tasks' tasks = require './tasks'
sanitize = require('./sanitize').sanitize normalize = require('./normalize').normalize
db = require './db' db = require './db'
presets = require './presets' presets = require './presets'
mongodb = require 'mongodb' mongodb = require 'mongodb'
@@ -45,18 +45,21 @@ updateDevice = (currentRequest, actions, callback) ->
now = new Date(Date.now()) now = new Date(Date.now())
updates = {} updates = {}
deletes = {}
if actions.inform if actions.inform
updates['_last_inform'] = now updates['_last_inform'] = now
if actions.parameterValues? if actions.parameterValues?
for p in 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]}." path = if common.endsWith(p[0], '.') then p[0] else "#{p[0]}."
if v is undefined if v == origValue
updates["#{path}_value"] = p[1] deletes["#{path}_orig"] = 1
else else
updates["#{path}_value"] = v updates["#{path}_orig"] = origValue
updates["#{path}_orig"] = p[1] updates["#{path}_value"] = v
updates["#{path}_timestamp"] = now updates["#{path}_timestamp"] = now
if actions.parameterNames? if actions.parameterNames?
@@ -68,7 +71,7 @@ updateDevice = (currentRequest, actions, callback) ->
updates["#{path}_timestamp"] = now updates["#{path}_timestamp"] = now
if Object.keys(updates).length > 0 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) if (err)
callback(err) if callback? callback(err) if callback?
return return
+39
View File
@@ -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',
},
}
-23
View File
@@ -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