Permalink
Browse files

general update

  • Loading branch information...
cbries committed Dec 13, 2017
1 parent a7adeec commit 558eae334ff3a1886b8cd752179bde114421d89c
@@ -21,12 +21,12 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Communicator;
using Ecos2Core;
using Ecos2Core.Replies;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Dispatcher
{
@@ -66,7 +66,7 @@ public bool Shutdown()
{
try
{
if(_client != null)
if (_client != null)
return _client.Stop();
return true;
}
@@ -106,7 +106,7 @@ private bool ConnectToEcos2()
HasError = false;
ErrorMessage = "";
_client = new Connector {Cfg = Cfg};
_client = new Connector { Cfg = Cfg };
_client.Started += COnStarted;
_client.Stopped += COnStopped;
_client.Failed += COnFailed;
@@ -115,7 +115,7 @@ private bool ConnectToEcos2()
return r;
}
catch(Exception ex)
catch (Exception ex)
{
HasError = true;
ErrorMessage = ex.Message;
@@ -151,7 +151,7 @@ private void COnMessageReceived(object o, string msg)
BlocksReceived(this, blocks);
_lines.Clear();
}
}
}
private void COnFailed(object o, string errmsg)
@@ -170,7 +170,7 @@ private void COnStopped(object o)
HasError = false;
ErrorMessage = "";
if(CommunicationStopped != null)
if (CommunicationStopped != null)
CommunicationStopped(this, EventArgs.Empty);
Logger?.Log("Communication stopped\r\n");
View
@@ -323,6 +323,11 @@ private void CommunicationOnCommunicationFailed(object sender, EventArgs eventAr
}
}
public void HandleFakeBlocks(IReadOnlyList<IBlock> blocks)
{
CommunicationOnBlocksReceived(null, blocks);
}
private void CommunicationOnBlocksReceived(object sender, IReadOnlyList<IBlock> blocks)
{
if (Logger != null)
View
@@ -105,7 +105,7 @@ public static IReadOnlyList<IBlock> GetBlocks(IReadOnlyList<string> lines)
if (firstLine.StartsWith("<EVENT ", StringComparison.OrdinalIgnoreCase))
instance = new EventBlock();
else if(firstLine.StartsWith("<REPLY ", StringComparison.OrdinalIgnoreCase))
else if (firstLine.StartsWith("<REPLY ", StringComparison.OrdinalIgnoreCase))
instance = new ReplyBlock();
if (instance != null)
@@ -124,5 +124,25 @@ public static IReadOnlyList<IBlock> GetBlocks(IReadOnlyList<string> lines)
return blocks;
}
public static IReadOnlyList<IBlock> GetBlocks(string msg)
{
var line = msg.Trim();
List<string> lines = new List<string>();
if (Utils.HasAnyBlock(msg))
{
lines.Clear();
lines.AddRange(msg.Split(new[] { '\r' }, StringSplitOptions.RemoveEmptyEntries));
}
else
{
lines.Add(line);
}
if (Utils.HasAnyBlock(lines))
return Utils.GetBlocks(lines);
return null;
}
}
}
@@ -37,6 +37,6 @@ public interface IRailwayEssentialModel
void UpdateTrackUi();
// winid: 1:=Locomotives, TBD
void UpdateWindowUi(int winid);
void ExecuteJs(string code, object sender=null);
void ExecuteJs(string code, object sender = null);
}
}
@@ -703,6 +703,16 @@ private async void AfterOpen()
var dataProvider = _dispatcher.GetDataProvider();
dataProvider.DataChanged += OnDataChanged;
dataProvider.CommandsReady += DataProviderOnCommandsReady;
dataProvider.FakeCommands += (sender, blocks) =>
{
foreach (var blk in blocks)
{
if (blk == null)
continue;
dataProvider.Add(blk);
}
};
foreach (var objFilename in Project.Objects)
{
@@ -1042,6 +1052,12 @@ private async void DataProviderOnCommandsReady(object sender, IReadOnlyList<ICom
await _dispatcher.ForwardCommands(commands);
}
private void ForwardFakeCommands(object sender, IReadOnlyList<IBlock> blocks)
{
if (_dispatcher != null)
_dispatcher.HandleFakeBlocks(blocks);
}
private void OnDataChanged(object sender)
{
_ctx.Send(state =>
@@ -22,6 +22,7 @@
* SOFTWARE.
*/
using Ecos2Core;
using Ecos2Core.Replies;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
@@ -307,20 +308,20 @@ public void DoChange(int index)
{
CommandFactory.Create($"request(11, control, force)"),
CommandFactory.Create($"set(11, switch[{Protocol}{s}])"),
CommandFactory.Create($"release(11, control)")
};
OnCommandsReady(this, ctrlCmds);
CommandFactory.Create($"release(11, control)"),
// get update
ctrlCmds.Clear();
ctrlCmds = new List<ICommand>
{
CommandFactory.Create($"get({ObjectId}, state)"),
// request update for current object
CommandFactory.Create($"get({ObjectId}, state)")
};
OnCommandsReady(this, ctrlCmds);
// create dummy block to trigger trackviewer immediatelly
//<EVENT 11>
// 11 switch[DCC4r]
// <END 0(OK)>
var blocks = Utils.GetBlocks($"<EVENT {ObjectId}>\r\n{ObjectId} switch[{Protocol}{s}]\r\n<END 0(OK)>\r\n");
OnFakeCommands(this, blocks);
}
}
}
@@ -43,6 +43,7 @@ public class DataProvider : IDataProvider, INotifyPropertyChanged
{
public event DataChangedDelegator DataChanged;
public event CommandsReadyDelegator CommandsReady;
public event FakeCommandsDelegator FakeCommands;
private readonly ObservableCollection<IItem> _objects = new ObservableCollection<IItem>();
@@ -126,7 +127,7 @@ public bool LoadObjects(string targetPath)
var e = new Locomotive();
e.ParseJson(arItem as JObject);
e.CommandsReady += CommandsReady;
e.CommandsReadyItem += CommandsReady;
_objects.Add(e);
DataChanged?.Invoke(this);
}
@@ -149,7 +150,8 @@ public bool LoadObjects(string targetPath)
var e = new Accessory();
e.ParseJson(arItem as JObject);
e.CommandsReady += CommandsReady;
e.CommandsReadyItem += CommandsReady;
e.FakeCommands += FakeCommands;
_objects.Add(e);
DataChanged?.Invoke(this);
}
@@ -168,7 +170,7 @@ public bool LoadObjects(string targetPath)
var e = new Route();
e.ParseJson(arItem as JObject);
e.CommandsReady += CommandsReady;
e.CommandsReadyItem += CommandsReady;
_objects.Add(e);
DataChanged?.Invoke(this);
}
@@ -187,7 +189,7 @@ public bool LoadObjects(string targetPath)
var e = new S88();
e.ParseJson(arItem as JObject);
e.CommandsReady += CommandsReady;
e.CommandsReadyItem += CommandsReady;
_objects.Add(e);
DataChanged?.Invoke(this);
}
@@ -249,7 +251,7 @@ private bool HandleEvent(IBlock block)
}
var s88 = new S88 { ObjectId = s88Info.ObjectId, Index = n };
s88.CommandsReady += CommandsReady;
s88.CommandsReadyItem += CommandsReady;
_objects.Add(s88);
DataChanged?.Invoke(this);
s88.EnableView();
@@ -268,7 +270,7 @@ private bool HandleEvent(IBlock block)
if (HandleEventS88(item, e))
continue;
if (HandleEventSwitch(item, e))
if (HandleEventAccessory(item, e))
continue;
if (HandleEventLocomotive(item, e))
@@ -296,19 +298,16 @@ private bool HandleEventLocomotive(IItem item, ListEntry listEntry)
return true;
}
private bool HandleEventSwitch(IItem item, ListEntry listEntry)
private bool HandleEventAccessory(IItem item, ListEntry listEntry)
{
Accessory e = item as Accessory;
if (e == null)
if (!(item is Accessory e))
return false;
var itemSwitch = GetObjectBy(e.ObjectId) as Accessory;
if (itemSwitch != null)
if (GetObjectBy(e.ObjectId) is Accessory itemAccessory)
{
itemSwitch.Parse(listEntry.Arguments);
itemSwitch.UpdateTitle();
itemSwitch.UpdateSubTitle();
itemAccessory.Parse(listEntry.Arguments);
itemAccessory.UpdateTitle();
itemAccessory.UpdateSubTitle();
}
return true;
@@ -371,7 +370,7 @@ public bool Add(IBlock block)
return HandleBaseobject(block);
case 11: // switch or route
return AddSwitchOrRoute(block);
return AddAccessoryOrRoute(block);
case 10: // locomotive
return AddLocomotive(block);
@@ -435,7 +434,7 @@ private bool HandleBaseobject(IBlock block)
return true;
}
private bool AddSwitchOrRoute(IBlock block)
private bool AddAccessoryOrRoute(IBlock block)
{
if (block == null)
return false;
@@ -449,24 +448,28 @@ private bool AddSwitchOrRoute(IBlock block)
if (sid.StartsWith("20", StringComparison.OrdinalIgnoreCase))
{
var sw = GetObjectBy(e.ObjectId);
if (sw == null)
var accessory = GetObjectBy(e.ObjectId);
if (accessory == null)
{
sw = new Accessory { ObjectId = e.ObjectId };
sw.Parse(e.Arguments);
sw.CommandsReady += CommandsReady;
_objects.Add(sw);
accessory = new Accessory { ObjectId = e.ObjectId };
accessory.Parse(e.Arguments);
accessory.CommandsReadyItem += CommandsReady;
accessory.FakeCommands += FakeCommands;
_objects.Add(accessory);
DataChanged?.Invoke(this);
sw.EnableView();
accessory.EnableView();
}
else
{
sw.Parse(e.Arguments);
sw.CommandsReady -= CommandsReady;
sw.CommandsReady += CommandsReady;
accessory.Parse(e.Arguments);
accessory.CommandsReadyItem -= CommandsReady;
accessory.CommandsReadyItem += CommandsReady;
accessory.FakeCommands -= FakeCommands;
accessory.FakeCommands += FakeCommands;
DataChanged?.Invoke(this);
if (!sw.HasView)
sw.EnableView();
if (!accessory.HasView)
accessory.EnableView();
}
}
else if (sid.StartsWith("30", StringComparison.OrdinalIgnoreCase))
@@ -476,16 +479,16 @@ private bool AddSwitchOrRoute(IBlock block)
{
r = new Route { ObjectId = e.ObjectId };
r.Parse(e.Arguments);
r.CommandsReady += CommandsReady;
r.CommandsReadyItem += CommandsReady;
_objects.Add(r);
DataChanged?.Invoke(this);
r.EnableView();
}
else
{
r.Parse(e.Arguments);
r.CommandsReady -= CommandsReady;
r.CommandsReady += CommandsReady;
r.CommandsReadyItem -= CommandsReady;
r.CommandsReadyItem += CommandsReady;
if (!r.HasView)
r.EnableView();
DataChanged?.Invoke(this);
@@ -515,7 +518,7 @@ private bool AddLocomotive(IBlock block)
var l = new Locomotive { ObjectId = e.ObjectId };
l.IsKnownByCommandStation = true;
l.Parse(e.Arguments);
l.CommandsReady += CommandsReady;
l.CommandsReadyItem += CommandsReady;
_objects.Add(l);
DataChanged?.Invoke(this);
l.EnableView();
@@ -528,8 +531,8 @@ private bool AddLocomotive(IBlock block)
{
l.IsKnownByCommandStation = true;
l.Parse(e.Arguments);
l.CommandsReady -= CommandsReady;
l.CommandsReady += CommandsReady;
l.CommandsReadyItem -= CommandsReady;
l.CommandsReadyItem += CommandsReady;
if (!l.HasView)
l.EnableView();
@@ -569,16 +572,16 @@ private bool AddS88(IBlock block)
{
s88 = new S88 { ObjectId = e.ObjectId, Index = n };
s88.Parse(e.Arguments);
s88.CommandsReady += CommandsReady;
s88.CommandsReadyItem += CommandsReady;
_objects.Add(s88);
DataChanged?.Invoke(this);
s88.EnableView();
}
else
{
s88.Parse(e.Arguments);
s88.CommandsReady -= CommandsReady;
s88.CommandsReady += CommandsReady;
s88.CommandsReadyItem -= CommandsReady;
s88.CommandsReadyItem += CommandsReady;
if (!s88.HasView)
s88.EnableView();
DataChanged?.Invoke(this);
Oops, something went wrong.

0 comments on commit 558eae3

Please sign in to comment.