package art.servers.asfserver.controller; import art.library.interop.InteropParameter; import art.library.interop.InteropParameters; import art.library.interop.InteropResponse; import art.library.interop.serialization.Serialization; import art.library.interop.serialization.SerializationException; import art.library.model.devices.DeviceAction; import art.library.model.devices.DeviceActionResult; import art.library.model.devices.DeviceCommands; import art.library.model.devices.DeviceStatus; import art.library.model.devices.vms.asf.Asf; import art.library.model.devices.vms.asf.AsfCommands; import art.library.model.devices.vms.asf.AsfStatus; import art.library.utils.synchro.Mutex; import art.servers.ServerException; import art.servers.Shared; import art.servers.AsfServer; import art.servers.asfserver.protocols.dgt.AsfDgtConstants; import art.servers.asfserver.protocols.dgt.AsfDgtProtocolAnalyser; import java.util.Arrays; import java.util.List; import art.servers.asfserver.protocols.dgt.AsfDgtProtocolConstructor; import java.util.ArrayList; public class ControllerAsfDGTEru extends ControllerAsf { private String name = null; private boolean firstTime = true; private final Mutex mutex = new Mutex(); public ControllerAsfDGTEru(Asf asf) { super(asf); this.device = asf; this.name = Shared.getMessage("Controller asf") + " " + asf.toString(); this.setName(name); if (device.getDeviceStatus() == null) { device.status = new AsfStatus(); } } public void run() { Shared.traceInformation(name, "Starting"); while ((isInterrupted() == false) && (exit == false)) { long startTimestamp = System.currentTimeMillis(); try { if (art.servers.Shared.isServerEnabled() == true) { update(); } } catch (Exception e) { } long timetowait = (device.getDeviceInformation().polling * 1000) - (System.currentTimeMillis() - startTimestamp); timetowait = Math.min(timetowait, (device.getDeviceInformation().polling * 1000)); if (timetowait > 0) { try { sleep(timetowait); } catch (Exception e) { } } else { try { sleep(50); } catch (Exception e) { }; } if (Shared.model.existsDevice(device.getIdentifier()) == false) { exit = true; } } Shared.traceInformation(name, "Finishing"); } // private void update() throws Exception { mutex.lockWrite(); Asf deviceclone = (Asf) Serialization.clone(device); deviceclone.getDeviceAlarms().clear(); try { readAlarms(deviceclone); if (deviceclone.getAlarm("alarm_offline") > 0) { offline(deviceclone); return; } readConfiguration(deviceclone); readStatus(deviceclone); online(deviceclone); } catch (Exception e) { offline(deviceclone); } finally { mutex.releaseWrite(); } } private void offline(Asf deviceclone) throws Exception { deviceclone.setAlarm("alarm_offline", System.currentTimeMillis()); Shared.model.updateDevice(device, deviceclone); } private void online(Asf deviceclone) throws Exception { if ((device.alarms.alarm_offline > 0) || (firstTime == true)) { firstTime = false; String message = art.servers.Shared.getMessage("Asf online"); Shared.println(this.name, message); } deviceclone.setAlarm("alarm_offline", false); deviceclone.setAlarm("alarm_invalid", false); Shared.model.updateDevice(device, deviceclone); } private void readAlarms(Asf deviceclone) throws Exception { try { InteropParameters parameters = new InteropParameters(); parameters.addParameter(new InteropParameter("operation", "getVmsEya")); parameters.addParameter(new InteropParameter("language", "en-GB")); parameters.addParameter(new InteropParameter("device", deviceclone.getDeviceInformation().connectionEru.eru)); parameters.addParameter(new InteropParameter("vms", "" + deviceclone.getDeviceInformation().connectionEru.vmsNumber)); Shared.println(name, "1.ReadAlarms: " + deviceclone.getDeviceInformation().connectionEru.eru + " " + deviceclone.getDeviceInformation().connectionEru.vmsNumber); InteropResponse response = (InteropResponse) Serialization.invoke("get", parameters, deviceclone.getDeviceInformation().connectionEru.address, deviceclone.getDeviceInformation().connectionEru.port, deviceclone.getDeviceInformation().connectionEru.timeout); Shared.println(name, "2.ReadAlarms: " + deviceclone.getDeviceInformation().connectionEru.eru + " " + deviceclone.getDeviceInformation().connectionEru.vmsNumber); Object[] lobject = Arrays.copyOf(response.getValue(), response.getValue().length, Object[].class); Shared.println(name, "3.ReadAlarms: " + deviceclone.getDeviceInformation().connectionEru.eru + " " + deviceclone.getDeviceInformation().connectionEru.vmsNumber + " - " + lobject.length); List lbytes = new ArrayList(); for (Object o : lobject) { lbytes.add((Integer) o); } int numberOfVmss = lbytes.get(0).intValue(); Shared.println(name, "4.ReadAlarms: " + deviceclone.getDeviceInformation().connectionEru.eru + " " + deviceclone.getDeviceInformation().connectionEru.vmsNumber + " - " + numberOfVmss); int vmsNumber = lbytes.get(1).intValue(); Shared.println(name, "5.ReadAlarms: " + deviceclone.getDeviceInformation().connectionEru.eru + " " + deviceclone.getDeviceInformation().connectionEru.vmsNumber + " - " + vmsNumber); if (vmsNumber == deviceclone.getDeviceInformation().connectionEru.vmsNumber) { // Message received corresponds to vms String s = " EYA << "; int[] information = new int[lbytes.size() - 2]; for (int i = 2; i < lbytes.size(); i++) { information[i - 2] = lbytes.get(i); s += " " + information[i-2]; } Shared.println(name, s); AsfDgtProtocolAnalyser.analyseResponseEyA(deviceclone, information); } } catch (Exception e) { Shared.printstack(name, e); throw e; } } private void readConfiguration(Asf deviceclone) throws Exception { try { InteropParameters parameters = new InteropParameters(); parameters.addParameter(new InteropParameter("operation", "getVmsPp")); parameters.addParameter(new InteropParameter("language", "en-GB")); parameters.addParameter(new InteropParameter("device", deviceclone.getDeviceInformation().connectionEru.eru)); parameters.addParameter(new InteropParameter("vms", "" + deviceclone.getDeviceInformation().connectionEru.vmsNumber)); InteropResponse response = (InteropResponse) Serialization.invoke("get", parameters, deviceclone.getDeviceInformation().connectionEru.address, deviceclone.getDeviceInformation().connectionEru.port, deviceclone.getDeviceInformation().connectionEru.timeout); Object[] lobject = Arrays.copyOf(response.getValue(), response.getValue().length, Object[].class); List lbytes = new ArrayList<>(); for (Object o : lobject) { lbytes.add((Integer) o); } int numberOfVmss = lbytes.get(0).intValue(); int vmsNumber = lbytes.get(1).intValue(); if (vmsNumber == deviceclone.getDeviceInformation().connectionEru.vmsNumber) { // Message received corresponds to vms int[] information = new int[lbytes.size() - 2]; for (int i = 2; i < lbytes.size(); i++) { information[i - 2] = lbytes.get(i); } AsfDgtProtocolAnalyser.analyseResponsePP(deviceclone, information); } } catch (Exception e) { throw e; } } private void readStatus(Asf deviceclone) throws Exception { try { InteropParameters parameters = new InteropParameters(); parameters.addParameter(new InteropParameter("operation", "getVmsEp")); parameters.addParameter(new InteropParameter("language", "en-GB")); parameters.addParameter(new InteropParameter("device", deviceclone.getDeviceInformation().connectionEru.eru)); parameters.addParameter(new InteropParameter("vms", "" + deviceclone.getDeviceInformation().connectionEru.vmsNumber)); InteropResponse response = (InteropResponse) Serialization.invoke("get", parameters, deviceclone.getDeviceInformation().connectionEru.address, deviceclone.getDeviceInformation().connectionEru.port, deviceclone.getDeviceInformation().connectionEru.timeout); Object[] lobject = Arrays.copyOf(response.getValue(), response.getValue().length, Object[].class); List lbytes = new ArrayList<>(); for (Object o : lobject) { lbytes.add((Integer) o); } int numberOfVmss = lbytes.get(0).intValue(); int vmsNumber = lbytes.get(1).intValue(); if (vmsNumber == deviceclone.getDeviceInformation().connectionEru.vmsNumber) { // Message received corresponds to vms int[] information = new int[lbytes.size() - 2]; for (int i = 2; i < lbytes.size(); i++) { information[i - 2] = lbytes.get(i); } AsfDgtProtocolAnalyser.analyseResponseEP(deviceclone, information); } } catch (Exception e) { if (this.device.getIdentifier().equalsIgnoreCase(AsfServer.devicelog) == true) { e.printStackTrace(); } throw e; } } // // public DeviceAction[] sendCommands(InteropParameters parameters) throws ServerException, SerializationException, Exception { try { List result = new ArrayList<>(); AsfCommands asfCommands = (AsfCommands) parameters.getBodyContentValue(AsfCommands.class); Shared.println("AsfCommand", Serialization.toPrettyString(asfCommands)); Asf clone = (Asf) Serialization.clone(device); if (asfCommands.liberate == DeviceCommands.CONDITION_YES) { AsfCommands command = new AsfCommands(); command.liberate = asfCommands.liberate; result.add(liberate(parameters, command, clone)); } if (asfCommands.state != AsfCommands.STATE_NOTHING) { AsfCommands command = new AsfCommands(); command.state = asfCommands.state; command.priority = asfCommands.priority; result.add(mep(parameters, command, clone)); } if ((asfCommands.brightness != -2) || (asfCommands.blinkOn > -1) || (asfCommands.blinkOff > -1)) { AsfCommands command = new AsfCommands(); if (asfCommands.brightness != -2) { command.brightness = asfCommands.brightness; } if (asfCommands.blinkOn > -1) { command.blinkOn = asfCommands.blinkOn; } if (asfCommands.blinkOff > -1) { command.blinkOff = asfCommands.blinkOff; } result.add(mpp(parameters, command, clone)); } if (asfCommands.reset == AsfCommands.CONDITION_YES) { AsfCommands command = new AsfCommands(); command.reset = asfCommands.reset; result.add(reset(parameters, command, clone)); } if (asfCommands.timeout >= 0) { AsfCommands command = new AsfCommands(); command.timeout = asfCommands.timeout; result.add(timeout(parameters, command, clone)); } mutex.lockWrite(); { Shared.model.updateDevice(device, clone); } mutex.releaseWrite(); return result.toArray(new DeviceAction[result.size()]); } catch (Exception exception) { //Shared.printstack("update exception", exception); return (art.servers.asfserver.Shared.responseError(device, parameters, exception)); } } private DeviceAction mpp(InteropParameters parameters, AsfCommands command, Asf deviceclone) { String language = (String) parameters.getParameterValue("language"); DeviceAction action = new DeviceAction(device, parameters, command); action.actionName = command.getActionName(); try { int currentPriority = this.device.getDeviceStatus().priority; if ((command.priority >= 0) && (command.priority < currentPriority)) { DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_ERROR, Shared.getMessage("Insufficient priority")); action.setResult(actionResult); Shared.model.addAction(action); return action; } else { InteropParameters parametersOrder = new InteropParameters(); parametersOrder.addParameter(new InteropParameter("operation", "setVmsPp")); parametersOrder.addParameter(new InteropParameter("language", language)); parametersOrder.addParameter(new InteropParameter("device", deviceclone.getDeviceInformation().connectionEru.eru)); parametersOrder.addParameter(new InteropParameter("vms", "" + deviceclone.getDeviceInformation().connectionEru.vmsNumber)); int brightness = -1; // Auto if (deviceclone.getDeviceStatus().brightnessMode != AsfStatus.BRIGHTNESS_MODE_AUTO) { brightness = deviceclone.getDeviceStatus().brightnessLevel; } if (command.brightness != -2) { brightness = command.brightness; } int blinkon = (int) Math.round(deviceclone.getDeviceStatus().blinkOn * 1000); if (command.blinkOn > -1) { blinkon = (int) Math.round(command.blinkOn * 10); } int blinkoff = (int) Math.round(deviceclone.getDeviceStatus().blinkOff * 1000); if (command.blinkOff > -1) { blinkoff = (int) Math.round(command.blinkOff * 10); } if (blinkon == 0) { blinkon = 500; } if (blinkoff == 0) { blinkoff = 500; } int messagetime = 2; if (brightness == -1) { brightness = 'A'; } else { brightness = AsfDgtProtocolConstructor.getBrightnessDGTLevel(brightness, deviceclone.getDeviceInformation().maximumBrightnessLevel); } parametersOrder.addParameter(new InteropParameter("brightness", "" + brightness)); parametersOrder.addParameter(new InteropParameter("blinkon", "" + blinkon)); parametersOrder.addParameter(new InteropParameter("blinkoff", "" + blinkoff)); parametersOrder.addParameter(new InteropParameter("messagetime", "" + messagetime)); InteropResponse response = (InteropResponse) Serialization.invoke("set", parametersOrder, deviceclone.getDeviceInformation().connectionEru.address, deviceclone.getDeviceInformation().connectionEru.port, deviceclone.getDeviceInformation().connectionEru.timeout); Object[] lobject = Arrays.copyOf(response.getValue(), response.getValue().length, Object[].class); int asfNumber = (Integer) lobject[1]; if (asfNumber == deviceclone.getDeviceInformation().connectionEru.vmsNumber) { int[] information = new int[lobject.length - 2]; for (int i = 0; i < information.length; i++) { information[i] = (Integer) lobject[i + 2]; } AsfDgtProtocolAnalyser.analyseResponsePP(deviceclone, information); } deviceclone.getDeviceStatus().priority = command.priority; deviceclone.getDeviceStatus().timestampOrder = System.currentTimeMillis(); DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_CORRECT); action.setResult(actionResult); Shared.model.addAction(action); return action; } } catch (Exception exception) { DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_ERROR, exception.getMessage()); action.setResult(actionResult); Shared.model.addAction(action); return action; } } private DeviceAction reset(InteropParameters parameters, AsfCommands command, Asf deviceclone) { String language = (String) parameters.getParameterValue("language"); DeviceAction action = new DeviceAction(device, parameters, command); action.actionName = command.getActionName(); try { int currentPriority = this.device.getDeviceStatus().priority; if ((command.priority >= 0) && (command.priority < currentPriority)) { DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_ERROR, Shared.getMessage("Insufficient priority")); action.setResult(actionResult); Shared.model.addAction(action); return action; } else { InteropParameters parametersOrder = new InteropParameters(); parametersOrder.addParameter(new InteropParameter("operation", "setVmsReset")); parametersOrder.addParameter(new InteropParameter("language", language)); parametersOrder.addParameter(new InteropParameter("device", device.getDeviceInformation().connectionEru.eru)); parametersOrder.addParameter(new InteropParameter("vms", "" + device.getDeviceInformation().connectionEru.vmsNumber)); InteropResponse response = (InteropResponse) Serialization.invoke("set", parametersOrder, device.getDeviceInformation().connectionEru.address, device.getDeviceInformation().connectionEru.port, device.getDeviceInformation().connectionEru.timeout); Object[] lobject = Arrays.copyOf(response.getValue(), response.getValue().length, Object[].class); List lbytes = new ArrayList(); for (Object o : lobject) { lbytes.add((Integer) o); } deviceclone.getDeviceStatus().priority = command.priority; deviceclone.getDeviceStatus().timestampOrder = System.currentTimeMillis(); DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_CORRECT); action.setResult(actionResult); Shared.model.addAction(action); return action; } } catch (Exception exception) { DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_ERROR, exception.getMessage()); action.setResult(actionResult); Shared.model.addAction(action); return action; } } private DeviceAction timeout(InteropParameters parameters, AsfCommands command, Asf deviceclone) { String language = (String) parameters.getParameterValue("language"); DeviceAction action = new DeviceAction(device, parameters, command); action.actionName = command.getActionName(); try { int currentPriority = this.device.getDeviceStatus().priority; if ((command.priority >= 0) && (command.priority < currentPriority)) { DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_ERROR, Shared.getMessage("Insufficient priority")); action.setResult(actionResult); Shared.model.addAction(action); return action; } else { InteropParameters parametersOrder = new InteropParameters(); parametersOrder.addParameter(new InteropParameter("operation", "setVmsTimeout")); parametersOrder.addParameter(new InteropParameter("language", language)); parametersOrder.addParameter(new InteropParameter("device", device.getDeviceInformation().connectionEru.eru)); parametersOrder.addParameter(new InteropParameter("vms", "" + device.getDeviceInformation().connectionEru.vmsNumber)); parametersOrder.addParameter(new InteropParameter("timeout", "" + command.timeout)); InteropResponse response = (InteropResponse) Serialization.invoke("set", parametersOrder, device.getDeviceInformation().connectionEru.address, device.getDeviceInformation().connectionEru.port, device.getDeviceInformation().connectionEru.timeout); Object[] lobject = Arrays.copyOf(response.getValue(), response.getValue().length, Object[].class); List lbytes = new ArrayList<>(); for (Object o : lobject) { lbytes.add((Integer) o); } deviceclone.getDeviceStatus().priority = command.priority; deviceclone.getDeviceStatus().timestampOrder = System.currentTimeMillis(); DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_CORRECT); action.setResult(actionResult); Shared.model.addAction(action); return action; } } catch (Exception exception) { DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_ERROR, exception.getMessage()); action.setResult(actionResult); Shared.model.addAction(action); return action; } } private DeviceAction liberate(InteropParameters parameters, AsfCommands command, Asf deviceclone) { String language = (String) parameters.getParameterValue("language"); DeviceAction action = new DeviceAction(device, parameters, command); action.actionName = command.getActionName(); try { deviceclone.getDeviceStatus().priority = -1; deviceclone.getDeviceStatus().timestampOrder = System.currentTimeMillis(); DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_CORRECT); action.setResult(actionResult); Shared.model.addAction(action); return action; } catch (Exception exception) { DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_ERROR, exception.getMessage()); action.setResult(actionResult); Shared.model.addAction(action); return action; } } private DeviceAction mep(InteropParameters parameters, AsfCommands command, Asf deviceclone) { String language = (String) parameters.getParameterValue("language"); String username = (String) parameters.getParameterValue("username"); DeviceAction action = new DeviceAction(device, parameters, command); action.actionName = command.getActionName(); try { int currentPriority = this.device.getDeviceStatus().priority; if ((command.priority >= 0) && (command.priority < currentPriority)) { DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_ERROR, Shared.getMessage("Insufficient priority")); action.setResult(actionResult); Shared.model.addAction(action); return action; } else { InteropParameters parametersOrder = new InteropParameters(); parametersOrder.addParameter(new InteropParameter("operation", "setVmsMep")); parametersOrder.addParameter(new InteropParameter("language", language)); parametersOrder.addParameter(new InteropParameter("device", device.getDeviceInformation().connectionEru.eru)); parametersOrder.addParameter(new InteropParameter("vms", "" + device.getDeviceInformation().connectionEru.vmsNumber)); byte[] message = AsfDgtProtocolConstructor.getBodyContentMessage(command.state, deviceclone); parametersOrder.addBodycontent(message); InteropResponse response = (InteropResponse) Serialization.invoke("set", parametersOrder, deviceclone.getDeviceInformation().connectionEru.address, deviceclone.getDeviceInformation().connectionEru.port, deviceclone.getDeviceInformation().connectionEru.timeout); deviceclone.getDeviceStatus().priority = command.priority; Object[] lobject = Arrays.copyOf(response.getValue(), response.getValue().length, Object[].class); int asfNumber = (Integer) lobject[1]; if (asfNumber == deviceclone.getDeviceInformation().connectionEru.vmsNumber) { int[] information = new int[lobject.length - 2]; for (int i = 0; i < information.length; i++) { information[i] = (Integer) lobject[i + 2]; } AsfDgtProtocolAnalyser.analyseResponseEP(deviceclone, information); } deviceclone.getDeviceStatus().timestampOrder = System.currentTimeMillis(); DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_CORRECT); action.setResult(actionResult); Shared.model.addAction(action); return action; } } catch (Exception exception) { //Shared.printstack("Exception", exception); DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_ERROR, exception.getMessage()); action.setResult(actionResult); Shared.model.addAction(action); return action; } } // }