Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | |||
programmatic_sample_code [2024/10/14 14:30] wikiadmin |
programmatic_sample_code [2024/10/15 10:28] (current) wikiadmin |
||
---|---|---|---|
Line 117: | Line 117: | ||
} | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | The OPC DA standard is optimized for a subscription model. Here's an example of reading multiple servers. | ||
+ | |||
+ | <code java> | ||
+ | import com.ergotech.util.SimulationManager; | ||
+ | import com.ergotech.util.TargetLicenseManager; | ||
+ | import com.ergotech.vib.exceptions.BadParameterException; | ||
+ | import com.ergotech.vib.exceptions.VIBUpdateFailedException; | ||
+ | import com.ergotech.vib.servers.SimpleDataSource; | ||
+ | import com.ergotech.vib.servers.opc.opcclient.OPC; | ||
+ | import com.ergotech.vib.utils.DataSourceContainer; | ||
+ | import com.ergotech.vib.valueobjects.ValueChangedEvent; | ||
+ | import com.ergotech.vib.valueobjects.ValueObjectInterface; | ||
+ | |||
+ | import java.io.InputStream; | ||
+ | import java.nio.file.Files; | ||
+ | import java.nio.file.Path; | ||
+ | import java.util.HashMap; | ||
+ | |||
+ | public class OPCDemo { | ||
+ | |||
+ | // HashMap to store the latest values for each item | ||
+ | private HashMap< | ||
+ | |||
+ | public static void main(String[] args) throws InterruptedException { | ||
+ | OPCDemo opcDemo = new OPCDemo(); | ||
+ | |||
+ | // Configure the license, required for OPC component to function. | ||
+ | opcDemo.setupLicense(); | ||
+ | |||
+ | // Disable simulation mode to connect to the actual OPC server. | ||
+ | SimulationManager.getSimulationManager().setSimulating(false); | ||
+ | |||
+ | // Initialize and start the OPC setup. | ||
+ | opcDemo.setupOpc(); | ||
+ | |||
+ | // Keep the application running for 5 minutes to receive updates from the OPC server. | ||
+ | Thread.sleep(1000 * 60 * 5); | ||
+ | } | ||
+ | |||
+ | public void setupLicense() { | ||
+ | try (InputStream license = Files.newInputStream(Path.of(" | ||
+ | // Initialize the license manager with the appropriate target. | ||
+ | final TargetLicenseManager manager = new TargetLicenseManager(" | ||
+ | license, | ||
+ | OPCDemo.class.getClassLoader()); | ||
+ | |||
+ | // Set the license manager for components. | ||
+ | SimpleDataSource.setClassLicenseManager(manager); | ||
+ | } catch (Exception e) { | ||
+ | // Handle any failure during license setup. | ||
+ | throw new RuntimeException(" | ||
+ | } | ||
+ | } | ||
+ | |||
+ | public void setupOpc() { | ||
+ | DataSourceContainer container; | ||
+ | |||
+ | try { | ||
+ | // Create a new OPC container to hold the OPC components. | ||
+ | container = DataSourceContainer.getRootContainer().createNewChildContainer(" | ||
+ | } catch (BadParameterException e) { | ||
+ | // Occurs if a container with the same name already exists. | ||
+ | throw new RuntimeException(" | ||
+ | } | ||
+ | |||
+ | // List of items to monitor | ||
+ | String[] items = { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | }; | ||
+ | |||
+ | // Create a single listener instance | ||
+ | ValueChangedCallback listener = new ValueChangedCallback(); | ||
+ | |||
+ | for (String item : items) { | ||
+ | OPC opc = new OPC(); | ||
+ | |||
+ | try { | ||
+ | // Assign a unique, case-insensitive name to the OPC component. | ||
+ | opc.setName(" | ||
+ | |||
+ | // Assign the host and item to connect to in the OPC server. | ||
+ | opc.setHost(" | ||
+ | opc.setItem(item); | ||
+ | } catch (BadParameterException e) { | ||
+ | // Handle invalid configuration for the OPC component. | ||
+ | throw new RuntimeException(" | ||
+ | } | ||
+ | |||
+ | try { | ||
+ | // Add the configured OPC component to the container. | ||
+ | container.addComponent(opc, | ||
+ | } catch (BadParameterException e) { | ||
+ | // Occurs if a component with the same name is already added to the container. | ||
+ | throw new RuntimeException(" | ||
+ | } | ||
+ | |||
+ | // Register the same listener to handle value change events from the OPC server. | ||
+ | opc.addValueChangedListener(listener); | ||
+ | |||
+ | try { | ||
+ | // Initialize and start the OPC component. | ||
+ | opc.init(); | ||
+ | opc.start(); | ||
+ | } catch (BadParameterException | VIBUpdateFailedException e) { | ||
+ | // Handle any failure during initialization or startup. | ||
+ | throw new RuntimeException(" | ||
+ | } | ||
+ | |||
+ | // Force the server to register for value updates even though there are no listeners | ||
+ | opc.setAutoSuspend(SimpleDataSource.AUTOSUSPEND_NEVER); | ||
+ | } | ||
+ | |||
+ | // Access current values from the HashMap | ||
+ | for (String item : items) { | ||
+ | ValueObjectInterface valueObject = itemValueMap.get(item); | ||
+ | if (valueObject != null) { | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | } else { | ||
+ | System.out.println(" | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | // Callback to process value changes from the OPC component. | ||
+ | public class ValueChangedCallback { | ||
+ | |||
+ | // Any object with a method " | ||
+ | public void valueInput(ValueChangedEvent valueChangedEvent) { | ||
+ | // Logic to handle changes in OPC data, triggered whenever the OPC value changes. | ||
+ | ValueObjectInterface valueObject = valueChangedEvent.getValueObject(); | ||
+ | OPC opc = (OPC) valueChangedEvent.getSource(); | ||
+ | String itemName = opc.getItem(); | ||
+ | |||
+ | // Update the HashMap with the new value | ||
+ | itemValueMap.put(itemName, | ||
+ | |||
+ | System.out.println(" | ||
+ | } | ||
+ | } | ||
} | } | ||
</ | </ |