package art.servers.colorsserver.M.protocol;
|
|
|
import art.library.interop.serialization.Serialization;
|
import art.library.model.devices.colors.controller.M.M_Controller;
|
import art.library.model.devices.colors.controller.M.M_ControllerInformation;
|
import art.library.model.devices.colors.controller.M.configuration.SPTP.M_SPTP_Configuration;
|
import art.library.model.devices.colors.controller.M.configuration.SPTP.M_SPTP_ConfigurationBusLocalPreferenceOperation;
|
import art.library.model.devices.colors.controller.M.configuration.SPTP.M_SPTP_ConfigurationTimetableLocalPreferenceOperation;
|
import art.library.model.devices.colors.controller.M.configuration.SPTP.M_SPTP_Configuration_BusLineAreaConfiguration;
|
import art.library.model.devices.colors.controller.M.tables.M_Table;
|
import art.library.model.devices.colors.controller.M.tables.M_TableWriter;
|
import art.library.model.devices.colors.controller.M.tables.subcontroller.SPTP.M_Table_SPTP_BusLineAreaConfiguration;
|
import art.library.model.devices.colors.controller.M.tables.subcontroller.SPTP.M_Table_SPTP_BusLocalPreferenceOperation;
|
import art.library.model.devices.colors.controller.M.tables.subcontroller.SPTP.M_Table_SPTP_GeneralParameters;
|
import art.library.model.devices.colors.controller.M.tables.subcontroller.SPTP.M_Table_SPTP_TimetableLocalPreferenceOperation;
|
import art.servers.ColorsServer;
|
import art.servers.colorsserver.Shared;
|
import art.servers.colorsserver.controller.ControllerController;
|
import java.io.InputStream;
|
import java.io.OutputStream;
|
import java.net.Socket;
|
import java.util.ArrayList;
|
import java.util.List;
|
|
|
public class M_ControllerComunicator extends Thread
|
{
|
// private Socket socket = null;
|
// private InputStream is = null;
|
// private OutputStream os = null;
|
private boolean hasCRC = false;
|
private M_Controller m_controller = null;
|
private ControllerController controllerController = null;
|
|
|
public M_ControllerComunicator(M_Controller m_controller, ControllerController controllerController)
|
{
|
this.m_controller = m_controller;
|
this.controllerController = controllerController;
|
// this.socket = socket;
|
// try{this.socket.setSoTimeout(15000);} catch (Exception e){};
|
// try{this.is = socket.getInputStream();} catch (Exception e){};
|
// try{this.os = socket.getOutputStream();} catch (Exception e){};
|
this.hasCRC = controllerController.hasCRC;
|
}
|
|
|
|
|
public void run()
|
{
|
}
|
|
|
|
public void writeSPTPTables(int subcontroller, String configurationCode, String controllerNumber, M_SPTP_Configuration configuration) throws Exception
|
{
|
try
|
{
|
startRecordingTables(configurationCode, controllerNumber);
|
|
if (this.m_controller.getDeviceInformation().type == M_ControllerInformation.TYPE_DYNAMIC)
|
{
|
writeTable(subcontroller, configuration, 0x4A);
|
writeTable(subcontroller, configuration, 0x4B);
|
writeTable(subcontroller, configuration, 0x4C);
|
writeTable(subcontroller, configuration, 0x4D);
|
}
|
else
|
{
|
writeTable(subcontroller, configuration, M_Table.M_TABLE_SPTP_GENERAL_PARAMETERS);
|
writeTable(subcontroller, configuration, M_Table.M_TABLE_SPTP_BUS_LINE_AREA_CONFIGURATION);
|
writeTable(subcontroller, configuration, M_Table.M_TABLE_SPTP_BUS_LOCAL_PREFERENCE_OPERATION);
|
writeTable(subcontroller, configuration, M_Table.M_TABLE_SPTP_TIMETABLE_LOCAL_PREFERENCE_OPERATION);
|
}
|
|
}
|
catch (Exception e)
|
{
|
Shared.printstack("Comunicator", e);
|
throw e;
|
}
|
finally
|
{
|
endRecordingTables();
|
}
|
}
|
|
|
public void writeTable(int subcontroller, M_SPTP_Configuration configuration, int tableCode) throws Exception
|
{
|
List<Byte> lTableContent = new ArrayList<Byte>();
|
switch (tableCode)
|
{
|
case M_Table.M_TABLE_SPTP_GENERAL_PARAMETERS:
|
{
|
M_Table_SPTP_GeneralParameters m_table = new M_Table_SPTP_GeneralParameters();
|
byte[] data = m_table.serialize(subcontroller, configuration);
|
for (int i=0; i<data.length; i++) lTableContent.add(new Byte(data[i]));
|
}
|
break;
|
|
case M_Table.M_TABLE_SPTP_BUS_LINE_AREA_CONFIGURATION:
|
{
|
M_Table_SPTP_BusLineAreaConfiguration m_table = new M_Table_SPTP_BusLineAreaConfiguration();
|
byte[] data = m_table.serialize(subcontroller, configuration);
|
for (int i=0; i<data.length; i++) lTableContent.add(new Byte(data[i]));
|
|
if (ColorsServer.TEST_SPTP == true)
|
{
|
byte[] res = new byte[data.length-4];
|
System.arraycopy(data, 3, res, 0, res.length);
|
List<M_SPTP_Configuration_BusLineAreaConfiguration> l = new M_Table_SPTP_BusLineAreaConfiguration().deserialize(res);
|
Shared.println("Comunicator", ">>>>>>>>>>>>>>>>>>>>>>>> " + Serialization.toPrettyString(l));
|
}
|
}
|
break;
|
|
case M_Table.M_TABLE_SPTP_BUS_LOCAL_PREFERENCE_OPERATION:
|
{
|
M_Table_SPTP_BusLocalPreferenceOperation m_table = new M_Table_SPTP_BusLocalPreferenceOperation();
|
byte[] data = m_table.serialize(subcontroller, configuration);
|
for (int i=0; i<data.length; i++) lTableContent.add(new Byte(data[i]));
|
|
if (ColorsServer.TEST_SPTP == true)
|
{
|
byte[] res = new byte[data.length-4];
|
System.arraycopy(data, 3, res, 0, res.length);
|
List<M_SPTP_ConfigurationBusLocalPreferenceOperation> l = new M_Table_SPTP_BusLocalPreferenceOperation().deserialize(res);
|
Shared.println("Comunicator", ">>>>>>>>>>>>>>>>>>>>>>>> " + Serialization.toPrettyString(l));
|
}
|
}
|
break;
|
|
case M_Table.M_TABLE_SPTP_TIMETABLE_LOCAL_PREFERENCE_OPERATION:
|
{
|
M_Table_SPTP_TimetableLocalPreferenceOperation m_table = new M_Table_SPTP_TimetableLocalPreferenceOperation();
|
byte[] data = m_table.serialize(subcontroller, configuration);
|
for (int i=0; i<data.length; i++) lTableContent.add(new Byte(data[i]));
|
|
if (ColorsServer.TEST_SPTP == true)
|
{
|
byte[] res = new byte[data.length-4];
|
System.arraycopy(data, 3, res, 0, res.length);
|
List<M_SPTP_ConfigurationTimetableLocalPreferenceOperation> l = new M_Table_SPTP_TimetableLocalPreferenceOperation().deserialize(res);
|
Shared.println("Comunicator", ">>>>>>>>>>>>>>>>>>>>>>>> " + Serialization.toPrettyString(l));
|
}
|
}
|
break;
|
|
case 0x4A:
|
{
|
M_Table_SPTP_GeneralParameters m_table = new M_Table_SPTP_GeneralParameters();
|
byte[] data = m_table.serialize(subcontroller, configuration);
|
data[2] = (byte)(0xCA);
|
for (int i=0; i<data.length; i++) lTableContent.add(new Byte(data[i]));
|
}
|
break;
|
|
case 0x4B:
|
{
|
M_Table_SPTP_BusLineAreaConfiguration m_table = new M_Table_SPTP_BusLineAreaConfiguration();
|
byte[] data = m_table.serialize(subcontroller, configuration);
|
data[2] = (byte)(0xCB);
|
for (int i=0; i<data.length; i++) lTableContent.add(new Byte(data[i]));
|
}
|
break;
|
|
case 0x4C:
|
{
|
M_Table_SPTP_BusLocalPreferenceOperation m_table = new M_Table_SPTP_BusLocalPreferenceOperation();
|
byte[] data = m_table.serialize(subcontroller, configuration);
|
data[2] = (byte)(0xCC);
|
for (int i=0; i<data.length; i++) lTableContent.add(new Byte(data[i]));
|
}
|
break;
|
|
case 0x4D:
|
{
|
M_Table_SPTP_TimetableLocalPreferenceOperation m_table = new M_Table_SPTP_TimetableLocalPreferenceOperation();
|
byte[] data = m_table.serialize(subcontroller, configuration);
|
data[2] = (byte)(0xCD);
|
for (int i=0; i<data.length; i++) lTableContent.add(new Byte(data[i]));
|
}
|
break;
|
}
|
|
if (lTableContent.size() <= 0) return;
|
|
M_Message message = M_ProtocolConstructor.writeTableMessage(subcontroller, lTableContent, hasCRC);
|
byte[] cmd = message.toByteArray();
|
String s = "Write table (" + tableCode + ") >> ";
|
for (int i=0; i<cmd.length; i++)
|
{
|
s += String.format(" %02X", cmd[i]);
|
}
|
Shared.traceInformation("Comunicator", s);
|
|
if (ColorsServer.TEST_SPTP == false)
|
{
|
// M_Message response = read(command);
|
M_Request request = new M_Request();
|
request.controller = this.controllerController;
|
request.request = message;
|
|
this.controllerController.getControllerConnection().addM_Request(request);
|
request.waitResponse(this.m_controller.getDeviceInformation().timeout);
|
M_Message response = request.response;
|
|
boolean salir = false;
|
while (salir == false)
|
{
|
if (M_ProtocolAnalyser.isACKMessage(response) == true)
|
{
|
salir = true;
|
}
|
else if (M_ProtocolAnalyser.isNACKMessage(response) == true)
|
{
|
throw new Exception(Shared.getMessage("Error sending table %1 (nack)").replaceAll("%1", "" + tableCode));
|
}
|
else
|
{
|
// response = read(new byte[0]);
|
request = new M_Request();
|
request.controller = this.controllerController;
|
request.request = message;
|
|
this.controllerController.getControllerConnection().addM_Request(request);
|
request.waitResponse(this.m_controller.getDeviceInformation().timeout);
|
response = request.response;
|
}
|
}
|
}
|
}
|
|
|
public void writeEmptyTable(int controller, int tableCode) throws Exception
|
{
|
List<Byte> lTableContent = new ArrayList<Byte>();
|
M_TableWriter writer = new M_TableWriter();
|
writer.STX();
|
writer.write(controller);
|
writer.write(tableCode);
|
writer.EOB12();
|
writer.ETX();
|
byte[] content = writer.toByteArray();
|
for (int i=0; i<content.length; i++)
|
lTableContent.add(new Byte(content[i]));
|
|
M_Message message = M_ProtocolConstructor.writeTableMessage(controller, lTableContent, hasCRC);
|
// M_Message response = read(command);
|
M_Request request = new M_Request();
|
request.controller = this.controllerController;
|
request.request = message;
|
|
this.controllerController.getControllerConnection().addM_Request(request);
|
request.waitResponse(this.m_controller.getDeviceInformation().timeout);
|
M_Message response = request.response;
|
|
boolean salir = false;
|
while (salir == false)
|
{
|
if (M_ProtocolAnalyser.isACKMessage(response) == true)
|
{
|
salir = true;
|
}
|
else if (M_ProtocolAnalyser.isNACKMessage(response) == true)
|
{
|
throw new Exception(Shared.getMessage("Error sending table %1 (nack)").replaceAll("%1", "" + tableCode));
|
}
|
else
|
{
|
// response = read(new byte[0]);
|
request = new M_Request();
|
request.controller = this.controllerController;
|
request.request = message;
|
|
this.controllerController.getControllerConnection().addM_Request(request);
|
request.waitResponse(this.m_controller.getDeviceInformation().timeout);
|
response = request.response;
|
}
|
}
|
}
|
|
|
|
|
private void startRecordingTables(String configurationCode, String controllerNumber) throws Exception
|
{
|
M_Message message = M_ProtocolConstructor.startRecordingTablesMessage(0x00, configurationCode, controllerNumber, hasCRC);
|
byte[] cmd = message.toByteArray();
|
String s = "Start recording >> ";
|
for (int i=0; i<cmd.length; i++)
|
{
|
s += String.format(" %02X", cmd[i]);
|
}
|
Shared.traceInformation("Comunicator", s);
|
|
if (ColorsServer.TEST_SPTP == false)
|
{
|
// M_Message response = read(command);
|
M_Request request = new M_Request();
|
request.controller = this.controllerController;
|
request.request = message;
|
|
this.controllerController.getControllerConnection().addM_Request(request);
|
request.waitResponse(this.m_controller.getDeviceInformation().timeout);
|
M_Message response = request.response;
|
|
boolean salir = false;
|
while (salir == false)
|
{
|
if (M_ProtocolAnalyser.isStartRecordingResponse(response) == true)
|
{
|
salir = true;
|
}
|
else if (M_ProtocolAnalyser.isNACKMessage(response) == true)
|
{
|
throw new Exception(Shared.getMessage("Error starting recording (nack)"));
|
}
|
else
|
{
|
// response = read(new byte[0]);
|
request = new M_Request();
|
request.controller = this.controllerController;
|
request.request = message;
|
|
this.controllerController.getControllerConnection().addM_Request(request);
|
request.waitResponse(this.m_controller.getDeviceInformation().timeout);
|
response = request.response;
|
}
|
}
|
}
|
}
|
|
|
private void endRecordingTables() throws Exception
|
{
|
M_Message message = M_ProtocolConstructor.endRecordingTablesMessage(0x00, hasCRC);
|
byte[] cmd = message.toByteArray();
|
controllerController.getControllerConnection().socket.setSoTimeout(60000);
|
String s = "End recording >> ";
|
for (int i=0; i<cmd.length; i++)
|
{
|
s += String.format(" %02X", cmd[i]);
|
}
|
Shared.traceInformation("Comunicator", s);
|
|
if (ColorsServer.TEST_SPTP == false)
|
{
|
// M_Message response = read(command);
|
M_Request request = new M_Request();
|
request.controller = this.controllerController;
|
request.request = message;
|
|
this.controllerController.getControllerConnection().addM_Request(request);
|
request.waitResponse(this.m_controller.getDeviceInformation().timeout);
|
M_Message response = request.response;
|
|
boolean salir = false;
|
while (salir == false)
|
{
|
if (M_ProtocolAnalyser.isEndRecordingResponse(response) == true)
|
{
|
M_ProtocolAnalyser.analyseEndRecordingResponse(response);
|
salir = true;
|
}
|
else if (M_ProtocolAnalyser.isNACKMessage(response) == true)
|
{
|
throw new Exception(Shared.getMessage("End recording response is NACK"));
|
}
|
else
|
{
|
// response = read(new byte[0]);
|
request = new M_Request();
|
request.controller = this.controllerController;
|
request.request = message;
|
|
this.controllerController.getControllerConnection().addM_Request(request);
|
request.waitResponse(this.m_controller.getDeviceInformation().timeout);
|
response = request.response;
|
}
|
}
|
}
|
}
|
|
|
// private M_Message read(byte[] command) throws Exception
|
// {
|
// try
|
// {
|
// if (command.length > 0)
|
// send(command);
|
// return receive();
|
// }
|
// catch (Exception e)
|
// {
|
// throw e;
|
// }
|
// }
|
//
|
//
|
//
|
// private void send(byte[] command) throws Exception
|
// {
|
// socket.getOutputStream().write(command);
|
// socket.getOutputStream().flush();
|
// }
|
//
|
//
|
//
|
// private M_Message receive() throws Exception
|
// {
|
// boolean salir = false;
|
// int res = 0;
|
// byte[] trama = null;
|
//
|
// int longitud = 0;
|
//
|
// try
|
// {
|
// while ((res = is.read()) == 0){sleep(10);};
|
//
|
// if (res != -1)
|
// {
|
// if ((res == 0x06) || (res == 0x15) || ((res >= 0x30) && (res <= 0x3F)))
|
// {
|
// M_Message message = new M_Message();
|
// int[] result = new int[1];
|
// result[0] = res;
|
// message.setData(result);
|
// return(message);
|
// }
|
//
|
// int apuntador = 0;
|
// trama = new byte[4096];
|
// trama[apuntador] = (byte)res;
|
// apuntador++;
|
//
|
// // Obtenemos todos los datos disponibles
|
// while (res != 0x03)
|
// {
|
// res = is.read();
|
// trama[apuntador] = (byte)res;
|
// apuntador++;
|
// }
|
//
|
// longitud = apuntador;
|
// }
|
// }
|
// catch (Exception e)
|
// {
|
// throw e;
|
// }
|
//
|
// if (res == -1)
|
// throw new Exception(Shared.getMessage("Timeout Error"));
|
//
|
// // Enviamos el resultado
|
// byte[] resultado = new byte[longitud];
|
// System.arraycopy(trama, 0, resultado, 0, longitud);
|
// M_Message message = new M_Message();
|
// message.setData(resultado);
|
// return(message);
|
// }
|
|
}
|