libconfig.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/usr/bin/python3
  2. import os
  3. import json
  4. h = os.environ["HOME"]
  5. def cprint(txt,*args):
  6. print(txt)
  7. def _load_remote_ip(cfg_file):
  8. f = open(cfg_file)
  9. lines = f.readlines()
  10. f.close()
  11. ip = ""
  12. for line in lines:
  13. # take the last IP in config file
  14. if "DMX-REMOTE-IP" in line:
  15. try:
  16. jd=json.loads(line)
  17. jip = jd["DMX-REMOTE-IP"]
  18. if ":" in jip and jip.count(".") :
  19. ip = jip
  20. except:pass
  21. return ip
  22. def _write_default_remote_ip(cfg_file):
  23. try:
  24. f = open(cfg_file ,"a")
  25. f.seek(99999999999)
  26. txt ='\n'
  27. txt+=json.dumps({"DMX-REMOTE-IP":"10.10.10.13:0"})
  28. f.write(txt)
  29. f.flush()
  30. f.close()
  31. except Exception as e:
  32. print("EXCEPT",e)
  33. def _create_default_config():
  34. txt=''
  35. txt+='{"POS_TOP":10}'
  36. txt+="\n"
  37. txt+='{"POS_LEFT":10}'
  38. txt+="\n"
  39. for i in range(10):
  40. txt+='{"DMX-FADER-'+str(i+1)+'":500}'
  41. txt+="\n"
  42. txt+='{"START_MODE":"PROx"}'
  43. txt+="\n"
  44. txt+='{"START_MODE":"EASYx"}'
  45. txt+="\n"
  46. txt+=json.dumps({"DMX-REMOTE-IP":"10.10.10.13:0"})
  47. txt+="\n"
  48. f = open(h +"/LibreLight/config.json","w")
  49. f.write(txt)
  50. f.close()
  51. def _load_config():
  52. _config = []
  53. lines = [{}]
  54. try:
  55. f = open(h +"/LibreLight/config.json")
  56. lines = f.readlines()
  57. except FileNotFoundError as e: #Exception as e:
  58. _create_default_config()
  59. cprint("Exception:",e)
  60. try:
  61. cprint("config read")
  62. for line in lines:
  63. line=line.strip()
  64. print(" config:",line)
  65. row = json.loads(line)
  66. _config.append(row)
  67. except Exception as e:
  68. cprint("Exception:",e)
  69. return _config
  70. def load_remote_ip():
  71. cfg_file = "/home/user/LibreLight/config.json"
  72. ip = _load_remote_ip(cfg_file)
  73. if not ip:
  74. _write_default_remote_ip(cfg_file)
  75. ip = _load_remote_ip(cfg_file)
  76. if not ip:
  77. ip = "0.0.0.0:err"
  78. return ip
  79. def check_pro_easy():
  80. try:
  81. f = open("/home/user/LibreLight/config.json")
  82. lines = f.readlines()
  83. f.close()
  84. for line in lines:
  85. if '{"START_MODE":"PRO"}' in line:
  86. print(" PRO")
  87. return "PRO"
  88. elif '{"START_MODE":"EASY"}' in line:
  89. print(" EASY")
  90. return "EASY"
  91. print(line)
  92. except Exception as e:
  93. print("Exception",e)
  94. return ""