public override TrafficIntersection getIntersection() { TrafficIntersection intersection = new TrafficIntersection(); if (isZ) { intersection.stationaryX = (inX1.GetComponent <IncomingCounter>().getNumberCars() + inX2.GetComponent <IncomingCounter>().getNumberCars()); } else if (isX) { intersection.stationaryY = (inZ1.GetComponent <IncomingCounter>().getNumberCars() + inZ2.GetComponent <IncomingCounter>().getNumberCars()); } //intersection.movingX = (outX1.GetComponent<OutgoingCounter>().getNumberCars() + outX2.GetComponent<OutgoingCounter>().getNumberCars()); //intersection.movingY = (outZ1.GetComponent<OutgoingCounter>().getNumberCars() + outZ2.GetComponent<OutgoingCounter>().getNumberCars()); intersection.movingX = (inX1.GetComponent <IncomingCounter>().getMovingCars() + inX2.GetComponent <IncomingCounter>().getMovingCars()); intersection.movingY = (inZ1.GetComponent <IncomingCounter>().getMovingCars() + inZ2.GetComponent <IncomingCounter>().getMovingCars()); if (isX) { intersection.phase = 0; } else if (isZ) { intersection.phase = 1; } else { intersection.phase = 2; } return(intersection); }
private IEnumerator CalcIntersectionData() { while (true) { TrafficIntersection IntersectionData = GetComponentInParent <IntersectionParent>().getIntersection(); //hard coded atm float totalCars = IntersectionData.movingX + IntersectionData.movingY + IntersectionData.stationaryX + IntersectionData.stationaryY; float stationaryCars = IntersectionData.stationaryX + IntersectionData.stationaryY; float movingCars = IntersectionData.movingX + IntersectionData.movingY; UI_intersectionData.text = "Number of cars: " + totalCars.ToString() + "\n" + "stationary cars: " + stationaryCars.ToString() + "\n" + "moving cars: " + movingCars.ToString() + "\n"; yield return(new WaitForSeconds(0.1f)); } }
private static void Main(string[] args) { Log.Logger = new LoggerConfiguration() .WriteTo.ColoredConsole() .WriteTo.RollingFile("TrafficController-{Date}.log") .CreateLogger(); var trafficController = new TrafficController(Scheduler.Default); var intersection = new TrafficIntersection(trafficController); intersection.NorthLight.Subscribe(x => Log.Information("North is now {x}", x)); intersection.SouthLight.Subscribe(x => Log.Information("South is now {x}", x)); intersection.WestLight.Subscribe(x => Log.Information("West is now {x}", x)); intersection.EastLight.Subscribe(x => Log.Information("East is now {x}", x)); while (true) { Thread.Sleep(TimeSpan.FromSeconds(1)); } }
public void TrafficLightsShouldChangetoExpected() { new TestScheduler().With( scheduler => { scheduler.Start(); var trafficController = new TrafficController(scheduler); var intersection = new TrafficIntersection(trafficController); // progress by one second past RedRed state to the RedGreen state. scheduler.AdvanceBy(TimeSpan.FromSeconds(1).Ticks); intersection.NorthLight.Subscribe(state => { state.Should().Be(Light.Red); }); intersection.SouthLight.Subscribe(state => { state.Should().Be(Light.Red); }); intersection.WestLight.Subscribe(state => { state.Should().Be(Light.Green); }); intersection.EastLight.Subscribe(state => { state.Should().Be(Light.Green); }); }); }
/** * getIntersection() - Returns traffic light object with updates data */ public override TrafficIntersection getIntersection() { TrafficIntersection intersection = new TrafficIntersection(); if (isZ) { intersection.stationaryX = (inX1.GetComponent <IncomingCounter>().getNumberCars() + inX2.GetComponent <IncomingCounter>().getNumberCars()); } else if (isX) { intersection.stationaryY = (inZ1.GetComponent <IncomingCounter>().getNumberCars() + inZ2.GetComponent <IncomingCounter>().getNumberCars()); } intersection.movingX = (inX1.GetComponent <IncomingCounter>().getMovingCars() + inX2.GetComponent <IncomingCounter>().getMovingCars()); intersection.movingY = (inZ1.GetComponent <IncomingCounter>().getMovingCars() + inZ2.GetComponent <IncomingCounter>().getMovingCars()); if (isX) { intersection.phase = 0; } else if (isZ) { intersection.phase = 1; } else { intersection.phase = 2; } intersection.period = (float)Math.Floor(defaultCycle - timeLeft); if (timeLeft < 0.0f) { intersection.period = defaultCycle; } return(intersection); }
IEnumerator Upload() { int i = 0; TrafficIntersection obj = null; String json = "{\"statistics\":["; for (; i < intersections.Length - 1; i++) { try { obj = intersections[i].getIntersection(); } catch (NullReferenceException) { obj = intersections[i].getIntersection(); } obj.name = "intersection" + (i + 1); //obj.name = intersections[i].name; json += obj.toJson(i + 1); json += ","; } ////Debug.Log("length: " + intersections.Length); ////Debug.Log("I: " + i); try { obj = intersections[i].getIntersection(); } catch (NullReferenceException) { obj = intersections[i].getIntersection(); } obj.name = "intersection" + (i + 1); //obj.name = intersections[i].name; json += obj.toJson(i + 1); json += "],\"numStationaryCars\":" + GameObject.Find("GlobalData").GetComponent <MetaData>().stopped + "}"; //Debug.Log("Sending: " + json); byte[] bytes = Encoding.UTF8.GetBytes(json); UnityWebRequest apiRequest = UnityWebRequest.Put(localSpringServerURL, bytes);//.SetRequestHeader("content-type", "application/json" ); apiRequest.method = "POST"; apiRequest.SetRequestHeader("accept", "application/json; charset=UTF-8"); apiRequest.SetRequestHeader("content-type", "application/json; charset=UTF-8"); yield return(apiRequest.SendWebRequest()); if (apiRequest.isNetworkError || apiRequest.isNetworkError) { ////Debug.LogError(apiRequest.error); yield break; } else { String stringResponse = apiRequest.downloadHandler.text; // Example: 32 = 100000 if (stringResponse == "-1") { GameObject[] carsArray = GameObject.FindGameObjectsWithTag("CarSpawned"); for (int k = 0; k < carsArray.Length; k++) { Destroy(carsArray[k]); } for (int k = 0; k < intersections.Length; k++) { intersections[k].resetGeneration(); } } else { int intResponse = Convert.ToInt32(stringResponse); String bitStream = Convert.ToString(intResponse, 2); //Debug.Log("Original bitstream: " + bitStream); //Debug.Log("Bitstream length: " + bitStream.Length); //Debug.Log("Intersections length: " + intersections.Length); if (bitStream.Length < intersections.Length) { String temp = ""; int padAmount = (intersections.Length - bitStream.Length); for (int k = 0; k < padAmount; k++) { temp += "0"; } bitStream = temp += bitStream; } //Debug.Log("After padding: " + bitStream); Debug.Log("response: " + (string)apiRequest.downloadHandler.text); for (int j = 0; j < intersections.Length; j++) { if (bitStream.ToCharArray().GetValue(j).Equals('1')) { Debug.Log("Making change to intersection: " + intersections[j].name); intersections[j].makeChange(); } } } } }