Exemplo n.º 1
0
        public MachinaBridgeWindow()
        {
            InitializeComponent();

            dc = new BoundContent(this);

            DataContext = dc;

            uiContext = SynchronizationContext.Current;

            _maxLogLevel          = Machina.LogLevel.VERBOSE;
            Logger.CustomLogging += Logger_CustomLogging;

            Logger.Info("Machina Bridge: " + Version + "; Core: " + Robot.Version);

            InitializeWebSocketServer();

            // Handle pasting text on the input
            DataObject.AddPastingHandler(InputBlock, InputBlock_Paste);

#if DEBUG
            var item = combo_LogLevel.Items.GetItemAt(5) as ComboBoxItem;
            item.IsSelected = true;
#endif
        }
        protected override void OnOpen()
        {
            //base.OnOpen();
            //Console.WriteLine("  BRIDGE: opening bridge");
            _clientName = Context.QueryString["name"];
            _parent._connectedClients.Add(_clientName);
            //_parent.UpdateClientBox();
            _parent.uiContext.Post(x =>
            {
                _parent.UpdateClientBox();
            }, null);

            Logger.Info("Client \"" + _clientName + "\" connected...");
        }
Exemplo n.º 3
0
        private bool InitializeWebSocketServer()
        {
            if (!ParseWebSocketURL())
            {
                Logger.Error("Invalid WebSocket URL \"" + txtbox_WSServerURL.Text + "\"; try something like \"ws://127.0.0.1/Bridge\"");
                return(false);
            }

            if (wssv != null && wssv.IsListening)
            {
                StopWebSocketServer();
            }

            wssv = new WebSocketServer(wssvURL);
            wssv.AddWebSocketService(wssvBehavior, () => new BridgeBehavior(bot, this));

            // @TODO: add a check here if the port is in use, and try a different port instead
            try
            {
                wssv.Start();
            }
            catch
            {
                Logger.Error("Default websocket server is not available, please enter a new one manually...");
                return(false);
            }

            if (wssv.IsListening)
            {
                //Machina.Logger.Info($"Listening on port {wssv.Port}, and providing WebSocket services:");
                //foreach (var path in wssv.WebSocketServices.Paths) Machina.Logger.Info($"- {path}");
                Logger.Info("Waiting for incoming connections on " + (wssvURL + wssvBehavior));
            }

            return(true);
            //lbl_ServerURL.Content = wssvURL + wssvBehavior;
        }
Exemplo n.º 4
0
        internal void DownloadDrivers()
        {
            Logger.Info("Downloading Machina Drivers for " + _robotBrand + " robot on " + txtbox_IP.Text + ":" + txtbox_Port.Text);

            // Create a fake robot not to interfere with the main one
            Robot driverBot = Robot.Create(_robotName, _robotBrand);

            driverBot.ControlMode(ControlType.Online);
            var parameters = new Dictionary <string, string>()
            {
                { "HOSTNAME", txtbox_IP.Text },
                { "PORT", txtbox_Port.Text }
            };

            var files = driverBot.GetDeviceDriverModules(parameters);

            // Clear temp folder
            string path = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "machina_modules");

            //https://stackoverflow.com/a/1288747/1934487
            System.IO.DirectoryInfo di = new DirectoryInfo(path);
            if (di.Exists)
            {
                Logger.Debug("Clearing " + path);
                foreach (FileInfo file in di.GetFiles())
                {
                    Logger.Debug("Deleting " + file.FullName);
                    file.Delete();
                }
                foreach (DirectoryInfo dir in di.GetDirectories())
                {
                    Logger.Debug("Deleting " + dir.FullName);
                    dir.Delete(true);
                }
            }
            else
            {
                di.Create();
                Logger.Debug("Created " + path);
            }

            // Save temp files
            foreach (var pair in files)
            {
                string filename = pair.Key;
                string content  = pair.Value;


                string filepath = System.IO.Path.Combine(path, filename);
                try
                {
                    System.IO.File.WriteAllText(filepath, content, Encoding.ASCII);
                }
                catch
                {
                    Logger.Error("Could not save " + filename + " to " + filepath);
                    Logger.Error("Could not download drivers");
                    return;
                }

                Logger.Debug("Saved module to " + filepath);
            }

            // Zip the file
            string zipPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "machina_modules.zip");

            System.IO.FileInfo fi = new FileInfo(zipPath);
            if (fi.Exists)
            {
                fi.Delete();
                Logger.Debug("Deleted previous " + zipPath);
            }
            ZipFile.CreateFromDirectory(path, zipPath);
            Logger.Debug("Zipped files to " + zipPath);

            // Prompt file save dialog: https://www.wpf-tutorial.com/dialogs/the-savefiledialog/
            SaveFileDialog saveFileDialog = new SaveFileDialog();

            saveFileDialog.Filter           = "Zip file (*.zip)|*.zip";
            saveFileDialog.DefaultExt       = "zip";
            saveFileDialog.AddExtension     = true;
            saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            saveFileDialog.FileName         = "machina_modules.zip";

            if (saveFileDialog.ShowDialog() == true)
            {
                fi = new FileInfo(saveFileDialog.FileName);
                if (fi.Exists)
                {
                    fi.Delete();
                    Logger.Debug("Deleted previous " + saveFileDialog.FileName);
                }
                File.Copy(zipPath, saveFileDialog.FileName);
                Logger.Debug("Copied " + zipPath + " to " + saveFileDialog.FileName);

                Logger.Info("Drivers saved to " + saveFileDialog.FileName);
            }
        }
        /// <summary>
        /// Initialize WebSocket communication infrastructure
        /// </summary>
        /// <returns></returns>
        private bool InitializeWebSocketServer()
        {
            // Close previous instances, if applicable
            if (wssv != null && wssv.IsListening)
            {
                StopWebSocketServer();
            }
            if (wscl != null && wscl.IsAlive)
            {
                StopWebSocketClient();
            }

            wsURL    = txtbox_WSServerURL.Text;
            _authkey = psswrdbox_Key.Password;

            // Check if given URL is a valid Machina Server
            try
            {
                Logger.Verbose("Trying connection to Machina Server on " + wsURL);

                // Add relevant auth info as query parameters
                string fullURL = $"{wsURL}?name={_robotName}&client=machina-bridge&authkey={_authkey}";

                // Establish connection
                wscl            = new WebSocket(fullURL);
                wscl.OnOpen    += Wscl_OnOpen;
                wscl.OnMessage += Wscl_OnMessage;
                wscl.OnClose   += Wscl_OnClose;
                wscl.OnError   += Wscl_OnError;
                wscl.Connect();
                //wscl.Send("hello from Bridge");

                if (wscl.IsAlive)
                {
                    Logger.Info("Successful connection to Machina Remote Server on " + wsURL);
                }
                else
                {
                    throw new Exception();
                }
            }
            catch
            {
                Logger.Verbose("Could not connect to existing Machina Server, initializing locally...");

                // Check validity of URL
                if (!ParseWebSocketURL())
                {
                    Logger.Error("Invalid WebSocket URL \"" + txtbox_WSServerURL.Text + "\"; try something like \"ws://127.0.0.1/Bridge\"");
                    return(false);
                }

                wssv = new WebSocketServer(wsURL);
                wssv.AddWebSocketService(wssvBehavior, () => new WSServerBehavior(bot, this));

                // @TODO: add a check here if the port is in use, and try a different port instead
                try
                {
                    wssv.Start();
                }
                catch
                {
                    Logger.Error("Default websocket server is not available, please enter a new one manually...");
                    return(false);
                }

                if (wssv.IsListening)
                {
                    //Machina.Logger.Info($"Listening on port {wssv.Port}, and providing WebSocket services:");
                    //foreach (var path in wssv.WebSocketServices.Paths) Machina.Logger.Info($"- {path}");
                    Logger.Verbose("Successful initialization of Machina Local Server on " + wsURL);
                    Logger.Info("Waiting for incoming connections on Machina Local Server " + (wsURL + wssvBehavior));
                }
            }

            return(true);
            //lbl_ServerURL.Content = wssvURL + wssvBehavior;
        }