private void _updateTimer_Elapsed(object sender) { var scsTelemetry = sharedMemory.UpdateData(); // check if sdk is NOT running if (!scsTelemetry.SdkActive && !paused) { // if so don't check so often the data var tsInterval = new TimeSpan(0, 0, 0, 0, _defaultPausedUpdateInterval); updateTimer.Change(tsInterval.Add(tsInterval), tsInterval); paused = true; // if sdk not active we don't need to do something return; } if (paused && scsTelemetry.SdkActive) { // ok sdk is active now paused = false; Resume(); // going back to normal update rate } var time = scsTelemetry.Timestamp; var updated = false; if (time != lastTime || wasPaused != scsTelemetry.Paused) { // time changed or game state change -> update data Data?.Invoke(scsTelemetry, true); wasPaused = scsTelemetry.Paused; lastTime = time; updated = true; } //TODO: make it nicer thats a lot of code for such less work // Job start event if (wasOnJob != scsTelemetry.SpecialEventsValues.OnJob) { wasOnJob = scsTelemetry.SpecialEventsValues.OnJob; if (wasOnJob) { if (!updated) { Data?.Invoke(scsTelemetry, true); updated = true; } JobStarted?.Invoke(this, new EventArgs()); } } if (cancelled != scsTelemetry.SpecialEventsValues.JobCancelled) { cancelled = scsTelemetry.SpecialEventsValues.JobCancelled; if (cancelled) { if (!updated) { Data?.Invoke(scsTelemetry, true); updated = true; } JobCancelled?.Invoke(this, new EventArgs()); } } if (delivered != scsTelemetry.SpecialEventsValues.JobDelivered) { delivered = scsTelemetry.SpecialEventsValues.JobDelivered; if (delivered) { if (!updated) { Data?.Invoke(scsTelemetry, true); updated = true; } JobDelivered?.Invoke(this, new EventArgs()); } } if (fined != scsTelemetry.SpecialEventsValues.Fined) { fined = scsTelemetry.SpecialEventsValues.Fined; if (fined) { Fined?.Invoke(this, new EventArgs()); } } if (tollgate != scsTelemetry.SpecialEventsValues.Tollgate) { tollgate = scsTelemetry.SpecialEventsValues.Tollgate; if (tollgate) { Tollgate?.Invoke(this, new EventArgs()); } } if (ferry != scsTelemetry.SpecialEventsValues.Ferry) { ferry = scsTelemetry.SpecialEventsValues.Ferry; if (ferry) { if (!updated) { Data?.Invoke(scsTelemetry, true); updated = true; } Ferry?.Invoke(this, new EventArgs()); } } if (train != scsTelemetry.SpecialEventsValues.Train) { train = scsTelemetry.SpecialEventsValues.Train; if (train) { if (!updated) { Data?.Invoke(scsTelemetry, true); updated = true; } Train?.Invoke(this, new EventArgs()); } } if (refuel != scsTelemetry.SpecialEventsValues.Refuel) { refuel = scsTelemetry.SpecialEventsValues.Refuel; if (scsTelemetry.SpecialEventsValues.Refuel) { RefuelStart?.Invoke(this, new EventArgs()); } else { RefuelEnd?.Invoke(this, new EventArgs()); } } if (refuelPayed != scsTelemetry.SpecialEventsValues.RefuelPaid) { refuelPayed = scsTelemetry.SpecialEventsValues.RefuelPaid; if (scsTelemetry.SpecialEventsValues.RefuelPaid) { RefuelPaid?.Invoke(this, new EventArgs()); } } // currently the design is that the event is called, doesn't matter if data changed // also the old demo didn't used the flag and expected to be refreshed each call // so without making a big change also call the event without update with false flag if (!updated) { Data?.Invoke(scsTelemetry, false); } }
private void _updateTimer_Elapsed(object sender) { var scsTelemetry = SharedMemory.Update <SCSTelemetry>(); // check if sdk is NOT running if (!scsTelemetry.SdkActive && !paused) { // if so don't check so often the data var tsInterval = new TimeSpan(0, 0, 0, 0, DefaultPausedUpdateInterval); _updateTimer.Change(tsInterval.Add(tsInterval), tsInterval); paused = true; // if sdk not active we don't need to do something return; } if (paused && scsTelemetry.SdkActive) { // ok sdk is active now paused = false; resume(); // going back to normal update rate } var time = scsTelemetry.Timestamp; Data?.Invoke(scsTelemetry, time != lastTime); //TODO: make it nicer thats a lot of code for such less work // Job start event if (wasOnJob != scsTelemetry.SpecialEventsValues.OnJob) { wasOnJob = scsTelemetry.SpecialEventsValues.OnJob; if (scsTelemetry.SpecialEventsValues.OnJob) { JobStarted?.Invoke(this, new EventArgs()); } } if (cancelled != scsTelemetry.SpecialEventsValues.JobCancelled) { cancelled = scsTelemetry.SpecialEventsValues.JobCancelled; if (scsTelemetry.SpecialEventsValues.JobCancelled) { JobCancelled?.Invoke(this, new EventArgs()); } } if (delivered != scsTelemetry.SpecialEventsValues.JobDelivered) { delivered = scsTelemetry.SpecialEventsValues.JobDelivered; if (scsTelemetry.SpecialEventsValues.JobDelivered) { JobDelivered?.Invoke(this, new EventArgs()); } } if (fined != scsTelemetry.SpecialEventsValues.Fined) { fined = scsTelemetry.SpecialEventsValues.Fined; if (scsTelemetry.SpecialEventsValues.Fined) { Fined?.Invoke(this, new EventArgs()); } } if (tollgate != scsTelemetry.SpecialEventsValues.Tollgate) { tollgate = scsTelemetry.SpecialEventsValues.Tollgate; if (scsTelemetry.SpecialEventsValues.Tollgate) { Tollgate?.Invoke(this, new EventArgs()); } } if (ferry != scsTelemetry.SpecialEventsValues.Ferry) { ferry = scsTelemetry.SpecialEventsValues.Ferry; if (scsTelemetry.SpecialEventsValues.Ferry) { Ferry?.Invoke(this, new EventArgs()); } } if (train != scsTelemetry.SpecialEventsValues.Train) { train = scsTelemetry.SpecialEventsValues.Train; if (scsTelemetry.SpecialEventsValues.Train) { Train?.Invoke(this, new EventArgs()); } } if (refuel != scsTelemetry.SpecialEventsValues.Refuel) { refuel = scsTelemetry.SpecialEventsValues.Refuel; if (scsTelemetry.SpecialEventsValues.Refuel) { Refuel?.Invoke(this, new EventArgs()); } } lastTime = time; }