/// <summary> /// Inserts a completely new program. /// </summary> /// <param name="id"></param> /// <param name="program"></param> /// <returns></returns> public TraCIResponse <object> SetCompleteRedYellowGreenDefinition(string id, TrafficLightProgram program) { // TODO: move this to TraCICommandHelper.ExecuteSetCommand var bytes = new List <byte> { TraCIConstants.TL_COMPLETE_PROGRAM_RYG }; //messageType (0x2c) bytes.AddRange(TraCIDataConverter.GetTraCIBytesFromASCIIString(id)); bytes.Add(TraCIConstants.TYPE_COMPOUND); //value type compound bytes.AddRange(TraCIDataConverter.GetTraCIBytesFromInt32(5 + (program.Phases.Count * 4))); //item number bytes.Add(TraCIConstants.TYPE_STRING); //value type string bytes.AddRange(TraCIDataConverter.GetTraCIBytesFromASCIIString(program.ProgramId)); //program ID bytes.Add(TraCIConstants.TYPE_INTEGER); //value type integer bytes.AddRange(TraCIDataConverter.GetTraCIBytesFromInt32(0)); //Type (always 0) bytes.Add(TraCIConstants.TYPE_COMPOUND); //value type compound bytes.AddRange(TraCIDataConverter.GetTraCIBytesFromInt32(0)); //Compound Length (always 0!) bytes.Add(TraCIConstants.TYPE_INTEGER); //value type integer bytes.AddRange(TraCIDataConverter.GetTraCIBytesFromInt32(program.PhaseIndex)); //Phase Index bytes.Add(TraCIConstants.TYPE_INTEGER); //value type integer bytes.AddRange(TraCIDataConverter.GetTraCIBytesFromInt32(program.Phases.Count)); //Phase Number foreach (var p in program.Phases) //Phases { bytes.Add(TraCIConstants.TYPE_DOUBLE); //value type integer bytes.AddRange(TraCIDataConverter.GetTraCIBytesFromDouble(p.Duration)); //Duration[ms] bytes.Add(TraCIConstants.TYPE_DOUBLE); //value type integer bytes.AddRange(TraCIDataConverter.GetTraCIBytesFromDouble(0)); //unused bytes.Add(TraCIConstants.TYPE_DOUBLE); //value type integer bytes.AddRange(TraCIDataConverter.GetTraCIBytesFromDouble(0)); //unused bytes.Add(TraCIConstants.TYPE_STRING); //value type string bytes.AddRange(TraCIDataConverter.GetTraCIBytesFromASCIIString(p.Definition)); //State (light/priority-tuple) } var command = new TraCICommand { Identifier = TraCIConstants.CMD_SET_TL_VARIABLE, Contents = bytes.ToArray() }; var response = Client.SendMessage(command); #warning is the try catch necessary? try { return(TraCIDataConverter.ExtractDataFromResponse <object>(response, TraCIConstants.CMD_SET_TL_VARIABLE, TraCIConstants.TL_COMPLETE_PROGRAM_RYG)); } catch { throw; } }
/// <summary> /// Instruct SUMO to execute a single simulation step /// Note: the size of the step is set via the relevant .sumcfg file /// </summary> /// <param name="targetTime">If this is not 0, SUMO will run until target time is reached</param> public TraCIResponse <object> SimStep(double targetTime = 0) { var command = new TraCICommand { Identifier = TraCIConstants.CMD_SIMSTEP, Contents = TraCIDataConverter.GetTraCIBytesFromDouble(targetTime) }; var response = Client.SendMessage(command); var tmp = TraCIDataConverter.ExtractDataFromResponse <object>(response, TraCIConstants.CMD_SIMSTEP); if (tmp.Content != null) { var listOfSubscriptions = tmp.Content as List <TraCISubscriptionResponse>; foreach (var item in listOfSubscriptions) { bool isVariableSubscription = true; SubscriptionEventArgs eventArgs; // subscription can only be Variable or Context Subrciption. If it isnt the first then it is the latter var subscription = item as TraCIVariableSubscriptionResponse; if (subscription != null) { eventArgs = new VariableSubscriptionEventArgs( item.ObjectId, item.VariableCount, subscription.ResponseByVariableCode ); isVariableSubscription = true; } else { var i = (item as TraCIContextSubscriptionResponse); eventArgs = new ContextSubscriptionEventArgs ( i.ObjectId, i.VariableSubscriptionByObjectId, i.ContextDomain, i.VariableCount, i.ObjectCount ); isVariableSubscription = false; } eventArgs.Responses = item.Responses; switch (item.ResponseCode) { case TraCIConstants.RESPONSE_SUBSCRIBE_INDUCTIONLOOP_VARIABLE: Client.OnInductionLoopSubscription(eventArgs); break; case TraCIConstants.RESPONSE_SUBSCRIBE_MULTIENTRYEXIT_VARIABLE: Client.OnMultiEntryExitSubscription(eventArgs); break; case TraCIConstants.RESPONSE_SUBSCRIBE_TL_VARIABLE: Client.OnTrafficLightSubscription(eventArgs); break; case TraCIConstants.RESPONSE_SUBSCRIBE_LANE_VARIABLE: Client.OnLaneSubscription(eventArgs); break; case TraCIConstants.RESPONSE_SUBSCRIBE_VEHICLE_VARIABLE: Client.OnVehicleSubscription(eventArgs); break; case TraCIConstants.RESPONSE_SUBSCRIBE_VEHICLETYPE_VARIABLE: Client.OnVehicleTypeSubscription(eventArgs); break; case TraCIConstants.RESPONSE_SUBSCRIBE_ROUTE_VARIABLE: Client.OnRouteSubscription(eventArgs); break; case TraCIConstants.RESPONSE_SUBSCRIBE_POI_VARIABLE: Client.OnPOISubscription(eventArgs); break; case TraCIConstants.RESPONSE_SUBSCRIBE_POLYGON_VARIABLE: Client.OnPolygonSubscription(eventArgs); break; case TraCIConstants.RESPONSE_SUBSCRIBE_JUNCTION_VARIABLE: Client.OnJunctionSubscription(eventArgs); break; case TraCIConstants.RESPONSE_SUBSCRIBE_EDGE_VARIABLE: Client.OnEdgeSubscription(eventArgs); break; case TraCIConstants.RESPONSE_SUBSCRIBE_SIM_VARIABLE: Client.OnSimulationSubscription(eventArgs); break; case TraCIConstants.RESPONSE_SUBSCRIBE_GUI_VARIABLE: Client.OnGUISubscription(eventArgs); break; case TraCIConstants.RESPONSE_SUBSCRIBE_LANEAREA_VARIABLE: Client.OnLaneAreaSubscription(eventArgs); break; case TraCIConstants.RESPONSE_SUBSCRIBE_PERSON_VARIABLE: Client.OnPersonSubscription(eventArgs); break; case TraCIConstants.RESPONSE_SUBSCRIBE_INDUCTIONLOOP_CONTEXT: Client.OnInductionLoopContextSubscription(eventArgs as ContextSubscriptionEventArgs); break; case TraCIConstants.RESPONSE_SUBSCRIBE_LANE_CONTEXT: Client.OnLaneContextSubscription(eventArgs as ContextSubscriptionEventArgs); break; case TraCIConstants.RESPONSE_SUBSCRIBE_VEHICLE_CONTEXT: Client.OnVehicleContextSubscription(eventArgs as ContextSubscriptionEventArgs); break; case TraCIConstants.RESPONSE_SUBSCRIBE_POI_CONTEXT: Client.OnPOIContextSubscription(eventArgs as ContextSubscriptionEventArgs); break; case TraCIConstants.RESPONSE_SUBSCRIBE_POLYGON_CONTEXT: Client.OnPolygonContextSubscription(eventArgs as ContextSubscriptionEventArgs); break; case TraCIConstants.RESPONSE_SUBSCRIBE_JUNCTION_CONTEXT: Client.OnJunctionContextSubscription(eventArgs as ContextSubscriptionEventArgs); break; case TraCIConstants.RESPONSE_SUBSCRIBE_EDGE_CONTEXT: Client.OnEdgeContextSubscription(eventArgs as ContextSubscriptionEventArgs); break; default: throw new ArgumentOutOfRangeException(); } } } return(tmp); }