Created
November 6, 2014 12:05
-
-
Save CptMauli/1964e6e9f14da8f12584 to your computer and use it in GitHub Desktop.
Beispiel, dispose fährt alles suaber runter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package maven.test; | |
import java.util.Observable; | |
import java.util.Observer; | |
import org.eclipse.scada.core.ConnectionInformation; | |
import org.eclipse.scada.core.client.AutoReconnectController; | |
import org.eclipse.scada.core.client.ConnectionFactory; | |
import org.eclipse.scada.core.client.ConnectionState; | |
import org.eclipse.scada.core.client.ConnectionStateListener; | |
import org.eclipse.scada.da.client.Connection; | |
import org.eclipse.scada.da.client.DataItem; | |
import org.eclipse.scada.da.client.DataItemValue; | |
import org.eclipse.scada.da.client.ItemManagerImpl; | |
public class Application { | |
public static void main(String[] args) { | |
new Thread(new Runnable() { | |
@Override | |
public void run() { | |
try { | |
Thread.sleep(6000); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
}).start(); | |
try { | |
Class.forName("org.eclipse.scada.da.client.ngp.ConnectionImpl"); | |
} catch (ClassNotFoundException e) { | |
System.err.println(e.getMessage()); | |
System.exit(1); | |
} | |
final String uri = "da:ngp://admin:admin12@scada.eclipse.org:2101"; | |
final ConnectionInformation ci = ConnectionInformation.fromURI(uri); | |
final Connection connection = (Connection) ConnectionFactory.create(ci); | |
if (connection == null) { | |
System.err | |
.println("Unable to find a connection driver for specified URI"); | |
System.exit(1); | |
} | |
// just print the current connection state | |
connection.addConnectionStateListener(new ConnectionStateListener() { | |
@Override | |
public void stateChange( | |
org.eclipse.scada.core.client.Connection connection, | |
ConnectionState state, Throwable error) { | |
System.out.println("Connection state is now: " + state); | |
} | |
}); | |
// although it is possible to use the plain connection, the | |
// AutoReconnectController automatically connects to the server | |
// again if the connection is lost | |
final AutoReconnectController controller = new AutoReconnectController( | |
connection); | |
controller.connect(); | |
// although it is possible to subscribe to an item directly, | |
// the recommended way is to use the ItemManager, which handles the | |
// subscriptions automatically | |
final ItemManagerImpl itemManager = new ItemManagerImpl(connection); | |
final DataItem dataItem = new DataItem("ES.DEMO.ARDUINO001.LIGHT.V", itemManager); | |
dataItem.addObserver(new Observer() { | |
@Override | |
public void update(final Observable observable, final Object update) { | |
final DataItemValue div = (DataItemValue) update; | |
System.out.println(div); | |
} | |
}); | |
try { | |
Thread.sleep(3000); | |
} catch (InterruptedException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
// dataItem.deleteObservers(); | |
controller.dispose(); | |
connection.dispose(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment