This is an old revision of the document!
Host Connection Status in MIStudio/TransSECS Applications
After building a GEM Host in TransSECS you can register to receive the current status of the connection:
host.addConnectionStatusListener(new ConnectionStatusListener() { @Override public void connectionStatusChanged(int connectionStatus, String comment) { System.out.println ("Current Connection Status " + connectionStatus + " Status String \"" + comment + "\""); } });
For this code snippet to work correctly, host must not be null. This can happen if this code is run in an initialization process, such as in a StartupTrigger script, because the StartupTrigger code could be (and probably will be) run before the normal TransSECS/MIStudio startup sequence which starts the host controller. See the Thread code example below Threaded Example to solve this issue.
TransSECS GEM Host Connection States
The GEM Host can have one of these connection states depending on the status of the connection to the tool:
/** Possible connection states */ public static enum ConnectionStates { ENABLE_ALL_EVENTS_FAILED(-80), ENABLE_EVENTS_FAILED(-70), LINK_REPORTS_FAILED(-60), CREATE_REPORTS_FAILED(-50), ENABLE_ALARMS_FAILED(-40), ONLINE_FAILED(-30), COMMUNICATION_FAILED(-20), CONNECT_FAILED(-10), DISCONNECTED(-1), OPERATIONAL(0), // connection complete and ready CONNECTED(10); // connection to tool. Configuration starting
For normal operation, the status must be OPERATIONAL - 0 Any other state is an either an error (negative numbers) or transient (CONNECTED). The description of the states, with corrective action (if any) are:
- CONNECTED: The host has successfully connected to the tool and is beginning configuration of the tool.
- OPERATIONAL - This is the required status for normal operation
- DISCONNECTED - Indicates that the host has been disconnected from the tool. You need take no action on this status. The host will automatically attempt to reconnect so ideally the next status should be CONNECTED
- CONNECT_FAILED - The host has attempted to connect to the tool but the connection was not accepted. This typically means that the tool is not listening for a connection or there is a networking error. Check that the port definitions in the host and tool match and that you can “ping” the tool from from the host computer.
- COMMUNICATION_FAILED - The host successfully made a connection to the tool but the tool did not respond to the S1F13 message sent by the host.
- ONLINE_FAILED - the host has a successful connection to the tool, but the tool rejected a request to go online. The comment may provide more information. This is typically because the online/offline switch at the tool is set to offline. Corrective action is to switch the tool from offline to online
- ENABLE_ALARMS_FAILED - This indicates that an attempt to enable alarms failed. If you receive this status the tool may still go to “OPERATIONAL” but you will not receive any alarm notifications (S5F1 messages). You should correct the error and restart the host.
- CREATE_REPORTS_FAILED - The tool rejected the host request to create event reports. This is usually because one of the reports contains a VID that is not known to the tool. The host will not go to OPERATIONAL. You must correct the error and restart the host.
- LINK_REPORTS_FAILED - The tool rejected the host request to link event reports. This is usually because one of the configured CEIDs is not known to the tool. The host will not go to OPERATIONAL. You must correct the error and restart the host.
- ENABLE_EVENTS_FAILED - The tool rejected the host request to enable event reports. This is usually because one of the configured CEIDs is not known to the tool. The host will not go to OPERATIONAL. You must correct the error and restart the host.
JavaScript Example for MIStudio/TransSECS
This code example is run from a StartupTrigger script in TransSECS (or a script triggered by the StartupTrigger in MIStudio) to set up the ConnetionStatusListener. For MIStudio, this script pushes data to two BroadcastServers (StatusListenerCommand and StatusListenerValue) whenever there is a connection status change.
//sets up a listener to handle connection status changes var TransSecsController = Java.type("com.ergotech.transsecs.secs.TransSecsController"); var ConnectionStatusListener = Java.type("com.ergotech.transsecs.secs.host.ConnectionStatusListener"); var statusListener = new ConnectionStatusListener() { connectionStatusChanged : function(connectionStatus,comment) { print ("Current Connection Status " + connectionStatus + " Status String \"" + comment + "\""); StatusListenerComment->setStringValue(comment); StatusListenerValue->setIntValue(connectionStatus); } } host=TransSecsController.findController("GEMHost"); //use your GEM Host name host.addConnectionStatusListener(statusListener);
Threaded Example To Wait for Host Initialization
In the code below, which is run from a StartupTrigger script in TransSECS (or a script triggered by the StartupTrigger in MIStudio), a Thread is started which waits for the host not to be null and then sets up the ConnectionStatusListener.
//sets up a listener to handle connection status changes var TransSecsController = Java.type("com.ergotech.transsecs.secs.TransSecsController"); var ConnectionStatusListener = Java.type("com.ergotech.transsecs.secs.host.ConnectionStatusListener"); var Thread = Java.type("java.lang.Thread"); function waitForHost() { print("wait for host THREAD started"); host=TransSecsController.findController("GEMHost"); //use your host name here while (host==null) { //print("host is still null"); Thread.sleep(50); //wait 50 ms host=TransSecsController.findController("GEMHost"); } host=TransSecsController.findController("GEMHost"); // print("host is not null, setting up connection status listener"); var connectionStatusListener = new ConnectionStatusListener() { connectionStatusChanged : function (connectionStatus, comment) { updateToolStatus(connectionStatus,comment); //below is for MIStudio projects, two BroadcastServers are written to with the connection status values StatusListenerComment->setStringValue(comment); StatusListenerValue->setIntValue(connectionStatus); print ("Current Connection Status " + connectionStatus + " Status String \"" + comment + "\""); } }; // add this listener to the host host.addConnectionStatusListener(connectionStatusListener); print("Connection Status Trigger Registered"); }//end of function to wait for host /// START OF MAIN JAVASCRIPT CODE /// print("Startup Trigger"); var r = new java.lang.Runnable() { run: function() { waitForHost(); } } new Thread(r).start();