コード例 #1
0
ファイル: DroneList.cs プロジェクト: Hendrik410/KKS_Drone
        public DroneList(Config config)
        {
            if (config == null)
                throw new ArgumentNullException(nameof(config));

            this.config = config;

            client = new UdpClient(config.ProtocolHelloPort);
            client.EnableBroadcast = true;
            client.BeginReceive(ReceivePacket, null);
        }
コード例 #2
0
ファイル: Drone.cs プロジェクト: Hendrik410/KKS_Drone
        public Drone(IPAddress address, Config config)
        {
            if (address == null)
                throw new ArgumentNullException(nameof(address));

            this.Config = config;
            this.Address = address;

            controlSocket = new UdpClient();
            controlSocket.Connect(address, Config.ProtocolControlPort);

            dataSocket = new UdpClient(Config.ProtocolDataPort);

            packetBuffer = new PacketBuffer(packetStream);

            controlSocket.BeginReceive(ReceivePacket, null);
            dataSocket.BeginReceive(ReceiveDataPacket, null);

            // Ping senden und ein ResetRevision Paket senden damit die Revision wieder zurück gesetzt wird
            SendPing();

            OnConnected += (sender, args) =>
            {
                Log.Info("Connected to {0}", Address);

                currentRevision = 1;
                lastDataDroneRevision = 0;
                lastDataLogRevision = 0;
                lastDataDebugRevision = 0;

                lastPing = Environment.TickCount;
                lastDataTime = Environment.TickCount;

                // alle Pending Packets leeren, damit die Drone nach Reconnect nicht überfordert wird
                lock (packetsToAcknowledge)
                {
                    packetsToAcknowledge.Clear();
                    packetSendTime.Clear();
                    packetAcknowlegdeEvents.Clear();
                }

                SendGetInfo();
                SendPacket(new PacketResetRevision(), true);
                SendPacket(new PacketCalibrateGyro(), true);
                SendPacket(new PacketSubscribeDataFeed(), true);
            };
        }
コード例 #3
0
ファイル: Config.cs プロジェクト: Hendrik410/KKS_Drone
        /// <summary>
        /// Lädt die Einstellungen aus einer Textdatei.
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public static Config LoadConfigFromFile(string file)
        {
            string[] fileLines = File.ReadAllLines(file);
            Config config = new Config();
            Type configType = config.GetType();

            for (int i = 0; i < fileLines.Length; i++)
            {
                string line = fileLines[i].Trim();
                if (line.Length == 0 || line.StartsWith("#")) // # wird für Kommentare benutzt
                    continue;

                string[] keyValue = line.Split('=');
                if (keyValue.Length == 0)
                    throw new InvalidDataException(string.Format("Unexpected line {0} in config file: '=' was not found.", i + 1));

                // Feld in der Config-Klasse finden
                PropertyInfo field = configType.GetProperty(keyValue[0].Trim(), BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
                if (field == null) // wenn Feld nicht gefunden dann ignorieren
                    continue;

                if (field.PropertyType == typeof(string))
                    field.SetValue(config, keyValue[1].Trim());
                else if (field.PropertyType == typeof(int))
                {
                    int value;
                    if (!TryParseInt(keyValue[1].Trim(), out value))
                        throw new InvalidDataException(string.Format("Unexpected line {0} in config file: can not parse number.", i + 1));

                    Range range = field.GetCustomAttribute<Range>();
                    if (range != null)
                    {
                        if (value < range.Min)
                            throw new InvalidDataException(string.Format("Unexpected line {0} in config file: value is smaller then {1}.", i + 1, range.Min));
                        else if (value > range.Max)
                            throw new InvalidDataException(string.Format("Unexpected line {0} in config file: value is bigger then {1}.", i + 1, range.Max));
                    }
                    field.SetValue(config, value);
                }
                else if (field.PropertyType == typeof(bool))
                {
                    string value = keyValue[1].Trim().ToLower();
                    if (value == "true" || value == "yes")
                        field.SetValue(config, true);
                    else if (value == "false" || value == "no")
                        field.SetValue(config, false);
                    else
                        throw new InvalidDataException(string.Format("Unexpected line {0} in config file: can not parse boolean value.", i + 1));
                }
                else if (field.PropertyType.IsEnum)
                {
                    string value = keyValue[1].Trim().ToLower();
                    field.SetValue(config, Enum.Parse(field.PropertyType, value, true));
                }
                else
                    throw new NotImplementedException();
            }

            return config;
        }