I am using a terminal emulator library to create a terminal and then I use it to send the data entered over serial to a serial device. Not that I have that working I want to create some other graphical interface, maybe just a textBox, on screen which shows a briefer version of the results for the user alongside the full version in the terminal. They can just glance at this for a quicker view of results. I'm wondering how to parse it best. When I send a command a byte array is sent back to me containing data like this as an example of one set of results:
Interface IP-Address Status Protocol
vlan1 192.168.1.1 up up
Fa0/1i unassigned administratively down down
Fa0/1o unassigned down down
Fa1/0 unassigned down down
Fa1/1 unassigned down down
Fa1/2 unassigned down down
Fa1/3 unassigned down down
Fa1/4 unassigned down down
Fa1/5 unassigned down down
Fa1/6 unassigned down down
Fa1/7 unassigned down down
Fa1/8 unassigned up up
Fa1/9 unassigned down down
Fa1/10 unassigned up up
Fa1/11 unassigned down down
Gi0 unassigned up up
switch#
How would I parse this? I would like something like "The interface vlan1 is up with an IP of 192.1.1.168" or "the interface Fa1/8 is up with an unassigned IP address." to appear for the user, just listing the most pertinent data. Would I just say something like:
if (dataReceived.charAt(i) == 'v'
&& dataReceived.charAt(i + 1) == 'l'
&& dataReceived.charAt(i + 2) == 'a'
&& dataReceived.charAt(i + 3) == 'n')
&& dataReceived.charAt(i + 4) == '1')
&& etc //check i is in bounds, check is the next non-whitespace character a number or a u somehow and so on
{
//do things here
//print out "The interface vlan1 is up with an IP of 192.1.1.168"
}
This way seems quite tedious, checking every byte, seeing what the next bytes are and so on. Is this the best/a correct way however?
EDIT: here is a sample I have at the moment, where I am receiving data, and where I am testing it for certain data:
public void onDataReceived(int id, byte[] data) {
dataReceived = new String(data);
try {
dataReceivedByte = dataReceived.getBytes("ASCII");
} catch (UnsupportedEncodingException e) {
Log.d(TAG, "exception");
e.printStackTrace();
}
statusBool = true;
Log.d(TAG, "in data received " + dataReceived);
((MyBAIsWrapper) bis).renew(data);
runOnUiThread(new Runnable(){
@Override
public void run() {
}});
viewHandler.post(updateView);
}
Handler viewHandler = new Handler();
Runnable updateView = new Runnable() {
@Override
public void run() {
mEmulatorView.invalidate();
if (statusBool == true) {
for (int i = 1; i < dataReceived.length() - 1; i++) {
if (dataReceived.charAt(i) == '>') {
Log.d(TAG, "found >");
deviceStatus = 0;
}
if (dataReceived.charAt(i) == '#'
&& dataReceived.charAt(i - 1) != ')') {
Log.d(TAG, "found #");
deviceStatus = 1;
}
if ((i + 1) <= (dataReceived.length())
&& dataReceived.charAt(i) == ')'
&& dataReceived.charAt(i + 1) == '#') {
Log.d(TAG, "found config )#");
deviceStatus = 2;
}
}
statusBool = false;
viewHandler.postDelayed(updateView, 1000);
}
}
};
bytesorchars?