Przeglądaj źródła

improve console tool

mikymod 12 lat temu
rodzic
commit
9dcf2af444
1 zmienionych plików z 66 dodań i 9 usunięć
  1. 66 9
      tools/gui/console/console.py

+ 66 - 9
tools/gui/console/console.py

@@ -4,9 +4,19 @@ import socket
 
 from gi.repository import Gtk
 
-#------------------------------------------------------------------------------
-class Console:
+# Client Console commands
+CMD_CLEAR   = "clear"   # Clear console output
+CMD_EXIT    = "exit"    # Close console
+CMD_HELP    = "help"    # Console help
+
+# Server Console commands
+CMD_STOP    = "device stop" # Stop Engine and close console
+
+# Help message
+MSG_HELP    =   "1- clear - clear screen output\n2- exit  - terminate console\n3- device stop - terminate engine and console\n4- help - print this message\n"
 
+class Console:
+#------------------------------------------------------------------------------
     def __init__(self):   
         builder = Gtk.Builder()
         builder.add_from_file("ui/console.glade")
@@ -21,29 +31,76 @@ class Console:
 
         builder.connect_signals(self)
 
-        #self.m_sock = socket.create_connection(('localhost', 10000))
+        self.m_sock = socket.create_connection(('localhost', 10000))
 
         Gtk.main()
-    
+
+#------------------------------------------------------------------------------
     def on_destroy(self, *args):
-        #self.m_sock.close()
+        self.m_sock.shutdown()
         Gtk.main_quit(*args)
 
+#------------------------------------------------------------------------------
     def on_key_pressed(self, entry, event):
         # If return is pressed, run command
-        if event.keyval == 0xff0d:  
-            self.run_command(entry.get_text())
+        if event.keyval == 0xff0d :
+            cmd = entry.get_text()
+            self.parse_command(cmd)
+
+#------------------------------------------------------------------------------
+    def parse_command(self, cmd):
+        if cmd == CMD_CLEAR:
+            self.m_buffer.set_text("")
+            self.m_entry.set_text("")
+
+        elif cmd == CMD_EXIT:
+            self.on_destroy()
+
+        elif cmd == CMD_STOP:
+            self.run_command(cmd)
+            self.popup_dialog("Crown has stopped!", "Console connection will be closed")
+            self.on_destroy()
+
+        elif cmd == CMD_HELP:
+            self.print_help()
 
+        else:    
+            self.run_command(cmd)        
+
+#------------------------------------------------------------------------------
     def run_command(self, cmd):
         # Send command to Crown
-            #self.m_sock.send(cmd)
+        self.m_sock.send(cmd.encode())
+        self.print_command(cmd)
+
+#------------------------------------------------------------------------------
+    def print_command(self, cmd):
         # Print command to console
         end_iter = self.m_buffer.get_end_iter()
         a_string = "> " + cmd + "\n"
+        # Append command to the end of buffer
         self.m_buffer.insert(end_iter, a_string, len(a_string))
+        # Reset entry
+        self.m_entry.set_text("")
+
+
+#------------------------------------------------------------------------------
+    def print_help(self):
+        end_iter = self.m_buffer.get_end_iter()
+        a_string = MSG_HELP + "\n"
+        # Append command to the end of buffer
+        self.m_buffer.insert(end_iter, a_string, len(a_string))
+        # Reset entry
         self.m_entry.set_text("")
-    
 
+#------------------------------------------------------------------------------
+    def popup_dialog(self, message, expl):
+        dialog = Gtk.MessageDialog(self.m_window, 0, Gtk.MessageType.INFO, Gtk.ButtonsType.OK, message)
+        dialog.format_secondary_text(expl)
+        dialog.run()
+        dialog.destroy()
+
+#------------------------------------------------------------------------------
 #------------------------------------------------------------------------------
 def main():
    console = Console()