log_cpu.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/usr/bin/env python
  2. import numpy as np
  3. import os, time,socket,sys,json,random
  4. from meas_data import meas_data
  5. sensor="CPU"
  6. pathname = os.path.dirname(sys.argv[0])
  7. abspath=os.path.abspath(pathname)
  8. idfile=abspath+"/id.json"
  9. id_conf=0
  10. try:
  11. idf=open(idfile,"r")
  12. id_conf=json.load(idf)['id']
  13. idf.close()
  14. except:
  15. id_conf=0
  16. configfile=abspath+"/config.json"
  17. try:
  18. cf=open(configfile,"r")
  19. except:
  20. cf=open(configfile+".template","r")
  21. log_conf=json.load(cf)
  22. cf.close()
  23. parameter={"device":socket.gethostname(),"deviceid":"FF","mean_count":5,"ring_length":300,"wait":0.1,"cycle":20,"check_last":5,"gpg_keyid":"","digits":4,"store_dir":"/home/pi/log/"}
  24. for n in parameter:
  25. if n in log_conf:
  26. parameter[n]=log_conf[n]
  27. bsensor=False
  28. if sensor in log_conf:
  29. if "enable" in log_conf[sensor]:
  30. if log_conf[sensor]["enable"] == 1:
  31. bsensor=True
  32. if bsensor:
  33. parameter["sensor"]=sensor
  34. if "temperature" in log_conf[sensor]["var_name"]:
  35. parameter["var_name"]="temperature"
  36. else:
  37. bsensor=False
  38. if bsensor:
  39. cpu_meas=meas_data(parameter)
  40. # cpu_meas=meas_data(var_name="temperature",ring_length=parameter['ring_length'],device=parameter['device'],sensor="CPU",store_dir="/home/pi/log",digits=4,check_last=parameter['check_last'],id_conf=id_conf,deviceid=parameter['deviceid'])
  41. cpu_meas.set_sql(host="localhost",port=8080,min_wait=parameter['wait'])
  42. cpu_meas.set_rsa("25A4CF79414F10FD")
  43. bmqtt=False
  44. if "mqtt" in log_conf:
  45. bmqtt=True
  46. lcmq=log_conf['mqtt']
  47. mbroker=""
  48. if 'broker' in lcmq:
  49. mbroker=lcmq['broker']
  50. else:
  51. bmqtt=False
  52. mport=1883
  53. if 'port' in lcmq:
  54. mport=lcmq['port']
  55. if bmqtt:
  56. cpu_meas.set_mqtt(broker=mbroker,port=mport)
  57. a = 2
  58. while True:
  59. # get cpu temperature
  60. cpu_meas.append(int(open('/sys/class/thermal/thermal_zone0/temp').read()))
  61. time.sleep(parameter['wait'])
  62. # close the client
  63. print("done")