package art.servers.rtzserver.controller.RTZ32;
|
|
import art.library.interop.serialization.Serialization;
|
import art.library.model.devices.colors.controller.RTZ32.RTZ32_Controller;
|
import art.library.utils.synchro.Mutex;
|
import art.servers.Shared;
|
import art.servers.rtzserver.controller.Controller_RTZ32;
|
import java.net.InetSocketAddress;
|
import java.net.Socket;
|
import java.net.SocketException;
|
import java.net.SocketTimeoutException;
|
|
|
|
|
public class RTZ32_Talker
|
{
|
public static final byte STX = 0x02;
|
public static final byte ETX = 0x03;
|
public static final byte DC1 = 0x11;
|
public static final byte DC3 = 0x13;
|
public static final byte ACK = 0x06;
|
public static final byte NACK = 0x15;
|
public static final byte DET = 0x20;
|
public static final byte HTR = 0x33;
|
public static final byte SYNC = 0x40;
|
public static final byte SYNCD = 0x50;
|
public static final byte SCYCLE = 0x60;
|
|
protected String name = null;
|
protected RTZ32_Talker_Listener listener = null;
|
protected Thread threadReadData = null;
|
protected Thread threadReadControl = null;
|
protected RTZ32_Talker_Reader_Data readerData = null;
|
protected RTZ32_Talker_Reader_Control readerControl = null;
|
protected Socket socketData = null;
|
protected Socket socketControl = null;
|
public RTZ32_Talker_Writer writer = null;
|
public RTZ32_Controller rtz32 = null;
|
|
|
|
protected Mutex mutex_PET_DETECTORES = null;
|
protected Mutex mutex_PET_PLAN_DESFASE = null;
|
protected Mutex mutex_PET_ESTADO = null;
|
protected Mutex mutex_PET_ALARMAS = null;
|
protected Mutex mutex_PET_FH = null;
|
protected Mutex mutex_PET_CONTADORES = null;
|
protected Mutex mutex_PET_GRUPO = null;
|
protected Mutex mutex_PET_TABLA = null;
|
protected Mutex mutex_PET_VERSION = null;
|
protected Mutex mutex_PET_CAMBIO_FASE = null;
|
protected Mutex mutex_PET_TIEMPO_REAL = null;
|
|
protected Mutex mutex_ORD_TABLA = null;
|
protected Mutex mutex_ORD_GRABACION = null;
|
|
private Controller_RTZ32 controllerRTZ32 = null;
|
|
|
public RTZ32_Talker(String name, RTZ32_Controller rtz32, Controller_RTZ32 controllerRTZ32)
|
{
|
this.name = name;
|
this.rtz32 = Serialization.clone(rtz32);
|
this.rtz32.realtime = rtz32.realtime;
|
this.controllerRTZ32 = controllerRTZ32;
|
this.connect();
|
|
threadReadData = new Thread(new Runnable()
|
{
|
public void run()
|
{
|
if (rtz32.getDeviceInformation().connection.address == null)
|
{
|
try{threadReadData.sleep(15000);} catch (Exception e){};
|
}
|
else
|
{
|
readData();
|
}
|
}
|
});
|
|
|
threadReadControl = new Thread(new Runnable()
|
{
|
public void run()
|
{
|
if (rtz32.getDeviceInformation().connection.address == null)
|
{
|
try{threadReadControl.sleep(15000);} catch (Exception e){};
|
}
|
else
|
{
|
readControl();
|
}
|
}
|
});
|
|
threadReadData.setName("Reader data, " + name);
|
threadReadData.start();
|
threadReadData.setName("Reader control, " + name);
|
threadReadControl.start();
|
}
|
|
|
|
public void setListener(RTZ32_Talker_Listener listener)
|
{
|
this.listener = listener;
|
}
|
|
|
|
|
|
private void readData()
|
{
|
while (threadReadData.isInterrupted() == false)
|
{
|
try
|
{
|
if ((rtz32.getDeviceInformation().connection.address != null) && (isCentralized() == true))
|
{
|
connect();
|
readerData.read(rtz32, controllerRTZ32);
|
}
|
else
|
{
|
disconnect(1);
|
}
|
}
|
catch (SocketTimeoutException exception)
|
{
|
// Timeout
|
// Nothing to do
|
}
|
catch (SocketException exception)
|
{
|
// Timeout
|
// Nothing to do
|
Shared.println(name, exception.getMessage());
|
|
try
|
{
|
threadReadData.sleep(5000);
|
}
|
catch (Exception e)
|
{
|
}
|
|
disconnect(2);
|
connect();
|
}
|
catch (Exception exception)
|
{
|
// Shared.println(name, exception.getMessage());
|
//
|
// try
|
// {
|
// threadReadData.sleep(5000);
|
// }
|
// catch (Exception e)
|
// {
|
// }
|
//
|
// disconnect();
|
// connect();
|
|
rtz32.setAlarm("alarm_controller_offline", true);
|
}
|
}
|
}
|
|
|
|
private void readControl()
|
{
|
while (threadReadControl.isInterrupted() == false)
|
{
|
try
|
{
|
if ((rtz32.getDeviceInformation().connection.address != null) && (isCentralized() == true))
|
{
|
connect();
|
readerControl.read();
|
}
|
else
|
{
|
disconnect(3);
|
}
|
}
|
catch (SocketTimeoutException exception)
|
{
|
// Timeout
|
// Nothing to do
|
}
|
catch (SocketException exception)
|
{
|
Shared.println(name, exception.getMessage());
|
|
try
|
{
|
threadReadControl.sleep(5000);
|
}
|
catch (Exception e)
|
{
|
}
|
|
disconnect(4);
|
connect();
|
}
|
catch (Exception exception)
|
{
|
// Shared.println(name, exception.getMessage());
|
//
|
// try
|
// {
|
// threadReadControl.sleep(5000);
|
// }
|
// catch (Exception e)
|
// {
|
// }
|
//
|
// disconnect();
|
// connect();
|
|
rtz32.setAlarm("alarm_controller_offline", true);
|
}
|
}
|
}
|
|
//<editor-fold desc="Connection">
|
|
|
|
private Mutex mutexConnection = new Mutex();
|
|
|
|
private boolean isCentralized()
|
{
|
try
|
{
|
return (rtz32.getDeviceStatus().rtz32.isCentralized());
|
}
|
catch (Exception exception)
|
{
|
}
|
|
return true;
|
}
|
|
|
public boolean isConnected()
|
{
|
boolean connected = ((socketData != null) && (socketData.isConnected() == true) && (socketControl != null) && (socketControl.isConnected() == true));
|
|
if ((rtz32.getDeviceInformation().number <= 9003) && (connected == false))
|
{
|
Shared.println(name, Shared.getMessage("Not connected") + " : " + rtz32.getDeviceInformation().connection.address + ":" + rtz32.getDeviceInformation().connection.dataPort);
|
}
|
|
return connected;
|
}
|
|
|
public void connect()
|
{
|
if (rtz32.getDeviceInformation().connection.address == null) return;
|
|
mutexConnection.lockWrite();
|
{
|
try
|
{
|
if ((isConnected() == false) && (isCentralized() == true))
|
{
|
disconnect(5);
|
|
if (rtz32.getDeviceInformation().number <= 9003)
|
{
|
Shared.println(name, Shared.getMessage("Connecting") + " : " + rtz32.getDeviceInformation().connection.address + ":" + rtz32.getDeviceInformation().connection.dataPort);
|
}
|
|
socketData = new Socket();
|
socketData.connect(new InetSocketAddress(rtz32.getDeviceInformation().connection.address, rtz32.getDeviceInformation().connection.dataPort), rtz32.getDeviceInformation().connection.connectionTimeout);
|
socketData.setSoTimeout(rtz32.getDeviceInformation().connection.connectionTimeout);
|
|
socketControl = new Socket();
|
socketControl.connect(new InetSocketAddress(rtz32.getDeviceInformation().connection.address, rtz32.getDeviceInformation().connection.controlPort), rtz32.getDeviceInformation().connection.connectionTimeout);
|
socketControl.setSoTimeout(rtz32.getDeviceInformation().connection.connectionTimeout);
|
|
writer = new RTZ32_Talker_Writer(this, socketData, socketControl);
|
readerData = new RTZ32_Talker_Reader_Data(this, socketData, socketControl);
|
readerControl = new RTZ32_Talker_Reader_Control(this, socketControl);
|
|
Shared.println(name, Shared.getMessage("Connected"));
|
|
rtz32.setAlarm("alarm_offline", false);
|
rtz32.setAlarm("alarm_controller_offline", false);
|
if (listener != null) listener.connected();
|
|
if (rtz32.getDeviceInformation().number <= 9003)
|
{
|
Shared.println(name, Shared.getMessage("Connected") + " : " + rtz32.getDeviceInformation().connection.address + ":" + rtz32.getDeviceInformation().connection.dataPort);
|
}
|
}
|
}
|
catch (Exception exception)
|
{
|
if (rtz32.getDeviceInformation().number <= 9003)
|
{
|
Shared.printstack(name, exception);
|
}
|
|
disconnect(6);
|
}
|
}
|
mutexConnection.releaseWrite();
|
}
|
|
|
|
|
public void disconnect(int n)
|
{
|
if (rtz32.getDeviceInformation().number <= 9003)
|
{
|
Shared.println(name, Shared.getMessage("Disconnecting") + " (" + n + ") : " + rtz32.getDeviceInformation().connection.address + ":" + rtz32.getDeviceInformation().connection.dataPort);
|
}
|
try
|
{
|
socketData.close();
|
}
|
catch (Exception exception)
|
{
|
}
|
|
try
|
{
|
socketControl.close();
|
}
|
catch (Exception exception)
|
{
|
}
|
|
socketData = null;
|
socketControl = null;
|
readerData = null;
|
writer = null;
|
rtz32.setAlarm("alarm_offline", true);
|
if (listener != null) listener.disconnected();
|
}
|
|
|
|
|
|
//</editor-fold>
|
|
|
|
|
}
|