internal static TraCICommand GetCommand(string id, byte commandType, byte messageType, List <string> values) { var bytes = new List <byte> { messageType }; bytes.AddRange(TraCIDataConverter.GetTraCIBytesFromASCIIString(id)); if (values != null && values.Count > 0) { bytes.Add(TraCIConstants.TYPE_STRINGLIST); bytes.AddRange(TraCIDataConverter.GetTraCIBytesFromInt32(values.Count)); foreach (var parameter in values) { bytes.AddRange(TraCIDataConverter.GetTraCIBytesFromASCIIString(parameter)); } } var command = new TraCICommand { Identifier = commandType, Contents = bytes.ToArray() }; return(command); }
/// <summary> /// Tells TraCI to reload the simulation with the given options /// <remarks>Loading does not work when using multiple clients, currently</remarks> /// </summary> /// <param name="options">List of options to pass to SUMO</param> public void SetOrder(int index) { var command = new TraCICommand { Identifier = TraCIConstants.CMD_GETVERSION, Contents = BitConverter.GetBytes(index).Reverse().ToArray() }; var response = Client.SendMessage(command); }
/// <summary> /// Instruct SUMO to stop the simulation and close /// </summary> public void Close() { var command = new TraCICommand { Identifier = TraCIConstants.CMD_CLOSE, Contents = null }; // ReSharper disable once UnusedVariable var response = Client.SendMessage(command); }
/// <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 void SimStep(int targetTime = 0) { var command = new TraCICommand { Identifier = TraCIConstants.CMD_SIMSTEP, Contents = TraCIDataConverter.GetTraCIBytesFromInt32(targetTime) }; // ReSharper disable once UnusedVariable var response = Client.SendMessage(command); // TODO: handle response }
/// <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> /// Gets an identifying version number as described here: http://sumo.dlr.de/wiki/TraCI/Control-related_commands /// </summary> public int GetVersionId() { var command = new TraCICommand { Identifier = TraCIConstants.CMD_GETVERSION, Contents = null }; var response = Client.SendMessage(command); if (response?.Length == 2) { return(BitConverter.ToInt32(response[1].Response.Take(4).Reverse().ToArray(), 0)); } return(-1); }
internal static TraCICommand GetCommand(string id, byte commandType, byte messageType) { var bytes = new List <byte> { messageType }; bytes.AddRange(TraCIDataConverter.GetTraCIBytesFromASCIIString(id)); var command = new TraCICommand { Identifier = commandType, Contents = bytes.ToArray() }; return(command); }
internal static Tres ExecuteSetCommand <Tres, Tvalue>(TraCIClient client, string id, byte commandType, byte messageType, Tvalue value) { TraCICommand command = null; switch (value) { case int i: command = GetCommand(id, commandType, messageType, i); break; case double d: command = GetCommand(id, commandType, messageType, d); break; case string s: command = GetCommand(id, commandType, messageType, s); break; case List <string> los: command = GetCommand(id, commandType, messageType, los); break; default: { throw new InvalidCastException($"Type {value.GetType().Name} is not implemented in method TraCICommandHelper.ExecuteSetCommand()."); } } if (command != null) { var response = client.SendMessage(command); try { return((Tres)TraCIDataConverter.ExtractDataFromResponse(response, commandType, messageType)); } catch { throw; } } else { return(default(Tres)); } }
/// <summary> /// Gets a user friendly string describing the version of SUMO /// </summary> public string GetVersionString() { var command = new TraCICommand { Identifier = TraCIConstants.CMD_GETVERSION, Contents = null }; var response = Client.SendMessage(command); if (response?.Length == 2) { var strlen = response[1].Response.Skip(4).Take(4).Reverse().ToArray(); var idl = BitConverter.ToInt32(strlen, 0); var ver = Encoding.ASCII.GetString(response[1].Response, 8, idl); return(ver); } return(null); }
/// <summary> /// Tells TraCI to reload the simulation with the given options /// <remarks>Loading does not work when using multiple clients, currently</remarks> /// </summary> /// <param name="options">List of options to pass to SUMO</param> public void Load(List <string> options) { var command = new TraCICommand { Identifier = TraCIConstants.CMD_LOAD }; var n = new List <byte>(); n.AddRange(TraCIDataConverter.GetTraCIBytesFromInt32(options.Count)); foreach (var opt in options) { n.AddRange(TraCIDataConverter.GetTraCIBytesFromInt32(opt.Length)); n.AddRange(TraCIDataConverter.GetTraCIBytesFromASCIIString(opt)); } command.Contents = n.ToArray(); // ReSharper disable once UnusedVariable var response = Client.SendMessage(command); }
internal static byte[] GetMessageBytes(TraCICommand command) { return(GetMessagesBytes(new[] { command })); }
/// <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); }
internal static TraCIResponse <Tres> ExecuteSetCommand <Tres, Tvalue>(TraCIClient client, string id, byte commandType, byte messageType, Tvalue value) { TraCICommand command = null; switch (value) { case byte b: command = GetCommand(id, commandType, messageType, b); break; case int i: command = GetCommand(id, commandType, messageType, i); break; case double d: command = GetCommand(id, commandType, messageType, d); break; case string s: command = GetCommand(id, commandType, messageType, s); break; case List <string> los: command = GetCommand(id, commandType, messageType, los); break; case CompoundObject co: command = GetCommand(id, commandType, messageType, co); break; case Color c: command = GetCommand(id, commandType, messageType, c); break; case Position2D p2d: command = GetCommand(id, commandType, messageType, p2d); break; case Polygon p: command = GetCommand(id, commandType, messageType, p); break; case BoundaryBox bb: command = GetCommand(id, commandType, messageType, bb); break; default: { throw new InvalidCastException($"Type {value.GetType().Name} is not implemented in method TraCICommandHelper.ExecuteSetCommand()."); } } if (command != null) { var response = client.SendMessage(command); try { return(TraCIDataConverter.ExtractDataFromResponse <Tres>(response, commandType, messageType)); } catch { throw; } } else { return(default);