public void ParsePythonApi()
        {
            var json = loadJsonFile("pythonApi.json");

            var simData = json.ToObject <SimulationData>();

            SimulationConfigUtils.ProcessKnownTemplates(ref simData);

            Assert.That(simData.Template != null, Is.True);
            Assert.That(simData.Template.Alias, Is.EqualTo("pythonApi"));
            Assert.That(simData.Template.Parameters.Length, Is.EqualTo(3));

            Assert.That(simData.ApiOnly, Is.True);

            var s = Newtonsoft.Json.JsonConvert.SerializeObject(simData, JsonSettings.camelCase);

            Debug.Log($"Simulation data: {s}");

            Assert.That(simData.Map == null, Is.True);
            Assert.That(simData.Vehicles == null, Is.True);

            var environment = new Dictionary <string, string>();

            SimulationConfigUtils.UpdateTestCaseEnvironment(simData.Template, environment);

            Assert.That(environment["SIMULATOR_API_ONLY"], Is.EqualTo("1"));
            Assert.That(environment["SIMULATOR_TC_FILENAME"], Is.EqualTo("scenario.py"));
        }
        public void ParseRandomTrafficFull()
        {
            var json = loadJsonFile("randomTrafficFull.json");

            var simData = json.ToObject <SimulationData>();

            SimulationConfigUtils.ProcessKnownTemplates(ref simData);

            Assert.That(simData.Template != null, Is.True);
            Assert.That(simData.Template.Alias, Is.EqualTo("randomTraffic"));
            Assert.That(simData.Template.Parameters.Length, Is.EqualTo(12));

            Assert.That(simData.ApiOnly, Is.False);

            var s = Newtonsoft.Json.JsonConvert.SerializeObject(simData, JsonSettings.camelCase);

            Debug.Log($"Simulation data: {s}");

            Assert.That(simData.Map != null, Is.True);
            Assert.That(simData.Map.Id, Is.EqualTo("a7b32bdb-acc3-40e9-b893-29e4955a209f"));
            Assert.That(simData.Vehicles != null, Is.True);
            Assert.That(simData.Vehicles.Length, Is.EqualTo(1));
            Assert.That(simData.Vehicles[0].Id, Is.EqualTo("a88495aa-e4f9-4e1f-a790-cdbca41deeff"));
            Assert.That(simData.Seed, Is.EqualTo(42));
        }
        public void ParseRandomTrafficWithoutVehicles()
        {
            // Config data does not contain SIMULATOR_MAP parameter
            var json = loadJsonFile("randomTrafficWithoutVehicles.json");

            var simData = json.ToObject <SimulationData>();

            Assert.That(
                () => { SimulationConfigUtils.ProcessKnownTemplates(ref simData); },
                Throws.InstanceOf <MissedTemplateParameterError>().With.Message.EqualTo("SIMULATOR_VEHICLES")
                );
        }
        public void ParseLeagcy01()
        {
            var json = loadJsonFile("legacy01.json");

            var simData = json.ToObject <SimulationData>();

            SimulationConfigUtils.ProcessKnownTemplates(ref simData);

            Assert.That(simData.Map != null, Is.True);
            Assert.That(simData.Map.Id, Is.EqualTo("841b956b-7848-4c04-a017-5bd4d8385bbd"));
            Assert.That(simData.Vehicles != null, Is.True);
            Assert.That(simData.Vehicles[0].Id, Is.EqualTo("a65e1504-27d9-4338-8161-e59cef6de2bb"));
        }
        public void ParseApiOnly()
        {
            var json = loadJsonFile("apiOnly.json");

            var simData = json.ToObject <SimulationData>();

            SimulationConfigUtils.ProcessKnownTemplates(ref simData);

            Assert.That(simData.ApiOnly, Is.True);
            Assert.True(simData.ApiOnly);

            var s = Newtonsoft.Json.JsonConvert.SerializeObject(simData, JsonSettings.camelCase);

            Debug.Log($"Simulation data: {s}");
        }
        public void ParseRandomTrafic()
        {
            var json = loadJsonFile("randomTraffic.json");

            var simData = json.ToObject <SimulationData>();

            SimulationConfigUtils.ProcessKnownTemplates(ref simData);

            Assert.That(simData.Template != null, Is.True);
            Assert.That(simData.Template.Alias, Is.EqualTo("randomTraffic"));
            Assert.That(simData.Template.Parameters.Length, Is.EqualTo(3));

            Assert.That(simData.ApiOnly, Is.False);

            var s = Newtonsoft.Json.JsonConvert.SerializeObject(simData, JsonSettings.camelCase);

            Debug.Log($"Simulation data: {s}");

            Assert.That(simData.Map != null, Is.True);
            Assert.That(simData.Map.Id, Is.EqualTo("841b956b-7848-4c04-a017-5bd4d8385bbd"));
            Assert.That(simData.Vehicles != null, Is.True);
            Assert.That(simData.Vehicles[0].Id, Is.EqualTo("a65e1504-27d9-4338-8161-e59cef6de2bb"));
        }
예제 #7
0
    async Task Parse(string s)
    {
        // Debug.Log(s);
        if (string.IsNullOrEmpty(s))
        {
            return;
        }

        try
        {
            if (s.StartsWith("data:") && !string.IsNullOrEmpty(s.Substring(6)))
            {
                JObject deserialized = JObject.Parse(s.Substring(5));
                if (deserialized != null && deserialized.HasValues)
                {
                    var status = deserialized.GetValue("status");
                    if (status != null)
                    {
                        switch (status.ToString())
                        {
                        case "Unrecognized":
                            RunOnUnityThread(() =>
                            {
                                Status = ConnectionStatus.Connected;
                                ConnectionUI.instance.UpdateStatus();
                            });
                            break;

                        case "OK":
                            RunOnUnityThread(() =>
                            {
                                Status = ConnectionStatus.Online;
                                ConnectionUI.instance.UpdateStatus();
                            });
                            break;

                        case "Config":
                            RunOnUnityThread(() =>
                            {
                                Status = ConnectionStatus.Online;
                                ConnectionUI.instance.UpdateStatus();

                                SimulationData simData;
                                try
                                {
                                    simData = deserialized["data"].ToObject <SimulationData>();
                                    SimulationConfigUtils.ProcessKnownTemplates(ref simData);
                                }
                                catch (Exception e) when(e is InvalidCastException ||
                                                         e is NullReferenceException)
                                {
                                    Debug.LogError($"[CONN] Failed to parse Config data: '{s}'");
                                    Debug.LogException(e);
                                    throw;
                                }
                                Loader.StartSimulation(simData);
                            });
                            break;

                        case "Disconnect":
                            RunOnUnityThread(() =>
                            {
                                DisconnectReason = deserialized.GetValue("reason")?.ToString() ?? "unknown reason";
                                Disconnect();
                            });
                            break;

                        case "Timeout":
                            RunOnUnityThread(() =>
                            {
                                DisconnectReason = deserialized.GetValue("reason")?.ToString() ?? "unknown reason";
                                Disconnect();
                            });
                            break;

                        case "Stop":
                            if (Loader.Instance.Status == SimulatorStatus.Idle || Loader.Instance.Status == SimulatorStatus.Stopping)
                            {
                                SimulationData simData = Newtonsoft.Json.JsonConvert.DeserializeObject <SimulationData>(deserialized.GetValue("data").ToString());
                                Debug.Log("not running");
                                await API.UpdateStatus("Stopping", simData.Id, "stop requested but was not running simulation");

                                await API.UpdateStatus("Idle", simData.Id, "");

                                return;
                            }
                            Loader.StopAsync();
                            break;

                        default:
                            Debug.LogError($"Unknown Status '{status.ToString()}'! Disconnecting.");
                            RunOnUnityThread(() =>
                            {
                                Disconnect();
                            });
                            break;
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Debug.LogException(ex);
        }
    }