package art.servers.rtzserver.controller;
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.colors.controller.RTZ32.RTZ32_ControllerConfiguration;
import art.library.model.devices.colors.controller.RTZ32.commands.RTZ32_Commands_Configuration_Write;
import art.library.model.devices.colors.controller.RTZ32.configuration.RTZ32_Configuration;
import art.library.model.devices.colors.controller.RTZ32.information.RTZ32_Information_Program;
import art.library.model.devices.colors.controller.RTZ32.information.RTZ32_Information_Programs;
import art.library.model.devices.colors.controller.RTZ32.types.RTZ32_Persistent_Configuration;
import art.library.model.devices.colors.controller.RTZ32.types.RTZ32_Persistent_Configurations;
import art.library.model.devices.colors.controller.RTZ32.types.RTZ32_Validation_Result;
import art.servers.ServerException;
import art.servers.Shared;
import art.servers.rtzserver.utils.VALIDATION;
import art.servers.types.HttpAuthentication;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class ListenerImplementation extends art.servers.controller.ListenerImplementation
{
public InteropResponse sendCommands(InteropParameters parameters) throws SerializationException
{
String language = (String)parameters.getParameterValue("language");
try
{
String identifier = (parameters.hasParameter("device") == true) ? (String)parameters.getParameterValue("device") : null;
Controller_RTZ32 controller = (art.servers.rtzserver.controller.Controller_RTZ32)Shared.getDeviceController(identifier);
DeviceAction[] laction = controller.sendCommands(parameters);
return(new InteropResponse(laction));
}
catch (ServerException exception)
{
throw new SerializationException(Shared.getMessage(language, exception.getMessage()));
}
catch (Exception exception)
{
throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception);
}
}
//
public InteropResponse listPrograms(InteropParameters parameters) throws SerializationException
{
String language = (String)parameters.getParameterValue("language");
try
{
String deviceIdentifier = (parameters.hasParameter("device") == true) ? (String)parameters.getParameterValue("device") : null;
RTZ32_Information_Programs programs = Controller_RTZ32_Database.listPrograms(deviceIdentifier);
return(new InteropResponse(programs, false));
}
catch (Exception exception)
{
throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception);
}
}
public InteropResponse getProgram(InteropParameters parameters) throws SerializationException
{
String language = (String)parameters.getParameterValue("language");
try
{
String deviceIdentifier = (parameters.hasParameter("device") == true) ? (String)parameters.getParameterValue("device") : null;
String programNumber = (String)parameters.getParameterValue("program");
RTZ32_Information_Program program = Controller_RTZ32_Database.getProgram(deviceIdentifier, programNumber);
return(new InteropResponse(program, false));
}
catch (Exception exception)
{
throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception);
}
}
public InteropResponse addProgram(InteropParameters parameters) throws SerializationException
{
String language = (String)parameters.getParameterValue("language");
try
{
RTZ32_Information_Program program = parameters.getBodyContentValue(RTZ32_Information_Program.class);
return(new InteropResponse(Controller_RTZ32_Database.addProgram(program), false));
}
catch (Exception exception)
{
throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception);
}
}
public InteropResponse deleteProgram(InteropParameters parameters) throws SerializationException
{
String language = (String)parameters.getParameterValue("language");
try
{
String deviceIdentifier = (parameters.hasParameter("device") == true) ? (String)parameters.getParameterValue("device") : null;
String programIdentifier = (String)parameters.getParameterValue("program");
return(new InteropResponse(Controller_RTZ32_Database.deleteProgram(deviceIdentifier, programIdentifier), false));
}
catch (Exception exception)
{
throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception);
}
}
public InteropResponse saveProgram(InteropParameters parameters) throws SerializationException
{
String language = (String)parameters.getParameterValue("language");
try
{
RTZ32_Information_Program program = parameters.getBodyContentValue(RTZ32_Information_Program.class);
String identifier = (parameters.hasParameter("device") == true) ? (String)parameters.getParameterValue("device") : null;
String username = (parameters.hasParameter("username") == true) ? (String)parameters.getParameterValue("username") : "?";
Controller_RTZ32 controller = (art.servers.rtzserver.controller.Controller_RTZ32)Shared.getDeviceController(identifier);
RTZ32_Validation_Result result1 = VALIDATION.validate(controller.getDevice(), program, language);
RTZ32_Validation_Result result2 = VALIDATION.eprom(controller.getDevice(), program, language);
RTZ32_Validation_Result result = new RTZ32_Validation_Result();
result.errors.addAll(result1.errors);
result.errors.addAll(result2.errors);
result.warnings.addAll(result1.warnings);
result.warnings.addAll(result2.warnings);
if (result.hasErrors() == true)
{
return new InteropResponse(result, false);
}
else
{
program.validated = true;
program.username = username;
Controller_RTZ32_Database.addProgram(program);
}
return new InteropResponse(result, false);
}
catch (Exception exception)
{
throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception);
}
}
//
//
public InteropResponse validateProgramNormal(InteropParameters parameters) throws SerializationException
{
String language = (String)parameters.getParameterValue("language");
try
{
RTZ32_Information_Program program = parameters.getBodyContentValue(RTZ32_Information_Program.class);
String identifier = (parameters.hasParameter("device") == true) ? (String)parameters.getParameterValue("device") : null;
Controller_RTZ32 controller = (art.servers.rtzserver.controller.Controller_RTZ32)Shared.getDeviceController(identifier);
return new InteropResponse(VALIDATION.validate(controller.getDevice(), program, language), false);
}
catch (Exception exception)
{
throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception);
}
}
public InteropResponse validateProgramEPROM(InteropParameters parameters) throws SerializationException
{
String language = (String)parameters.getParameterValue("language");
try
{
RTZ32_Information_Program program = parameters.getBodyContentValue(RTZ32_Information_Program.class);
String identifier = (parameters.hasParameter("device") == true) ? (String)parameters.getParameterValue("device") : null;
Controller_RTZ32 controller = (art.servers.rtzserver.controller.Controller_RTZ32)Shared.getDeviceController(identifier);
return new InteropResponse(VALIDATION.eprom(controller.getDevice(), program, language), false);
}
catch (Exception exception)
{
throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception);
}
}
//
//
public InteropResponse listConfigurations(InteropParameters parameters) throws SerializationException
{
String language = (String)parameters.getParameterValue("language");
try
{
String deviceIdentifier = (String)parameters.getParameterValue("device");
long timestamp1 = (parameters.hasParameter("from") == true) ? getTimestamp((String)parameters.getParameterValue("from")) : 0;
long timestamp2 = (parameters.hasParameter("to") == true) ? getTimestamp((String)parameters.getParameterValue("to")) : System.currentTimeMillis();
RTZ32_Persistent_Configurations configurations = Controller_RTZ32_Database.listConfigurations(deviceIdentifier, timestamp1, timestamp2);
return(new InteropResponse(configurations, false));
}
catch (Exception exception)
{
throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception);
}
}
public InteropResponse getConfiguration(InteropParameters parameters) throws SerializationException
{
String language = (String)parameters.getParameterValue("language");
try
{
String deviceIdentifier = (String)parameters.getParameterValue("device");
long timestamp = getTimestamp((String)parameters.getParameterValue("timestamp"));
RTZ32_Persistent_Configuration configuration = Controller_RTZ32_Database.getConfiguration(deviceIdentifier, timestamp);
return(new InteropResponse(configuration, false));
}
catch (Exception exception)
{
throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception);
}
}
public InteropResponse addConfiguration(InteropParameters parameters) throws SerializationException
{
String language = (String)parameters.getParameterValue("language");
try
{
RTZ32_Persistent_Configuration configuration = parameters.getBodyContentValue(RTZ32_Persistent_Configuration.class);
configuration.username = (String)parameters.getParameterValue("username");
return(new InteropResponse(Controller_RTZ32_Database.addConfiguration(configuration), false));
}
catch (Exception exception)
{
throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception);
}
}
public InteropResponse deleteConfiguration(InteropParameters parameters) throws SerializationException
{
String language = (String)parameters.getParameterValue("language");
HttpAuthentication authentication = getHttpAuthentication(parameters);
try
{
String deviceIdentifier = (String)parameters.getParameterValue("device");
long timestamp = getTimestamp((String)parameters.getParameterValue("timestamp"));
InteropResponse response = new InteropResponse(Controller_RTZ32_Database.deleteConfiguration(deviceIdentifier, timestamp), false);
Controller_RTZ32 controller = (art.servers.rtzserver.controller.Controller_RTZ32)Shared.getDeviceController(deviceIdentifier);
Shared.traceInformation(Shared.getApplicationName(), "Delete configuration", controller.name + ", " + new Date(timestamp), authentication, language);
return response;
}
catch (Exception exception)
{
throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception);
}
}
public InteropResponse previewConfiguration(InteropParameters parameters) throws SerializationException
{
String language = (String)parameters.getParameterValue("language");
try
{
String identifier = (parameters.hasParameter("device") == true) ? (String)parameters.getParameterValue("device") : null;
Controller_RTZ32 controller = (art.servers.rtzserver.controller.Controller_RTZ32)Shared.getDeviceController(identifier);
RTZ32_Commands_Configuration_Write commands = parameters.getBodyContentValue(RTZ32_Commands_Configuration_Write.class);
RTZ32_ControllerConfiguration configurationCurrent = Serialization.clone(controller.getController().getDeviceConfiguration());
RTZ32_Configuration configurationSelected = commands.configuration;
Controller_RTZ32_Factory_Configuration factoryCurrent = new Controller_RTZ32_Factory_Configuration(configurationCurrent.rtz32);
Controller_RTZ32_Factory_Configuration factorySelected = new Controller_RTZ32_Factory_Configuration(configurationSelected);
factoryCurrent.serialize();
factorySelected.serialize();
if (commands.operationTables == false)
{
for (int i=5; i<=7; i++) factorySelected.tables.put(i, factoryCurrent.tables.get(i));
for (int i=33; i<=34; i++) factorySelected.tables.put(i, factoryCurrent.tables.get(i));
for (int i=38; i<=40; i++) factorySelected.tables.put(i, factoryCurrent.tables.get(i));
for (int i=41; i<=45; i++) factorySelected.tables.put(i, factoryCurrent.tables.get(i));
for (int i=50; i<=60; i++) factorySelected.tables.put(i, factoryCurrent.tables.get(i));
for (int i=64; i<=69; i++) factorySelected.tables.put(i, factoryCurrent.tables.get(i));
for (int i=31; i<=32; i++) factorySelected.tables.put(i, factoryCurrent.tables.get(i));
for (int i=70; i<=72; i++) factorySelected.tables.put(i, factoryCurrent.tables.get(i));
factorySelected.tables.put(75, factoryCurrent.tables.get(75));
factorySelected.tables.put(78, factoryCurrent.tables.get(78));
factorySelected.tables.put(80, factoryCurrent.tables.get(80));
for (int i=81; i<=85; i++) factorySelected.tables.put(i, factoryCurrent.tables.get(i));
}
if (commands.distributionTables == true)
{
for (int i=1; i<=4; i++) factorySelected.tables.put(i, factoryCurrent.tables.get(i));
for (int i=35; i<=37; i++) factorySelected.tables.put(i, factoryCurrent.tables.get(i));
for (int i=72; i<=74; i++) factorySelected.tables.put(i, factoryCurrent.tables.get(i));
factorySelected.tables.put(46, factoryCurrent.tables.get(46));
factorySelected.tables.put(79, factoryCurrent.tables.get(79));
factorySelected.tables.put(80, factoryCurrent.tables.get(80));
factorySelected.tables.put(95, factoryCurrent.tables.get(95));
if ((commands.programs == null) || (commands.programs.size() == 0))
{
for (int i=11; i<=28; i++) factorySelected.tables.put(i, factoryCurrent.tables.get(i));
}
else
{
for(Integer programNumber : commands.programs)
{
if ((programNumber >=1) && (programNumber <=18))
{
factorySelected.tables.put(programNumber + 10, factoryCurrent.tables.get(programNumber + 10));
}
}
}
}
return new InteropResponse(factorySelected.deserialize(), false);
}
catch (Exception exception)
{
throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception);
}
}
//
//
public InteropResponse listMaps(InteropParameters parameters) throws SerializationException
{
String language = parameters.getParameterValue("language");
try
{
String identifier = (String)parameters.getParameterValue("controller");
if (identifier == null) identifier = (String)parameters.getParameterValue("identifier");
if (identifier == null) identifier = (String)parameters.getParameterValue("device");
List maps = new ArrayList();
File folder = new File("data/" + Shared.getApplicationName() + "/maps/" + identifier + "/");
if (folder.exists() == false) throw new SerializationException(Shared.getMessage(language, "No maps"));
for (File file : folder.listFiles()) maps.add(file.getName().replace(".svg", ""));
InteropResponse response = new InteropResponse(maps.toArray(new String[maps.size()]), true);
Object[] object = response.getResponse();
return response;
}
catch (SerializationException exception)
{
throw exception;
}
catch (Exception exception)
{
throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception);
}
}
public InteropResponse getMap(InteropParameters parameters) throws SerializationException
{
String language = (String)parameters.getParameterValue("language");
try
{
String identifier = (String)parameters.getParameterValue("controller");
if (identifier == null) identifier = (String)parameters.getParameterValue("identifier");
if (identifier == null) identifier = (String)parameters.getParameterValue("device");
String name = (String)parameters.getParameterValue("name");
File file = new File("data/" + Shared.getApplicationName() + "/maps/" + identifier + "/" + name + ".svg");
if (file.exists() == false) throw new SerializationException(Shared.getMessage(language, "Map does not exists"));
byte[] data = new byte[(int)file.length()];
FileInputStream fis = new FileInputStream(file);
fis.read(data);
fis.close();
InteropResponse response = new InteropResponse(data);
response.mime = "image/svg+xml";
return response;
}
catch (SerializationException exception)
{
throw exception;
}
catch (Exception exception)
{
throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception);
}
}
public InteropResponse addMap(InteropParameters parameters) throws SerializationException
{
String language = (String)parameters.getParameterValue("language");
try
{
String identifier = (String)parameters.getParameterValue("controller");
if (identifier == null) identifier = (String)parameters.getParameterValue("identifier");
if (identifier == null) identifier = (String)parameters.getParameterValue("device");
String name = parameters.getParameterValue("name");
String newIdentifier = identifier.substring(0, identifier.length()-1);
newIdentifier = newIdentifier + "0";
byte[] data = parameters.getBodyContentValue(byte[].class);
File file = new File("data/" + Shared.getApplicationName() + "/maps/" + identifier + "/" + name + ".svg");
file.getParentFile().mkdirs();
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
bos.write(data);
bos.close();
return new InteropResponse(new Boolean(true));
}
catch (Exception exception)
{
throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception);
}
}
public InteropResponse deleteMap(InteropParameters parameters) throws SerializationException
{
String language = (String)parameters.getParameterValue("language");
try
{
String identifier = (String)parameters.getParameterValue("controller");
if (identifier == null) identifier = (String)parameters.getParameterValue("identifier");
if (identifier == null) identifier = (String)parameters.getParameterValue("device");
String name = parameters.getParameterValue("name");
String newIdentifier = identifier.substring(0, identifier.length()-1);
newIdentifier = newIdentifier + "0";
File file = new File("data/" + Shared.getApplicationName() + "/maps/" + identifier + "/" + name + ".svg");
if (file.exists() == true) return new InteropResponse(new Boolean(file.delete()));
throw new SerializationException(Shared.getMessage(language, "Map does not exists"));
}
catch (SerializationException exception)
{
throw exception;
}
catch (Exception exception)
{
throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception);
}
}
//
// https://172.16.11.210:8002/art?operation=getDevices×tamp=0&token=test,test
// https://172.16.11.210:8002/art?operation=listConfigurations&device=controller-zaragoza-90020&&token=test,test
// https://172.16.11.210:8002/art?operation=getConfiguration&device=controller-zaragoza-90020×tamp=2021-07-08T10:27:44.905Z&token=test,test
}