gui_log_haus.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. from Tkinter import *
  2. import numpy as np
  3. import os, time,socket,sys,json
  4. payloads=["wasser","gas","kwh_haus","kwh_herd"]
  5. def set_date():
  6. datafields["date"].delete(0,END)
  7. datafields["date"].insert(0,str(round(1000*time.time())))
  8. def send_data():
  9. json_out={"device":socket.gethostname(),"payload":{}}
  10. for x in payloads:
  11. json_out["payload"][x]={"time":0,"sensor":"rechnung","value":0,"i2c":0}
  12. local_time=round(1000*time.time())
  13. try:
  14. local_time=int(datafields["date"].get())
  15. except:
  16. local_time=round(1000*time.time())
  17. for x in payloads:
  18. try:
  19. json_out["payload"][x]["value"]=int(datafields[x].get())
  20. except:
  21. del(json_out["payload"][x])
  22. print("kein ",x)
  23. else:
  24. json_out["payload"][x]["time"]=local_time
  25. if len(json_out["payload"])>0:
  26. try:
  27. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  28. except:
  29. print("{}: could not connect to database".format(time.time()))
  30. else:
  31. try:
  32. s.connect(("banana", 24048))
  33. except:
  34. print("{}: could not connect to database".format(time.time()))
  35. else:
  36. s.sendall(json.dumps(json_out))
  37. s.close()
  38. json_out={"device":socket.gethostname(),"payload":{}}
  39. for x in payloads:
  40. json_out["payload"][x]={"time":0,"sensor":"rechnung","value":0,"i2c":0}
  41. groot=Tk()
  42. groot.wm_title("Hausverbrauch aufnehmen")
  43. groot.config(background="#FFFFFF")
  44. gframe=Frame(groot,width=400,height=300)
  45. gframe.grid(row=0,column=0,padx=10,pady=3)
  46. button_date=Button(gframe,text="Epoch",bg="#FF0000",width=15,command=set_date)
  47. button_date.grid(row=0,column=0,padx=10,pady=3)
  48. label_water=Label(gframe,text="Wasserzaehler")
  49. label_water.grid(row=1,column=0,padx=10,pady=3)
  50. label_gas=Label(gframe,text="Gaszaehler")
  51. label_gas.grid(row=2,column=0,padx=10,pady=3)
  52. label_electricity=Label(gframe,text="Stromzaehler")
  53. label_electricity.grid(row=3,column=0,padx=10,pady=3)
  54. label_oven=Label(gframe,text="Stromzaehler Herd")
  55. label_oven.grid(row=4,column=0,padx=10,pady=3)
  56. datafields={}
  57. felder=["date","wasser","gas","kwh_haus","kwh_herd"]
  58. for x in range(len(felder)):
  59. datafields[felder[x]]=Entry(gframe,width=18)
  60. datafields[felder[x]].grid(row=x,column=1,padx=10,pady=3)
  61. button_send=Button(gframe,text="Absenden",bg="#00FF00",width=15,command=send_data)
  62. button_send.grid(row=5,column=1,padx=10,pady=3)
  63. datafields["date"].insert(0,str(round(1000*time.time())))
  64. groot.mainloop()