/** Connect to the DSC Alarm System through the EyezOn Envisalink 3/2DS or the DSC IT-100. */ public boolean open() { switch (connectorType) { case SERIAL: if (dscAlarmConnector == null) { dscAlarmConnector = new SerialConnector(serialPort, baudRate); } break; case TCP: if (StringUtils.isNotBlank(ipAddress)) { if (dscAlarmConnector == null) { dscAlarmConnector = new TCPConnector(ipAddress, DEFAULT_TCP_PORT, TCP_CONNECTION_TIMEOUT); } } else { logger.error("open(): Unable to Make API TCP Connection!"); connected = false; return connected; } break; default: break; } dscAlarmConnector.open(); connected = dscAlarmConnector.isConnected(); if (connected) { if (connectorType == DSCAlarmConnectorType.TCP) sendCommand(APICode.NetworkLogin); connected = dscAlarmConnector.isConnected(); } if (!connected) logger.error("open(): Unable to Make API Connection!"); logger.debug( "open(): Connected = {}, Connection Type: {}", connected ? true : false, connectorType); return connected; }
/** * Send an API command to the DSC Alarm system. * * @param apiCode * @param apiData * @return */ public boolean sendCommand(APICode apiCode, String... apiData) { boolean successful = false; boolean validCommand = false; String command = apiCode.getCode(); String data = ""; switch (apiCode) { case Poll: /*000*/ case StatusReport: /*001*/ validCommand = true; break; case LabelsRequest: /*002*/ if (!connectorType.equals(DSCAlarmConnectorType.SERIAL)) { break; } validCommand = true; break; case NetworkLogin: /*005*/ if (!connectorType.equals(DSCAlarmConnectorType.TCP)) { break; } if (password == null || password.length() < 1 || password.length() > 6) { logger.error( "sendCommand(): Password is invalid, must be between 1 and 6 chars", password); break; } data = password; validCommand = true; break; case DumpZoneTimers: /*008*/ if (!connectorType.equals(DSCAlarmConnectorType.TCP)) { break; } validCommand = true; break; case SetTimeDate: /*010*/ Date date = new Date(); SimpleDateFormat dateTime = new SimpleDateFormat("HHmmMMddYY"); data = dateTime.format(date); validCommand = true; break; case CommandOutputControl: /*020*/ if (apiData[0] == null || !apiData[0].matches("[1-8]")) { logger.error( "sendCommand(): Partition number must be a single character string from 1 to 8, it was: " + apiData[0]); break; } if (apiData[1] == null || !apiData[1].matches("[1-4]")) { logger.error( "sendCommand(): Output number must be a single character string from 1 to 4, it was: " + apiData[1]); break; } data = apiData[0]; validCommand = true; break; case KeepAlive: /*074*/ if (!connectorType.equals(DSCAlarmConnectorType.TCP)) { break; } case PartitionArmControlAway: /*030*/ case PartitionArmControlStay: /*031*/ case PartitionArmControlZeroEntryDelay: /*032*/ if (apiData[0] == null || !apiData[0].matches("[1-8]")) { logger.error( "sendCommand(): Partition number must be a single character string from 1 to 8, it was: {}", apiData[0]); break; } data = apiData[0]; validCommand = true; break; case PartitionArmControlWithUserCode: /*033*/ case PartitionDisarmControl: /*040*/ if (apiData[0] == null || !apiData[0].matches("[1-8]")) { logger.error( "sendCommand(): Partition number must be a single character string from 1 to 8, it was: {}", apiData[0]); break; } if (dscAlarmUserCode == null || dscAlarmUserCode.length() < 4 || dscAlarmUserCode.length() > 6) { logger.error( "sendCommand(): User Code is invalid, must be between 4 and 6 chars: {}", dscAlarmUserCode); break; } data = apiData[0] + dscAlarmUserCode; validCommand = true; break; case VirtualKeypadControl: /*058*/ if (!connectorType.equals(DSCAlarmConnectorType.SERIAL)) { break; } case TimeStampControl: /*055*/ case TimeDateBroadcastControl: /*056*/ case TemperatureBroadcastControl: /*057*/ if (apiData[0] == null || !apiData[0].matches("[0-1]")) { logger.error( "sendCommand(): Value must be a single character string of 0 or 1: {}", apiData[0]); break; } data = apiData[0]; validCommand = true; break; case TriggerPanicAlarm: /*060*/ if (apiData[0] == null || !apiData[0].matches("[1-8]")) { logger.error( "sendCommand(): Partition number must be a single character string from 1 to 8, it was: {}", apiData[0]); break; } if (apiData[1] == null || !apiData[1].matches("[1-3]")) { logger.error( "sendCommand(): FAPcode must be a single character string from 1 to 3, it was: {}", apiData[1]); break; } data = apiData[0] + apiData[1]; validCommand = true; break; case KeyStroke: /*070*/ if (apiData[0] == null || apiData[0].length() != 1 || !apiData[0].matches("[0-9]|A|#|\\*")) { logger.error( "sendCommand(): \'keystroke\' must be a single character string from 0 to 9, *, #, or A, it was: {}", apiData[0]); break; } data = apiData[0]; validCommand = true; break; case KeySequence: /*071*/ if (!connectorType.equals(DSCAlarmConnectorType.TCP)) { break; } if (apiData[0] == null || apiData[0].length() > 6 || !apiData[0].matches("(\\d|#|\\*)+")) { logger.error( "sendCommand(): \'keysequence\' must be a string of up to 6 characters consiting of 0 to 9, *, or #, it was: {}", apiData[0]); break; } data = apiData[0]; validCommand = true; break; case CodeSend: /*200*/ if (dscAlarmUserCode == null || dscAlarmUserCode.length() < 4 || dscAlarmUserCode.length() > 6) { logger.error( "sendCommand(): Access Code is invalid, must be between 4 and 6 chars: {}", apiData[0]); break; } data = dscAlarmUserCode; validCommand = true; break; default: validCommand = false; break; } if (validCommand) { APICommand apiCommand = new APICommand(); apiCommand.setAPICommand(command, data); dscAlarmConnector.write(apiCommand.toString()); successful = true; logger.debug("sendCommand(): Command Sent - {}", apiCommand.toString()); } else logger.error("sendCommand(): Command Not Sent - Invalid!"); return successful; }
/** Return Connected Status. */ public boolean isConnected() { return dscAlarmConnector.isConnected(); }
/** Read a API message received from the DSC Alarm system. */ public String read() { return dscAlarmConnector.read(); }
/** Close the connection to the DSC Alarm system. */ public boolean close() { logger.debug("close(): Disconnecting from API Connection!"); dscAlarmConnector.close(); connected = dscAlarmConnector.isConnected(); return connected; }
/** * Remove event listener * * @param listener */ public synchronized void removeEventListener(DSCAlarmEventListener listener) { dscAlarmConnector.removeEventListener(listener); }
/** * Add event listener * * @param listener */ public void addEventListener(DSCAlarmEventListener listener) { dscAlarmConnector.addEventListener(listener); }