Skip to content
Snippets Groups Projects
Commit 44e7b15e authored by jkerdreu's avatar jkerdreu
Browse files

Added refresh_rate for attributes / description / db

git-svn-id: https://redmine.imt-atlantique.fr/svn/xaal/code/Python/trunk@1964 b32b6428-25c9-4566-ad07-03861ab6144f
parent b233be23
No related branches found
No related tags found
No related merge requests found
import time,random
from enum import Enum
import logging
logger = logging.getLogger(__name__)
# how often we force refresh the devices attributes/description/keyvalues
REFRESH_RATE = 300
def now():
return int(time.time())
class Device:
def __init__(self, addr, devtype, version):
self.address = addr
self.devtype = devtype
self.version = version
# device cache
self.attributes = {}
self.description = {}
self.db = {}
# Alive management
self.last_alive = int(time.time())
self.next_alive = 0
# Refresh rate
self.refresh = 0
#print("Adding %s - %s" % (addr, devtype))
self.refresh_attributes = 0
self.refresh_description = 0
self.refresh_db = 0
def update_attributes(self, data):
""" rude update attributes. Return true if updated"""
keys = list(data.keys())
change = False
# really no the best comparaison, but we just need a flag
if self.attributes == data:
return False
self.attributes.update(data)
self.refresh_attributes = now()
return True
def update_description(self, data):
self.description.update(data)
self.refresh_description = now()
def update_db(self,data):
self.db.update(data)
self.refresh_db = now()
def alive(self,value):
self.last_alive = int(time.time())
......@@ -55,6 +66,7 @@ class Device:
class Devices:
""" Device List for monitoring """
def __init__(self):
self.__devs = {}
self.__list_cache = None
......@@ -63,6 +75,7 @@ class Devices:
dev = Device(addr,devtype,version)
self.__devs.update({addr : dev})
self.__list_cache = None
return dev
def get(self):
......@@ -143,6 +156,12 @@ class Devices:
print("%s %s" % (d.address,d.devtype))
class Notification(Enum):
new_device = 0
drop_device = 1 # sending drop_device notif is not implemented yet,
attribute_change = 2
class Monitor:
"""
class xAAL Monitor:
......@@ -167,7 +186,8 @@ class Monitor:
return
if msg.source not in self.devices:
self.add_device(msg)
dev = self.add_device(msg)
self.notify(Notification.new_device,dev)
dev = self.devices.get_with_addr(msg.source)
......@@ -177,7 +197,7 @@ class Monitor:
elif msg.is_attributes_change() or msg.is_get_attribute_reply():
r = dev.update_attributes(msg.body)
if r:
self.notify('ATTRIBUTE_CHANGE',dev)
self.notify(Notification.attribute_change,dev)
elif msg.is_get_description_reply():
dev.update_description(msg.body)
......@@ -205,7 +225,7 @@ class Monitor:
s(ev_type,device)
def add_device(self,msg):
self.devices.add(msg.source,msg.devtype,msg.version)
return self.devices.add(msg.source,msg.devtype,msg.version)
def send_isalive(self):
self.engine.send_isAlive(self.dev, "any.any")
......@@ -217,22 +237,26 @@ class Monitor:
def refresh_devices(self):
now = int(time.time())
for dev in self.devices:
if dev.refresh + 300 < now:
if self.db_server:
if dev.refresh + REFRESH_RATE < now:
if dev.refresh_db + REFRESH_RATE < now:
self.request_metadb(dev.address)
if dev.refresh_attributes + REFRESH_RATE < now:
self.engine.send_get_attributes(self.dev,[dev.address,])
if dev.refresh_description + REFRESH_RATE < now:
self.engine.send_get_description(self.dev,[dev.address,])
# to avoid bulk send, we introduce this salt in refresh
dev.refresh = now - random.randint(0,20)
def request_metadb(self,addr):
if self.db_server:
self.engine.send_request(self.dev,[self.db_server,],'getKeysValues',{'device':addr})
def is_reply_metadb(self,msg):
if msg.msgtype == 'reply' and msg.action == 'getKeysValues':
if msg.msgtype == 'reply' and msg.action == 'getKeysValues' and msg.source == self.db_server:
return True
return False
def is_update_metadb(self,msg):
if msg.msgtype == 'notify' and msg.action == 'keysValuesChanged':
if msg.msgtype == 'notify' and msg.action == 'keysValuesChanged' and msg.source == self.db_server:
return True
return False
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment