Skip to content

Commit

Permalink
Upgrade to C# 7.1; adjusted pattern matching in TraCICommandHelper cl…
Browse files Browse the repository at this point in the history
…ass.
  • Loading branch information
menno authored and menno committed Jul 31, 2018
1 parent 8b5f3cc commit ae67cfc
Show file tree
Hide file tree
Showing 29 changed files with 3,411 additions and 25 deletions.
39 changes: 14 additions & 25 deletions CodingConnected.TraCI.NET/Helpers/TraCICommandHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,23 @@ internal static Tres ExecuteSetCommand<Tres, Tvalue>(TraCIClient client, string
{
TraCICommand command = null;

switch (Type.GetTypeCode(typeof(Tvalue)))
switch (value)
{
case TypeCode.Int32:
{
int i = Convert.ToInt32(value);
command = GetCommand(id, commandType, messageType, i);
break;
}
case TypeCode.Double:
{
double d = Convert.ToDouble(value);
command = GetCommand(id, commandType, messageType, d);
break;
}
case TypeCode.String:
{
string s = value as string;
command = GetCommand(id, commandType, messageType, s);
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;
default:
{
if (value is List<string>)
{
List<string> los = value as List<string>;
command = GetCommand(id, commandType, messageType, los);
}
break;
throw new InvalidCastException($"Type {value.GetType().Name} is not implemented in method TraCICommandHelper.ExecuteSetCommand().");
}
}

Expand Down
88 changes: 88 additions & 0 deletions CodingConnected.Traci.NET/CodingConnected.TraCI.NET.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{2E2F7244-3E8C-41F0-8AD2-567ACF862B75}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>CodingConnected.TraCI.NET</RootNamespace>
<AssemblyName>CodingConnected.TraCI.NET</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<LangVersion>7.1</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<LangVersion>7.1</LangVersion>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Commands\ControlCommands.cs" />
<Compile Include="Commands\EdgeCommands.cs" />
<Compile Include="Commands\GuiCommands.cs" />
<Compile Include="Commands\InductionLoopCommands.cs" />
<Compile Include="Commands\JunctionCommands.cs" />
<Compile Include="Commands\LaneAreaDetectorCommands.cs" />
<Compile Include="Commands\LaneCommands.cs" />
<Compile Include="Commands\MultiEntryExitDetectorCommands.cs" />
<Compile Include="Commands\PersonCommands.cs" />
<Compile Include="Commands\POICommands.cs" />
<Compile Include="Commands\PolygonCommands.cs" />
<Compile Include="Commands\RouteCommands.cs" />
<Compile Include="Commands\SimulationCommands.cs" />
<Compile Include="Commands\TraCICommandsBase.cs" />
<Compile Include="Commands\TrafficLightCommands.cs" />
<Compile Include="Commands\VehicleCommands.cs" />
<Compile Include="Commands\VehicleTypeCommands.cs" />
<Compile Include="Helpers\TraCICommandHelper.cs" />
<Compile Include="Helpers\TraCIDataConverter.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TraCIClient.cs" />
<Compile Include="TraCICommand.cs" />
<Compile Include="TraCICommandException.cs" />
<Compile Include="TraCIConstants.cs" />
<Compile Include="TraCIResult.cs" />
<Compile Include="Types\TrafficLightProgram.cs" />
<Compile Include="Types\BoundaryBox.cs" />
<Compile Include="Types\Color.cs" />
<Compile Include="Types\ComposedTypeBase.cs" />
<Compile Include="Types\LonLatAltPosition.cs" />
<Compile Include="Types\LonLatPosition.cs" />
<Compile Include="Types\Polygon.cs" />
<Compile Include="Types\RoadMapPosition.cs" />
<Compile Include="Types\Position3D.cs" />
<Compile Include="Types\Position2D.cs" />
<Compile Include="Types\TrafficLightPhaseList.cs" />
</ItemGroup>
<ItemGroup>
<None Include="LICENSE.md" />
<None Include="README.md" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
132 changes: 132 additions & 0 deletions CodingConnected.Traci.NET/Commands/ControlCommands.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CodingConnected.TraCI.NET.Helpers;

namespace CodingConnected.TraCI.NET.Commands
{
public class ControlCommands : TraCICommandsBase
{
#region Public Methods

/// <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;
}

/// <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>
/// 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>
/// 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>
/// 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);
}

/// <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);
}

#endregion // Public Methods

#region Constructor

public ControlCommands(TraCIClient client) : base(client)
{
}

#endregion // Constructor
}
}
Loading

0 comments on commit ae67cfc

Please sign in to comment.