package art.servers.rtzserver.controller.RTZ32;
|
|
import art.library.model.devices.colors.controller.RTZ32.RTZ32_Controller;
|
import art.library.utils.synchro.Mutex;
|
import art.servers.Shared;
|
import java.net.InetSocketAddress;
|
import java.net.Socket;
|
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_current = null;
|
public RTZ32_Controller rtz32_clon = 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;
|
|
|
|
|
public RTZ32_Talker(String name, RTZ32_Controller rtz32_current, RTZ32_Controller rtz32_clon)
|
{
|
this.name = name;
|
this.rtz32_current = rtz32_current;
|
this.rtz32_clon = rtz32_clon;
|
this.connect();
|
|
threadReadData = new Thread(new Runnable()
|
{
|
public void run()
|
{
|
readData();
|
}
|
});
|
|
|
threadReadControl = new Thread(new Runnable()
|
{
|
public void run()
|
{
|
readControl();
|
}
|
});
|
|
|
threadReadData.start();
|
threadReadControl.start();
|
}
|
|
|
|
public void setListener(RTZ32_Talker_Listener listener)
|
{
|
this.listener = listener;
|
}
|
|
|
|
public RTZ32_Controller getRTZ32()
|
{
|
return rtz32_clon;
|
}
|
|
|
|
private void readData()
|
{
|
while (threadReadData.isInterrupted() == false)
|
{
|
try
|
{
|
if (isCentralized() == true)
|
{
|
connect();
|
readerData.read(rtz32_current, rtz32_clon);
|
}
|
else
|
{
|
disconnect();
|
}
|
}
|
catch (SocketTimeoutException exception)
|
{
|
// Timeout
|
// Nothing to do
|
}
|
catch (Exception exception)
|
{
|
Shared.println(name, exception.getMessage());
|
|
try
|
{
|
threadReadData.sleep(5000);
|
}
|
catch (Exception e)
|
{
|
}
|
|
disconnect();
|
connect();
|
}
|
}
|
}
|
|
|
|
private void readControl()
|
{
|
while (threadReadControl.isInterrupted() == false)
|
{
|
try
|
{
|
if (isCentralized() == true)
|
{
|
connect();
|
readerControl.read();
|
}
|
else
|
{
|
disconnect();
|
}
|
}
|
catch (SocketTimeoutException exception)
|
{
|
// Timeout
|
// Nothing to do
|
}
|
catch (Exception exception)
|
{
|
Shared.println(name, exception.getMessage());
|
|
try
|
{
|
threadReadControl.sleep(5000);
|
}
|
catch (Exception e)
|
{
|
}
|
|
disconnect();
|
connect();
|
}
|
}
|
}
|
|
|
|
|
|
|
//<editor-fold desc="Connection">
|
|
|
|
private Mutex mutexConnection = new Mutex();
|
|
|
|
|
private boolean isCentralized()
|
{
|
try
|
{
|
return (rtz32_current.getDeviceStatus().rtz32.isCentralized());
|
}
|
catch (Exception exception)
|
{
|
}
|
|
return true;
|
}
|
|
|
public boolean isConnected()
|
{
|
return ((socketData != null) && (socketData.isConnected() == true) && (socketControl != null) && (socketControl.isConnected() == true));
|
}
|
|
|
private void connect()
|
{
|
mutexConnection.lockWrite();
|
{
|
try
|
{
|
if ((isConnected() == false) && (isCentralized() == true))
|
{
|
disconnect();
|
|
Shared.println(name, Shared.getMessage("Connecting"));
|
|
socketData = new Socket();
|
socketData.connect(new InetSocketAddress(rtz32_current.getDeviceInformation().connection.address, rtz32_current.getDeviceInformation().connection.dataPort), rtz32_current.getDeviceInformation().connection.connectionTimeout);
|
socketData.setSoTimeout(rtz32_current.getDeviceInformation().connection.connectionTimeout);
|
|
socketControl = new Socket();
|
socketControl.connect(new InetSocketAddress(rtz32_current.getDeviceInformation().connection.address, rtz32_current.getDeviceInformation().connection.controlPort), rtz32_current.getDeviceInformation().connection.connectionTimeout);
|
socketControl.setSoTimeout(rtz32_current.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"));
|
|
if (listener != null) listener.connected();
|
|
}
|
}
|
catch (Exception exception)
|
{
|
Shared.println(name, exception.getMessage());
|
disconnect();
|
}
|
}
|
mutexConnection.releaseWrite();
|
}
|
|
|
|
|
protected void disconnect()
|
{
|
try
|
{
|
socketData.close();
|
}
|
catch (Exception exception)
|
{
|
}
|
|
try
|
{
|
socketControl.close();
|
}
|
catch (Exception exception)
|
{
|
}
|
|
socketData = null;
|
socketControl = null;
|
readerData = null;
|
writer = null;
|
if (listener != null) listener.disconnected();
|
}
|
|
|
|
|
|
//</editor-fold>
|
|
|
|
|
}
|