__init__.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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,af_family):
  98. super(BeeWindow,self).__init__(title="BeeWatch")
  99. self.host = host
  100. self.port = port
  101. self.af_family = af_family
  102. self.client = None
  103. self.set_border_width(10)
  104. self.tb = digilib.misc.Container()
  105. self.prepare()
  106. self.add_key_shortcut("Control_L q",self.quit)
  107. self.add_key_shortcut("Control_L h e",lgui.info,["hello world"])
  108. self.add_key_shortcut("Control_L h i",lgui.info,["hi world"])
  109. # self.client = self.make_client()
  110. def prepare(self):
  111. self.child_console = ChildConsole(self.get_client)
  112. self.child_overview = ChildOverview()
  113. self.child_control = ChildControl()
  114. self.child_settings = ChildSettings()
  115. self.child_test = self.make_child_test()
  116. self.child_label = self.make_child_label()
  117. self.make_toolbar()
  118. self.stack = Gtk.Stack()
  119. self.stack.set_transition_type(Gtk.StackTransitionType.SLIDE_LEFT_RIGHT)
  120. self.stack.set_transition_duration(750)
  121. self.stack.add_titled(self.child_console, "child_console", "Console")
  122. self.stack.add_titled(self.child_overview, "child_overview", "Overview")
  123. self.stack.add_titled(self.child_control, "child_control", "Control")
  124. self.stack.add_titled(self.child_settings, "child_settings", "Settings")
  125. self.stack.add_titled(self.child_test, "check", "Check Button")
  126. self.stack.add_titled(self.child_label, "label", "A label")
  127. self.stack_switcher = Gtk.StackSwitcher()
  128. self.stack_switcher.set_stack(self.stack)
  129. self.scrolled_switcher = Gtk.ScrolledWindow()
  130. self.scrolled_switcher.add(self.stack_switcher)
  131. self.vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
  132. self.vbox.add(self.tb.toolbar)
  133. self.vbox.add(self.scrolled_switcher)
  134. self.vbox.add(self.stack)
  135. self.add(self.vbox)
  136. # self.vbox.pack_start(self.stack_switcher, True, True, 0)
  137. # self.vbox.pack_start(self.stack, True, True, 0)
  138. def make_toolbar(self):
  139. self.tb.toolbar = Gtk.Toolbar()
  140. self.tb.img_connect = Gtk.Image(stock=Gtk.STOCK_CONNECT)
  141. self.tb.img_connect.set_tooltip_text("Connect")
  142. self.tb.bconnect = Gtk.ToolButton()
  143. self.tb.bconnect.set_icon_widget(self.tb.img_connect)
  144. self.tb.bconnect.connect("clicked",self.on_connect_request)
  145. self.tb.img_disconnect = Gtk.Image(stock=Gtk.STOCK_DISCONNECT)
  146. self.tb.img_disconnect.set_tooltip_text("Disconnect")
  147. self.tb.bdisconnect = Gtk.ToolButton()
  148. self.tb.bdisconnect.set_icon_widget(self.tb.img_disconnect)
  149. self.tb.bdisconnect.connect("clicked",self.on_disconnect_request)
  150. self.tb.img_auto_scroll = Gtk.Image(stock=Gtk.STOCK_GOTO_BOTTOM)
  151. self.tb.img_auto_scroll.set_tooltip_text("Toggle auto scrolling")
  152. self.tb.bauto_scroll = Gtk.ToolButton()
  153. self.tb.bauto_scroll.set_icon_widget(self.tb.img_auto_scroll)
  154. self.tb.bauto_scroll.connect(
  155. "clicked",self.child_console.toggle_scroll)
  156. self.tb.img_scroll_down = Gtk.Image(stock=Gtk.STOCK_GO_DOWN)
  157. self.tb.img_scroll_down.set_tooltip_text("Toggle auto scrolling")
  158. self.tb.bscroll_down = Gtk.ToolButton()
  159. self.tb.bscroll_down.set_icon_widget(self.tb.img_scroll_down)
  160. self.tb.bscroll_down.connect(
  161. "clicked",self.child_console.scroll_down)
  162. self.tb.iconnected = Gtk.ToolItem()
  163. self.tb.img_connected = Gtk.Image(stock=Gtk.STOCK_YES)
  164. self.tb.iconnected.set_visible_horizontal(False)
  165. self.tb.iconnected.set_visible_vertical(False)
  166. self.tb.img_connected.set_tooltip_text("Connection Status: Connected")
  167. self.tb.idisconnected = Gtk.ToolItem()
  168. self.tb.img_disconnected = Gtk.Image(stock=Gtk.STOCK_NO)
  169. self.tb.img_disconnected.set_tooltip_text(
  170. "Connection Status: Disconnected")
  171. self.tb.iconnected.add(self.tb.img_connected)
  172. self.tb.idisconnected.add(self.tb.img_disconnected)
  173. self.tb.toolbar.add(self.tb.iconnected)
  174. self.tb.toolbar.add(self.tb.idisconnected)
  175. self.tb.toolbar.add(self.tb.bconnect)
  176. self.tb.toolbar.add(self.tb.bdisconnect)
  177. self.tb.toolbar.add(self.tb.bauto_scroll)
  178. self.tb.toolbar.add(self.tb.bscroll_down)
  179. def make_child_test(self):
  180. box_test = Gtk.FlowBox()
  181. for i in range(0,10):
  182. checkbutton = Gtk.CheckButton("Click me!")
  183. box_test.add(checkbutton)
  184. return box_test
  185. def make_child_label(self):
  186. grid = Gtk.Grid()
  187. label = Gtk.Label()
  188. label.set_markup("<big>A fancy label</big>")
  189. grid.add(label)
  190. return grid
  191. def make_client(self):
  192. client = digilib.network.Client(
  193. host=self.host,
  194. port=self.port,
  195. af_family=self.af_family,
  196. handle_data_func=self.child_console.add_msg_threadsafe,
  197. )
  198. return client
  199. def get_client(self):
  200. return self.client
  201. def quit(self,*args):
  202. lgui.debug("quit")
  203. if self.client:
  204. self.client.stop()
  205. # if self.client.is_connected:
  206. Gtk.main_quit()
  207. def start(self):
  208. self.css_provider = Gtk.CssProvider()
  209. self.css_provider.load_from_path("config/style.css")
  210. screen = Gdk.Screen.get_default()
  211. self.style_context = Gtk.StyleContext()
  212. self.style_context.add_provider_for_screen(
  213. screen,
  214. self.css_provider,
  215. Gtk.STYLE_PROVIDER_PRIORITY_USER,
  216. )
  217. self.show_all()
  218. self.stack.set_visible_child(self.child_console)
  219. Gdk.threads_init()
  220. Gtk.main()
  221. def on_connect_request(self,widget):
  222. try:
  223. lgui.debug("connect request")
  224. if self.client == None:
  225. self.client = self.make_client()
  226. if not self.client.is_running():
  227. self.client.start()
  228. if not self.client.is_connected:
  229. if self.client.connect():
  230. self.tb.iconnected.set_visible_horizontal(True)
  231. self.tb.iconnected.set_visible_vertical(True)
  232. self.tb.idisconnected.set_visible_horizontal(False)
  233. self.tb.idisconnected.set_visible_vertical(False)
  234. lgui.debug("client started")
  235. else:
  236. lgui.debug(
  237. "client failed to connect, maybe it is connected")
  238. except Exception as e:
  239. lgui.error("error trying to connect",exc_info=e)
  240. def on_disconnect_request(self,widget):
  241. try:
  242. lgui.debug("disconnect request")
  243. if self.client != None:
  244. if self.client.is_connected:
  245. self.client.disconnect()
  246. self.tb.iconnected.set_visible_horizontal(False)
  247. self.tb.iconnected.set_visible_vertical(False)
  248. self.tb.idisconnected.set_visible_horizontal(True)
  249. self.tb.idisconnected.set_visible_vertical(True)
  250. lgui.debug("client disconnected")
  251. return
  252. lgui.debug(
  253. "client failed to disconnect, maybe it wasn't connected")
  254. except Exception as e:
  255. lgui.error("error trying to disconnect",exc_info=e)
  256. def on_scroll_down_request(self,widget):
  257. self.child_console.scroll_down()
  258. def on_toggle_scroll_request(self,widget):
  259. self.child_console.scroll = not self.child_console.scroll
  260. def on_exit_button_press(self,*args):
  261. self.destroy()
  262. Gtk.main_quit()
  263. return True