-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathECoSListener.Connector.cs
209 lines (180 loc) · 6.07 KB
/
ECoSListener.Connector.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
* MIT License
*
* Copyright (c) 2018 Dr. Christian Benjamin Ries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace ECoSListener
{
using System;
using System.Collections.Generic;
using ECoSConnector;
using ECoSCore;
using ECoSUtils;
public partial class ECoSListener
{
private void HandleBlocks(IReadOnlyList<IBlock> blocks)
{
foreach (var block in blocks)
{
if (block == null) continue;
if (string.IsNullOrEmpty(block.NativeBlock)) continue;
GetDataProvider().HandleData(block);
}
SendCommandsOfObjects();
Changed?.Invoke(this, GetDataProvider());
}
private void HandleBlocksS88(IReadOnlyList<IBlock> blocks)
{
foreach (var block in blocks)
{
if (block == null) continue;
if (string.IsNullOrEmpty(block.NativeBlock)) continue;
GetDataProviderS88().HandleData(block);
}
SendCommandsOfObjectsS88();
Changed?.Invoke(this, GetDataProviderS88());
}
private void SendCommandsOfObjects()
{
var dp = GetDataProvider();
var objs = dp.Objects;
var con = GetConnector();
if (con == null) return;
lock (objs)
{
foreach (var o in objs)
{
if (o == null)
continue;
var stack = o.Commands;
while (stack.Count > 0)
con.SendCommand(stack.Pop());
}
}
}
private void SendCommandsOfObjectsS88()
{
var dp = GetDataProviderS88();
var objs = dp.Objects;
var con = GetS88Connector();
if (con == null) return;
lock (objs)
{
foreach (var o in objs)
{
if (o == null)
continue;
var stack = o.Commands;
while (stack.Count > 0)
con.SendCommand(stack.Pop());
}
}
}
private void ConnectorOnFailed(object sender, MessageEventArgs ev)
{
Stop();
Log?.Error($"{ev.Message}");
}
private void ConnectorOnStopped(object sender, EventArgs ev)
{
Stop();
Log?.Info("ECoS connection stopped");
}
private void ConnectorOnStarted(object sender, EventArgs ev)
{
Log?.Info("ECoS connection established");
List<ICommand> initialCommands = new List<ICommand>()
{
CommandFactory.Create($"request({Globals.ID_EV_BASEOBJECT}, view)"),
CommandFactory.Create($"get({Globals.ID_EV_BASEOBJECT}, info, status)"),
CommandFactory.Create($"request(5, view)"),
CommandFactory.Create($"request({Globals.ID_EV_LOCOMOTIVES}, view)"),
CommandFactory.Create($"request({Globals.ID_EV_ACCESSORIES}, view)"), // viewswitch
CommandFactory.Create($"queryObjects({Globals.ID_EV_ACCESSORIES}, addr, protocol, type, addrext, mode, symbol, name1, name2, name3)"),
CommandFactory.Create($"queryObjects({Globals.ID_EV_LOCOMOTIVES}, addr, name, protocol)")
};
Connector.SendCommands(initialCommands);
}
private readonly List<string> _lines = new List<string>();
private void ConnectorOnMessageReceived(object sender, MessageEventArgs ev)
{
var sw = new StopWatch();
sw.Start();
if (string.IsNullOrEmpty(ev.Message)) return;
if (BlockUtils.HasAnyBlock(ev.Message))
{
_lines.Clear();
_lines.AddRange(ev.Message.Split(new[] { '\r' }, StringSplitOptions.RemoveEmptyEntries));
}
else
{
_lines.Add(ev.Message.Trim());
}
if (BlockUtils.HasAnyBlock(_lines))
{
var blocks = BlockUtils.GetBlocks(_lines);
HandleBlocks(blocks);
_lines.Clear();
}
sw.Stop();
}
private void S88COnFailed(object sender, MessageEventArgs ev)
{
Stop();
Log?.Error($"{ev.Message}");
}
private void S88COnStopped(object sender, EventArgs e)
{
Stop();
Log?.Info("ECoS connection for S88 stopped");
}
private void S88COnStarted(object sender, EventArgs e)
{
Log?.Info("ECoS connection for S88 established");
List<ICommand> initialCommands = new List<ICommand>()
{
CommandFactory.Create($"queryObjects({Globals.ID_EV_S88}, ports)"),
CommandFactory.Create($"request({Globals.ID_EV_S88}, view)")
};
S88Connector.SendCommands(initialCommands);
}
private readonly List<string> _s88Lines = new List<string>();
private void S88COnMessageReceived(object sender, MessageEventArgs ev)
{
if (string.IsNullOrEmpty(ev.Message)) return;
if (BlockUtils.HasAnyBlock(ev.Message))
{
_s88Lines.Clear();
_s88Lines.AddRange(ev.Message.Split(new[] { '\r' }, StringSplitOptions.RemoveEmptyEntries));
}
else
{
_s88Lines.Add(ev.Message.Trim());
}
if (BlockUtils.HasAnyBlock(_s88Lines))
{
var blocks = BlockUtils.GetBlocks(_s88Lines);
HandleBlocksS88(blocks);
_s88Lines.Clear();
}
}
}
}