package art.servers.etdserver.controller; import art.library.interop.InteropParameter; import art.library.interop.InteropParameters; import art.library.interop.InteropResponse; import art.library.model.devices.DeviceStatus; import art.library.model.devices.etd.Etd; import art.library.interop.serialization.Serialization; import art.library.model.devices.Device; import art.library.model.transactions.traces.Trace; import art.servers.etdserver.Shared; import art.servers.etdserver.protocols.dgt.EtdDgtProtocolAnalyser; import static java.lang.Thread.sleep; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class ControllerEtdDgtEru extends ControllerEtd { private int status = DeviceStatus.STATUS_ALARM; private boolean firstTime = true; private final EtdDgtProtocolAnalyser protocolAnalyser = new EtdDgtProtocolAnalyser(); public ControllerEtdDgtEru(Etd etd) { super(etd); this.device = etd; this.name = Shared.getMessage("Controller etd") + " " + etd.information.name; } public void run() { String message = Shared.getMessage("Running"); // art.servers.Shared.println(Trace.getTraceInformation(art.servers.Shared.getApplicationName(), name, art.servers.Shared.getMessage("Starting"), art.servers.Shared.getMessage("Success")), true); // status = DeviceStatus.STATUS_ONLINE; while ((isInterrupted() == false) && (exit == false)) { long startTimestamp = System.currentTimeMillis(); try { if (art.servers.Shared.isServerEnabled() == true) { update(); } } catch (Exception e) { e.printStackTrace(); } if (startTimestamp > System.currentTimeMillis()) { startTimestamp = System.currentTimeMillis(); } long stopTimestamp = startTimestamp + (device.getDeviceInformation().polling * 1000); while ((System.currentTimeMillis() < stopTimestamp) && (exit == false)) { try { sleep(50); } catch (Exception e) { } } if (art.servers.Shared.model.existsDevice(device.getIdentifier()) == false) { art.servers.Shared.println(name, art.servers.Shared.getMessage("Device no longer exists")); exit = true; } } // art.servers.Shared.println(Trace.getTraceInformation(art.servers.Shared.getApplicationName(), name, art.servers.Shared.getMessage("Finishing"), art.servers.Shared.getMessage("Success")), true); // } /* Private controller functions */ private void update() throws Exception { Etd deviceclone = (Etd)Serialization.clone(device); try { readAlarms(deviceclone); if (deviceclone.getAlarm("alarm_offline") > 0) { offline(deviceclone); return; } readCurrentTrafficData(deviceclone); online(deviceclone); } catch (Exception exception) { exception.printStackTrace(); offline(deviceclone); } } private void readAlarms(Etd deviceclone) throws Exception { try { System.out.println(Device.getDate(System.currentTimeMillis()) + " - 1. " + deviceclone.getIdentifier() + " - EYA >> "); InteropParameters parameters = new InteropParameters(); parameters.addParameter(new InteropParameter("operation", "getEtdEya")); parameters.addParameter(new InteropParameter("language", "en-GB")); parameters.addParameter(new InteropParameter("device", deviceclone.getDeviceInformation().connectionEru.eru)); parameters.addParameter(new InteropParameter("etd", "" + deviceclone.getDeviceInformation().connectionEru.etdNumber)); InteropResponse response = (InteropResponse) Serialization.invoke("get", parameters, deviceclone.getDeviceInformation().connectionEru.address, deviceclone.getDeviceInformation().connectionEru.port, deviceclone.getDeviceInformation().connectionEru.timeout); System.out.println(Device.getDate(System.currentTimeMillis()) + " - 2. " + deviceclone.getIdentifier() + " - EYA >> "); Object[] lobject = Arrays.copyOf(response.getValue(), response.getValue().length, Object[].class); List lbytes = new ArrayList(); for (Object o : lobject){lbytes.add((Integer)o);} String s = deviceclone.getIdentifier() + " - EYA << "; for (Integer value : lbytes) { s += " " + String.format("%02X", value.intValue()); } System.out.println(s); int numberOfEtds = lbytes.get(0).intValue(); int etdNumber = lbytes.get(1).intValue(); if (etdNumber == deviceclone.getDeviceInformation().connectionEru.etdNumber) { // Message received corresponds to vms int[] information = new int[lbytes.size() - 2]; for (int i=2; i lbytes = new ArrayList(); for (Object o : lobject){lbytes.add((Integer)o);} String s = deviceclone.getIdentifier() + " - DUP << "; for (Integer value : lbytes) { s += " " + String.format("%02X", value.intValue()); } System.out.println(s); int numberOfEtds = lbytes.get(0).intValue(); int etdNumber = lbytes.get(1).intValue(); System.out.println(deviceclone.getIdentifier() + " 1.DUP: " + numberOfEtds + "." + etdNumber + " - " + deviceclone.getDeviceInformation().connectionEru.etdNumber); if (etdNumber == deviceclone.getDeviceInformation().connectionEru.etdNumber) { // Message received corresponds to vms int[] information = new int[lbytes.size() - 2]; for (int i=2; i 0) || (firstTime == true)) { firstTime = false; String message = art.servers.Shared.getMessage("Etd online"); art.servers.Shared.println(name, message); } etdclone.setAlarm("alarm_offline", false); etdclone.setAlarm("alarm_invalid", false); Shared.model.updateDevice(device, etdclone); } }