-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoreSlaveComponent.java
More file actions
231 lines (199 loc) · 5.86 KB
/
CoreSlaveComponent.java
File metadata and controls
231 lines (199 loc) · 5.86 KB
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
package discoverylab.telebot.slave.core;
import java.lang.reflect.InvocationTargetException;
import TelebotDDSCore.DDSCommunicator;
import com.rti.dds.infrastructure.InstanceHandle_t;
import com.rti.dds.infrastructure.StatusKind;
import com.rti.dds.subscription.DataReaderImpl;
import com.rti.dds.subscription.Subscriber;
import com.rti.dds.topic.Topic;
import discoverylab.telebot.slave.core.configurations.Config;
import discoverylab.telebot.slave.core.readers.CoreDataReaderAdapter;
import jssc.SerialPort;
import jssc.SerialPortException;
import static discoverylab.util.logging.LogUtils.*;
/**
*
* @author Irvin Steve Cardenas
*
*/
public abstract class CoreSlaveComponent {
public static String TAG = makeLogTag("CoreComponent");
// Is it a Slave Component. This is set in the constructor
private Boolean isSerialComponent = true;
// Serial
private SerialPort serialPort;
protected Boolean serialConnected = false;
private String serialPortName;
private Integer baudRate;
private Integer dataBits;
private Integer stopBits;
private Integer parityType;
private Integer eventMask;
// DDS
private DDSCommunicator communicator;
private static Topic topic = null;
private static DataReaderImpl reader = null;
private static CoreDataReaderAdapter listener = null;
// Object instance = new TMasterToHands();
InstanceHandle_t instance_handle = InstanceHandle_t.HANDLE_NIL;
/**
* Default Core Slave Component for non-serial components
*/
public CoreSlaveComponent(){
this.isSerialComponent = false;
}
/**
* Default Constructor Uses Default Values For Serial Connection
* @param serialPort
*/
public CoreSlaveComponent(SerialPort serialPort){
this.serialPort = serialPort;
LOGW(TAG, "Setting --Default-- Serial Configurations");
this.baudRate = Config.SERIAL_BAUD_RATE;
this.dataBits = Config.SERIAL_DATA_BITS;
this.stopBits = Config.SERIAL_STOP_BITS;
this.parityType = Config.SERIAL_PARITY_TYPE;
this.eventMask = Config.SERIAL_EVENT_MASK;
}
/**
*
* @param serialPortName
* @param baudRate
* @param dataBits
* @param stopBits
* @param parityType
* @param eventMask
*/
public CoreSlaveComponent(String serialPortName, int baudRate, int dataBits, int stopBits, int parityType, int eventMask){
this.serialPortName = serialPortName;
this.baudRate = baudRate;
this.dataBits = dataBits;
this.stopBits = stopBits;
this.parityType = parityType;
this.eventMask = eventMask;
serialPort = new SerialPort(serialPortName);
}
/**
* Slave Hands Initiate - Open Hand Serial Connection
* @return
*/
@SuppressWarnings("finally")
public boolean initiate(){
try {
serialPort.openPort();
if(!checkSerialParams()){
setSerialDefaultParams();
}
else {
serialPort.setParams(baudRate
, dataBits
, stopBits
, parityType);
serialPort.setEventsMask(eventMask);
}
} catch (SerialPortException e) {
LOGE(TAG, "Error opening SerialPort: " + serialPortName + " with Baudrate: " + baudRate);
e.printStackTrace();
}
finally{
return serialPort.isOpened();
}
}
/**
* Check if the user has defined Serial parameters
* @return
*/
private boolean checkSerialParams(){
return(serialPortName != null &&
baudRate != null &&
dataBits != null &&
stopBits != null &&
parityType != null &&
eventMask != null);
}
/**
* Utility method to set the default configuration parameters for a Serial object.
* This method is called if the user does not pass the Serial parameters.
*/
private void setSerialDefaultParams(){
LOGW(TAG, "Setting Serial --Default-- Paramaters");
try {
serialPort.setParams(Config.SERIAL_BAUD_RATE
, Config.SERIAL_DATA_BITS
, Config.SERIAL_STOP_BITS
, Config.SERIAL_PARITY_TYPE);
serialPort.setEventsMask(Config.SERIAL_EVENT_MASK);
} catch (SerialPortException e) {
LOGE(TAG, "Error setting Serial parameters: " + serialPortName + " with Baudrate: " + baudRate);
e.printStackTrace();
}
}
/**
* Perform Slave Component Calibration
* @return
*/
public abstract boolean calibrate();
/**
* Initiate DDS Protocol
* @return
*/
public boolean initiateTransmissionProtocol(String topicName, Class type, CoreDataReaderAdapter listener){
setListener(listener);
if(isSerialComponent){
getListener().setSerialPort(this.serialPort);
}
communicator = new DDSCommunicator();
try {
communicator.createParticipant();
} catch (Exception e) {
LOGE(TAG, "Error Creating Participant");
e.printStackTrace();
}
try {
communicator.createPublisher();
} catch (Exception e) {
LOGE(TAG, "Error Creating Publisher");
e.printStackTrace();
}
try {
communicator.createSubscriber();
} catch (Exception e) {
LOGE(TAG, "Error Creating Subscriber");
e.printStackTrace();
}
try {
this.topic = communicator.createTopic(topicName, type);
} catch (ClassNotFoundException | NoSuchMethodException
| SecurityException | IllegalAccessException
| IllegalArgumentException | InvocationTargetException
| InstantiationException e) {
LOGE(TAG, "Error Creating Topic");
e.printStackTrace();
}
try {
setReader((DataReaderImpl) communicator.getSubscriber()
.create_datareader(
topic,
Subscriber.DATAREADER_QOS_USE_TOPIC_QOS,
getListener(),
StatusKind.STATUS_MASK_ALL));
} catch (Exception e) {
LOGE(TAG, "Error Creating Reader");
e.printStackTrace();
}
//TODO Get assertion of Participant
return false;
}
private static CoreDataReaderAdapter getListener() {
return listener;
}
private static void setListener(CoreDataReaderAdapter listener) {
CoreSlaveComponent.listener = listener;
}
private static DataReaderImpl getReader() {
return reader;
}
private static void setReader(DataReaderImpl reader) {
CoreSlaveComponent.reader = reader;
}
}