log_tristar.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. sensor="tristar"
  7. pathname = os.path.dirname(sys.argv[0])
  8. abspath=os.path.abspath(pathname)
  9. idfile=abspath+"/id.json"
  10. id_conf=0
  11. try:
  12. idf=open(idfile,"r")
  13. id_conf=json.load(idf)['id']
  14. idf.close()
  15. except:
  16. id_conf=0
  17. configfile=abspath+"/config.json"
  18. try:
  19. cf=open(configfile,"r")
  20. except:
  21. cf=open(configfile+".template","r")
  22. log_conf=json.load(cf)
  23. cf.close()
  24. channel_names=[]
  25. channel_info={}
  26. 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,"sensor":sensor,"sigma":3}
  27. for n in parameter:
  28. if n in log_conf:
  29. parameter[n]=log_conf[n]
  30. # configure tristar
  31. btristar=False
  32. if sensor in log_conf:
  33. btristar=True
  34. if "enable" in log_conf[sensor]:
  35. if log_conf[sensor]['enable'] == 0:
  36. btristar=False
  37. bmqtt=False
  38. if "mqtt" in log_conf:
  39. bmqtt=True
  40. lcmq=log_conf['mqtt']
  41. mbroker=""
  42. if 'broker' in lcmq:
  43. mbroker=lcmq['broker']
  44. else:
  45. bmqtt=False
  46. mport=1883
  47. if 'port' in lcmq:
  48. mport=lcmq['port']
  49. if "sqlserver" in log_conf:
  50. hostname="localhost"
  51. port=8080
  52. if btristar:
  53. import smbus
  54. from pymodbus.client.sync import ModbusSerialClient as ModbusClient
  55. for n in parameter:
  56. if n in log_conf[sensor]:
  57. parameter[n]=log_conf[sensor][n]
  58. tri_port='/dev/ttyUSB0'
  59. if "port" in log_conf[sensor]:
  60. tri_port=log_conf[sensor]['port']
  61. tri_baud=9600
  62. if "baud" in log_conf[sensor]:
  63. tri_baud=log_conf[sensor]['baud']
  64. tri_timeout=1
  65. if "timeout" in log_conf[sensor]:
  66. tri_timeout=log_conf[sensor]['timeout']
  67. try:
  68. triclient = ModbusClient(method='rtu', port=tri_port, baudrate=tri_baud, timeout=tri_timeout)
  69. except:
  70. btristar=False
  71. else:
  72. triclient.connect()
  73. check_last=0
  74. if "check_last" in log_conf[sensor]:
  75. check_last=log_conf[sensor]['check_last']
  76. tsl_sigma=parameter['sigma']
  77. parameter["i2c"]=0
  78. if "sigma" in log_conf[sensor]:
  79. parameter['sigma']=int(log_conf['vedirect']['sigma'])
  80. if parameter['sigma'] < 1:
  81. parameter['sigma']=1
  82. varnamess={"volt_scale":"volt_scale","amp_scale":"amp_scale","volt_bat":"voltage","volt_bat_sens":"voltage","volt_arr":"voltage","amp_bat":"current","amp_arr":"current","temp_heatsink":"temperature","temp_bat":"temperature","ah_res":"charge","ah_tot":"charge","kwh_res":"kwh","kwh_tot":"kwh","watt_in":"power","watt_out":"power","hour_tot":"hour","state":"state","volt_sweep_mp":"voltage","volt_sweep_oc":"voltage"}
  83. mult={"volt_scale":1000000,"amp_scale":1000000,"volt_bat":1000,"volt_bat_sens":1000,"volt_arr":1000,"amp_bat":1000,"amp_arr":1000,"temp_heatsink":1,"temp_bat":1,"ah_res":1,"ah_tot":1,"kwh_res":1,"kwh_tot":1,"watt_in":1000,"watt_out":1000,"hour_tot":1,"state":1,"volt_sweep_mp":1000,"volt_sweep_oc":1000}
  84. sensort={"volt_scale":"tristar","amp_scale":"tristar","volt_bat":"tristar_battery","volt_bat_sens":"tristar_sense","volt_arr":"tristar_array","amp_bat":"tristar_battery","amp_arr":"tristar_array","temp_heatsink":"tristar","temp_bat":"tristar_battery","ah_res":"tristar_battery","ah_tot":"tristar","kwh_res":"tristar_battery","kwh_tot":"tristar","watt_in":"tristar_array","watt_out":"tristar_battery","hour_tot":"tristar","state":"tristar","volt_sweep_mp":"tristar_array","volt_sweep_oc":"tristar_array"}
  85. i2c={"volt_scale":0,"amp_scale":0,"volt_bat":0,"volt_bat_sens":0,"volt_arr":0,"amp_bat":0,"amp_arr":0,"temp_heatsink":0,"temp_bat":0,"ah_res":0,"ah_tot":0,"kwh_res":0,"kwh_tot":0,"watt_in":0,"watt_out":0,"hour_tot":0,"state":0,"volt_sweep_mp":1,"volt_sweep_oc":2}
  86. for i in ["volt_scale","amp_scale","volt_bat","volt_bat_sens","volt_arr","amp_bat","amp_arr","temp_heatsink","temp_bat","ah_res","ah_tot","kwh_res","kwh_tot","watt_in","watt_out","hour_tot","state","volt_sweep_mp","volt_sweep_oc"]:
  87. parch=parameter
  88. parch["var_name"]=i
  89. parch["multiplicator"]=mult[i]
  90. channel_info[i]={"meas_data": meas_data(parch)}
  91. if bmqtt and ("mqtt" in log_conf[sensor]):
  92. channel_info[i]['meas_data'].set_mqtt(broker=mbroker,port=mport)
  93. if "sqlserver" in log_conf:
  94. channel_info[i]['meas_data'].set_sql(host=hostname,port=port)
  95. div_scale=2**31
  96. if btristar:
  97. while True:
  98. try:
  99. rr = triclient.read_holding_registers(0,90,unit=1)
  100. except:
  101. print("could not get data from tristar")
  102. else:
  103. try:
  104. volt_scale=float(rr.registers[0]*65536+rr.registers[1])/div_scale
  105. except:
  106. print("could not read from tristar")
  107. else:
  108. amp_scale=float(rr.registers[2]*65536+rr.registers[3])/div_scale
  109. channel_info["volt_scale"]['meas_data'].append(volt_scale)
  110. channel_info["amp_scale"]['meas_data'].append(amp_scale)
  111. channel_info["volt_bat"]['meas_data'].append(float(rr.registers[25])*volt_scale)
  112. channel_info["volt_bat_sens"]['meas_data'].append(float(rr.registers[26])*volt_scale)
  113. channel_info["volt_arr"]['meas_data'].append(float(rr.registers[27])*volt_scale) # Array voltage
  114. channel_info["amp_bat"]['meas_data'].append(float(rr.registers[28])*amp_scale) # Battery current
  115. channel_info["amp_arr"]['meas_data'].append(float(rr.registers[29])*amp_scale) # Array current
  116. channel_info["temp_heatsink"]['meas_data'].append(int(rr.registers[35])) # Temperature heatsink
  117. channel_info["temp_bat"]['meas_data'].append(int(rr.registers[36])) # Temperature battery
  118. channel_info["ah_res"]['meas_data'].append(int(rr.registers[52] * 65536 + rr.registers[53])) # Ah resetable
  119. channel_info["ah_tot"]['meas_data'].append(int(rr.registers[54] * 65536 + rr.registers[55])) # Ah total
  120. channel_info["kwh_res"]['meas_data'].append(int(rr.registers[56])) # kwh resetable
  121. channel_info["kwh_tot"]['meas_data'].append(int(rr.registers[57])) # kwh total
  122. channel_info["watt_in"]['meas_data'].append(float(rr.registers[58])*volt_scale*amp_scale*2**13) # Power in
  123. channel_info["watt_out"]['meas_data'].append(float(rr.registers[59])*volt_scale*amp_scale*2**13) # Power out
  124. channel_info["hour_tot"]['meas_data'].append(int(rr.registers[42] * 65536 + rr.registers[43])) # hour total
  125. channel_info["state"]['meas_data'].append(int(rr.registers[50])) # State
  126. channel_info["volt_sweep_mp"]['meas_data'].append(float(rr.registers[61])*volt_scale) # Array voltage
  127. channel_info["volt_sweep_oc"]['meas_data'].append(float(rr.registers[62])*volt_scale) # Array voltage
  128. time.sleep(parameter['wait'])
  129. # close the client
  130. print("done")