__init__.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #!/usr/bin/env python3.5
  2. # Copyright 2017 Digital
  3. #
  4. # This file is part of BeeWatch.
  5. #
  6. # BeeWatch is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # BeeWatch is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with BeeWatch. If not, see <http://www.gnu.org/licenses/>.
  18. import gi
  19. gi.require_version('Gtk', '3.0')
  20. from gi.repository import Gtk
  21. from gi.repository import Gdk
  22. import logging
  23. import logging.config
  24. import os
  25. import pprint
  26. import random
  27. import string
  28. import sys
  29. import time
  30. import yaml
  31. # My libraries
  32. import beewatch
  33. import beewatch.gui
  34. import digilib.misc
  35. import digilib.gui
  36. import digilib.network
  37. ## Logging
  38. lgui = logging.getLogger(__name__+".gui")
  39. class ChildConsole(digilib.gui.ChildConsole):
  40. def __init__(self,get_client=None):
  41. super(ChildConsole,self).__init__(get_client)
  42. self.text_buffer_log.set_text("program:HAIIII")
  43. self.add_msg(digilib.misc.LOREM_IPSUM,"code")
  44. self.add_msg(digilib.misc.LOREM_IPSUM,"code")
  45. self.add_msg(digilib.misc.LOREM_IPSUM,"code")
  46. class ChildOverview(digilib.gui.ChildOverview):
  47. def __init__(self):
  48. super(ChildOverview,self).__init__()
  49. self.prepare()
  50. def prepare(self):
  51. self.infobox1 = digilib.gui.InfoFrame()
  52. self.infobox2 = digilib.gui.InfoFrame()
  53. self.make_infobox_controller()
  54. self.make_infobox_weight()
  55. self.make_infobox_test(self.infobox1)
  56. self.make_infobox_test(self.infobox2)
  57. self.layout.attach(self.infobox_controller,0,0,1,1)
  58. self.layout.attach(self.infobox_weight, 1,0,1,1)
  59. self.layout.attach(self.infobox1, 0,1,1,1)
  60. self.layout.attach(self.infobox2, 1,1,1,1)
  61. def make_infobox_controller(self):
  62. levelbar_cpu = Gtk.LevelBar()
  63. levelbar_cpu.set_hexpand(True)
  64. levelbar_mem = Gtk.LevelBar()
  65. levelbar_mem.set_hexpand(True)
  66. self.infobox_controller = digilib.gui.InfoFrame(label="Weight")
  67. self.infobox_controller.add_line("uptime","Uptime:","99:59:592017-03-06 12:56")
  68. self.infobox_controller.add_line("ip","IP:","30.47.10.O9",None)
  69. self.infobox_controller.add_line("cpu","CPU:",30,"%",
  70. levelbar_cpu,True)
  71. self.infobox_controller.add_line("mem","Memory:",0.40,"%",
  72. levelbar_mem,True)
  73. def make_infobox_weight(self):
  74. self.infobox_weight = digilib.gui.InfoFrame(label="Weight")
  75. self.infobox_weight.add_line("total","Total:","50 kg")
  76. self.infobox_weight.add_line("bees","Bees:","20 kg")
  77. self.infobox_weight.add_line("honey","Honey:","42 kg")
  78. self.infobox_weight.add_line("time","Time:","2017-03-06 12:56")
  79. def make_infobox_test(self,infobox):
  80. infobox.add_line("1","Label:","Value")
  81. infobox.add_line("2","Baum:","Haus")
  82. infobox.add_line("3","Weight:","40 kg")
  83. infobox.add_line("4","Wiff","Woff")
  84. class ChildControl(digilib.gui.ChildControl):
  85. def __init__(self):
  86. super(ChildControl, self).__init__()
  87. self.prepare()
  88. def prepare(self):
  89. pass
  90. class ChildSettings(digilib.gui.ChildSettings):
  91. def __init__(self):
  92. super(ChildSettings, self).__init__()
  93. self.prepare()
  94. def prepare(self):
  95. pass
  96. class BeeWindow(digilib.gui.WindowBase):
  97. def __init__(self,host,port):
  98. super(BeeWindow,self).__init__(title="BeeWatch")
  99. self.host = host
  100. self.port = port
  101. self.client = None
  102. self.set_border_width(10)
  103. self.tb = digilib.misc.Container()
  104. self.prepare()
  105. self.add_key_shortcut("Control_L q",self.quit)
  106. self.add_key_shortcut("Control_L h e",lgui.info,["hello world"])
  107. self.add_key_shortcut("Control_L h i",lgui.info,["hi world"])
  108. # self.client = self.make_client()
  109. def prepare(self):
  110. self.child_console = ChildConsole(self.get_client)
  111. self.child_overview = ChildOverview()
  112. self.child_control = ChildControl()
  113. self.child_settings = ChildSettings()
  114. self.child_test = self.make_child_test()
  115. self.child_label = self.make_child_label()
  116. self.make_toolbar()
  117. self.stack = Gtk.Stack()
  118. self.stack.set_transition_type(Gtk.StackTransitionType.SLIDE_LEFT_RIGHT)
  119. self.stack.set_transition_duration(750)
  120. self.stack.add_titled(self.child_console, "child_console", "Console")
  121. self.stack.add_titled(self.child_overview, "child_overview", "Overview")
  122. self.stack.add_titled(self.child_control, "child_control", "Control")
  123. self.stack.add_titled(self.child_settings, "child_settings", "Settings")
  124. self.stack.add_titled(self.child_test, "check", "Check Button")
  125. self.stack.add_titled(self.child_label, "label", "A label")
  126. self.stack_switcher = Gtk.StackSwitcher()
  127. self.stack_switcher.set_stack(self.stack)
  128. self.scrolled_switcher = Gtk.ScrolledWindow()
  129. self.scrolled_switcher.add(self.stack_switcher)
  130. self.vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
  131. self.vbox.add(self.tb.toolbar)
  132. self.vbox.add(self.scrolled_switcher)
  133. self.vbox.add(self.stack)
  134. self.add(self.vbox)
  135. # self.vbox.pack_start(self.stack_switcher, True, True, 0)
  136. # self.vbox.pack_start(self.stack, True, True, 0)
  137. def make_toolbar(self):
  138. self.tb.toolbar = Gtk.Toolbar()
  139. self.tb.img_connect = Gtk.Image(stock=Gtk.STOCK_CONNECT)
  140. self.tb.img_connect.set_tooltip_text("Connect")
  141. self.tb.bconnect = Gtk.ToolButton()
  142. self.tb.bconnect.set_icon_widget(self.tb.img_connect)
  143. self.tb.bconnect.connect("clicked",self.on_connect_request)
  144. self.tb.img_disconnect = Gtk.Image(stock=Gtk.STOCK_DISCONNECT)
  145. self.tb.img_disconnect.set_tooltip_text("Disconnect")
  146. self.tb.bdisconnect = Gtk.ToolButton()
  147. self.tb.bdisconnect.set_icon_widget(self.tb.img_disconnect)
  148. self.tb.bdisconnect.connect("clicked",self.on_disconnect_request)
  149. self.tb.img_auto_scroll = Gtk.Image(stock=Gtk.STOCK_GOTO_BOTTOM)
  150. self.tb.img_auto_scroll.set_tooltip_text("Toggle auto scrolling")
  151. self.tb.bauto_scroll = Gtk.ToolButton()
  152. self.tb.bauto_scroll.set_icon_widget(self.tb.img_auto_scroll)
  153. self.tb.bauto_scroll.connect(
  154. "clicked",self.child_console.toggle_scroll)
  155. self.tb.img_scroll_down = Gtk.Image(stock=Gtk.STOCK_GO_DOWN)
  156. self.tb.img_scroll_down.set_tooltip_text("Toggle auto scrolling")
  157. self.tb.bscroll_down = Gtk.ToolButton()
  158. self.tb.bscroll_down.set_icon_widget(self.tb.img_scroll_down)
  159. self.tb.bscroll_down.connect(
  160. "clicked",self.child_console.scroll_down)
  161. self.tb.iconnected = Gtk.ToolItem()
  162. self.tb.img_connected = Gtk.Image(stock=Gtk.STOCK_YES)
  163. self.tb.iconnected.set_visible_horizontal(False)
  164. self.tb.iconnected.set_visible_vertical(False)
  165. self.tb.img_connected.set_tooltip_text("Connection Status: Connected")
  166. self.tb.idisconnected = Gtk.ToolItem()
  167. self.tb.img_disconnected = Gtk.Image(stock=Gtk.STOCK_NO)
  168. self.tb.img_disconnected.set_tooltip_text(
  169. "Connection Status: Disconnected")
  170. self.tb.iconnected.add(self.tb.img_connected)
  171. self.tb.idisconnected.add(self.tb.img_disconnected)
  172. self.tb.toolbar.add(self.tb.iconnected)
  173. self.tb.toolbar.add(self.tb.idisconnected)
  174. self.tb.toolbar.add(self.tb.bconnect)
  175. self.tb.toolbar.add(self.tb.bdisconnect)
  176. self.tb.toolbar.add(self.tb.bauto_scroll)
  177. self.tb.toolbar.add(self.tb.bscroll_down)
  178. def make_child_test(self):
  179. box_test = Gtk.FlowBox()
  180. for i in range(0,10):
  181. checkbutton = Gtk.CheckButton("Click me!")
  182. box_test.add(checkbutton)
  183. return box_test
  184. def make_child_label(self):
  185. grid = Gtk.Grid()
  186. label = Gtk.Label()
  187. label.set_markup("<big>A fancy label</big>")
  188. grid.add(label)
  189. return grid
  190. def make_client(self):
  191. client = digilib.network.Client(
  192. self.host,
  193. self.port,
  194. self.child_console.add_msg_threadsafe,
  195. )
  196. return client
  197. def get_client(self):
  198. return self.client
  199. def quit(self,*args):
  200. lgui.debug("quit")
  201. if self.client:
  202. self.client.stop()
  203. # if self.client.is_connected:
  204. Gtk.main_quit()
  205. def start(self):
  206. self.css_provider = Gtk.CssProvider()
  207. self.css_provider.load_from_path("config/style.css")
  208. screen = Gdk.Screen.get_default()
  209. self.style_context = Gtk.StyleContext()
  210. self.style_context.add_provider_for_screen(
  211. screen,
  212. self.css_provider,
  213. Gtk.STYLE_PROVIDER_PRIORITY_USER,
  214. )
  215. self.show_all()
  216. self.stack.set_visible_child(self.child_console)
  217. Gdk.threads_init()
  218. Gtk.main()
  219. def on_connect_request(self,widget):
  220. try:
  221. lgui.debug("connect request")
  222. if self.client == None:
  223. self.client = self.make_client()
  224. if not self.client.is_running():
  225. self.client.start()
  226. if not self.client.is_connected:
  227. if self.client.connect():
  228. self.tb.iconnected.set_visible_horizontal(True)
  229. self.tb.iconnected.set_visible_vertical(True)
  230. self.tb.idisconnected.set_visible_horizontal(False)
  231. self.tb.idisconnected.set_visible_vertical(False)
  232. lgui.debug("client started")
  233. else:
  234. lgui.debug(
  235. "client failed to connect, maybe it is connected")
  236. except Exception as e:
  237. lgui.error("error trying to connect",exc_info=e)
  238. def on_disconnect_request(self,widget):
  239. try:
  240. lgui.debug("disconnect request")
  241. if self.client != None:
  242. if self.client.is_connected:
  243. self.client.disconnect()
  244. self.tb.iconnected.set_visible_horizontal(False)
  245. self.tb.iconnected.set_visible_vertical(False)
  246. self.tb.idisconnected.set_visible_horizontal(True)
  247. self.tb.idisconnected.set_visible_vertical(True)
  248. lgui.debug("client disconnected")
  249. return
  250. lgui.debug(
  251. "client failed to disconnect, maybe it wasn't connected")
  252. except Exception as e:
  253. lgui.error("error trying to disconnect",exc_info=e)
  254. def on_scroll_down_request(self,widget):
  255. self.child_console.scroll_down()
  256. def on_toggle_scroll_request(self,widget):
  257. self.child_console.scroll = not self.child_console.scroll
  258. def on_exit_button_press(self,*args):
  259. self.destroy()
  260. Gtk.main_quit()
  261. return True