Skip to content
Snippets Groups Projects
Commit 3d6ec674 authored by KERDREUX Jerome's avatar KERDREUX Jerome
Browse files

Added msg-stats

parent 35c2d3a9
No related branches found
No related tags found
No related merge requests found
import asyncio
import time
from xaal.lib import AsyncEngine, Message, MessageAction
MSG_STATS = {
"total": 0,
"alive": 0,
"request_isalive": 0,
"request": 0,
"reply": 0,
"get_description": 0,
"get_attributes": 0,
"attributes_change": 0,
"notify": 0,
}
DEVICE_STATS = {}
def count(msg: Message):
msg_count(msg)
device_count(msg)
def device_count(msg: Message):
addr = msg.source
try:
DEVICE_STATS[addr] += 1
except KeyError:
DEVICE_STATS[addr] = 1
def msg_count(msg: Message):
# Count the number of messages received
MSG_STATS["total"] += 1
if msg.is_request_isalive():
MSG_STATS["request_isalive"] += 1
return
if msg.is_alive():
MSG_STATS["alive"] += 1
return
if msg.is_reply() or msg.is_request():
# This msg is a get attribute or description type
if msg.action == MessageAction.GET_ATTRIBUTES.value:
MSG_STATS["get_attributes"] += 1
return
if msg.action == MessageAction.GET_DESCRIPTION.value:
MSG_STATS["get_description"] += 1
return
if msg.is_request():
# direct request / not isalive / not attributes / description
MSG_STATS["request"] += 1
if msg.is_reply():
MSG_STATS["reply"] += 1
if msg.is_attributes_change():
MSG_STATS["attributes_change"] += 1
if msg.is_notify():
MSG_STATS["notify"] += 1
async def show():
t0 = time.time()
while 1:
await asyncio.sleep(5)
t = time.time() - t0
print(MSG_STATS)
print(DEVICE_STATS)
def run():
eng = AsyncEngine()
eng.disable_msg_filter()
eng.subscribe(count)
eng.on_start(show)
eng.run()
if __name__ == "__main__":
run()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment