package art.servers.sosserver.controller.musatel;
|
|
import art.library.interop.serialization.Serialization;
|
import art.library.model.devices.sos.Sos;
|
import art.library.model.devices.sos.musatel.information.SosInformationMusatelConnection;
|
import art.library.model.devices.sos.musatel.status.SosStatusMusatelConnection;
|
import art.library.utils.synchro.Mutex;
|
import art.servers.Shared;
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.io.OutputStream;
|
import java.net.InetAddress;
|
import java.net.InetSocketAddress;
|
import java.net.Socket;
|
import java.net.SocketAddress;
|
|
|
|
|
public class ControllerSos_MusatelSender
|
{
|
private ControllerSos_Musatel controller = null;
|
private Socket socket = null;
|
private InputStream is = null;
|
private OutputStream os = null;
|
private Mutex mutex = new Mutex();
|
private String name = null;
|
|
|
public ControllerSos_MusatelSender(ControllerSos_Musatel controller)
|
{
|
this.controller = controller;
|
this.name = controller.getName();
|
}
|
|
|
|
// <editor-fold defaultstate="collapsed" desc="Actions">
|
|
protected void ORD_FON_ON (String username, String computer, String computerAddress, int audioPort, int priority) throws Exception
|
{
|
Sos clone = (Sos)Serialization.clone(controller.getDevice());
|
byte[] address = MusatelProtocol.getIpArray(computerAddress);
|
Shared.println(name, "> ORD_FON_ON " + computerAddress + ":" + audioPort);
|
function(MusatelProtocol.ORD_FON_ON, new int[]{address[0], address[1], address[2], address[3],(byte)(audioPort & 0xFF),(byte)((audioPort >> 8) & 0xFF)});
|
|
SosStatusMusatelConnection connection = new SosStatusMusatelConnection();
|
clone.getDeviceStatus().musatel.currentConnection = connection;
|
connection.timestamp = System.currentTimeMillis();
|
connection.user = username;
|
connection.computer = computer;
|
connection.address = computerAddress;
|
connection.audioPort = audioPort;
|
connection.priority = priority;
|
controller.setSosPriority(priority);
|
|
Shared.model.updateDevice(controller.getDevice(), clone);
|
}
|
|
|
protected void ORD_FON_ON (String computerAddress, int audioPort) throws Exception
|
{
|
Shared.println(name, "> ORD_FON_ON " + computerAddress + ":" + audioPort);
|
byte[] address = MusatelProtocol.getIpArray(computerAddress);
|
function(MusatelProtocol.ORD_FON_ON, new int[]{address[0], address[1], address[2], address[3],(byte)(audioPort & 0xFF),(byte)((audioPort >> 8) & 0xFF)});
|
}
|
|
|
protected void ORD_FON_OFF () throws Exception
|
{
|
Shared.println(name, "> ORD_FON_OFF");
|
function(MusatelProtocol.ORD_FON_OFF);
|
}
|
|
|
protected void ORD_TON_ON (String computerAddress, int audioPort, int type) throws Exception
|
{
|
Shared.println(name, "> ORD_TON_ON " + computerAddress + ":" + audioPort);
|
byte[] address = MusatelProtocol.getIpArray(computerAddress);
|
function(MusatelProtocol.ORD_TON_ON, new int[]{type, address[0], address[1], address[2], address[3],(byte)(audioPort & 0xFF),(byte)((audioPort >> 8) & 0xFF)});
|
}
|
|
protected void ORD_TON_OFF () throws Exception
|
{
|
Shared.println(name, "> ORD_TON_OFF");
|
function(MusatelProtocol.ORD_TON_OFF);
|
}
|
|
|
protected void ORD_TST_MAN () throws Exception
|
{
|
Shared.println(name, "> ORD_TST_MAN");
|
function(new byte[]{(byte)0x00, (byte)MusatelProtocol.ORD_TST_MAN, (byte)0xFF, (byte)0xFF});
|
}
|
|
|
protected void ORD_TST_FON (String computerAddress, int audioPort) throws Exception
|
{
|
Shared.println(name, "> ORD_TST_FON " + computerAddress + ":" + audioPort);
|
byte[] address = MusatelProtocol.getIpArray(computerAddress);
|
function(MusatelProtocol.ORD_TST_FON, new int[]{address[0], address[1], address[2], address[3],(byte)(audioPort & 0xFF),(byte)((audioPort >> 8) & 0xFF)});
|
}
|
|
|
|
protected void ORD_VOZ_ON(String username, String computer, String computerAddress, int audioPort, int message) throws Exception
|
{
|
Shared.println(name, "> ORD_VOZ_ON " + computerAddress + ":" + audioPort);
|
byte[] address = MusatelProtocol.getIpArray(computerAddress);
|
function(MusatelProtocol.ORD_FON_ON, new int[]{(byte)message, address[0], address[1], address[2], address[3],(byte)(audioPort & 0xFF),(byte)((audioPort >> 8) & 0xFF)});
|
}
|
|
|
|
protected void ORD_VOZ_OFF () throws Exception
|
{
|
Shared.println(name, "> ORD_VOZ_OFF");
|
function(MusatelProtocol.ORD_VOZ_OFF);
|
}
|
|
|
|
protected void ORD_ADJ_VOL (int level) throws Exception
|
{
|
if (level < 0) level = 0;
|
if (level > 7) level = 7;
|
Shared.println(name, "> ORD_ADJ_VOL " + Shared.getMessage("level") + " = " + level);
|
function(MusatelProtocol.ORD_ADJ_VOL, new int[]{level});
|
}
|
|
|
|
|
protected void ACK_DEM_USE() throws Exception
|
{
|
Shared.println(name, "> ACK_DEM_USE");
|
function(MusatelProtocol.ACK_DEM_USE, new int[]{MusatelProtocol.L_ESP});
|
}
|
|
|
protected void ACK_DEM_USE(String computerAddress, int audioPort) throws Exception
|
{
|
Shared.println(name, "> ACK_DEM_USE " + computerAddress + ":" + audioPort);
|
|
byte[] address = MusatelProtocol.getIpArray(computerAddress);
|
|
function(MusatelProtocol.ACK_DEM_USE, new int[]
|
{
|
MusatelProtocol.L_FON,
|
address[0],
|
address[1],
|
address[2],
|
address[3],
|
(byte)(audioPort & 0xFF),
|
(byte)((audioPort >> 8) & 0xFF)
|
});
|
}
|
|
|
|
protected void ACK_DEM_SER() throws Exception
|
{
|
Shared.println(name, "> ACK_DEM_SER");
|
function(MusatelProtocol.ACK_DEM_SER, new int[]{MusatelProtocol.L_ESP});
|
}
|
|
|
|
protected void ACK_DEM_SER(String computerAddress, int audioPort) throws Exception
|
{
|
Shared.println(name, "> ACK_DEM_SER " + computerAddress + ":" + audioPort);
|
|
byte[] address = MusatelProtocol.getIpArray(computerAddress);
|
|
function(MusatelProtocol.ACK_DEM_USE, new int[]
|
{
|
MusatelProtocol.L_FON,
|
address[0],
|
address[1],
|
address[2],
|
address[3],
|
(byte)(audioPort & 0xFF),
|
(byte)((audioPort >> 8) & 0xFF)
|
});
|
}
|
|
|
|
|
protected void ACK_ENV_ALA() throws Exception
|
{
|
Shared.println(name, "> ACK_ENV_ALA");
|
function(MusatelProtocol.ACK_ENV_ALA);
|
}
|
|
// </editor-fold>
|
|
|
|
|
// <editor-fold defaultstate="collapsed" desc="Communications">
|
|
|
private void connect () throws Exception
|
{
|
if (socket != null)
|
{
|
if (socket.isConnected() == true)
|
{
|
return;
|
}
|
}
|
|
|
disconnect();
|
|
SosInformationMusatelConnection connection = ((Sos)controller.getDevice()).getDeviceInformation().musatel.connection;
|
InetAddress inetAddress = InetAddress.getByName(connection.address);
|
SocketAddress sockaddress = new InetSocketAddress(inetAddress, connection.portSOS);
|
socket = new Socket();
|
socket.connect(sockaddress, connection.timeout);
|
socket.setSoLinger (true, 1);
|
socket.setSoTimeout(connection.timeout);
|
is = socket.getInputStream();
|
os = socket.getOutputStream();
|
}
|
|
|
|
private void function (int code) throws Exception
|
{
|
function(code, MusatelProtocol.P_MAS, new int[0]);
|
}
|
|
|
private void function (int code, int[] params) throws Exception
|
{
|
function(code, (byte)MusatelProtocol.P_MAS, params);
|
}
|
|
|
private void function (int code, int type, int[] params) throws Exception
|
{
|
byte[] send = new byte[5 + params.length];
|
send[0] = 0x00;
|
send[1] = (byte)code;
|
send[2] = (byte)type;
|
|
for (int i=0; i<params.length; i++)
|
{
|
send[3 + i] = (byte)params[i];
|
}
|
|
send[3 + params.length] = (byte)0xFF;
|
send[4 + params.length] = (byte)0xFF;
|
|
function(send);
|
}
|
|
|
|
private void function (byte[] send) throws Exception
|
{
|
mutex.lockWrite();
|
|
try
|
{
|
connect();
|
|
String command = ">";
|
|
for (int i=0; i<send.length; i++)
|
{
|
command = command + " " + String.format("%02X", send[i]);
|
}
|
|
Sos sos = controller.getDevice();
|
SosInformationMusatelConnection connection = sos.getDeviceInformation().musatel.connection;
|
socket.setSoTimeout(connection.timeout);
|
os.write(send);
|
os.flush();
|
Shared.println(controller.getName(), command);
|
}
|
catch (IOException exception)
|
{
|
disconnect();
|
throw exception;
|
}
|
catch (Exception exception)
|
{
|
throw exception;
|
}
|
finally
|
{
|
mutex.releaseWrite();
|
}
|
}
|
|
|
|
|
|
private void disconnect ()
|
{
|
try { os.close(); } catch (Exception e) { }
|
try { socket.close(); } catch (Exception e) { }
|
try { is.close(); } catch (Exception e) { }
|
|
socket = null;
|
os = null;
|
is = null;
|
}
|
|
// </editor-fold>
|
|
|
}
|