package art.servers.sosserver.controller.musatel; import art.library.model.devices.sos.Sos; import art.servers.Shared; import art.servers.controller.Controller; import art.servers.controller.ControllerDevice; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.util.concurrent.Executors; import java.util.concurrent.ThreadPoolExecutor; public class ListenerSos_Musatel extends Controller { private int port = 0; private String name = null; private ThreadPoolExecutor executor = null; public ListenerSos_Musatel(int port) { this.port = port; this.name = Shared.getMessage("Common listener" + ":" + port); } public int getPort() { return port; } public void run() { Shared.traceInformation(name, "Starting"); executor = (ThreadPoolExecutor)Executors.newCachedThreadPool(); while (isInterrupted() == false) { try { DatagramSocket socket = new DatagramSocket(port); byte[] receive = new byte[8192]; while (true) { DatagramPacket notification = new DatagramPacket(receive, receive.length); socket.receive(notification); if (notification.getLength() > 0) { Listener listener = new Listener(notification); executor.execute(listener); } } } catch (Exception e) { } } executor.shutdown(); Shared.traceInformation(name, "Finishing"); } public class Listener implements Runnable // Thread { private DatagramPacket notification = null; private ControllerSos_Musatel controller = null; private InputStream input = null; public Listener(DatagramPacket notification) { this.notification = notification; } public void run() { this.controller = getDeviceController(notification.getAddress().getHostAddress()); if (controller == null) { return; } Shared.println(name, notification.getAddress().getHostAddress()); this.input = new ByteArrayInputStream(notification.getData(), 0, notification.getLength()); manage(); } private void manage() { try { int address = input.read(); int function = input.read(); String command = "<"; command = command + " " + String.format("%02X", address); command = command + " " + String.format("%02X", function); Shared.println(name, command); if (controller != null) { switch (function) { case MusatelProtocol.DEM_USE: controller.receiver.DEM_USE(input.read()); break; case MusatelProtocol.DEM_SER: controller.receiver.DEM_SER(input.read()); break; case MusatelProtocol.ENV_ALA : controller.receiver.ENV_ALA(input.read(), input.read()); break; case MusatelProtocol.ACK_FON_ON : controller.receiver.ACK_FON_ON(input.read()); break; case MusatelProtocol.ACK_FON_OFF : controller.receiver.ACK_FON_OFF(input.read()); break; case MusatelProtocol.ACK_TON_ON : controller.receiver.ACK_TON_ON(input.read()); break; case MusatelProtocol.ACK_TON_OFF : controller.receiver.ACK_TON_OFF(input.read()); break; case MusatelProtocol.RES_TST_FON : controller.receiver.RES_TST_FON(input.read(), input.read()); break; case MusatelProtocol.RES_TST_MAN : controller.receiver.RES_TST_MAN(input); break; case MusatelProtocol.ACK_VOZ_ON : controller.receiver.ACK_VOZ_ON(input.read()); break; case MusatelProtocol.ACK_VOZ_OFF : controller.receiver.ACK_VOZ_OFF(input.read()); break; case MusatelProtocol.ACK_ADJ_VOL : controller.receiver.ACK_ADJ_VOL(input.read(), input.read()); break; case MusatelProtocol.NACK : controller.receiver.NACK(input.read()); break; default : controller.receiver.UNKNOWN(function); break; } } else { Shared.println(name, Shared.getMessage("Unknown address") + ", " + notification.getAddress().getHostAddress()); } int CRC1 = input.read(); int CRC2 = input.read(); } catch (Exception e) { e.printStackTrace(); } } private ControllerSos_Musatel getDeviceController (String address) { try { for (ControllerDevice controller : Shared.getDeviceControllers()) { Sos sos = controller.getDevice(); if (sos.getDeviceInformation().musatel.connection.address.equalsIgnoreCase(address) == true) { return (ControllerSos_Musatel)controller; } } } catch (Exception exception) { } return null; } } }