mc_api.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/usr/bin/python3
  2. import json
  3. import memcache
  4. import time
  5. mc = None
  6. def _connect():
  7. try:
  8. mc = memcache.Client(['127.0.0.1:11211'], debug=0)
  9. except:
  10. mc = None
  11. return mc
  12. mc = _connect()
  13. def mc_set_fix(index="fix",data=[]):
  14. #index = mc.get("index")
  15. mc.set("fix", data)
  16. def mc_set_patch_loop(FIXTURES):
  17. _FIXTURES = FIXTURES
  18. time.sleep(10)
  19. print("start loop MC_PATCH ...")
  20. while 1:
  21. try:
  22. mc_set_patch(FIXTURES)
  23. except Exception as e:
  24. print(e)
  25. time.sleep(5)
  26. def mc_set_patch(FIXTURES):
  27. _FIXTURES = FIXTURES
  28. #print("mc_set_patch")
  29. index_patch = []
  30. for fix,val in _FIXTURES.fixtures.items():
  31. #print("FIX",fix,val)
  32. val = json.dumps(val).encode()
  33. mc.set("PATCH-"+str(fix),val)
  34. index_patch.append(fix)
  35. val = json.dumps(index_patch).encode()
  36. mc.set("PATCH-INDEX",val)
  37. def get_programmer():
  38. if mc is None:
  39. return
  40. DATA=mc.get("fix")#cmd)
  41. out=[]
  42. for k,v in DATA.items():
  43. ok=0
  44. for kk,vv in v["ATTRIBUT"].items():
  45. if kk.startswith("_"):
  46. continue
  47. if vv["ACTIVE"]:
  48. tmp={"ID":k,"NAME":v["NAME"],"ATTR":kk,"NR":vv["NR"]} #,"FX3":vv["FX2"]}
  49. out.append(tmp)
  50. ok=1
  51. #if ok:
  52. # print()
  53. return out
  54. def get_patch():
  55. if mc is None:
  56. return
  57. index=mc.get("PATCH-INDEX") #index")#cmd)
  58. index=json.loads(index)
  59. out = []
  60. for v in index:
  61. patch=mc.get("PATCH-"+str(v)) #index")#cmd)
  62. patch=json.loads(patch)
  63. u = patch["UNIVERS"] #* 512
  64. d = patch["DMX"]
  65. attr = {}
  66. for a in patch["ATTRIBUT"]:
  67. nr =patch["ATTRIBUT"][a]["NR"]
  68. if nr <= 0:
  69. continue
  70. if a == "END":
  71. continue
  72. if a.startswith("_"):
  73. continue
  74. nr = nr + d + u*512
  75. attr[a] = nr-1
  76. tmp = {"FIX":v,"UNI":u,"DMX":d,"NR":nr,"ATTR":attr}
  77. out.append(tmp )
  78. return out
  79. def patch_order_by_fix(patch):
  80. out={}
  81. for p in patch:
  82. ID = p["FIX"]
  83. out[ID] = p
  84. #print("patch_order",[ID,p])
  85. return out
  86. def get_exec_meta(nr):
  87. data = {}
  88. try:
  89. data = mc.get("EXEC-META-"+str(nr))
  90. data = json.loads(data)
  91. except Exception as e:
  92. print(" ER1R mc...",e)
  93. return data
  94. modes = {}
  95. def modes_refresh():
  96. global modes
  97. try:
  98. data = mc.get("MODES")
  99. data = json.loads(data)
  100. modes = data
  101. except Exception as e:
  102. print(" ER7R mc...",e)
  103. print(mc)
  104. print(dir(mc))
  105. #time.sleep(3)
  106. return modes
  107. def modes_refresh_loop():
  108. time.sleep(3)
  109. while 1:
  110. modes_refresh()
  111. time.sleep(0.1)