Skip to content

Commit a34ef5b

Browse files
committed
All information are on rest api
1 parent eb09e5e commit a34ef5b

7 files changed

+495
-329
lines changed

clod-rest-server/Controllers/HomeController.cs

-34
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,69 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
53
using Microsoft.AspNetCore.Mvc;
6-
using Microsoft.Extensions.Logging;
4+
using System.Linq;
5+
using System.Reflection;
76

87
namespace clod_rest_server.Controllers
98
{
109
[ApiController]
1110
[Route("[controller]")]
1211
public class PlaneInformationController : ControllerBase
1312
{
13+
[HttpGet]
14+
public ServerInformation Get()
15+
{
16+
return new ServerInformation()
17+
{
18+
PlaneInformation = DictionaryToObject<PlaneInformation>(RefreshData())
19+
};
20+
}
1421

15-
private PlaneInformation RefreshData()
22+
private Dictionary<string, double?> RefreshData()
1623
{
17-
var planeInformation = new PlaneInformation();
1824
var gameCommunications = new GameCommunications();
19-
planeInformation.I_EngineRPM = gameCommunications.GetParameter(GameCommunications.ParameterTypes.I_EngineRPM, 0);
20-
return planeInformation;
25+
var dataDict = new Dictionary<string, double?> { };
26+
27+
foreach (GameCommunications.ParameterTypes parameter in (GameCommunications.ParameterTypes[])Enum.GetValues(typeof(GameCommunications.ParameterTypes)))
28+
{
29+
try
30+
{
31+
dataDict.Add(parameter.ToString(), gameCommunications.GetParameter(Convert.ToInt32(parameter), 0));
32+
}
33+
catch
34+
{
35+
dataDict.Add(parameter.ToString(), null);
36+
}
37+
38+
}
39+
40+
return dataDict;
2141
}
2242

23-
[HttpGet]
24-
public ServerInformation Get()
43+
private static T DictionaryToObject<T>(IDictionary<string, double?> dict) where T : new()
2544
{
26-
//var rng = new Random();
27-
//return Enumerable.Range(1, 5).Select(index => new PlaneInformation
28-
//{})
29-
//.ToArray();
45+
var t = new T();
46+
PropertyInfo[] properties = t.GetType().GetProperties();
3047

31-
return new ServerInformation()
48+
foreach (PropertyInfo property in properties)
3249
{
33-
planeInformation = RefreshData()
34-
};
50+
if (!dict.Any(x => x.Key.Equals(property.Name, StringComparison.InvariantCultureIgnoreCase)))
51+
continue;
52+
53+
KeyValuePair<string, double?> item = dict.First(x => x.Key.Equals(property.Name, StringComparison.InvariantCultureIgnoreCase));
54+
55+
// Find which property type (int, string, double? etc) the CURRENT property is...
56+
Type tPropertyType = t.GetType().GetProperty(property.Name).PropertyType;
57+
58+
// Fix nullables...
59+
Type newT = Nullable.GetUnderlyingType(tPropertyType) ?? tPropertyType;
60+
61+
// ...and change the type
62+
object newA = Convert.ChangeType(item.Value, newT);
63+
t.GetType().GetProperty(property.Name).SetValue(t, newA, null);
64+
}
65+
return t;
3566
}
67+
3668
}
3769
}

clod-rest-server/GameCommunication.cs

+238
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
using System;
2+
using System.IO.MemoryMappedFiles;
3+
4+
5+
namespace clod_rest_server
6+
{
7+
public class GameCommunications
8+
{
9+
private MemoryMappedFile m_MemoryMappedFile;
10+
private MemoryMappedViewAccessor m_MemoryMappedViewAccessor;
11+
public enum ParameterTypes
12+
{
13+
Nil,
14+
M_Random,
15+
M_Shake,
16+
M_CabinDamage,
17+
M_CabinState,
18+
M_NamedDamage,
19+
M_SystemWear,
20+
M_Health,
21+
C_Steering,
22+
C_Brake,
23+
C_Throttle,
24+
C_Trigger,
25+
C_Pitch,
26+
C_Mix,
27+
C_WaterRadiator,
28+
C_OilRadiator,
29+
C_RadiatorAutomation,
30+
C_PitchAutomation,
31+
C_Compressor,
32+
C_Afterburner,
33+
C_BoostEnabler,
34+
C_SlowRunningCutOut,
35+
C_Magneto,
36+
C_Feather,
37+
C_CarbHeater,
38+
C_HatchDoor,
39+
C_HatchJettison,
40+
C_Timer,
41+
C_Timer1,
42+
C_Aileron,
43+
C_Elevator,
44+
C_Rudder,
45+
C_AileronTrim,
46+
C_ElevatorTrim,
47+
C_RudderTrim,
48+
C_TailwheelLock,
49+
C_LandingFlap,
50+
C_LeadingEdgeSlats,
51+
C_Undercarriage,
52+
C_UndercarriageEmergency,
53+
C_BombBayDoor,
54+
C_Airbrake,
55+
C_FuelTankSelector,
56+
C_TelepirometroElettrico,
57+
C_AltimeterPinion,
58+
C_AnemometroPinion,
59+
C_BombSight,
60+
C_Sight,
61+
C_Bombenabwurfgerat,
62+
C_KraftstoffSelector,
63+
C_LiquidGauge0,
64+
C_LiquidGauge1,
65+
C_PriLights,
66+
C_SecLights,
67+
C_SightLights,
68+
C_PitotHeater,
69+
C_Handpumpe,
70+
C_RadTXRX,
71+
C_RadPriNav,
72+
C_RadSecNav,
73+
C_Kurssteuerung,
74+
A_Steering,
75+
A_Brake,
76+
A_Aileron,
77+
A_Elevator,
78+
A_Rudder,
79+
A_AileronTrim,
80+
A_ElevatorTrim,
81+
A_RudderTrim,
82+
A_Undercarriage,
83+
A_UndercarriageShock,
84+
A_UndercarriageWheel,
85+
A_HatchDoor,
86+
A_BombBayDoor,
87+
A_ImpellerAngle,
88+
A_ImpellerAngularVelocity,
89+
A_ImpellerUnfold,
90+
A_LandingFlap,
91+
A_Airbrake,
92+
A_EngineAirRadiator,
93+
A_EngineWaterRadiator,
94+
A_EngineOilRadiator,
95+
A_LeadingEdgeSlat,
96+
Z_Coordinates,
97+
Z_Orientation,
98+
Z_Overload,
99+
Z_AltitudeAGL,
100+
Z_AltitudeMSL,
101+
Z_VelocityIAS,
102+
Z_VelocityTAS,
103+
Z_VelocityMach,
104+
Z_AmbientAirTemperature,
105+
S_ElectricVoltage,
106+
S_ElectricIncandescingRatio,
107+
S_ElectricAmperage,
108+
S_ElectricPrimaryPitLight,
109+
S_ElectricSecondaryPitLight,
110+
S_ElectricSightLight,
111+
S_FuelReserve,
112+
S_HatchDoor,
113+
S_UndercarriageValve,
114+
S_PneumoContainerPressure,
115+
S_PneumoLinePressure,
116+
S_HydroPressure,
117+
S_HydroReserve,
118+
S_Sturzanlage,
119+
S_GunOperation,
120+
S_GunReserve,
121+
S_GunClipReserve,
122+
S_BombReserve,
123+
S_Fenster,
124+
S_PitotHeater,
125+
S_Bombenabwurfgerat,
126+
S_Turret,
127+
M_Reserved000,
128+
M_Reserved001,
129+
M_Reserved002,
130+
M_Reserved003,
131+
M_Reserved004,
132+
M_Reserved005,
133+
M_Reserved006,
134+
M_Reserved007,
135+
M_Reserved008,
136+
M_Reserved009,
137+
M_Reserved00A,
138+
M_Reserved00B,
139+
M_Reserved00C,
140+
M_Reserved00D,
141+
M_Reserved00E,
142+
M_Reserved00F,
143+
M_Reserved010,
144+
M_Reserved011,
145+
M_Reserved012,
146+
M_Reserved013,
147+
M_Reserved014,
148+
M_Reserved015,
149+
M_Reserved016,
150+
M_Reserved017,
151+
M_Reserved018,
152+
M_Reserved019,
153+
M_Reserved01A,
154+
M_Reserved01B,
155+
M_Reserved01C,
156+
M_Reserved01D,
157+
M_Reserved01E,
158+
M_Reserved01F,
159+
I_Timer,
160+
I_AmbientTemp,
161+
I_EngineRPM,
162+
I_EngineManPress,
163+
I_EngineBoostPress,
164+
I_EngineWatPress,
165+
I_EngineOilPress,
166+
I_EngineFuelPress,
167+
I_EngineWatTemp,
168+
I_EngineRadTemp,
169+
I_EngineOilTemp,
170+
I_EngineOilRadiatorTemp,
171+
I_EngineTemperature,
172+
I_EngineCarbTemp,
173+
I_Pitch,
174+
I_VelocityIAS,
175+
I_Altitude,
176+
I_Variometer,
177+
I_Slip,
178+
I_MagneticCompass,
179+
I_RepeaterCompass,
180+
I_Peilzeiger,
181+
I_FuelReserve,
182+
I_LiquidReserve,
183+
I_Voltamperemeter,
184+
I_Voltmeter,
185+
I_Amperemeter,
186+
I_HydroPressure,
187+
I_HydroEmPressure,
188+
I_Turn,
189+
I_AH,
190+
I_DirectionIndicator,
191+
I_SlavedCompass,
192+
I_Suction,
193+
I_AFN,
194+
I_ADF,
195+
I_RDF,
196+
I_RMI,
197+
I_FLRC,
198+
I_Kurssteuerung,
199+
I_BombSight
200+
}
201+
202+
public GameCommunications()
203+
{
204+
m_MemoryMappedFile = null;
205+
m_MemoryMappedViewAccessor = null;
206+
}
207+
208+
private void OpenMMF()
209+
{
210+
try
211+
{
212+
m_MemoryMappedFile = MemoryMappedFile.OpenExisting("CLODDeviceLink", MemoryMappedFileRights.Read);
213+
m_MemoryMappedViewAccessor = m_MemoryMappedFile.CreateViewAccessor(0, 10000 * sizeof(double), MemoryMappedFileAccess.Read);
214+
}
215+
catch (Exception e)
216+
{
217+
Console.Write(e.ToString());
218+
return;
219+
}
220+
}
221+
222+
public double? GetParameter(int ParameterType, int SubType)
223+
{
224+
if (m_MemoryMappedFile == null)
225+
OpenMMF();
226+
227+
try
228+
{
229+
return m_MemoryMappedViewAccessor.ReadDouble((((int)ParameterType * 10) + SubType) * sizeof(double));
230+
}
231+
catch (Exception e)
232+
{
233+
Console.Write(e.ToString());
234+
return null;
235+
}
236+
}
237+
}
238+
}

0 commit comments

Comments
 (0)