Skip to content
This repository has been archived by the owner on Jul 4, 2023. It is now read-only.

Commit

Permalink
allow to define waitTime for locomotive in reached blocks #107 (#108)
Browse files Browse the repository at this point in the history
Co-authored-by: Christian Benjamin Ries <c.ries@beckhoff.com>
  • Loading branch information
cbries and Christian Benjamin Ries committed Apr 16, 2021
1 parent 1288913 commit 060b0ec
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 5 deletions.
2 changes: 1 addition & 1 deletion EcosApp/railessentials.min.js

Large diffs are not rendered by default.

36 changes: 35 additions & 1 deletion railessentials/AutoMode/AutoMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Linq;
using System.Threading.Tasks;
using ecoslib.Entities;
using ecoslib.Utilities.Commands;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using railessentials.Analyzer;
Expand All @@ -30,7 +31,6 @@ namespace railessentials.AutoMode

public partial class AutoMode
{
public const int RunPauseForBlockSeconds = 10;
private const int RunDelayBetweenChecksMsecs = 2500;

public event AutoModeStarted Started;
Expand All @@ -53,6 +53,40 @@ public partial class AutoMode
private readonly object _autoModeTasksLock = new();
private readonly List<AutoModeTaskCore> _autoModeTasks = new();

private readonly Random _random = new Random();

internal static int GetSecondsToWaitFallback()
{
return 10;
}

internal int GetSecondsToWait()
{
var cfg = _ctx?._cfg?.Cfg;
if (cfg == null) return GetSecondsToWaitFallback();

if (cfg?.OccWait?.WaitModeStatic != null)
{
if (cfg.OccWait.WaitModeStatic.Enabled)
{
return cfg.OccWait.WaitModeStatic.Seconds;
}
}

if (cfg?.OccWait?.WaitModeRandom != null)
{
if (cfg.OccWait.WaitModeRandom.Enabled)
{
return _random.Next(
cfg.OccWait.WaitModeRandom.SecondsMin,
cfg.OccWait.WaitModeRandom.SecondsMax
);
}
}

return GetSecondsToWaitFallback();
}

internal ClientHandler.ClientHandler GetClientHandler()
{
return _ctx;
Expand Down
5 changes: 4 additions & 1 deletion railessentials/AutoMode/AutoModeTaskBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,10 @@ await DecelerateLocomotive(Route.Locomotive, maxSecsToStop: (int)durationSeconds
Ctx?.ResetRouteFor(Route.LocomotiveObjectId);
Route.OccBlock.FromBlock = finalBlock;
Route.OccBlock.ReachedTime = DateTime.Now;
Route.OccBlock.SecondsToWait = AutoMode.RunPauseForBlockSeconds;
if (Ctx != null)
Route.OccBlock.SecondsToWait = Ctx.GetSecondsToWait();
else
Route.OccBlock.SecondsToWait = AutoMode.GetSecondsToWaitFallback();
Ctx?.SaveOccAndPromote();
if (IsCanceled()) return;
Expand Down
2 changes: 1 addition & 1 deletion railessentials/ClientHandler/ClientHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class ClientHandlerCfg

public partial class ClientHandler
{
private ClientHandlerCfg _cfg;
internal ClientHandlerCfg _cfg;
internal Sniffer _sniffer;
private JArray _themeData;
internal Metadata _metadata;
Expand Down
22 changes: 22 additions & 0 deletions railessentials/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public class Configuration
public bool SaveOnEveryPlanfieldChange { get; set; } = false;
public int InitializeDelay { get; set; } = 500;

[JsonProperty(PropertyName = "Occ")]
public OccWaitModes OccWait { get; set; } = new();

private static string _cfgPath;

public static Configuration Load(string pathToCfg, out string errorMessage)
Expand Down Expand Up @@ -116,6 +119,25 @@ public void UpdateWebcam(JObject obj)
}
}

public class OccWaitModeStatic
{
public bool Enabled { get; set; } = true;
public int Seconds { get; set; } = 10;
}

public class OccWaitModeRandom
{
public bool Enabled { get; set; } = false;
public int SecondsMin { get; set; } = 5;
public int SecondsMax { get; set; } = 15;
}

public class OccWaitModes
{
public OccWaitModeStatic WaitModeStatic { get; set; } = new();
public OccWaitModeRandom WaitModeRandom { get; set; } = new();
}

public class ConfigurationEcos
{
public string Ip { get; set; }
Expand Down
13 changes: 12 additions & 1 deletion railessentials/railessentials.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,16 @@
"PlanBackground": "rgba(243,243,243,1)"
},
"SaveOnEveryPlanfieldChange": true,
"InitializeDelay": 500
"InitializeDelay": 500,
"Occ": {
"WaitModeStatic": {
"Enabled": false,
"Seconds": 9
},
"WaitModeRandom": {
"Enabled": true,
"SecondsMin": 4,
"SecondsMax": 14
}
}
}

0 comments on commit 060b0ec

Please sign in to comment.