package art.servers.pvvserver.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.vms.asf.Asf;
import art.library.model.devices.vms.asf.AsfStatus;
import art.library.model.devices.vms.pvv.Pvv;
import art.library.model.devices.vms.pvv.PvvCommands;
import art.library.model.devices.vms.pvv.PvvStatus;
import art.library.model.devices.vms.pvv.commands.PvvCommandsConfiguration;
import art.library.model.devices.vms.pvv.commands.PvvCommandsMessage;
import art.library.model.devices.vms.pvv.commands.PvvCommandsMessageBlinkings;
import art.library.model.devices.vms.pvv.commands.PvvCommandsMessageVisibilities;
import art.library.model.devices.vms.pvv.information.PvvInformationConfigurationAddressSpeed;
import art.library.utils.common.NumberUtils;
import art.library.utils.synchro.Mutex;
import art.servers.ServerException;
import art.servers.Shared;
import art.servers.PvvServer;
import art.servers.pvvserver.protocols.dgt.PvvDgtConstants;
import art.servers.pvvserver.protocols.dgt.PvvDgtProtocolAnalyser;
import java.util.Arrays;
import java.util.List;
import art.servers.pvvserver.protocols.dgt.PvvDgtProtocolConstructor;
import java.util.ArrayList;
public class ControllerClvDGTEru extends ControllerPvv
{
private String name = null;
private boolean firstTime = true;
private Mutex mutex = new Mutex();
private boolean primeraVezCfg = true;
public ControllerClvDGTEru(Pvv pvv)
{
super(pvv);
this.device = pvv;
this.name = Shared.getMessage("Controller pvv dgt eru") + " " + pvv.toString();
this.setName(name);
if (device.getDeviceStatus() == null)
{
device.status = new PvvStatus();
}
}
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");
}
/**
* Updates device priority and timestampOrder from outside if identifier is
* correct. Used by ControllerPvvVirtual
*
* @param newDevice
*/
public void updateExternal(Pvv newDevice)
{
try
{
mutex.lockWrite();
if (newDevice.getIdentifier().equals(device.getIdentifier()))
{
Pvv clone = Serialization.clone(device);
clone.getDeviceStatus().priority = newDevice.getDeviceStatus().priority;
clone.getDeviceStatus().timestampOrder = newDevice.getDeviceStatus().timestampOrder;
Shared.model.updateDevice(device, clone);
} else
{
throw new ServerException("Incorrect device " + newDevice.getIdentifier() + ", expected " + device.getIdentifier());
}
} catch (Exception ex)
{
//Shared.printstack("Exception updating from clone", ex);
} finally
{
mutex.releaseWrite();
}
}
//
private void update() throws Exception
{
mutex.lockWrite();
Pvv deviceclone = (Pvv) Serialization.clone(device);
try
{
readAlarms(deviceclone);
if (deviceclone.getAlarm("alarm_offline") > 0)
{
offline(deviceclone);
return;
}
// readCalculatedBrightness(deviceclone);
readConfiguration(deviceclone);
readStatus(deviceclone);
//If device has a asf in cross state, send shutdown order
checkAsf(deviceclone);
online(deviceclone);
try
{
if (primeraVezCfg == true)
{
updateMpp();
}
primeraVezCfg = false;
}
catch (Exception e)
{
}
} catch (Exception e)
{
offline(deviceclone);
} finally
{
mutex.releaseWrite();
}
}
private void offline(Pvv deviceclone) throws Exception
{
if ((device.alarms.alarm_offline <= 0) || (firstTime == true))
{
firstTime = false;
String message = art.servers.Shared.getMessage("Pvv offline");
art.servers.Shared.println(name, message);
}
deviceclone.setAlarm("alarm_offline", System.currentTimeMillis());
Shared.model.updateDevice(device, deviceclone);
}
private void online(Pvv deviceclone) throws Exception
{
if ((device.alarms.alarm_offline > 0) || (firstTime == true))
{
firstTime = false;
String message = art.servers.Shared.getMessage("Pvv online");
Shared.println(this.name, message);
}
deviceclone.setAlarm("alarm_offline", false);
deviceclone.setAlarm("alarm_invalid", false);
Shared.model.updateDevice(device, deviceclone);
}
private void readAlarms(Pvv 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.pvvNumber));
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 numberOfPvvs = lbytes.get(0);
int pvvNumber = lbytes.get(1);
if (pvvNumber == deviceclone.getDeviceInformation().connectionEru.pvvNumber)
{
// Message received corresponds to pvvs
int[] information = new int[lbytes.size() - 2];
for (int i = 2; i < lbytes.size(); i++)
{
information[i - 2] = lbytes.get(i);
}
PvvDgtProtocolAnalyser.analyseResponseEyA(deviceclone, information);
}
} catch (Exception e)
{
//Shared.printstack("exception pvv on readAlarms ConnectionEru " + Serialization.toPrettyString(deviceclone.getDeviceInformation().connectionEru), e);
throw e;
}
}
//TODO: Implement in ERU VIGICAT
private void readCalculatedBrightness(Pvv deviceclone) throws Exception
{
try
{
InteropParameters parameters = new InteropParameters();
parameters.addParameter(new InteropParameter("operation", "getPvvCalculatedBrightness"));
parameters.addParameter(new InteropParameter("language", "en-GB"));
parameters.addParameter(new InteropParameter("device", deviceclone.getDeviceInformation().connectionEru.eru));
parameters.addParameter(new InteropParameter("pvv", "" + deviceclone.getDeviceInformation().connectionEru.pvvNumber));
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 numberOfPvvs = lbytes.get(0);
int pvvNumber = lbytes.get(1);
if (pvvNumber == deviceclone.getDeviceInformation().connectionEru.pvvNumber)
{
//Código C3h: Retorno de luminosidad calculada, Valores de ‘0’ a ‘7’
int brightness = lbytes.get(2);
if (brightness >= 0x30)
{
brightness = brightness - 0x30;
}
device.getDeviceStatus().calculatedBrightnessLevel = PvvDgtProtocolConstructor.getBrightnessClientLevel(brightness, 100, device.getDeviceInformation().maximumBrightnessLevel);
}
} catch (Exception e)
{
//Shared.printwarning("readCalculatedBrightness", "Not implemented in ERU VIGICAT");
//Shared.printstack("exception on readCalculatedBrightness", e);
//throw e;
}
}
private void readConfiguration(Pvv 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.pvvNumber));
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 pvvNumber = lbytes.get(1);
if (pvvNumber == deviceclone.getDeviceInformation().connectionEru.pvvNumber)
{
// Message received corresponds to pvv
int[] information = new int[lbytes.size() - 2];
for (int i = 2; i < lbytes.size(); i++)
{
information[i - 2] = lbytes.get(i);
}
PvvDgtProtocolAnalyser.analyseResponsePPDgtEru(deviceclone, information);
}
} catch (Exception e)
{
//Shared.printstack("exception on readConfiguration", e);
throw e;
}
}
private void readStatus(Pvv 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.pvvNumber));
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 pvvNumber = lbytes.get(1);
if (pvvNumber == deviceclone.getDeviceInformation().connectionEru.pvvNumber)
{
// Message received corresponds to pvv
int[] information = new int[lbytes.size() - 2];
for (int i = 2; i < lbytes.size(); i++)
{
information[i - 2] = lbytes.get(i);
}
PvvDgtProtocolAnalyser.analyseResponseEP(device, deviceclone, information);
}
} catch (Exception e)
{
//Shared.printstack("exception on readStatus", e);
if (this.device.getIdentifier().equalsIgnoreCase(PvvServer.devicelog) == true)
{
e.printStackTrace();
}
throw e;
}
}
private void checkAsf(Pvv deviceclone) throws Exception
{
try
{
if (deviceclone.getDeviceInformation().asf != null)
{
Asf asfElem = (Asf) Shared.model.getDeviceExternal(deviceclone.getDeviceInformation().asf);
if (asfElem != null && asfElem.getDeviceStatus().isValid() && asfElem.getDeviceStatus().state == AsfStatus.STATE_CROSS)
{
PvvCommands command = new PvvCommands();
command.turnOff = PvvCommands.CONDITION_YES;
InteropParameters parameters = new InteropParameters();
parameters.addParameter(new InteropParameter("language", "en-GB"));
parameters.addParameter(new InteropParameter("username", Shared.getMessage("system")));
mep(parameters, command, deviceclone);
Shared.printwarning("checkAsf", "Turn off PVV because its Asf is CROSS");
}
}
} catch (Exception e)
{
Shared.printstack("exception on checkAsf", e);
}
}
//
//
@Override
public DeviceAction[] sendCommands(InteropParameters parameters) throws ServerException, SerializationException, Exception
{
try
{
List result = new ArrayList<>();
PvvCommands pvvCommands = (PvvCommands) parameters.getBodyContentValue(PvvCommands.class);
Shared.println(name, Serialization.toPrettyString(pvvCommands));
Pvv clone = (Pvv) Serialization.clone(device);
//Liberate
if (pvvCommands.liberate == DeviceCommands.CONDITION_YES)
{
PvvCommands command = new PvvCommands();
command.liberate = pvvCommands.liberate;
result.add(liberate(parameters, command, clone));
}
//Turn off
if (pvvCommands.turnOff == DeviceCommands.CONDITION_YES)
{
PvvCommands command = new PvvCommands();
command.priority = pvvCommands.priority;
command.turnOff = pvvCommands.turnOff;
result.add(mep(parameters, command, clone));
}
//Reset
if (pvvCommands.reset == PvvCommands.CONDITION_YES)
{
PvvCommands command = new PvvCommands();
command.priority = pvvCommands.priority;
command.reset = pvvCommands.reset;
result.add(reset(parameters, command, clone));
}
//Messages
if (pvvCommands.message != null)
{
PvvCommands commandMep = new PvvCommands();
commandMep.priority = pvvCommands.priority;
commandMep.message = new PvvCommandsMessage();
if (pvvCommands.message.timeout >= 0)
{
//Timeout
commandMep.message.timeout = pvvCommands.message.timeout;
}
commandMep.message.speed = pvvCommands.message.speed;
commandMep.message.synchroActivation = pvvCommands.message.synchroActivation;
//Blinkings
if (pvvCommands.message.blinkings == null)
{
pvvCommands.message.blinkings = new PvvCommandsMessageBlinkings();
pvvCommands.message.blinkings.blinkingCircle = this.device.getDeviceStatus().blinkingCircle ? DeviceCommands.CONDITION_YES : DeviceCommands.CONDITION_NO;
pvvCommands.message.blinkings.blinkingFlashes = this.device.getDeviceStatus().blinkingFlashes ? DeviceCommands.CONDITION_YES : DeviceCommands.CONDITION_NO;
pvvCommands.message.blinkings.blinkingSpeed = this.device.getDeviceStatus().blinkingSpeed ? DeviceCommands.CONDITION_YES : DeviceCommands.CONDITION_NO;
}
commandMep.message.blinkings = new PvvCommandsMessageBlinkings();
if (pvvCommands.message.blinkings.blinkingCircle > -1)
{
commandMep.message.blinkings.blinkingCircle = pvvCommands.message.blinkings.blinkingCircle;
}
if (pvvCommands.message.blinkings.blinkingFlashes > -1)
{
commandMep.message.blinkings.blinkingFlashes = pvvCommands.message.blinkings.blinkingFlashes;
}
if (pvvCommands.message.blinkings.blinkingSpeed > -1)
{
commandMep.message.blinkings.blinkingSpeed = pvvCommands.message.blinkings.blinkingSpeed;
}
//Visibilities
if (pvvCommands.message.visibilities == null)
{
pvvCommands.message.visibilities = new PvvCommandsMessageVisibilities();
pvvCommands.message.visibilities.visibilityCircle = this.device.getDeviceStatus().visibilityCircle;
pvvCommands.message.visibilities.visibilityFlashes = this.device.getDeviceStatus().visibilityFlashes;
pvvCommands.message.visibilities.visibilitySpeed = this.device.getDeviceStatus().visibilitySpeed;
}
commandMep.message.visibilities = new PvvCommandsMessageVisibilities();
commandMep.message.visibilities.visibilityCircle = pvvCommands.message.visibilities.visibilityCircle;
commandMep.message.visibilities.visibilityFlashes = pvvCommands.message.visibilities.visibilityFlashes;
commandMep.message.visibilities.visibilitySpeed = pvvCommands.message.visibilities.visibilitySpeed;
result.add(mep(parameters, commandMep, clone));
}
if (pvvCommands.configuration != null)
{
PvvCommands commandMppConfiguration = new PvvCommands();
commandMppConfiguration.priority = pvvCommands.priority;
commandMppConfiguration.configuration = pvvCommands.configuration;
result.add(mpp(parameters, commandMppConfiguration, clone));
}
mutex.lockWrite();
{
Shared.model.updateDevice(device, clone);
}
mutex.releaseWrite();
return result.toArray(new DeviceAction[result.size()]);
} catch (Exception exception)
{
Shared.printstack(name, exception);
return (art.servers.pvvserver.Shared.responseError(device, parameters, exception));
}
}
private void updateMpp()
{
try
{
PvvCommandsConfiguration pvvCommandsConfiguration = new PvvCommandsConfiguration();
InteropParameters parametersOrder = new InteropParameters();
parametersOrder.addParameter(new InteropParameter("operation", "setPvvMpp"));
parametersOrder.addParameter(new InteropParameter("language", "ca-ES"));
parametersOrder.addParameter(new InteropParameter("device", this.device.getDeviceInformation().connectionEru.eru));
parametersOrder.addParameter(new InteropParameter("pvv", "" + this.device.getDeviceInformation().connectionEru.pvvNumber));
int blinkon = 500;
int blinkoff = 500;
int brightness = 'A';
pvvCommandsConfiguration.blinkOn = blinkon;
pvvCommandsConfiguration.blinkOff = blinkoff;
pvvCommandsConfiguration.brightness = brightness;
pvvCommandsConfiguration.brightnessControl = PvvCommandsConfiguration.BRIGHTNESS_CONTROL_MANUAL;
pvvCommandsConfiguration.flashPeriod = 20;
pvvCommandsConfiguration.flashStopTime = 10;
pvvCommandsConfiguration.flashesOnPercentage = 70;
pvvCommandsConfiguration.flashesxPeriod = 10;
parametersOrder.addBodycontent(pvvCommandsConfiguration);
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);
}
int pvvNumber = lbytes.get(1);
if (pvvNumber == device.getDeviceInformation().connectionEru.pvvNumber)
{
// Message received corresponds to pvv
int[] information = new int[lbytes.size() - 2];
for (int i = 2; i < lbytes.size(); i++)
{
information[i - 2] = lbytes.get(i);
}
PvvDgtProtocolAnalyser.analyseResponsePPDgtEru(device, information);
}
}
catch (Exception exception)
{
}
}
private DeviceAction mpp(InteropParameters parameters, PvvCommands command, Pvv deviceclone)
{
Shared.println(name, "1.MPP: " + command.configuration);
String language = (String) parameters.getParameterValue("language");
DeviceAction action = new DeviceAction(deviceclone, 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.pvvNumber));
int brightness = -1; // Auto
if (deviceclone.getDeviceConfiguration().brightnessMode != PvvStatus.BRIGHTNESS_MODE_AUTO)
{
brightness = deviceclone.getDeviceStatus().brightnessLevel;
}
if (command.configuration.brightness != -2)
{
brightness = command.configuration.brightness;
}
int blinkon = (int) Math.round(deviceclone.getDeviceConfiguration().blinkOn * 1000);
if (command.configuration.blinkOn > -1)
{
blinkon = (int) Math.round(command.configuration.blinkOn * 1000);
}
int blinkoff = (int) Math.round(deviceclone.getDeviceConfiguration().blinkOff * 1000);
if (command.configuration.blinkOff > -1)
{
blinkoff = (int) Math.round(command.configuration.blinkOff * 1000);
}
if (blinkon == 0)
{
blinkon = 500;
}
if (blinkoff == 0)
{
blinkoff = 500;
}
if (brightness == -1)
{
brightness = 'A';
} else
{
brightness = PvvDgtProtocolConstructor.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", "2"));
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);
List lbytes = new ArrayList<>();
for (Object o : lobject)
{
lbytes.add((Integer) o);
}
int pvvNumber = lbytes.get(1);
if (pvvNumber == deviceclone.getDeviceInformation().connectionEru.pvvNumber)
{
// Message received corresponds to pvv
int[] information = new int[lbytes.size() - 2];
for (int i = 2; i < lbytes.size(); i++)
{
information[i - 2] = lbytes.get(i);
}
PvvDgtProtocolAnalyser.analyseResponsePPDgtEru(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, PvvCommands command, Pvv 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.pvvNumber));
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, PvvCommands command, Pvv deviceclone)
{
String language = (String) parameters.getParameterValue("language");
DeviceAction action = new DeviceAction(device, parameters, command);
action.actionName = command.getActionName();
try
{
deviceclone.getDeviceStatus().priority = -1;
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, PvvCommands command, Pvv 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.pvvNumber));
byte[] message = getBodyContentMessage(command);
parametersOrder.addBodycontent(message);
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);
List lbytes = new ArrayList<>();
for (Object o : lobject)
{
lbytes.add((Integer) o);
}
int pvvNumber = lbytes.get(1);
if (pvvNumber == deviceclone.getDeviceInformation().connectionEru.pvvNumber)
{
// Message received corresponds to pvvs
int[] information = new int[lbytes.size() - 2];
for (int i = 2; i < lbytes.size(); i++)
{
information[i - 2] = lbytes.get(i);
}
PvvDgtProtocolAnalyser.analyseResponseEP(device, deviceclone, information);
}
deviceclone.getDeviceStatus().priority = command.priority;
deviceclone.getDeviceStatus().timestampOrder = System.currentTimeMillis();
//If has sincro activation, send OPVV_SINCRO
if (command.message != null && command.message.synchroActivation == DeviceCommands.CONDITION_YES)
{
InteropParameters parametersSynchro = new InteropParameters();
parametersSynchro.addParameter(new InteropParameter("operation", "setPvvSincro"));
parametersSynchro.addParameter(new InteropParameter("language", language));
parametersSynchro.addParameter(new InteropParameter("device", deviceclone.getDeviceInformation().connectionEru.eru));
parametersSynchro.addParameter(new InteropParameter("pvv", "" + deviceclone.getDeviceInformation().connectionEru.pvvNumber));
parametersSynchro.addParameter(new InteropParameter("sincro", "1"));
Serialization.invoke("set", parametersSynchro, deviceclone.getDeviceInformation().connectionEru.address, deviceclone.getDeviceInformation().connectionEru.port, deviceclone.getDeviceInformation().connectionEru.timeout);
}
DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_CORRECT);
action.setResult(actionResult);
Shared.model.addAction(action);
return action;
}
} catch (Exception exception)
{
//Shared.printstack("PVVDGT mep", exception);
DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_ERROR, exception.getMessage());
action.setResult(actionResult);
Shared.model.addAction(action);
return action;
}
}
private byte[] getBodyContentMessage(PvvCommands command) throws Exception
{
try
{
if (command.turnOff == PvvCommands.CONDITION_YES || command.message.speed == 0)
{
return new byte[]
{
PvvDgtConstants.PVV_OFF
};
}
PvvInformationConfigurationAddressSpeed addressSpeed = this.device.getDeviceInformation().configuration.getBySpeed(command.message.speed);
if (addressSpeed == null)
{
throw new Exception(String.format("Error: Speed '%s' not found", command.message.speed));
}
List lresult = new ArrayList<>();
lresult.add((byte) PvvDgtConstants.PVV_FIX);
lresult.add((byte) PvvDgtConstants.PVV_TOPOLOGY_1);
lresult.add((byte) NumberUtils.IntToAscii(1));
List lgraphicA = new ArrayList<>();
lgraphicA.add((byte) '1');
lgraphicA.add((byte) '1');
lgraphicA.add((byte) addressSpeed.address);
lgraphicA.add((byte) 'N');
lresult.addAll(lgraphicA);
byte[] aux = new byte[lresult.size()];
for (int i = 0; i < aux.length; i++)
{
aux[i] = lresult.get(i);
}
return (aux);
} catch (Exception exception)
{
exception.printStackTrace();
throw exception;
}
}
//
}