Some events related fixes

This commit is contained in:
Zaid Abdulla committed 2016-11-03 14:42:38 -07:00
1 parent f9e96fe7f4
commit 044734b567
3 files changed
+75 -41

No files matched your search

+1 -1
View File
@@ -157,7 +157,7 @@ applyPresets = (currentRequest) ->
deviceEvents = {} deviceEvents = {}
iter = currentRequest.sessionData.deviceData.paths.subset(['Events', '*']) iter = currentRequest.sessionData.deviceData.paths.subset(['Events', '*'])
while (p = iter.next().value) while (p = iter.next().value)
if currentRequest.sessionData.timestamp == currentRequest.sessionData.deviceData.attributes.get(p)?.value?[1][0] if currentRequest.sessionData.timestamp <= currentRequest.sessionData.deviceData.attributes.get(p)?.value?[1][0]
deviceEvents[p[1]] = true deviceEvents[p[1]] = true
parameters = {} parameters = {}
+63 -31
View File
@@ -159,6 +159,10 @@ fetchDevice = (id, timestamp, patterns, callback) ->
func([], patterns, projectionTree) func([], patterns, projectionTree)
delete projectionTree['DeviceID']
delete projectionTree['Events']
delete projectionTree['Tags']
if projection['']? if projection['']?
proj = {} proj = {}
else else
@@ -252,43 +256,61 @@ fetchDevice = (id, timestamp, patterns, callback) ->
loaded.push([p, ((1 << path.length) - 1) ^ ((1 << MAX_DEPTH) - 1)]) loaded.push([p, ((1 << path.length) - 1) ^ ((1 << MAX_DEPTH) - 1)])
for k, v of device for k, v of device
if k == '_lastInform' switch k
device['Events'] ?= {'_timestamp': timestamp, '_writable': false, '_object': true} when '_lastInform'
device['Events']['Inform'] = {'_writable': false, '_value': v, '_type' : 'xsd:dateTime', '_timestamp': v} res.push([['Events'], timestamp,
{object: [timestamp, 1], writable: [timestamp, 0]}])
res.push([['Events', 'Inform'], +v,
{object: [+v, 0], writable: [+v, 0], value: [+v, [+v, 'xsd:dateTime']]}])
delete device[k] delete device[k]
else if k == '_lastBoot' when '_lastInform'
device['Events'] ?= {'_timestamp': timestamp, '_writable': false, '_object': true} res.push([['Events'], timestamp,
device['Events']['1_BOOT'] = {'_writable': false, '_value': v, '_type' : 'xsd:dateTime', '_timestamp': v} {object: [timestamp, 1], writable: [timestamp, 0]}])
res.push([['Events', '1_BOOT'], +v,
{object: [+v, 0], writable: [+v, 0], value: [+v, [+v, 'xsd:dateTime']]}])
delete device[k] delete device[k]
else if k == '_lastBootstrap' when '_lastBootstrap'
device['Events'] ?= {'_timestamp': timestamp, '_writable': false, '_object': true} res.push([['Events'], timestamp,
device['Events']['0_BOOTSTRAP'] = {'_writable': false, '_value': v, '_type' : 'xsd:dateTime', '_timestamp': v} {object: [timestamp, 1], writable: [timestamp, 0]}])
res.push([['Events', '0_BOOTSTRAP'], +v,
{object: [+v, 0], writable: [+v, 0], value: [+v, [+v, 'xsd:dateTime']]}])
delete device[k] delete device[k]
else if k == '_registered' when '_registered'
device['Events'] ?= {'_timestamp': timestamp, '_writable': false, '_object': true} res.push([['Events'], timestamp,
device['Events']['Registered'] = {'_writable': false, '_value': v, '_type' : 'xsd:dateTime', '_timestamp': v} {object: [timestamp, 1], writable: [timestamp, 0]}])
# Use current timestamp for registered event attribute timestamps
res.push([['Events', 'Registered'], timestamp,
{object: [timestamp, 0], writable: [timestamp, 0], value: [timestamp, [v, 'xsd:dateTime']]}])
delete device[k] delete device[k]
else if k == '_id' when '_id'
device['DeviceID'] ?= {'_timestamp': timestamp, '_writable': false, '_object': true} res.push([['DeviceID'], timestamp,
device['DeviceID']['ID'] = {'_writable': false, '_value': v, '_type' : 'xsd:string', '_timestamp': timestamp} {object: [timestamp, 1], writable: [timestamp, 0]}])
res.push([['DeviceID', 'ID'], timestamp,
{object: [timestamp, 0], writable: [timestamp, 0], value: [timestamp, [v, 'xsd:string']]}])
delete device[k] delete device[k]
else if k == '_tags' when '_tags'
device['Tags'] ?= {'_timestamp': timestamp, '_writable': false, '_object': true} res.push([['Tags'], timestamp,
{object: [timestamp, 1], writable: [timestamp, 0]}])
for t in v for t in v
t = t.replace(/[^a-zA-Z0-9\-]+/g, '_') t = t.replace(/[^a-zA-Z0-9\-]+/g, '_')
device['Tags'][t] = {'_writable': true, '_value': true, '_type' : 'xsd:boolean', '_timestamp': timestamp} res.push([['Tags', t], timestamp,
{object: [timestamp, 0], writable: [timestamp, 1], value: [timestamp, [true, 'xsd:boolean']]}])
delete device[k] delete device[k]
else if k == '_deviceId' when '_deviceId'
device['DeviceID'] ?= {'_timestamp': timestamp, '_writable': false, '_object': true} res.push([['DeviceID'], timestamp,
for kk, vv of v {object: [timestamp, 1], writable: [timestamp, 0]}])
if kk == '_Manufacturer' if v['_Manufacturer']?
device['DeviceID']['Manufacturer'] = {'_writable': false, '_value': vv, '_type' : 'xsd:string', '_timestamp': timestamp} res.push([['DeviceID', 'Manufacturer'], timestamp,
else if kk == '_OUI' {object: [timestamp, 0], writable: [timestamp, 0], value: [timestamp, [v['_Manufacturer'], 'xsd:string']]}])
device['DeviceID']['OUI'] = {'_writable': false, '_value': vv, '_type' : 'xsd:string', '_timestamp': timestamp} if v['_OUI']?
if kk == '_ProductClass' res.push([['DeviceID', 'OUI'], timestamp,
device['DeviceID']['ProductClass'] = {'_writable': false, '_value': vv, '_type' : 'xsd:string', '_timestamp': timestamp} {object: [timestamp, 0], writable: [timestamp, 0], value: [timestamp, [v['_OUI'], 'xsd:string']]}])
if kk == '_SerialNumber' if v['_ProductClass']?
device['DeviceID']['SerialNumber'] = {'_writable': false, '_value': vv, '_type' : 'xsd:string', '_timestamp': timestamp} res.push([['DeviceID', 'ProductClass'], timestamp,
{object: [timestamp, 0], writable: [timestamp, 0], value: [timestamp, [v['_ProductClass'], 'xsd:string']]}])
if v['_SerialNumber']?
res.push([['DeviceID', 'SerialNumber'], timestamp,
{object: [timestamp, 0], writable: [timestamp, 0], value: [timestamp, [v['_SerialNumber'], 'xsd:string']]}])
delete device[k] delete device[k]
storeParams(device, [], 0, false, projectionTree) storeParams(device, [], 0, false, projectionTree)
@@ -318,7 +340,18 @@ saveDevice = (deviceId, deviceData, isNew, callback) ->
path = diff[0] path = diff[0]
switch path[0] switch path[0]
when 'Events' when 'Events'
if diff[0].length == 2 and diff[2].value?[1][0] != diff[1]?.value?[1][0] if diff[0].length == 2 and diff[2]?.value?[1][0] != diff[1]?.value?[1][0]
if not diff[2]
switch path[1]
when 'Inform'
update['$unset']['_lastInform'] = 1
when '1_BOOT'
update['$unset']['_lastBoot'] = 1
when '0_BOOTSTRAP'
update['$unset']['_lastBootstrap'] = 1
when 'Registered'
update['$unset']['_registered'] = 1
else
t = new Date(diff[2].value[1][0]) t = new Date(diff[2].value[1][0])
switch path[1] switch path[1]
when 'Inform' when 'Inform'
@@ -385,7 +418,6 @@ saveDevice = (deviceId, deviceData, isNew, callback) ->
update['$unset'][path.concat('_type').join('.')] = 1 update['$unset'][path.concat('_type').join('.')] = 1
update['$unset'][path.concat('_timestamp').join('.')] = 1 update['$unset'][path.concat('_timestamp').join('.')] = 1
# Remove empty keys # Remove empty keys
for k of update for k of update
if k == '$addToSet' if k == '$addToSet'
+11 -9
View File
@@ -80,10 +80,7 @@ loadParameters = (sessionData, callback) ->
# Device not available in database, mark as new # Device not available in database, mark as new
sessionData.new = true sessionData.new = true
loaded = [[[], (1 << config.get('MAX_DEPTH', sessionData.deviceId)) - 1]] loaded = [[[], (1 << config.get('MAX_DEPTH', sessionData.deviceId)) - 1]]
parameters = [ parameters = []
[['DeviceID', '*'], sessionData.timestamp],
[['Events', '*'], sessionData.timestamp]
]
for p in loaded for p in loaded
path = sessionData.deviceData.paths.add(p[0]) path = sessionData.deviceData.paths.add(p[0])
@@ -114,7 +111,7 @@ generateRpcId = (sessionData) ->
inform = (sessionData, rpcReq, callback) -> inform = (sessionData, rpcReq, callback) ->
timestamp = sessionData.timestamp timestamp = sessionData.timestamp + sessionData.iteration + 1
params = [] params = []
params.push([['DeviceID', 'Manufacturer'], timestamp, params.push([['DeviceID', 'Manufacturer'], timestamp,
@@ -179,8 +176,8 @@ addProvisions = (sessionData, channel, provisions) ->
sessionData.channels.push(channel) sessionData.channels.push(channel)
if sessionData.revisions.length > 1 or sessionData.revisions[0] > 0 if sessionData.revisions.length > 1 or sessionData.revisions[0] > 0
v.collapse(1) for k, v of sessionData.timestamps v.collapse(1) for k, v of sessionData.deviceData.timestamps
v.collapse(1) for k, v of sessionData.attributes v.collapse(1) for k, v of sessionData.deviceData.attributes
sessionData.rpcCount = 0 sessionData.rpcCount = 0
sessionData.revisions = [0] sessionData.revisions = [0]
sessionData.extensionsCache.length = 0 sessionData.extensionsCache.length = 0
@@ -434,9 +431,12 @@ rpcRequest = (sessionData, _declarations, callback) ->
for ad in device.getAliasDeclarations(d[0], 1) for ad in device.getAliasDeclarations(d[0], 1)
loadPath(sessionData, ad[0]) loadPath(sessionData, ad[0])
return loadParameters(sessionData, (err) -> return clear(sessionData, toClear, (err) ->
return callback(err) if err return callback(err) if err
return rpcRequest(sessionData, _declarations, callback) loadParameters(sessionData, (err) ->
return callback(err) if err
rpcRequest(sessionData, _declarations, callback)
)
) )
) )
@@ -867,6 +867,8 @@ processDeclarations = (sessionData, allDeclareTimestamps, allDeclareAttributeTim
when 'Tags' when 'Tags'
if currentPath.length == 2 and currentPath.wildcard == 0 and declareAttributeValues?.value? if currentPath.length == 2 and currentPath.wildcard == 0 and declareAttributeValues?.value?
syncState.tags.set(currentPath, device.sanitizeParameterValue([declareAttributeValues.value[0], 'xsd:boolean'])[0]) syncState.tags.set(currentPath, device.sanitizeParameterValue([declareAttributeValues.value[0], 'xsd:boolean'])[0])
when 'Events', 'DeviceID' then
# Do nothing
when 'VirtualParameters' when 'VirtualParameters'
if currentPath.length <= 2 if currentPath.length <= 2
d = null d = null