#!/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))