-
Notifications
You must be signed in to change notification settings - Fork 325
/
JmriSRCPThrottleServer.java
427 lines (389 loc) · 18.3 KB
/
JmriSRCPThrottleServer.java
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
package jmri.jmris.srcp;
import java.beans.PropertyChangeListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import jmri.DccLocoAddress;
import jmri.DccThrottle;
import jmri.JmriException;
import jmri.LocoAddress;
import jmri.Throttle;
import jmri.ThrottleManager;
import jmri.jmris.AbstractThrottleServer;
import jmri.jmrix.SystemConnectionMemo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Interface between the JMRI Throttles and an SRCP network connection
*
* @author Paul Bender Copyright (C) 2016
*/
public class JmriSRCPThrottleServer extends AbstractThrottleServer {
private static final Logger log = LoggerFactory.getLogger(JmriSRCPThrottleServer.class);
private final DataOutputStream output;
private final ArrayList<Integer> busList;
private final ArrayList<LocoAddress> addressList;
public JmriSRCPThrottleServer(DataInputStream inStream, DataOutputStream outStream) {
super();
busList = new ArrayList<>();
addressList = new ArrayList<>();
output = outStream;
}
/*
* Protocol Specific Functions
*/
@Override
public void sendStatus(LocoAddress l) throws IOException {
TimeStampedOutput.writeTimestamp(output, Bundle.getMessage("Error499"));
}
/*
* send the status of the specified throttle address on the specified bus
* @param bus bus number.
* @param address locomoitve address.
*/
public void sendStatus(int bus, int address) throws IOException {
log.debug("send Status called with bus {} and address {}", bus, address);
/* translate the bus into a system connection memo */
java.util.List<SystemConnectionMemo> list = jmri.InstanceManager.getList(SystemConnectionMemo.class);
SystemConnectionMemo memo;
try {
memo = list.get(bus - 1);
} catch (java.lang.IndexOutOfBoundsException obe) {
TimeStampedOutput.writeTimestamp(output, Bundle.getMessage("Error412"));
return;
}
/* request the throttle for this particular locomotive address */
if (memo.provides(jmri.ThrottleManager.class)) {
ThrottleManager t = memo.get(jmri.ThrottleManager.class);
// we will use getThrottleInfo to request information about the
// address, so we need to convert the address to a DccLocoAddress
// object first.
DccLocoAddress addr = new DccLocoAddress(address, !(t.canBeShortAddress(address)));
Boolean isForward = (Boolean) t.getThrottleInfo(addr, "IsForward");
Float speedSetting = (Float) t.getThrottleInfo(addr, "SpeedSetting");
Integer speedStepMode = (Integer) t.getThrottleInfo(addr, "SpeedStepMode");
Boolean f0 = (Boolean) t.getThrottleInfo(addr, "F0");
Boolean f1 = (Boolean) t.getThrottleInfo(addr, "F1");
Boolean f2 = (Boolean) t.getThrottleInfo(addr, "F2");
Boolean f3 = (Boolean) t.getThrottleInfo(addr, "F3");
Boolean f4 = (Boolean) t.getThrottleInfo(addr, "F4");
Boolean f5 = (Boolean) t.getThrottleInfo(addr, "F5");
Boolean f6 = (Boolean) t.getThrottleInfo(addr, "F6");
Boolean f7 = (Boolean) t.getThrottleInfo(addr, "F7");
Boolean f8 = (Boolean) t.getThrottleInfo(addr, "F8");
Boolean f9 = (Boolean) t.getThrottleInfo(addr, "F9");
Boolean f10 = (Boolean) t.getThrottleInfo(addr, "F10");
Boolean f11 = (Boolean) t.getThrottleInfo(addr, "F11");
Boolean f12 = (Boolean) t.getThrottleInfo(addr, "F12");
Boolean f13 = (Boolean) t.getThrottleInfo(addr, "F13");
Boolean f14 = (Boolean) t.getThrottleInfo(addr, "F14");
Boolean f15 = (Boolean) t.getThrottleInfo(addr, "F15");
Boolean f16 = (Boolean) t.getThrottleInfo(addr, "F16");
Boolean f17 = (Boolean) t.getThrottleInfo(addr, "F17");
Boolean f18 = (Boolean) t.getThrottleInfo(addr, "F18");
Boolean f19 = (Boolean) t.getThrottleInfo(addr, "F19");
Boolean f20 = (Boolean) t.getThrottleInfo(addr, "F20");
Boolean f21 = (Boolean) t.getThrottleInfo(addr, "F21");
Boolean f22 = (Boolean) t.getThrottleInfo(addr, "F22");
Boolean f23 = (Boolean) t.getThrottleInfo(addr, "F23");
Boolean f24 = (Boolean) t.getThrottleInfo(addr, "F24");
Boolean f25 = (Boolean) t.getThrottleInfo(addr, "F25");
Boolean f26 = (Boolean) t.getThrottleInfo(addr, "F26");
Boolean f27 = (Boolean) t.getThrottleInfo(addr, "F27");
Boolean f28 = (Boolean) t.getThrottleInfo(addr, "F28");
// and now build the output string to send
String StatusString = "100 INFO " + bus + " GL " + address + " ";
StatusString += isForward ? "1 " : "0 ";
switch (speedStepMode) {
case DccThrottle.SpeedStepMode14:
StatusString += (int) java.lang.Math.ceil(speedSetting * 14) + " " + 14;
break;
case DccThrottle.SpeedStepMode27:
StatusString += (int) java.lang.Math.ceil(speedSetting * 27) + " " + 27;
break;
case DccThrottle.SpeedStepMode28:
StatusString += (int) java.lang.Math.ceil(speedSetting * 28) + " " + 28;
break;
case DccThrottle.SpeedStepMode128:
StatusString += (int) java.lang.Math.ceil(speedSetting * 126) + " " + 126;
break;
default:
StatusString += (int) java.lang.Math.ceil(speedSetting * 100) + " " + 100;
}
StatusString += f0 ? " 1" : " 0";
StatusString += f1 ? " 1" : " 0";
StatusString += f2 ? " 1" : " 0";
StatusString += f3 ? " 1" : " 0";
StatusString += f4 ? " 1" : " 0";
StatusString += f5 ? " 1" : " 0";
StatusString += f6 ? " 1" : " 0";
StatusString += f7 ? " 1" : " 0";
StatusString += f8 ? " 1" : " 0";
StatusString += f9 ? " 1" : " 0";
StatusString += f10 ? " 1" : " 0";
StatusString += f11 ? " 1" : " 0";
StatusString += f12 ? " 1" : " 0";
StatusString += f13 ? " 1" : " 0";
StatusString += f14 ? " 1" : " 0";
StatusString += f15 ? " 1" : " 0";
StatusString += f16 ? " 1" : " 0";
StatusString += f17 ? " 1" : " 0";
StatusString += f18 ? " 1" : " 0";
StatusString += f19 ? " 1" : " 0";
StatusString += f20 ? " 1" : " 0";
StatusString += f21 ? " 1" : " 0";
StatusString += f22 ? " 1" : " 0";
StatusString += f23 ? " 1" : " 0";
StatusString += f24 ? " 1" : " 0";
StatusString += f25 ? " 1" : " 0";
StatusString += f26 ? " 1" : " 0";
StatusString += f27 ? " 1" : " 0";
StatusString += f28 ? " 1" : " 0";
StatusString += "\n\r";
TimeStampedOutput.writeTimestamp(output, StatusString);
} else {
TimeStampedOutput.writeTimestamp(output, Bundle.getMessage("Error412"));
}
}
@Override
public void sendErrorStatus() throws IOException {
TimeStampedOutput.writeTimestamp(output, Bundle.getMessage("Error499"));
}
@Override
public void parsecommand(String statusString) throws JmriException, IOException {
}
@Override
public void sendThrottleFound(jmri.LocoAddress address) throws IOException {
Integer bus;
if (addressList.contains(address)) {
bus = busList.get(addressList.indexOf(address));
} else {
// we didn't request this address.
return;
}
// Build the output string to send
String StatusString = "101 INFO " + bus + " GL " + address.getNumber() + " "; // assume DCC for now.
StatusString += address.getProtocol() == jmri.LocoAddress.Protocol.DCC_SHORT ? "N 1 28" : "N 2 28";
StatusString += "\n\r";
TimeStampedOutput.writeTimestamp(output, StatusString);
}
@Override
public void sendThrottleReleased(jmri.LocoAddress address) throws IOException {
Integer bus;
if (addressList.contains(address)) {
bus = busList.get(addressList.indexOf(address));
} else {
// we didn't request this address.
return;
}
// Build the output string to send
String StatusString = "102 INFO " + bus + " GL " + address.getNumber(); // assume DCC for now.
StatusString += address.getProtocol() == jmri.LocoAddress.Protocol.DCC_SHORT ? "N 1 28" : "N 2 28";
StatusString += "\n\r";
TimeStampedOutput.writeTimestamp(output, StatusString);
}
public void initThrottle(int bus, int address, boolean isLong,
int speedsteps, int functions) throws IOException {
log.debug("initThrottle called with bus {} and address {}", bus, address);
/* translate the bus into a system connection memo */
java.util.List<SystemConnectionMemo> list = jmri.InstanceManager.getList(SystemConnectionMemo.class);
SystemConnectionMemo memo;
try {
memo = list.get(bus - 1);
} catch (java.lang.IndexOutOfBoundsException obe) {
TimeStampedOutput.writeTimestamp(output, Bundle.getMessage("Error412"));
return;
}
/* request the throttle for this particular locomotive address */
if (memo.provides(jmri.ThrottleManager.class)) {
ThrottleManager t = memo.get(jmri.ThrottleManager.class);
// we will use getThrottleInfo to request information about the
// address, so we need to convert the address to a DccLocoAddress
// object first.
DccLocoAddress addr = new DccLocoAddress(address, isLong);
busList.add(bus);
addressList.add(addr);
t.requestThrottle(addr, this);
}
}
public void releaseThrottle(int bus, int address) throws IOException {
log.debug("releaseThrottle called with bus {} and address {}", bus, address);
/* translate the bus into a system connection memo */
java.util.List<SystemConnectionMemo> list = jmri.InstanceManager.getList(SystemConnectionMemo.class);
SystemConnectionMemo memo;
try {
memo = list.get(bus - 1);
} catch (java.lang.IndexOutOfBoundsException obe) {
TimeStampedOutput.writeTimestamp(output, Bundle.getMessage("Error412"));
return;
}
/* release the throttle for this particular locomotive address */
if (memo.provides(jmri.ThrottleManager.class)) {
ThrottleManager t = memo.get(jmri.ThrottleManager.class);
DccLocoAddress addr = new DccLocoAddress(address, t.canBeLongAddress(address));
t.releaseThrottle((DccThrottle) throttleList.get(addressList.indexOf(addr)), this);
throttleList.remove(addressList.indexOf(addr));
sendThrottleReleased(addr);
busList.remove(addressList.indexOf(addr));
addressList.remove(addr);
}
}
/*
* Set Throttle Speed and Direction
*
* @param bus, bus the throttle is on.
* @param l address of the locomotive to change speed of.
* @param speed float representing the speed, -1 for emergency stop.
* @param isForward boolean, true if forward, false if reverse or
* undefined.
*/
public void setThrottleSpeedAndDirection(int bus, int address, float speed, boolean isForward) {
log.debug("Setting Speed for address {} bus {} to {} with direction {}",
address, bus, speed, isForward ? "forward" : "reverse");
java.util.List<SystemConnectionMemo> list = jmri.InstanceManager.getList(SystemConnectionMemo.class);
SystemConnectionMemo memo;
try {
memo = list.get(bus - 1);
} catch (java.lang.IndexOutOfBoundsException obe) {
try {
TimeStampedOutput.writeTimestamp(output, Bundle.getMessage("Error412"));
} catch (IOException ioe) {
log.error("Error writing to network port");
}
return;
}
/* request the throttle for this particular locomotive address */
if (memo.provides(jmri.ThrottleManager.class)) {
ThrottleManager tm = memo.get(jmri.ThrottleManager.class);
// we will use getThrottleInfo to request information about the
// address, so we need to convert the address to a DccLocoAddress
// object first.
DccLocoAddress addr = new DccLocoAddress(address, tm.canBeLongAddress(address));
// get the throttle for the address.
if (addressList.contains(addr)) {
log.debug("Throttle in throttle list");
Throttle t = throttleList.get(addressList.indexOf(addr));
// set the speed and direction.
t.setSpeedSetting(speed);
t.setIsForward(isForward);
}
}
}
/*
* Set Throttle Functions on/off
*
* @param bus, bus the throttle is on.
* @param l address of the locomotive to change speed of.
* @param fList an ArrayList of boolean values indicating whether the
* function is active or not.
*/
public void setThrottleFunctions(int bus, int address, ArrayList<Boolean> fList) {
log.debug("Setting Functions for address {} bus {}",
address, bus);
java.util.List<SystemConnectionMemo> list = jmri.InstanceManager.getList(SystemConnectionMemo.class);
SystemConnectionMemo memo;
try {
memo = list.get(bus - 1);
} catch (java.lang.IndexOutOfBoundsException obe) {
try {
TimeStampedOutput.writeTimestamp(output, Bundle.getMessage("Error412"));
} catch (IOException ioe) {
log.error("Error writing to network port");
}
return;
}
/* request the throttle for this particular locomotive address */
if (memo.provides(jmri.ThrottleManager.class)) {
ThrottleManager tm = memo.get(jmri.ThrottleManager.class);
// we will use getThrottleInfo to request information about the
// address, so we need to convert the address to a DccLocoAddress
// object first.
DccLocoAddress addr = new DccLocoAddress(address, tm.canBeLongAddress(address));
// get the throttle for the address.
if (addressList.contains(addr)) {
log.debug("Throttle in throttle list");
Throttle t = throttleList.get(addressList.indexOf(addr));
for (int i = 0; i < fList.size(); i++) {
try {
java.lang.reflect.Method setter = t.getClass()
.getMethod("setF" + i, boolean.class);
setter.invoke(t, fList.get(i));
} catch (java.lang.NoSuchMethodException
| java.lang.IllegalAccessException
| java.lang.reflect.InvocationTargetException ex1) {
log.error("", ex1);
try {
sendErrorStatus();
} catch (IOException ioe) {
log.error("Error writing to network port");
}
}
}
}
}
}
// implementation of ThrottleListener
@Override
public void notifyThrottleFound(DccThrottle t) {
log.debug("notified throttle found");
throttleList.add(t);
try {
sendThrottleFound(t.getLocoAddress());
t.addPropertyChangeListener(new SRCPThrottlePropertyChangeListener(this, t, busList.get(addressList.indexOf(t.getLocoAddress()))));
} catch (java.io.IOException ioe) {
//Something failed writing data to the port.
}
}
static class SRCPThrottlePropertyChangeListener implements PropertyChangeListener {
int bus;
int address;
JmriSRCPThrottleServer clientServer = null;
SRCPThrottlePropertyChangeListener(JmriSRCPThrottleServer ts, Throttle t,
int bus) {
log.debug("property change listener created");
clientServer = ts;
this.bus = bus;
address = t.getLocoAddress().getNumber();
}
// update the state of this throttle if any of the properties change
@Override
public void propertyChange(java.beans.PropertyChangeEvent e) {
if (log.isDebugEnabled()) {
log.debug("Property change event received " + e.getPropertyName() + " / " + e.getNewValue());
}
switch (e.getPropertyName()) {
case "SpeedSetting":
case "SpeedSteps":
case "IsForward":
try {
clientServer.sendStatus(bus, address);
} catch (IOException ioe) {
log.error("Error writing to network port");
}
break;
default:
for (int i = 0; i <= 28; i++) {
if (e.getPropertyName().equals("F" + i)) {
try {
clientServer.sendStatus(bus, address);
} catch (IOException ioe) {
log.error("Error writing to network port");
}
break; // stop the loop, only one function property
// will be matched.
} else if (e.getPropertyName().equals("F" + i + "Momentary")) {
try {
clientServer.sendStatus(bus, address);
} catch (IOException ioe) {
log.error("Error writing to network port");
}
break; // stop the loop, only one function property
// will be matched.
}
}
break;
}
}
}
}