Example #1
0
        public CommandExecuter(Settings setting)
        {
            this.setting = setting;

            servo = new ProcessStartInfo("/bin/bash");
            servo.UseShellExecute = false;
            servo.CreateNoWindow = true;
            servo.RedirectStandardOutput = true;
            servo.RedirectStandardError = true;
         
#if !DEBUG
            pin1 = ConnectorPin.P1Pin38.Output();
            pin2 = ConnectorPin.P1Pin40.Output();
            connection = new GpioConnection(pin1, pin2);
#endif
        }
Example #2
0
        static void Main(string[] args)
        {
            if (!File.Exists(SettingFileName))
            {
                var settings = new Settings() { CarName = "carName", HubName = "CentralHost", Url = "http://url", ServoPin = "7", ServoLeft = "0", ServoRight = "100", ServoStraight = "50" };
                using (var file = File.Create(SettingFileName))
                {
                    using (var writer = new StreamWriter(file))
                    {
                        writer.Write(JsonConvert.SerializeObject(settings));
                        writer.Flush();
                    }
                }
                return;
            }

            HubConnector connector;
            CommandExecuter executer;
            try
            {
                using (var file = File.OpenText(SettingFileName))
                {
                    var setting = JsonConvert.DeserializeObject<Settings>(file.ReadToEnd());
                    connector = new HubConnector(setting.Url, setting.HubName, setting.CarName);
                    executer = new CommandExecuter(setting);
                }
                connector.ConectStart().Wait();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return;
            }

            try
            {
                Console.WriteLine("Ready");

                string ipaddress = "";
                IPHostEntry ipentry = Dns.GetHostEntry(Dns.GetHostName());

                foreach (IPAddress ip in ipentry.AddressList)
                {
                    if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                    {
                        ipaddress = ip.ToString();
                        break;
                    }
                }

                connector.SentMessage(ipaddress);
                System.Threading.CancellationTokenSource source = new System.Threading.CancellationTokenSource();
                connector.GetCommand += command =>
                {
                    source?.Cancel();
                    source = new System.Threading.CancellationTokenSource();
                    connector.StartExcuting();
                    executer.Excute(command, source.Token).ContinueWith(t => connector.EndExcuting()).Start();
                };

                connector.AbortExcuting += () =>
                {
                    if(!source.IsCancellationRequested)
                    {
                        source.Cancel();
                    }
                };

                connector.Shutdown += () =>
                {
                    Console.WriteLine("Shutdown");
                };

                connector.Reboot += () =>
                {
                    Console.WriteLine("Reboot");
                };

                connector.ConnectingToClient += client => Console.WriteLine($"{client} is connected");

                connector.DisconnectingFromClient += () => Console.WriteLine("disconected");

                Console.Read();
            }
            catch (Exception ex)
            {
                connector.SentMessage(ex.ToString());
            }
            finally
            {
                connector.ConectStop().Wait();
                executer?.Dispose();
            }
        }