Menu

Java Protocol tutorial sources

Thierry CHOMAUD

Main.java source

~~~~~~~~~~~~
/**

  • Main Class of Waveport protocol example
  • This example will demonstrate sequentially how to
    • Read the radio address of connected radio modem
    • Send an application request to a distant device
    • Send an application message to a distant device (with 1 relay)
      */
      import java.text.SimpleDateFormat;
      import java.util.Date;

import com.coronis.sdk.domain.Payload;
import com.coronis.sdk.domain.RadioAddress;
import com.coronis.sdk.protocol.api.CommandResponse;
import com.coronis.sdk.protocol.api.DynamicStandardParams;
import com.coronis.sdk.protocol.api.IPayload;
import com.coronis.sdk.protocol.api.IProtocolService;
import com.coronis.sdk.protocol.api.IRadioAddress;
import com.coronis.sdk.protocol.api.LocalNodeServiceCommand;
import com.coronis.sdk.protocol.api.LocalNodeServiceResponse;
import com.coronis.sdk.protocol.api.NodeServiceId;
import com.coronis.sdk.protocol.api.ProtocolException;
import com.coronis.sdk.protocol.api.RadioExchangeCommand;
import com.coronis.sdk.protocol.api.RequestResponse;
import com.coronis.sdk.protocol.api.ResponseStatus;
import com.coronis.sdk.protocol.waveport.Waveport;
import com.coronis.sdk.serialdriver.api.SerialDriverException;
import com.coronis.sdk.serialdriver.rs232driver.RS232Driver;

public class Main {

// port ID to use
private static String defaultPortId = "COM4";
// radio address of the radio device to request (use the coronis radio address implementation (domain))
private static IRadioAddress destinationAddress = new RadioAddress("11604A329237");
private static IRadioAddress relayAddress = new RadioAddress("11604A32924C");
// payload to send declaration (use the coronis payload implementation (domain))
private static IPayload payload = new Payload("20");
/**

 * Main method
 * @param args
 */
public static void main(String[] args) {
    // SerialDriver instantiation
    RS232Driver serialDriver = new RS232Driver();
    Waveport wp = new Waveport();
    // Protocol instantiation
    IProtocolService protocol = new Waveport();
    try {
        // set port Id to use
        serialDriver.setSerialPortId(defaultPortId);

        // Protocol initialization (use the define serialDriver)
        protocol.setSerialDriverInstance(serialDriver);
        protocol.init();

        // Open the Protocol connection (will open the linked serial driver)
        protocol.open();

        // Read of the radio modem radioAddress
        processLocalService(protocol);

        // Send a radio application request (wait for radio response)
        processSendRequest(protocol);

        // Send a radio application message (no wait for radio response)
        processSendMessage(protocol);

    } catch (SerialDriverException e) {
        System.out.println(toPrintableDate(new Date())+ "serial driver exception: "+e.toString());
    } catch (ProtocolException e) {
        System.out.println(toPrintableDate(new Date())+ "protocol exception: "+e.toString());
    } finally {
        try {
            // Close the Protocol connection (will close serialDriver)
            protocol.close();
        } catch (ProtocolException e1) {
            e1.printStackTrace();
        }
    }
}

/**

 * Read the radio address of connected radio modem
 * @param protocol which must be opened.
 */
private static void processLocalService(IProtocolService protocol) {
    System.out.println(toPrintableDate(new Date())+ "process local node service: start");
    // Prepare the local node service command to execute
    LocalNodeServiceCommand localService = new LocalNodeServiceCommand(
            NodeServiceId.READ_RADIO_ADDRESS);
    // Execute the local node service
    try {
        LocalNodeServiceResponse response = protocol.requestLocalNodeService(localService);
        System.out.println(toPrintableDate(new Date())+ "process local service status: "+ response.getStatus());
        if (response.getStatus()==ResponseStatus.OK_PROTOCOL_SUCCESS) {
            System.out.println(toPrintableDate(new Date())+ "process local node service response: "+ response.getCommand().toString());
        }
    } catch (ProtocolException e) {
        System.out.println(toPrintableDate(new Date())+ "process local node service exception: "+e.toString());
    }
}

/**

 * Send an application request to a distant device
 * @param protocol which must be opened.
 */
private static void processSendRequest(IProtocolService protocol) {
    System.out.println(toPrintableDate(new Date())+ "process send request: start");
    // Prepare the request to send (without relay)
    RadioExchangeCommand request = new RadioExchangeCommand(
            payload,
            new DynamicStandardParams(destinationAddress));
    // Execute the application message
    try {
        RequestResponse response = protocol.sendApplicationRequest(request);
        System.out.println(toPrintableDate(new Date())+ "process send request status: "+ response.getStatus());
        if (response.getStatus()==ResponseStatus.OK_PROTOCOL_SUCCESS) {
            System.out.println(toPrintableDate(new Date())+ "process send request response: "+ response.getCommand().toString());
        }
    } catch (ProtocolException e) {
        System.out.println(toPrintableDate(new Date())+ "process send request exception: "+e.toString());
    }
}

/**

 * Send an application message to a distant device (with 1 relay)
 * @param protocol which must be opened.
 */
private static void processSendMessage(IProtocolService protocol) {
    System.out.println(toPrintableDate(new Date())+ "process send message: start");
    // Prepare the message to send (throw 1 relay)
    RadioExchangeCommand message = new RadioExchangeCommand(
            payload,
            new DynamicStandardParams(destinationAddress, new IRadioAddress[]{relayAddress}));
    // Execute the application message
    try {
        CommandResponse response = protocol.sendApplicationMessage(message);
        System.out.println(toPrintableDate(new Date())+ "process send message status: "+ response.getStatus());
    } catch (ProtocolException e) {
        System.out.println(toPrintableDate(new Date())+ "process send message exception: "+e.toString());
    }
}

/**

 * Return a printable given date
 * @param date
 * @return
 */
public static String toPrintableDate(Date date) {
    SimpleDateFormat formatter = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss:SSS");
    return formatter.format(date).concat(" ");
}

}


Related

Wiki: Java_Protocol_UserGuide

MongoDB Logo MongoDB