From 93a5bf7fe553af816c0a74f43f1d07c0ac926aa5 Mon Sep 17 00:00:00 2001 From: alban Date: Tue, 29 Sep 2020 17:53:46 +0200 Subject: [PATCH] [enh] clitools: Adds a redis generator --- clitools/generators/fromRedis.py | 73 ++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100755 clitools/generators/fromRedis.py diff --git a/clitools/generators/fromRedis.py b/clitools/generators/fromRedis.py new file mode 100755 index 0000000..7e127d3 --- /dev/null +++ b/clitools/generators/fromRedis.py @@ -0,0 +1,73 @@ +#!/usr/bin/python3 +# -*- coding: utf-8 -*- +# -*- mode: Python -*- + + +''' + +This generator reads a frame from redis +v0.1.0 + +Use it to create feedback loops by writing to the same frame +or to copy the frame from someone else + +LICENCE : CC + +by cocoa + +''' + +from __future__ import print_function +import ast +import argparse +import json +import redis +import sys +import time +name="generator::fromRedis" + + +def debug(*args, **kwargs): + if( verbose == False ): + return + print(*args, file=sys.stderr, **kwargs) + + +argsparser = argparse.ArgumentParser(description="Dummy generator") +argsparser.add_argument("-k","--key",required=True,help="Redis key to look after",default=30,type=str) +argsparser.add_argument("-i","--ip",help="IP address of the Redis server ",default="127.0.0.1",type=str) +argsparser.add_argument("-p","--port",help="Port of the Redis server ",default="6379",type=str) +argsparser.add_argument("-f","--fps",help="Frame Per Second",default=30,type=int) +argsparser.add_argument("-v","--verbose",action="store_true",help="Verbose output") +args = argsparser.parse_args() + +fps = args.fps +verbose = args.verbose +key = args.key +ip = args.ip +port = args.port +optimal_looptime = 1 / fps +debug(name+" optimal looptime "+str(optimal_looptime)) + +r = redis.Redis( + host=ip, + port=port) + +while True: + start = time.time() + # Read from Redis + line = r.get(key) + # Decode as list of tuples + pointsList = ast.literal_eval(line.decode('ascii')) + # convert to list of lists + pointsList = [list(elem) for elem in pointsList] + # Convert to JSON string + line = json.dumps( pointsList ) + debug(name,"Key:{} line:{}".format(key,line)) + print(line, flush=True); + looptime = time.time() - start + if( looptime < optimal_looptime ): + time.sleep( optimal_looptime - looptime) + debug(name+" micro sleep:"+str( optimal_looptime - looptime)) + +