package art.servers.sosserver.controller.musatel; import art.library.interop.InteropParameters; import art.library.interop.serialization.Serialization; import art.library.interop.serialization.SerializationException; import art.library.model.devices.DeviceAction; import art.library.model.devices.DeviceActionResult; import art.library.model.devices.DeviceCommands; import art.library.model.devices.DeviceStatus; import art.library.model.devices.sos.Sos; import art.library.model.devices.sos.SosCommands; import art.library.model.devices.sos.SosStatus; import art.library.model.devices.sos.musatel.alarms.SosAlarmsMusatel; import art.library.model.devices.sos.musatel.commands.SosCommandsMusatel; import art.library.model.devices.sos.musatel.status.SosStatusMusatel; import art.library.model.devices.sos.musatel.status.SosStatusMusatelConnection; import art.library.utils.synchro.Mutex; import art.servers.ServerException; import art.servers.Shared; import art.servers.sosserver.controller.ControllerSos; import java.util.ArrayList; import java.util.List; import java.util.Random; public class ControllerSos_Musatel extends ControllerSos { private String name = null; private boolean initialised = false; private Mutex mutex = new Mutex(); protected ControllerSos_MusatelSender sender = null; protected ControllerSos_MusatelReceiver receiver = null; private int priority = -1; public ControllerSos_Musatel(Sos sos) { super(sos); this.device = sos; this.name = Shared.getMessage("Controller sos") + " " + sos.toString(); this.setName(name); this.sender = new ControllerSos_MusatelSender(this); this.receiver = new ControllerSos_MusatelReceiver(this); if (device.getDeviceStatus() == null) { SosStatus status = new SosStatus(); status.musatel = new SosStatusMusatel(); device.status = status; } if (device.getDeviceAlarms().musatel == null) { device.getDeviceAlarms().musatel = new SosAlarmsMusatel(); } } public void run() { Shared.traceInformation(name, Shared.getMessage("Starting")); // Random, to avoid all execute at same time try { Random random = new Random(); int timetowait = random.nextInt(10); if (timetowait > 0) sleep(timetowait * 1000); } catch (Exception e) { } // Start while (isInterrupted() == false) { long timestamp = System.currentTimeMillis(); { if (art.servers.Shared.isServerEnabled() == true) { update(); } } long period = device.getDeviceInformation().polling * 1000; long timetowait = period - (System.currentTimeMillis() - timestamp); timetowait = Math.min(timetowait, period); try { if (timetowait > 0) { sleep(timetowait); } } catch (Exception e) { } } Shared.traceInformation(name, Shared.getMessage("Finishing")); } private void offline(Sos clone)throws Exception { clone.getDeviceStatus().status = DeviceStatus.STATUS_OFFLINE; long tsOffline = clone.getAlarm("alarm_offline"); if (tsOffline <= 0) tsOffline = System.currentTimeMillis(); clone.alarms.clear(); clone.setAlarm("alarm_offline", tsOffline); Shared.model.updateDevice(device, clone); } public void online(Sos clone) throws Exception { clone.getDeviceStatus().status = DeviceStatus.STATUS_ONLINE; clone.setAlarm("alarm_offline", false); clone.setAlarm("alarm_invalid", false); Shared.model.updateDevice(device, clone); } private void update() { try { Sos sos = getDevice(); if (sos.getDeviceStatus().musatel == null) { sos.getDeviceStatus().musatel = new SosStatusMusatel(); } // Keep connection alive if (sos.getDeviceStatus().musatel.state == SosStatusMusatel.STATE_ACTIVE_CALL) { SosStatusMusatelConnection connection = sos.getDeviceStatus().musatel.currentConnection; if (connection != null) { sender.ORD_FON_ON(connection.address, connection.audioPort); } } else { // Update state, alarms Sos clone = (Sos)Serialization.clone(getDevice()); try { sender.ORD_TST_MAN(); online(clone); } catch (Exception e) { offline(clone); } } } catch (Exception e) { } } // public DeviceAction[] sendCommands(InteropParameters parameters) throws ServerException, SerializationException, Exception { try { List result = new ArrayList(); SosCommands sosCommands = parameters.getBodyContentValue(SosCommands.class); if (sosCommands.musatel.speakerVolume >= 0) { SosCommands command = new SosCommands(); command.musatel = new SosCommandsMusatel(); command.musatel.speakerVolume = sosCommands.musatel.speakerVolume; result.add(setSpeakerVolume(parameters, command)); } if (sosCommands.musatel.call != null) { SosCommands command = new SosCommands(); command.musatel = new SosCommandsMusatel(); command.musatel.call = sosCommands.musatel.call; result.add(openAudio(parameters, command)); } if (sosCommands.musatel.endCall == DeviceCommands.CONDITION_YES) { SosCommands command = new SosCommands(); command.musatel = new SosCommandsMusatel(); command.musatel.endCall = sosCommands.musatel.endCall; result.add(closeAudio(parameters, command)); } if (sosCommands.musatel.pauseCall == DeviceCommands.CONDITION_YES) { SosCommands command = new SosCommands(); command.musatel = new SosCommandsMusatel(); command.musatel.pauseCall = sosCommands.musatel.pauseCall; result.add(pauseAudio(parameters, command)); } if (sosCommands.musatel.resumeCall == DeviceCommands.CONDITION_YES) { SosCommands command = new SosCommands(); command.musatel = new SosCommandsMusatel(); command.musatel.resumeCall = sosCommands.musatel.resumeCall; result.add(resumeAudio(parameters, command)); } if (sosCommands.musatel.maintenanceTest == DeviceCommands.CONDITION_YES) { SosCommands command = new SosCommands(); command.musatel = new SosCommandsMusatel(); command.musatel.maintenanceTest = sosCommands.musatel.maintenanceTest; result.add(doMaintenanceTest(parameters, command)); } if (sosCommands.musatel.audioTest != null) { SosCommands command = new SosCommands(); command.musatel = new SosCommandsMusatel(); command.musatel.audioTest = sosCommands.musatel.audioTest; if (command.musatel.audioTest.type == SosCommandsMusatel.TEST_AUDIO) { result.add(startAudioTest(parameters, command)); } else if (command.musatel.audioTest.type == SosCommandsMusatel.TEST_AUDIO_STOP) { result.add(endAudioTest(parameters, command)); } } if (sosCommands.musatel.messagesTest != null) { SosCommands command = new SosCommands(); command.musatel = new SosCommandsMusatel(); command.musatel.messagesTest = sosCommands.musatel.messagesTest; if ((command.musatel.messagesTest.type == SosCommandsMusatel.MESSAGE_OUT_OF_SERVICE) || (command.musatel.messagesTest.type == SosCommandsMusatel.MESSAGE_CALL_WAITING)) { result.add(startMessagesTest(parameters, command)); } else if (command.musatel.messagesTest.type == SosCommandsMusatel.MESSAGE_STOP) { result.add(endMessagesTest(parameters, command)); } } if (sosCommands.musatel.toneTest != null) { SosCommands command = new SosCommands(); command.musatel = new SosCommandsMusatel(); command.musatel.toneTest = sosCommands.musatel.toneTest; if ((command.musatel.toneTest.type == SosCommandsMusatel.TONE_CALL) || (command.musatel.toneTest.type == SosCommandsMusatel.TONE_BUSY)) { result.add(startToneTest(parameters, command)); } else if (command.musatel.toneTest.type == SosCommandsMusatel.TONE_STOP) { result.add(endToneTest(parameters, command)); } } if (sosCommands.musatel.reset == DeviceCommands.CONDITION_YES) { SosCommands command = new SosCommands(); command.musatel = new SosCommandsMusatel(); command.musatel.reset = sosCommands.musatel.reset; result.add(reset(parameters, command)); } return result.toArray(new DeviceAction[result.size()]); } catch (Exception exception) { throw exception; } } private DeviceAction startAudioTest(InteropParameters parameters, SosCommands command) { String language = (String)parameters.getParameterValue("language"); DeviceAction action = new DeviceAction(device, parameters, command); action.actionName = Shared.getMessage(language, "Start audio test"); try { sender.ORD_TST_FON(command.musatel.audioTest.audioDestinationIp, command.musatel.audioTest.audioDestinationPort); DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_CORRECT); action.setResult(actionResult); Shared.model.addAction(action); return action; } catch (Exception exception) { DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_ERROR, exception.getMessage()); action.setResult(actionResult); Shared.model.addAction(action); return action; } } private DeviceAction endAudioTest(InteropParameters parameters, SosCommands command) { String language = (String)parameters.getParameterValue("language"); DeviceAction action = new DeviceAction(device, parameters, command); action.actionName = Shared.getMessage(language, "Stop audio test"); try { sender.ORD_FON_OFF(); DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_CORRECT); action.setResult(actionResult); Shared.model.addAction(action); return action; } catch (Exception exception) { DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_ERROR, exception.getMessage()); action.setResult(actionResult); Shared.model.addAction(action); return action; } } private DeviceAction openAudio(InteropParameters parameters, SosCommands command) { // TODO: Y el volumen y el micro?, si speakerVolume > 0 enviarlo, ya se envía en sendCommands. // Volumen de micro no se puede ajustar en los musatel // TODO: La prioridad no se usa String language = (String)parameters.getParameterValue("language"); String username = (String)parameters.getParameterValue("username"); String computer = (String)parameters.getParameterValue("computer"); DeviceAction action = new DeviceAction(device, parameters, command); action.actionName = Shared.getMessage(language, "Start call"); int commandPriority = command.musatel.call.priority; try { int currentPriority = -1; if (this.device.getDeviceStatus().musatel.currentConnection != null) { currentPriority = this.device.getDeviceStatus().musatel.currentConnection.priority; } if ((commandPriority >= 0) && (commandPriority < currentPriority)) { DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_ERROR, Shared.getMessage("Insufficient priority")); action.setResult(actionResult); Shared.model.addAction(action); return action; } if (this.device.getDeviceStatus().musatel.currentConnection != null) { // Cerrar conexión anterior sender.ORD_FON_OFF(); } sender.ORD_FON_ON(username, computer, command.musatel.call.audioDestinationIp, command.musatel.call.audioDestinationPort, commandPriority); DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_CORRECT); action.setResult(actionResult); Shared.model.addAction(action); return action; } catch (Exception exception) { DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_ERROR, exception.getMessage()); action.setResult(actionResult); Shared.model.addAction(action); return action; } } private DeviceAction closeAudio(InteropParameters parameters, SosCommands command) { String language = (String)parameters.getParameterValue("language"); DeviceAction action = new DeviceAction(device, parameters, command); action.actionName = Shared.getMessage(language, "End call"); try { // TODO Mirar la prioridad del close, si no es superior a la de la llamada actual, no se puede cerrar la llamada??????????? sender.ORD_FON_OFF(); DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_CORRECT); action.setResult(actionResult); Shared.model.addAction(action); return action; } catch (Exception exception) { DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_ERROR, exception.getMessage()); action.setResult(actionResult); Shared.model.addAction(action); return action; } } private DeviceAction pauseAudio(InteropParameters parameters, SosCommands command) { String language = (String)parameters.getParameterValue("language"); DeviceAction action = new DeviceAction(device, parameters, command); action.actionName = Shared.getMessage(language, "Pause call"); try { sender.ORD_FON_OFF(); DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_CORRECT); action.setResult(actionResult); Shared.model.addAction(action); return action; } catch (Exception exception) { DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_ERROR, exception.getMessage()); action.setResult(actionResult); Shared.model.addAction(action); return action; } } private DeviceAction resumeAudio(InteropParameters parameters, SosCommands command) { String language = (String)parameters.getParameterValue("language"); String username = (String)parameters.getParameterValue("username"); String computer = (String)parameters.getParameterValue("computer"); DeviceAction action = new DeviceAction(device, parameters, command); action.actionName = Shared.getMessage(language, "Resume call"); try { sender.ORD_FON_ON(username, computer, command.musatel.call.audioDestinationIp, command.musatel.call.audioDestinationPort, this.priority); DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_CORRECT); action.setResult(actionResult); Shared.model.addAction(action); return action; } catch (Exception exception) { DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_ERROR, exception.getMessage()); action.setResult(actionResult); Shared.model.addAction(action); return action; } } private DeviceAction doMaintenanceTest(InteropParameters parameters, SosCommands command) { String language = (String)parameters.getParameterValue("language"); DeviceAction action = new DeviceAction(device, parameters, command); action.actionName = Shared.getMessage(language, "Maintenance test"); try { sender.ORD_TST_MAN(); DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_CORRECT); action.setResult(actionResult); Shared.model.addAction(action); return action; } catch (Exception exception) { DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_ERROR, exception.getMessage()); action.setResult(actionResult); Shared.model.addAction(action); return action; } } private DeviceAction startMessagesTest(InteropParameters parameters, SosCommands command) { String language = (String)parameters.getParameterValue("language"); String username = (String)parameters.getParameterValue("username"); String computer = (String)parameters.getParameterValue("computer"); DeviceAction action = new DeviceAction(device, parameters, command); if (command.musatel.messagesTest.type == SosCommandsMusatel.MESSAGE_OUT_OF_SERVICE) action.actionName = Shared.getMessage(language, "Out of service message test"); if (command.musatel.messagesTest.type == SosCommandsMusatel.MESSAGE_CALL_WAITING) action.actionName = Shared.getMessage(language, "Received call message test"); try { sender.ORD_VOZ_ON(username, computer, command.musatel.messagesTest.audioDestinationIp, command.musatel.messagesTest.audioDestinationPort, command.musatel.messagesTest.type); DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_CORRECT); action.setResult(actionResult); Shared.model.addAction(action); return action; } catch (Exception exception) { DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_ERROR, exception.getMessage()); action.setResult(actionResult); Shared.model.addAction(action); return action; } } private DeviceAction endMessagesTest(InteropParameters parameters, SosCommands command) { String language = (String)parameters.getParameterValue("language"); DeviceAction action = new DeviceAction(device, parameters, command); action.actionName = Shared.getMessage(language, "Stop message test"); try { sender.ORD_VOZ_OFF(); DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_CORRECT); action.setResult(actionResult); Shared.model.addAction(action); return action; } catch (Exception exception) { DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_ERROR, exception.getMessage()); action.setResult(actionResult); Shared.model.addAction(action); return action; } } private DeviceAction startToneTest(InteropParameters parameters, SosCommands command) { String language = (String)parameters.getParameterValue("language"); DeviceAction action = new DeviceAction(device, parameters, command); if (command.musatel.toneTest.type == SosCommandsMusatel.TONE_CALL) action.actionName = Shared.getMessage(language, "Call tone test"); if (command.musatel.toneTest.type == SosCommandsMusatel.TONE_BUSY) action.actionName = Shared.getMessage(language, "Busy tone test"); try { sender.ORD_TON_ON(command.musatel.toneTest.audioDestinationIp, command.musatel.toneTest.audioDestinationPort, command.musatel.toneTest.type); DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_CORRECT); action.setResult(actionResult); Shared.model.addAction(action); return action; } catch (Exception exception) { DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_ERROR, exception.getMessage()); action.setResult(actionResult); Shared.model.addAction(action); return action; } } private DeviceAction endToneTest(InteropParameters parameters, SosCommands command) { String language = (String)parameters.getParameterValue("language"); DeviceAction action = new DeviceAction(device, parameters, command); action.actionName = Shared.getMessage(language, "Stop tone test"); try { sender.ORD_TON_OFF(); DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_CORRECT); action.setResult(actionResult); Shared.model.addAction(action); return action; } catch (Exception exception) { DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_ERROR, exception.getMessage()); action.setResult(actionResult); Shared.model.addAction(action); return action; } } private DeviceAction setSpeakerVolume(InteropParameters parameters, SosCommands command) { String language = (String)parameters.getParameterValue("language"); DeviceAction action = new DeviceAction(device, parameters, command); action.actionName = Shared.getMessage(language, "Change speaker volume"); try { sender.ORD_ADJ_VOL(command.musatel.speakerVolume); DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_CORRECT); action.setResult(actionResult); Shared.model.addAction(action); return action; } catch (Exception exception) { DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_ERROR, exception.getMessage()); action.setResult(actionResult); Shared.model.addAction(action); return action; } } private DeviceAction reset(InteropParameters parameters, SosCommands command) { String language = (String)parameters.getParameterValue("language"); DeviceAction action = new DeviceAction(device, parameters, command); action.actionName = Shared.getMessage(language, "Reset"); try { sender.ORD_FON_OFF(); DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_CORRECT); action.setResult(actionResult); Shared.model.addAction(action); return action; } catch (Exception exception) { DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_ERROR, exception.getMessage()); action.setResult(actionResult); Shared.model.addAction(action); return action; } } // public void setSosPriority (int priority) { this.priority = priority; } }