Skip to content

Commit

Permalink
Version 1.4.0.
Browse files Browse the repository at this point in the history
- Cargo fields are emptied upon 'delivery'. Delivery is checked by as suggested by kudla188 in issue #16; by checking for trailer attached set to false while navigation distance is near zero. This seems to work pretty well.
- Job.Cargo in C# Demo is duplicated from TrailerName. This field was non-functional before.
- Job started & finished events in SDK.
Job started & finished are basically that the SDK has reported there is a trailer attached or delivered. Job Started will be re-fired when the user loads an existing game.
The finished event may fire prematurely when the user unattaches trailer at the parking lot, but not in the parking area.
  • Loading branch information
nlhans committed Sep 15, 2015
1 parent 219ca54 commit f610520
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 44 deletions.
10 changes: 5 additions & 5 deletions ets2-client/C#/Ets2SdkClient.Demo/Ets2SdkClient.Demo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,16 @@
<Compile Include="CustomTabControl.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Form1.cs">
<Compile Include="Ets2SdkClientDemo.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Form1.Designer.cs">
<DependentUpon>Form1.cs</DependentUpon>
<Compile Include="Ets2SdkClientDemo.Designer.cs">
<DependentUpon>Ets2SdkClientDemo.cs</DependentUpon>
</Compile>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<EmbeddedResource Include="Form1.resx">
<DependentUpon>Form1.cs</DependentUpon>
<EmbeddedResource Include="Ets2SdkClientDemo.resx">
<DependentUpon>Ets2SdkClientDemo.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@

namespace Ets2SdkClient.Demo
{
public partial class Form1 : Form
public partial class Ets2SdkClientDemo : Form
{
public Ets2SdkTelemetry Telemetry;

public Form1()
public Ets2SdkClientDemo()
{
InitializeComponent();

Telemetry = new Ets2SdkTelemetry();
Telemetry.Data += Telemetry_Data;

Telemetry.JobFinished += TelemetryOnJobFinished;
Telemetry.JobStarted += TelemetryOnJobStarted;

if (Telemetry.Error != null)
{
lbGeneral.Text =
Expand All @@ -26,6 +29,16 @@ public Form1()
}
}

private void TelemetryOnJobFinished(object sender, EventArgs args)
{
MessageBox.Show("Job finished, or at least unloaded nearby cargo destination.");
}

private void TelemetryOnJobStarted(object sender, EventArgs e)
{
MessageBox.Show("Just started job OR loaded game with active.");
}

private void Telemetry_Data(Ets2Telemetry data, bool updated)
{
try
Expand Down
4 changes: 1 addition & 3 deletions ets2-client/C#/Ets2SdkClient.Demo/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace Ets2SdkClient.Demo
Expand All @@ -15,7 +13,7 @@ static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
Application.Run(new Ets2SdkClientDemo());
}
}
}
4 changes: 3 additions & 1 deletion ets2-client/C#/Ets2SdkClient/Ets2SdkData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,9 @@ public struct Ets2SdkData

[FieldOffset(1016)]
public int gearDashboard;


[FieldOffset(1020)] public byte onJob;
[FieldOffset(1021)] public byte jobFinished;

public bool GetBool(Ets2SdkBoolean i)
{
Expand Down
27 changes: 22 additions & 5 deletions ets2-client/C#/Ets2SdkClient/Ets2SdkTelemetry.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Timers;

Expand All @@ -27,6 +22,12 @@ public class Ets2SdkTelemetry

public event TelemetryData Data;

public event EventHandler JobStarted;
public event EventHandler JobFinished;

private bool wasOnJob;
private bool wasFinishingJob;

public Ets2SdkTelemetry()
{
Setup(DefaultSharedMemoryMap, DefaultUpdateInterval);
Expand Down Expand Up @@ -85,6 +86,22 @@ void _updateTimer_Elapsed(object sender, ElapsedEventArgs e)
if (Data != null)
Data(ets2telemetry, ets2RawData.time != lastTime);

// Job close & start events
if (wasFinishingJob != ets2telemetry.Job.JobFinished)
{
wasFinishingJob = ets2telemetry.Job.JobFinished;
if (ets2telemetry.Job.JobFinished)
JobFinished(this, new EventArgs());

}
if (wasOnJob != ets2telemetry.Job.OnJob)
{
wasOnJob = ets2telemetry.Job.OnJob;
if (ets2telemetry.Job.OnJob)
JobStarted(this, new EventArgs());

}

lastTime = ets2RawData.time;

}
Expand Down
10 changes: 7 additions & 3 deletions ets2-client/C#/Ets2SdkClient/Ets2Telemetry.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.ComponentModel;
using System.Dynamic;
using System.Text;

namespace Ets2SdkClient
Expand Down Expand Up @@ -108,6 +106,9 @@ public class _Controls

public class _Job
{
public bool OnJob { get; internal set; }
public bool JobFinished { get; internal set; }

public bool TrailerAttached { get; internal set; }
public float Mass { get; internal set; }
public string TrailerId { get; internal set; }
Expand Down Expand Up @@ -298,12 +299,15 @@ public Ets2Telemetry(Ets2SdkData raw, Ets2SdkUnmanaged rawUnmanaged)
Job.TrailerAttached = raw.GetBool(Ets2SdkBoolean.TrailerAttached);//TODO
Job.TrailerId = Encoding.UTF8.GetString(raw.trailerId).Replace('\0', ' ').Trim();
Job.TrailerName = Encoding.UTF8.GetString(raw.trailerName).Replace('\0', ' ').Trim();
Job.Cargo = rawUnmanaged.TrailerModel.Replace('\0', ' ').Trim();
Job.Cargo = Job.TrailerName; // trailerModel is actually deprecated

Job.NavigationDistanceLeft = raw.routeDistance;
Job.NavigationTimeLeft = raw.routeTime;
Job.SpeedLimit = raw.speedLimit;

Job.OnJob = raw.onJob != 0;
Job.JobFinished = raw.jobFinished != 0;

// Axilliary flags
Axilliary.AdblueWarning = raw.GetBool(Ets2SdkBoolean.AdblueWarning);
Axilliary.AirPressureEmergency = raw.GetBool(Ets2SdkBoolean.AirPressureEmergency);
Expand Down
2 changes: 0 additions & 2 deletions ets2-client/C#/Ets2SdkClient/SharedMemory.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Data.Common;
using System.IO.MemoryMappedFiles;
using System.Net.Mail;
using System.Runtime.InteropServices;

namespace Ets2SdkClient
Expand Down
10 changes: 8 additions & 2 deletions ets2-telemetry/inc/ets2-telemetry-common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
// - Shared memory map struct layout
// - [..]

#define ETS2_PLUGIN_REVID 4
#define ETS2_PLUGIN_REVID 5

#define ETS2_PLUGIN_LOGGING_ON 0
#define ETS2_PLUGIN_LOGGING_SHAREDMEMORY 1
#define ETS2_PLUGIN_LOGGING_SHAREDMEMORY 0
#define ETS2_PLUGIN_FILENAME_PREFIX "C:\ets2telem_"

#if ETS2_PLUGIN_LOGGING_ON == 1
Expand Down Expand Up @@ -189,6 +189,12 @@ typedef struct ets2TelemetryMap_s
int gearDashboard;
} tel_rev4; // added in sdk1.5

struct
{
bool onJob;
bool jobFinished;
} tel_rev5;

} ets2TelemetryMap_t;

#endif
43 changes: 43 additions & 0 deletions ets2-telemetry/src/ets2-telemetry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ SharedMemory *telemMem;
ets2TelemetryMap_t *telemPtr;
const wchar_t* ets2MmfName = ETS2_PLUGIN_MMF_NAME;

static bool onJob;

/**
* @brief Last timestamp we received.
*/
Expand All @@ -56,6 +58,7 @@ FILE *log_file = NULL;

SCSAPI_VOID telemetry_frame_start(const scs_event_t UNUSED(event), const void *const event_info, const scs_context_t UNUSED(context))
{
static int clearJobTicker = 0;
const struct scs_telemetry_frame_start_t *const info = static_cast<const scs_telemetry_frame_start_t *>(event_info);

// The following processing of the timestamps is done so the output
Expand Down Expand Up @@ -88,6 +91,46 @@ SCSAPI_VOID telemetry_frame_start(const scs_event_t UNUSED(event), const void *c
// Do a non-convential periodic update of this field:
telemPtr->tel_rev3.cruiseControl = (telemPtr->tel_rev3.cruiseControlSpeed > 0) ? true : false;

// Check if job could be finished ; if so empty the job field info
if (telemPtr->tel_rev5.onJob == true && telemPtr->tel_rev1.trailer_attached == false && telemPtr->tel_rev4.routeDistance <= 0.1f && telemPtr->tel_rev4.routeDistance >= 0.0f)
{
// if was carrying cargo and not anymore with navigation distance close to zero;
// then we assume the job has finished
// we allow some frames (see ticker) for the client to retrieve data
telemPtr->tel_rev5.onJob = false;

telemPtr->tel_rev5.jobFinished = true;
clearJobTicker = 0;
}
else if (telemPtr->tel_rev5.jobFinished)
{
clearJobTicker ++;

if (clearJobTicker > 10)
{
telemPtr->tel_rev2.jobIncome = 0;
telemPtr->tel_rev2.time_abs_delivery = 0;
telemPtr->tel_rev2.trailerMass = 0;

memset(telemPtr->tel_rev2.trailerId, 0, 64);
memset(telemPtr->tel_rev2.trailerName, 0, 64);

memset(telemPtr->tel_rev2.citySrc, 0, 64); // TODO: put 64-byte into global define
memset(telemPtr->tel_rev2.cityDst, 0, 64);
memset(telemPtr->tel_rev2.compSrc, 0, 64);
memset(telemPtr->tel_rev2.compDst, 0, 64);

telemPtr->tel_rev5.jobFinished = false;
}
}
else
{
if (telemPtr->tel_rev2.jobIncome != 0 && telemPtr->tel_rev1.trailer_attached)
{
telemPtr->tel_rev5.onJob = true;
}
}

}

}
Expand Down

0 comments on commit f610520

Please sign in to comment.