import sys
import os
import socket

from gi.repository import Gtk

# 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")
        
        self.m_view = builder.get_object("textview1")
        self.m_buffer = builder.get_object("textbuffer1")
        self.m_entry = builder.get_object("entry1")
        
        self.m_window = builder.get_object('window1')
        self.m_window.set_title("Crown Console")      
        self.m_window.show_all()

        builder.connect_signals(self)

        self.m_sock = socket.create_connection(('192.168.0.15', 10000))

        Gtk.main()

#------------------------------------------------------------------------------
    def on_destroy(self, *args):
        self.m_sock.close()
        Gtk.main_quit(*args)

#------------------------------------------------------------------------------
    def on_key_pressed(self, entry, event):
        # If return is pressed, run command
        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.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()

main()
    

@@ -298,16 +298,38 @@
  console
+%0ACMD_FRAME   = %22frame%22
 %0A%0A# Help
@@ -612,16 +612,25 @@
 t__(self
+, address
 ):   %0A  
@@ -1073,16 +1073,50 @@
 (self)%0A%0A
+        self.m_address = address%0A%0A
         
@@ -1159,22 +1159,22 @@
 on((
-'192.168.0.15'
+self.m_address
 , 10
@@ -2103,32 +2103,42 @@
 elf.on_destroy()
+          
 %0A%0A        elif c
@@ -3708,16 +3708,137 @@
 main():%0A
+    if len(sys.argv) != 2:%0A        print(%22Usage: console.py %3Cip-address%3E%22)%0A        exit(-1)%0A%0A    address = sys.argv%5B1%5D%0A%0A 
    conso
@@ -3850,17 +3850,25 @@
 Console(
-)
+address)%0A
 %0A%0Amain()

include ip address in console client