From 5f7c61f61652eccec261f91b9d8fddeab9c59837 Mon Sep 17 00:00:00 2001 From: alban Date: Tue, 13 Oct 2020 22:04:41 +0200 Subject: [PATCH] [enh] Bring back the clitools fromRedis generator --- clitools/generators/fromRedis.py | 72 ++++++++++++++++++++++++++++++++ 1 file changed, 72 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..8c65951 --- /dev/null +++ b/clitools/generators/fromRedis.py @@ -0,0 +1,72 @@ +#!/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)) +