internal TraCIResult[] SendMessage(TraCICommand command) { if (!_client.Connected) { return(null); } var msg = TraCIDataConverter.GetMessageBytes(command); _client.Client.Send(msg); try { var bytesRead = _stream.Read(_receiveBuffer, 0, 32768); if (bytesRead < 0) { // Read returns 0 if the client closes the connection throw new IOException(); } var response = _receiveBuffer.Take(bytesRead).ToArray(); #if NLOG _logger.Trace(" << {0}", BitConverter.ToString(response)); #endif var trresponse = TraCIDataConverter.HandleResponse(response); return(trresponse?.Length > 0 ? trresponse : null); } catch { return(null); // TODO } }
/// <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; } }
public void SetSingleTrafficLightState(string trafficLightId, string signalgroup, char singlestate) { var command = new TraCICommand { Identifier = TraCIConstants.CMD_SET_TL_VARIABLE }; var bytes = new List <byte> { TraCIConstants.TL_RED_YELLOW_GREEN_SINGLESTATE }; bytes.AddRange(TraCIDataConverter.GetTraCIBytesFromASCIIString(trafficLightId)); bytes.Add(TraCIConstants.TYPE_STRING); bytes.AddRange(BitConverter.GetBytes((signalgroup + ' ' + singlestate).Length).Reverse()); bytes.AddRange(Encoding.ASCII.GetBytes(signalgroup + ' ' + singlestate)); command.Contents = bytes.ToArray(); // ReSharper disable once UnusedVariable var response = SendMessage(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 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); }
// deprecated: below method only works with an outdated patched version of SUMO //public void SetSingleTrafficLightState(string trafficLightId, string signalgroup, char singlestate) //{ // var command = new TraCICommand { Identifier = TraCIConstants.CMD_SET_TL_VARIABLE }; // var bytes = new List<byte> { TraCIConstants.TL_RED_YELLOW_GREEN_SINGLESTATE }; // bytes.AddRange(TraCIDataConverter.GetTraCIBytesFromASCIIString(trafficLightId)); // bytes.Add(TraCIConstants.TYPE_STRING); // bytes.AddRange(BitConverter.GetBytes((signalgroup + ' ' + singlestate).Length).Reverse()); // bytes.AddRange(Encoding.ASCII.GetBytes(signalgroup + ' ' + singlestate)); // // command.Contents = bytes.ToArray(); // // ReSharper disable once UnusedVariable // var response = SendMessage(command); //} #endregion // Set Variable Methods #region Public Methods internal TraCIResult[] SendMessage(TraCICommand command) { if (!_client.Connected) { return(null); } var msg = TraCIDataConverter.GetMessageBytes(command); _client.Client.Send(msg); try { var bytesRead = _stream.Read(_receiveBuffer, 0, 32768); if (bytesRead < 0) { // Read returns 0 if the client closes the connection throw new IOException(); } var revLength = _receiveBuffer.Take(4).Reverse().ToArray(); var totlength = BitConverter.ToInt32(revLength, 0); var response = new List <byte>(); response.AddRange(_receiveBuffer.Take(bytesRead).ToArray()); if (bytesRead != totlength) { while (bytesRead < totlength) { var innerBytesRead = _stream.Read(_receiveBuffer, 0, 32768); response.AddRange(_receiveBuffer.Take(innerBytesRead).ToArray()); bytesRead += innerBytesRead; } } //var response = _receiveBuffer.Take(bytesRead).ToArray(); var trresponse = TraCIDataConverter.HandleResponse(response.ToArray()); return(trresponse?.Length > 0 ? trresponse : null); } catch { return(null); // TODO } }
/// <summary> /// Returns the links controlled by the traffic light, sorted by the signal index and described by giving /// the incoming, outgoing, and via lane. /// </summary> /// <param name="id"></param> /// <returns></returns> public TraCIResponse <ControlledLinks> GetControlledLinks(string id) { var tmp = TraCICommandHelper.ExecuteGetCommand <CompoundObject>( Client, id, TraCIConstants.CMD_GET_TL_VARIABLE, TraCIConstants.TL_CONTROLLED_LINKS); var controlledLinks = TraCIDataConverter.ConvertToControlledLinks(tmp.Content.Value); var ret = new TraCIResponse <ControlledLinks> { Content = controlledLinks, ErrorMessage = tmp.ErrorMessage, Identifier = tmp.Identifier, ResponseIdentifier = tmp.ResponseIdentifier, Result = tmp.Result, Variable = tmp.Variable }; return(ret); }
/// <summary> /// Returns the complete traffic light program, structure described under data types /// </summary> /// <param name="id"></param> /// <returns></returns> public TraCIResponse <TrafficCompleteLightProgram> GetCompleteDefinition(string id) { var tmp = TraCICommandHelper.ExecuteGetCommand <CompoundObject>( Client, id, TraCIConstants.CMD_GET_TL_VARIABLE, TraCIConstants.TL_COMPLETE_DEFINITION_RYG); var tmp2 = TraCIDataConverter.ConvertToTrafficLightCompleteProgramm(tmp.Content); var ret = new TraCIResponse <TrafficCompleteLightProgram> { Content = tmp2, ErrorMessage = tmp.ErrorMessage, Identifier = tmp.Identifier, ResponseIdentifier = tmp.ResponseIdentifier, Result = tmp.Result, Variable = tmp.Variable }; return(ret); }
/// <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); }