Alejandro Acuña
2024-12-18 44b33e24b644459038edd956cfce7345ce3236c1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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;
        }        
    }    
    
    
    
}