Skip to content

Receiving Data

vstarlinger edited this page Aug 29, 2017 · 2 revisions

Data in the ground station can be received through multiple input methods. The internal communication between the receivers and the other components of the ground station is decriped in the Internal Communication section.

Mail

Mails are received in the MailReceiver class which uses the javax.mail library to a predefined mail account. Currently the server, username and password need be specified in code. This is however subject to the next changes. Example code is provided below.

String host = "imap.gmail.com";
final String user = USER_NAME;
final String password = PW;

Once the connection is established, a new thread is started that reqularly checks for new e-mails (every 500ms).

When a new message is registered, the void getMessage(Folder folder, int i) method is called. There it is checked whether the message is valid or not. Currently this is done by checking whether the mail subject contains the String "message" and if the message contains an attachment.

if (message[i].getSubject().toLowerCase().contains("message")) {
    if (message[i].getContent() instanceof Multipart) {

If this is the case, then (only) the first attachment is read via an input stream and stored in a String. All listeners get updated via the update() method.

	InputStream stream = part.getInputStream();
	int b;
	String text = "";
	while ((b = stream.read()) != -1) {
		String t = Utility.intToBits(b);
		text += t;
	}
	text = text.trim();
	update(new MailEvent(message[i].getFrom(), message[i].getSubject(),
	part.getFileName(), text, System.currentTimeMillis(),
	message[i].getReceivedDate().getTime(), DataSource.MAIL));

Serial

Serial communication is done via the SerialComm class which uses the jssc library. To start the serial connection, the void start(String sp, int baudrate) method must be called. Since the SerialComm class uses the Singleton Pattern, this can be done by calling SerialComm.getInstance().start("Port",38000);.

Once this is done, the SerialComm class listens to the open port using a SerialPortEventListener and its void serialEvent(SerialPortEvent event) method. If data is available, the bytes are read from the port until a newLine character is recorded. On this trigger the buffer is pushed to the listeners via the messageReceived() method.

Following is the implementation of this serialEvent method:

    @Override
    public void serialEvent(SerialPortEvent event) {
        if (event.isRXCHAR()) {//If data is available
            try {
                for (byte c : serialPort.readBytes(event.getEventValue())) {
                    //every time a newLine char is read, send the previous chars 
                    //to all listeners
                    if (c == '\n') {
                        messageReceived(new SerialEvent(buffer, event.getPortName(),
                                 System.currentTimeMillis(), DataSource.SERIAL));
                        System.out.println(buffer);
                        buffer = "";
                    } else {
                        buffer += (char)c;
                    }
                }
            } catch (SerialPortException ex) {
                error("Error: Couldn't read incoming Bytes.");
            }
        }
    }

File

Files are read using the internal FileChooser and JFileChooser classes. Multiple files can be selected. Once the user approves their file selection, all files are read using a FileInputStream and added to the MessageController.

Clone this wiki locally