console.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import sys
  2. import os
  3. import socket
  4. from gi.repository import Gtk
  5. # Client Console commands
  6. CMD_CLEAR = "clear" # Clear console output
  7. CMD_EXIT = "exit" # Close console
  8. CMD_HELP = "help" # Console help
  9. # Server Console commands
  10. CMD_STOP = "device stop" # Stop Engine and close console
  11. # Help message
  12. MSG_HELP = "1- clear - clear screen output\n2- exit - terminate console\n3- device stop - terminate engine and console\n4- help - print this message\n"
  13. class Console:
  14. #------------------------------------------------------------------------------
  15. def __init__(self):
  16. builder = Gtk.Builder()
  17. builder.add_from_file("ui/console.glade")
  18. self.m_view = builder.get_object("textview1")
  19. self.m_buffer = builder.get_object("textbuffer1")
  20. self.m_entry = builder.get_object("entry1")
  21. self.m_window = builder.get_object('window1')
  22. self.m_window.set_title("Crown Console")
  23. self.m_window.show_all()
  24. builder.connect_signals(self)
  25. self.m_sock = socket.create_connection(('192.168.0.15', 10000))
  26. Gtk.main()
  27. #------------------------------------------------------------------------------
  28. def on_destroy(self, *args):
  29. self.m_sock.close()
  30. Gtk.main_quit(*args)
  31. #------------------------------------------------------------------------------
  32. def on_key_pressed(self, entry, event):
  33. # If return is pressed, run command
  34. if event.keyval == 0xff0d :
  35. cmd = entry.get_text()
  36. self.parse_command(cmd)
  37. #------------------------------------------------------------------------------
  38. def parse_command(self, cmd):
  39. if cmd == CMD_CLEAR:
  40. self.m_buffer.set_text("")
  41. self.m_entry.set_text("")
  42. elif cmd == CMD_EXIT:
  43. self.on_destroy()
  44. elif cmd == CMD_STOP:
  45. self.run_command(cmd)
  46. self.popup_dialog("Crown has stopped!", "Console connection will be closed")
  47. self.on_destroy()
  48. elif cmd == CMD_HELP:
  49. self.print_help()
  50. else:
  51. self.run_command(cmd)
  52. #------------------------------------------------------------------------------
  53. def run_command(self, cmd):
  54. # Send command to Crown
  55. self.m_sock.send(cmd.encode())
  56. self.print_command(cmd)
  57. #------------------------------------------------------------------------------
  58. def print_command(self, cmd):
  59. # Print command to console
  60. end_iter = self.m_buffer.get_end_iter()
  61. a_string = "> " + cmd + "\n"
  62. # Append command to the end of buffer
  63. self.m_buffer.insert(end_iter, a_string, len(a_string))
  64. # Reset entry
  65. self.m_entry.set_text("")
  66. #------------------------------------------------------------------------------
  67. def print_help(self):
  68. end_iter = self.m_buffer.get_end_iter()
  69. a_string = MSG_HELP + "\n"
  70. # Append command to the end of buffer
  71. self.m_buffer.insert(end_iter, a_string, len(a_string))
  72. # Reset entry
  73. self.m_entry.set_text("")
  74. #------------------------------------------------------------------------------
  75. def popup_dialog(self, message, expl):
  76. dialog = Gtk.MessageDialog(self.m_window, 0, Gtk.MessageType.INFO, Gtk.ButtonsType.OK, message)
  77. dialog.format_secondary_text(expl)
  78. dialog.run()
  79. dialog.destroy()
  80. #------------------------------------------------------------------------------
  81. #------------------------------------------------------------------------------
  82. def main():
  83. console = Console()
  84. main()