internal protected virtual void OnEdgeContextSubscription(ContextSubscriptionEventArgs eventArgs) { EdgeContextSubscription?.Invoke(this, eventArgs); }
internal protected virtual void OnPOIContextSubscription(ContextSubscriptionEventArgs eventArgs) { PointOfInterestContextSubscription?.Invoke(this, eventArgs); }
internal protected virtual void OnInductionLoopContextSubscription(ContextSubscriptionEventArgs eventArgs) { InductionLoopContextSubscription?.Invoke(this, eventArgs); }
/// <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); }
private static void Client_VehicleContextSubscriptionUsingResponses(object sender, ContextSubscriptionEventArgs e) { Console.WriteLine("*** Vehicle Context Subscription using responses ***"); Console.WriteLine("EGO Object id : " + e.ObjectId); Console.WriteLine("Context Domain : " + ByteToHex(e.ContextDomain)); Console.WriteLine("Variable Count : " + e.VariableCount); Console.WriteLine("Number of objects in range : " + e.ObjectCount); Console.WriteLine("Objects inside Context Range:"); foreach (var r in e.Responses) /* Responses are TraCIVariableSubscriptionResponse */ { var variableSubscriptionResponse = r as TraCIVariableSubscriptionResponse; var vehicleID = variableSubscriptionResponse.ObjectId; Console.WriteLine(" Object id: " + vehicleID); foreach (var response in variableSubscriptionResponse.Responses) { var variableResponse = ((IResponseInfo)response); var variableCode = variableResponse.Variable; switch (variableCode) { case TraCIConstants.VAR_SPEED: // Returns the speed of the named vehicle within the last step [m/s]; error value: -2^30 Console.WriteLine(" VAR_SPEED " + ((TraCIResponse <double>)response).Content); break; case TraCIConstants.VAR_ANGLE: // Returns the angle of the named vehicle within the last step [°]; error value: -2^30 Console.WriteLine(" VAR_ANGLE " + ((TraCIResponse <double>)variableResponse).Content); break; case TraCIConstants.VAR_ROUTE_ID: Console.WriteLine(" VAR_ROUTE_ID " + ((TraCIResponse <string>)variableResponse).Content); break; default: /* Intentionaly ommit VAR_ACCEL*/ Console.WriteLine($" Variable with code {ByteToHex(variableCode)} not handled "); break; } } } }
private static void Client_VehicleContextSubscriptionUsingDictionary(object sender, ContextSubscriptionEventArgs e) { Console.WriteLine("*** Vehicle Context Subscription using Dictionaries ***"); Console.WriteLine("EGO Object id : " + e.ObjectId); Console.WriteLine("Context Domain : " + ByteToHex(e.ContextDomain)); Console.WriteLine("Variable Count : " + e.VariableCount); Console.WriteLine("Number of objects in range : " + e.ObjectCount); var egoObjectID = e.ObjectId; Console.WriteLine("EGO Object " + " id: " + egoObjectID); Console.WriteLine("Iterating responses..."); Console.WriteLine("Objects inside Context Range:"); foreach (var r in e.Responses) /* Responses are TraCIVariableSubscriptionResponse */ { var variableSubscriptionResponse = r as TraCIVariableSubscriptionResponse; var vehicleID = variableSubscriptionResponse.ObjectId; Console.WriteLine(" Object id: " + vehicleID); Console.WriteLine(" VAR_SPEED " + (variableSubscriptionResponse.ResponseByVariableCode[TraCIConstants.VAR_SPEED]).GetContentAs <float>()); Console.WriteLine(" VAR_ACCEL " + (variableSubscriptionResponse.ResponseByVariableCode[TraCIConstants.VAR_ACCEL]).GetContentAs <float>()); Console.WriteLine(" VAR_ANGLE " + /* We can also use TraCIResult<> (). Warning using TraCIResponse<> we must use the exact type (i.e for angle is double) */ (variableSubscriptionResponse.ResponseByVariableCode[TraCIConstants.VAR_ANGLE] as TraCIResponse <double>).Content); Console.WriteLine(" VAR_ROUTE " + (variableSubscriptionResponse.ResponseByVariableCode[TraCIConstants.VAR_ROUTE_ID]).GetContentAs <string>()); } //We can also get TraCIVariableSubscriptionResponse by objectID Console.WriteLine("Iterating objectIds of dictionary with objects inside context range:" + "\n Printing VAR_SPEED for demonstration"); foreach (var id in e.VariableSubscriptionByObjectId.Keys) { Console.WriteLine(" Object inside ego object range id: " + id); var varResp = e.VariableSubscriptionByObjectId[id]; /* We can handle variable responses like before either iterating responses or * by using value by variable type */ //*Printing VAR_SPEED just for demostration Console.WriteLine(" VAR_SPEED" + varResp.ResponseByVariableCode[TraCIConstants.VAR_SPEED].GetContentAs <float>()); } }