log_vedirect.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #!/usr/bin/env python
  2. import numpy as np
  3. import os, serial,time,socket,sys,json,logging,requests,getopt
  4. from meas_data import meas_data
  5. # import the server implementation
  6. # import ADC
  7. sensor="vedirect"
  8. pathname = os.path.dirname(sys.argv[0])
  9. abspath=os.path.abspath(pathname)
  10. idfile=abspath+"/id.json"
  11. id_conf=0
  12. try:
  13. idf=open(idfile,"r")
  14. id_conf=json.load(idf)['id']
  15. idf.close()
  16. except:
  17. id_conf=0
  18. configfile=abspath+"/config.json"
  19. try:
  20. cf=open(configfile,"r")
  21. except:
  22. cf=open(configfile+".template","r")
  23. log_conf=json.load(cf)
  24. cf.close()
  25. channel_names=[]
  26. channel_info={}
  27. parameter={"device":socket.gethostname(),"deviceid":"FF","mean_count":5,"ring_length":10,"wait":0.5,"cycle":10,"check_last":5,"check_last_shrinking":0.9,"gpg_keyid":"","digits":3,"store_dir":"/home/pi/log/","multiplicator":1000,"sigma":3,"sensor":sensor}
  28. for n in parameter:
  29. if n in log_conf:
  30. parameter[n]=log_conf[n]
  31. # declare ve mppt
  32. bve=False
  33. if sensor in log_conf:
  34. from vedirect import vedirect
  35. bmqtt=False
  36. if "mqtt" in log_conf:
  37. bmqtt=True
  38. lcmq=log_conf['mqtt']
  39. mbroker=""
  40. if 'broker' in lcmq:
  41. mbroker=lcmq['broker']
  42. else:
  43. bmqtt=False
  44. mport=1883
  45. if 'port' in lcmq:
  46. mport=lcmq['port']
  47. if "sqlserver" in log_conf:
  48. hostname="localhost"
  49. port=8080
  50. bve=True
  51. if "enable" in log_conf[sensor]:
  52. if log_conf[sensor]['enable'] == 0:
  53. bve=False
  54. if "port" in log_conf[sensor]:
  55. bve_port=log_conf[sensor]['port']
  56. else:
  57. bve_port='/dev/serial/by-id/usb-VictronEnergy_BV_VE_Direct_cable_VE1SSBVT-if00-port0'
  58. print(bve_port)
  59. try:
  60. ve=vedirect(bve_port,60)
  61. except:
  62. bve=False
  63. else:
  64. tsl_sigma=parameter['sigma']
  65. parameter["i2c"]=0
  66. for n in parameter:
  67. if n in log_conf['vedirect']:
  68. parameter[n]=log_conf['vedirect'][n]
  69. if "sigma" in log_conf['vedirect']:
  70. parameter['sigma']=int(log_conf['vedirect']['sigma'])
  71. if parameter['sigma'] < 1:
  72. parameter['sigma']=1
  73. varnames={"volt_bat":"V","volt_arr":"VPV","amp_bat":"I","watt":"PPV","days":"HSDS"}
  74. varnamess={"volt_bat":"voltage","volt_arr":"voltage","amp_bat":"current","watt":"power","days":"days"}
  75. mult={"volt_bat":1000,"volt_arr":1000,"amp_bat":1,"watt":1,"days":1}
  76. sensorn={"volt_bat":"VE_Battery","volt_arr":"VE_Array","amp_bat":"VE_Battery","watt":"VE_Battery","days":"VE_Array"}
  77. for i in varnames:
  78. parch=parameter
  79. parch["var_name"]=i
  80. parch["multiplicator"]=mult[i]
  81. channel_info[i]={"meas_data": meas_data(parch)}
  82. if bmqtt and ("mqtt" in log_conf[sensor]):
  83. channel_info[i]['meas_data'].set_mqtt(broker=mbroker,port=mport)
  84. if "sqlserver" in log_conf:
  85. channel_info[i]['meas_data'].set_sql(host=hostname,port=port)
  86. if bve:
  87. while True:
  88. try:
  89. vedata=ve.read_data_single()
  90. except:
  91. print("could not read VE")
  92. else:
  93. for i in ("volt_bat","volt_arr","amp_bat"):
  94. vedata[varnames[i]]=float(vedata[varnames[i]])/1000
  95. for i in varnames:
  96. cn=varnames[i]
  97. if cn in vedata:
  98. channel_info[i]['meas_data'].append(vedata[cn])
  99. time.sleep(parameter['wait'])
  100. # close the client
  101. print("done")