libconfig.py 2.6 KB

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