package art.servers.colorsserver.m.protocol; import art.library.model.devices.Device; import art.library.model.devices.DeviceStatus; import art.library.model.devices.colors.controller.M.*; import art.library.model.devices.colors.controller.M.tables.*; import art.library.model.devices.colors.controller.M.realtime.M_ControllerRealtimeCountingDetector; import art.library.model.devices.colors.controller.M.realtime.M_ControllerRealtimeGroup; import art.library.model.devices.colors.controller.M.realtime.M_ControllerRealtimePresenceDetector; import art.library.model.devices.colors.controller.M.realtime.M_ControllerRealtimeSubcontroller; import art.library.model.devices.colors.controller.M.status.M_ControllerStatusGroup; import art.library.model.devices.colors.controller.M.M_ControllerStatusSubcontroller; import art.library.model.devices.colors.controller.M.configuration.M_Plan; import art.library.model.devices.etd.EtdStatus; import art.library.model.devices.etd.status.EtdStatusLane; import art.library.utils.common.TimeUtils; import art.servers.ServerException; import art.servers.colorsserver.Shared; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.HashMap; import java.util.List; public class M_ProtocolAnalyser { private static SimpleDateFormat sdhm = new SimpleDateFormat("HH:mm"); private static SimpleDateFormat sdh = new SimpleDateFormat("HH:mm:ss"); private static SimpleDateFormat sdfh = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); private static SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); public static final int STX = 0x02; public static final int ETX = 0x03; public static final int ACK = 0x06; public static final int NACK = 0x15; public static final int EOB12 = 0x0A; public static final int EOB14 = 0x0C; public static final int EOB15 = 0x0D; public static boolean isReadTableMessage(M_Message message, int controller, int tableCode) throws Exception { message.reset(); try { while (message.readByte() != STX); } catch (Exception e) { return(false); } int controllerReaded = message.readByte() & 0x7F; int function = message.readByte() & 0x7F; if ((controller == controllerReaded) && (function == M_ProtocolWriter.M_READ_TABLE_FULL)) { int tableCodeRead = message.readByte() & 0x7F; if (tableCode == tableCodeRead) return(true); } if ((controller == controllerReaded) && (function == M_ProtocolWriter.M_READ_TABLE_PARTIAL)) { int blockNumber = message.readByte() & 0x7F; int tableCodeRead = message.readByte() & 0x7F; if (tableCode == tableCodeRead) return(true); } return(false); } public static boolean isReadPartialTableMessage(M_Message message, int controller, int tableCode) throws Exception { message.reset(); try { while (message.readByte() != STX); } catch (Exception e) { return(false); } int controllerReaded = message.readByte() & 0x7F; int function = message.readByte() & 0x7F; if ((controller == controllerReaded) && (function == M_ProtocolWriter.M_READ_TABLE_PARTIAL)) { int blockNumber = message.readByte() & 0x7F; int tableCodeRead = message.readByte() & 0x7F; if (tableCode == tableCodeRead) return(true); } return(false); } public static boolean isACKMessage(M_Message message) throws Exception { return (message.readByte() == ACK); } public static boolean isNACKMessage(M_Message message) throws Exception { return (message.readByte() == NACK); } public static boolean isStartRecordingResponse(M_Message message) throws Exception { if (message.readByte() != STX) { return(false); } int controller = message.readByte(); int function = message.readByte() & 0x7F; if (function == M_ProtocolWriter.M_START_RECORDING_RESPONSE) return(true); return(false); } public static boolean isEndRecordingResponse(M_Message message) throws Exception { if (message.readByte() != STX) { return(false); } int controller = message.readByte(); int function = message.readByte() & 0x7F; if (function == M_ProtocolWriter.M_END_RECORDING_RESPONSE) return(true); return(false); } public static void analyseCurrentPlanResponse(M_ControllerStatusSubcontroller m_controllerStatusSubcontroller, M_ControllerRealtimeSubcontroller m_controllerRealtimeSubcontroller, M_Message message, int subcontroller) throws ServerException, Exception { message.reset(); try { while (message.readByte() != STX); } catch (Exception e) { throw new ServerException(Shared.getMessage("Message not start with 0x02")); } int controller = message.readByte() & 0x7F; if (controller != subcontroller) { throw new ServerException(Shared.getMessage("Message does not correspond to controller")); } int function = message.readByte() & 0x7F; if (function != M_ProtocolWriter.M_CURRENT_PLAN_QUERY) { return; } if (m_controllerStatusSubcontroller.colorsMode != M_ControllerStatusSubcontroller.COLORS_COLORS) { m_controllerRealtimeSubcontroller.cycle = 0; m_controllerRealtimeSubcontroller.adjust = 0; m_controllerRealtimeSubcontroller.phase = 0; m_controllerRealtimeSubcontroller.startTimestamp = 0; } else { try { m_controllerRealtimeSubcontroller.plan = message.readByte() & 0x7F; int hour = message.readByte() & 0x7F; int minute = message.readByte() & 0x7F; int second = message.readByte() & 0x7F; m_controllerRealtimeSubcontroller.phase = message.readByte() & 0x7F; m_controllerRealtimeSubcontroller.cycle = ((message.readByte() & 0x7F) << 7) + (message.readByte() & 0x7F); m_controllerRealtimeSubcontroller.adjust = (byte)(message.readByte() & 0x7F); String time = String.format("%02d:%02d:%02d", hour, minute, second); m_controllerRealtimeSubcontroller.startTimestamp = sdh.parse(time).getTime(); m_controllerStatusSubcontroller.plan = m_controllerRealtimeSubcontroller.plan; } catch (Exception e) { } } } public static int analyseAlarmsResponse(M_Controller m_controller, M_Message message, int controllerCode) throws ServerException, Exception { message.reset(); try { while (message.readByte() != STX); } catch (Exception e) { throw new ServerException(Shared.getMessage("Message not start with 0x02")); } int controller = message.readByte() & 0x7F; if (controller != controllerCode) { throw new ServerException(Shared.getMessage("Message does not correspond to controller")); } int function = message.readByte() & 0x7F; if (function != M_ProtocolWriter.M_ALARMS_QUERY) { return(0); } int alarms1 = message.readByte() & 0x7F; int alarms2 = message.readByte() & 0x7F; int alarms3 = message.readByte() & 0x7F; int alarms4 = message.readByte() & 0x7F; m_controller.setAlarm("alarm_incompatibility", ((alarms1 & 0x01) != 0), System.currentTimeMillis()); m_controller.setAlarm("alarm_sincronization", ((alarms1 & 0x02) != 0), System.currentTimeMillis()); m_controller.setAlarm("alarm_transmission", ((alarms1 & 0x04) != 0), System.currentTimeMillis()); m_controller.setAlarm("alarm_damaged_group", ((alarms1 & 0x08) != 0), System.currentTimeMillis()); m_controller.setAlarm("alarm_blown_lamp", ((alarms1 & 0x10) != 0), System.currentTimeMillis()); m_controller.setAlarm("alarm_damage_detector", ((alarms1 & 0x20) != 0), System.currentTimeMillis()); m_controller.setAlarm("alarm_open_door", ((alarms1 & 0x40) != 0), System.currentTimeMillis()); m_controller.setAlarm("alarm_manual_control", ((alarms2 & 0x01) != 0), System.currentTimeMillis()); m_controller.setAlarm("alarm_autotest", ((alarms2 & 0x02) != 0), System.currentTimeMillis()); m_controller.setAlarm("alarm_reset", ((alarms2 & 0x04) != 0), System.currentTimeMillis()); m_controller.setAlarm("alarm_clock", ((alarms2 & 0x08) != 0), System.currentTimeMillis()); m_controller.setAlarm("alarm_temperature", ((alarms2 & 0x10) != 0), System.currentTimeMillis()); m_controller.setAlarm("alarm_data_checksum", ((alarms2 & 0x20) != 0), System.currentTimeMillis()); m_controller.setAlarm("alarm_voltage", ((alarms2 & 0x40) != 0), System.currentTimeMillis()); m_controller.setAlarm("alarm_local_recording", ((alarms3 & 0x01) != 0), System.currentTimeMillis()); m_controller.setAlarm("alarm_remote_recording", ((alarms3 & 0x02) != 0), System.currentTimeMillis()); m_controller.setAlarm("alarm_incorrect_access", ((alarms3 & 0x04) != 0), System.currentTimeMillis()); m_controller.setAlarm("alarm_programmed_1", ((alarms4 & 0x01) != 0), System.currentTimeMillis()); m_controller.setAlarm("alarm_programmed_2", ((alarms4 & 0x02) != 0), System.currentTimeMillis()); m_controller.setAlarm("alarm_programmed_3", ((alarms4 & 0x04) != 0), System.currentTimeMillis()); m_controller.setAlarm("alarm_programmed_4", ((alarms4 & 0x08) != 0), System.currentTimeMillis()); m_controller.setAlarm("alarm_programmed_5", ((alarms4 & 0x10) != 0), System.currentTimeMillis()); m_controller.setAlarm("alarm_programmed_6", ((alarms4 & 0x20) != 0), System.currentTimeMillis()); m_controller.setAlarm("alarm_programmed_7", ((alarms4 & 0x40) != 0), System.currentTimeMillis()); int result = 0; if ((alarms1 & 0x01) != 0) result += 0x04; if ((alarms1 & 0x08) != 0) result += 0x02; if ((alarms1 & 0x10) != 0) result += 0x01; result = 7; // QUITARLO ES PARA PROBAR TODAS LAS LLAMADAS return(result); } public static void analyseBlownLampsAlarmsResponse(M_Controller m_controller, M_Message message, int controllerCode) throws ServerException, Exception { message.reset(); try { while (message.readByte() != STX); } catch (Exception e) { throw new ServerException(Shared.getMessage("Message not start with 0x02")); } int controller = message.readByte() & 0x7F; if (controller != controllerCode) { throw new ServerException(Shared.getMessage("Message does not correspond to controller")); } int function = message.readByte() & 0x7F; if (function != M_ProtocolWriter.M_BLOWN_LAMPS_ALARMS_QUERY) { return; } HashMap hgroupsAlarms = new HashMap(); while (message.eof() == false) { int group = message.readByte() & 0x7F; int lampAlarms = message.readByte() & 0x7F; boolean found = false; for (M_ControllerStatusGroup statusGroup : m_controller.getDeviceStatus().lgroup) { if (statusGroup.number == group) { found = true; hgroupsAlarms.put(new Integer(group), new Integer(group)); if ((lampAlarms & 0x01) != 0) statusGroup.redOutputBlown = M_ControllerStatusGroup.YES; else statusGroup.redOutputBlown = M_ControllerStatusGroup.NO; if ((lampAlarms & 0x02) != 0) statusGroup.yellowOutputBlown = M_ControllerStatusGroup.YES; else statusGroup.yellowOutputBlown = M_ControllerStatusGroup.NO; if ((lampAlarms & 0x04) != 0) statusGroup.greenOutputBlown = M_ControllerStatusGroup.YES; else statusGroup.greenOutputBlown = M_ControllerStatusGroup.NO; } } if (found == false) { M_ControllerStatusGroup statusGroup = new M_ControllerStatusGroup(); statusGroup.number = group; hgroupsAlarms.put(new Integer(group), new Integer(group)); if ((lampAlarms & 0x01) != 0) statusGroup.redOutputBlown = M_ControllerStatusGroup.YES; else statusGroup.redOutputBlown = M_ControllerStatusGroup.NO; if ((lampAlarms & 0x02) != 0) statusGroup.yellowOutputBlown = M_ControllerStatusGroup.YES; else statusGroup.yellowOutputBlown = M_ControllerStatusGroup.NO; if ((lampAlarms & 0x04) != 0) statusGroup.greenOutputBlown = M_ControllerStatusGroup.YES; else statusGroup.greenOutputBlown = M_ControllerStatusGroup.NO; m_controller.getDeviceStatus().lgroup.add(statusGroup); } } for (M_ControllerStatusGroup statusGroup : m_controller.getDeviceStatus().lgroup) { if (hgroupsAlarms.containsKey(new Integer(statusGroup.number)) == false) { statusGroup.redOutputBlown = M_ControllerStatusGroup.NO; statusGroup.yellowOutputBlown = M_ControllerStatusGroup.NO; statusGroup.greenOutputBlown = M_ControllerStatusGroup.NO; } } } public static void analyseDamagedGroupAlarmsResponse(M_Controller m_controller, M_Message message, int controllerCode) throws ServerException, Exception { message.reset(); try { while (message.readByte() != STX); } catch (Exception e) { throw new ServerException(Shared.getMessage("Message not start with 0x02")); } int controller = message.readByte() & 0x7F; if (controller != controllerCode) { throw new ServerException(Shared.getMessage("Message does not correspond to controller")); } int function = message.readByte() & 0x7F; if (function != M_ProtocolWriter.M_DAMAGED_GROUP_ALARMS_QUERY) { return; } HashMap hgroupsAlarms = new HashMap(); while (message.eof() == false) { int group = message.readByte() & 0x7F; int outputAlarms = message.readByte() & 0x7F; boolean found = false; for (M_ControllerStatusGroup statusGroup : m_controller.getDeviceStatus().lgroup) { if (statusGroup.number == group) { found = true; hgroupsAlarms.put(new Integer(group), new Integer(group)); if ((outputAlarms & 0x01) != 0) statusGroup.redOutputDamaged = M_ControllerStatusGroup.YES; else statusGroup.redOutputDamaged = M_ControllerStatusGroup.NO; if ((outputAlarms & 0x02) != 0) statusGroup.yellowOutputDamaged = M_ControllerStatusGroup.YES; else statusGroup.yellowOutputDamaged = M_ControllerStatusGroup.NO; if ((outputAlarms & 0x04) != 0) statusGroup.greenOutputDamaged = M_ControllerStatusGroup.YES; else statusGroup.greenOutputDamaged = M_ControllerStatusGroup.NO; } } if (found == false) { M_ControllerStatusGroup statusGroup = new M_ControllerStatusGroup(); statusGroup.number = group; hgroupsAlarms.put(new Integer(group), new Integer(group)); if ((outputAlarms & 0x01) != 0) statusGroup.redOutputDamaged = M_ControllerStatusGroup.YES; else statusGroup.redOutputDamaged = M_ControllerStatusGroup.NO; if ((outputAlarms & 0x02) != 0) statusGroup.yellowOutputDamaged = M_ControllerStatusGroup.YES; else statusGroup.yellowOutputDamaged = M_ControllerStatusGroup.NO; if ((outputAlarms & 0x04) != 0) statusGroup.greenOutputDamaged = M_ControllerStatusGroup.YES; else statusGroup.greenOutputDamaged = M_ControllerStatusGroup.NO; m_controller.getDeviceStatus().lgroup.add(statusGroup); } } for (M_ControllerStatusGroup statusGroup : m_controller.getDeviceStatus().lgroup) { if (hgroupsAlarms.containsKey(new Integer(statusGroup.number)) == false) { statusGroup.redOutputDamaged = M_ControllerStatusGroup.NO; statusGroup.yellowOutputDamaged = M_ControllerStatusGroup.NO; statusGroup.greenOutputDamaged = M_ControllerStatusGroup.NO; } } } public static void analyseIncompatibilityAlarmsResponse(M_Controller m_controller, M_Message message, int controllerCode) throws ServerException, Exception { message.reset(); try { while (message.readByte() != STX); } catch (Exception e) { throw new ServerException(Shared.getMessage("Message not start with 0x02")); } int controller = message.readByte() & 0x7F; if (controller != controllerCode) { throw new ServerException(Shared.getMessage("Message does not correspond to controller")); } int function = message.readByte() & 0x7F; if (function != M_ProtocolWriter.M_INCOMPATIBILITY_ALARMS_QUERY) { return; } HashMap hgroupsIncompatibilities = new HashMap(); while (message.eof() == false) { int group = message.readByte() & 0x7F; int groupIncompatible = message.readByte() & 0x7F; M_ControllerStatusGroup groupIncompatibility = hgroupsIncompatibilities.get(new Integer(group)); if (groupIncompatibility != null) { groupIncompatibility.lincompatibility.add(new Integer(groupIncompatible)); } else { boolean found = false; for (M_ControllerStatusGroup statusGroup : m_controller.getDeviceStatus().lgroup) { if (statusGroup.number == group) { found = true; hgroupsIncompatibilities.put(new Integer(group), statusGroup); statusGroup.lincompatibility.clear(); statusGroup.lincompatibility.add(new Integer(groupIncompatible)); } } if (found == false) { M_ControllerStatusGroup statusGroup = new M_ControllerStatusGroup(); statusGroup.number = group; hgroupsIncompatibilities.put(new Integer(group), statusGroup); statusGroup.lincompatibility.clear(); statusGroup.lincompatibility.add(new Integer(groupIncompatible)); m_controller.getDeviceStatus().lgroup.add(statusGroup); } } } for (M_ControllerStatusGroup statusGroup : m_controller.getDeviceStatus().lgroup) { if (hgroupsIncompatibilities.containsKey(new Integer(statusGroup.number)) == false) { statusGroup.lincompatibility.clear(); } } } public static long analyseCurrentTimeResponse(M_Controller m_controller, M_Message message, int controllerCode) throws ServerException, Exception { message.reset(); try { while (message.readByte() != STX); } catch (Exception e) { throw new ServerException(Shared.getMessage("Message not start with 0x02")); } int controller = message.readByte() & 0x7F; if (controller != controllerCode) { throw new ServerException(Shared.getMessage("Message does not correspond to controller")); } int function = message.readByte() & 0x7F; if (function != M_ProtocolWriter.M_CURRENT_TIME_QUERY) { throw new ServerException("Incorrect function code"); } try { int weekday = message.readByte() & 0x7F; int hour = message.readByte() & 0x7F; int minute = message.readByte() & 0x7F; int second = message.readByte() & 0x7F; int day = message.readByte() & 0x7F; int month = message.readByte() & 0x7F; int year = (message.readByte() & 0x7F) + 2000; String stime = String.format("%02d/%02d/%04d %02d:%02d:%02d", day, month, year, hour, minute, second); long timestamp = sdfh.parse(stime).getTime(); try{m_controller.getDeviceRealtime().controllerDateTime = timestamp;} catch (Exception e){}; return(timestamp); } catch (Exception e) { return(m_controller.getDeviceRealtime().controllerDateTime); } } public static void analyseCurrentStatusGroupsResponse(M_Controller m_controller, M_Message message, int controllerCode) throws ServerException, Exception { message.reset(); try { while (message.readByte() != STX); } catch (Exception e) { throw new ServerException(Shared.getMessage("Message not start with 0x02")); } int controller = message.readByte() & 0x7F; if (controller != controllerCode) { throw new ServerException(Shared.getMessage("Message does not correspond to controller")); } int function = message.readByte() & 0x7F; if (function != M_ProtocolWriter.M_CURRENT_STATUS_GROUPS_QUERY) { return; } try { int group = 1; while (message.eof() == false) { // 1BGGYYRR // B --> 0 Normal flashing, Tilt flashing // GG YY RR --> 0 Off, 1 On, 2 Flashing fast, 3 Flashing slow int status = message.readByte() & 0x7F; boolean found = false; for (M_ControllerRealtimeGroup realtimeGroup : m_controller.getDeviceRealtime().lgroup) { if (realtimeGroup.number == group) { found = true; if ((status & 0x40) != 0) realtimeGroup.tilt = M_ControllerRealtimeGroup.YES; else realtimeGroup.tilt = M_ControllerRealtimeGroup.NO; realtimeGroup.redOutputStatus = (byte)(status & 0x03); realtimeGroup.yellowOutputStatus = (byte)((status >> 2) & 0x03); realtimeGroup.greenOutputStatus = (byte)((status >> 4) & 0x03); } } if (found == false) { M_ControllerRealtimeGroup statusGroup = new M_ControllerRealtimeGroup(); statusGroup.number = group; if ((status & 0x40) != 0) statusGroup.tilt = M_ControllerRealtimeGroup.YES; else statusGroup.tilt = M_ControllerRealtimeGroup.NO; statusGroup.redOutputStatus = (byte)(status & 0x03); statusGroup.yellowOutputStatus = (byte)((status >> 2) & 0x03); statusGroup.greenOutputStatus = (byte)((status >> 4) & 0x03); m_controller.getDeviceRealtime().lgroup.add(statusGroup); } group++; } } catch (Exception e) { } } public static void analyseCurrentStateResponse(M_ControllerStatusSubcontroller m_controllerStatusSubcontroller, M_Message message, int subcontroller) throws ServerException, Exception { message.reset(); try { while (message.readByte() != STX); } catch (Exception e) { throw new ServerException(Shared.getMessage("Message not start with 0x02")); } int controller = message.readByte() & 0x7F; if (controller != subcontroller) { throw new ServerException(Shared.getMessage("Message does not correspond to controller")); } int function = message.readByte() & 0x7F; if (function != M_ProtocolWriter.M_CURRENT_SUBCONTROLLER_STATE_QUERY) { return; } m_controllerStatusSubcontroller.colorsMode = message.readByte() & 0x7F; int controlMode = message.readByte() & 0x7F; int coordination = message.readByte() & 0x7F; int operation = message.readByte() & 0x7F; if ((controlMode & 0x01) == 0) m_controllerStatusSubcontroller.planSelection = M_ControllerStatusSubcontroller.PLANSELECTION_TIMETABLE; if ((controlMode & 0x01) != 0) m_controllerStatusSubcontroller.planSelection = M_ControllerStatusSubcontroller.PLANSELECTION_EXTERNAL_PLANS; if ((controlMode & 0x02) != 0) m_controllerStatusSubcontroller.planSelection = M_ControllerStatusSubcontroller.PLANSELECTION_ICF; if ((controlMode & 0x04) != 0) m_controllerStatusSubcontroller.planSelection = M_ControllerStatusSubcontroller.PLANSELECTION_COMPUTER; if (((controlMode & 0x08) != 0)) m_controllerStatusSubcontroller.planSelectionComputer = M_ControllerStatusSubcontroller.PLANSELECTION_COMPUTER_ADAPTATIVE; else { if (m_controllerStatusSubcontroller.planSelection != M_ControllerStatusSubcontroller.PLANSELECTION_COMPUTER) m_controllerStatusSubcontroller.planSelectionComputer = M_ControllerStatusSubcontroller.UNKNOWN; else m_controllerStatusSubcontroller.planSelectionComputer = M_ControllerStatusSubcontroller.PLANSELECTION_COMPUTER_PLAN_SELECTION; } if ((controlMode & 0x10) != 0) m_controllerStatusSubcontroller.adaptativeDetectors = M_ControllerStatusSubcontroller.YES; else m_controllerStatusSubcontroller.adaptativeDetectors = M_ControllerStatusSubcontroller.NO; if ((coordination & 0x01) == 0) m_controllerStatusSubcontroller.coordinationMode = M_ControllerStatusSubcontroller.COORDINATION_EXTERNAL; if ((coordination & 0x01) != 0) m_controllerStatusSubcontroller.coordinationMode = M_ControllerStatusSubcontroller.COORDINATION_INTERNAL_CLOCK; if ((coordination & 0x02) != 0) m_controllerStatusSubcontroller.coordinationMode = M_ControllerStatusSubcontroller.COORDINATION_COMPUTER; if ((coordination & 0x04) != 0) m_controllerStatusSubcontroller.centralizedMode = M_ControllerStatusSubcontroller.YES; else m_controllerStatusSubcontroller.centralizedMode = M_ControllerStatusSubcontroller.NO; if (m_controllerStatusSubcontroller.userControlManual == M_ControllerCommands.CONDITION_YES) { if ((coordination & 0x08) != 0) m_controllerStatusSubcontroller.localControl = M_ControllerStatusSubcontroller.YES; else m_controllerStatusSubcontroller.localControl = M_ControllerStatusSubcontroller.NO; } if ((coordination & 0x10) != 0) m_controllerStatusSubcontroller.controlComputerDirectGroups = M_ControllerStatusSubcontroller.YES; else m_controllerStatusSubcontroller.controlComputerDirectGroups = M_ControllerStatusSubcontroller.NO; if ((operation & 0x01) == 0) m_controllerStatusSubcontroller.operationMode = M_ControllerStatusSubcontroller.OPERATIONMODE_FIXED; if ((operation & 0x01) != 0) m_controllerStatusSubcontroller.operationMode = M_ControllerStatusSubcontroller.OPERATIONMODE_SEMIACTUATED; if ((operation & 0x02) != 0) m_controllerStatusSubcontroller.operationMode = M_ControllerStatusSubcontroller.OPERATIONMODE_ACTUATED; if ((operation & 0x04) != 0) m_controllerStatusSubcontroller.emergency = M_ControllerStatusSubcontroller.YES; else m_controllerStatusSubcontroller.emergency = M_ControllerStatusSubcontroller.NO; } public static void analyseCurrentPresenceDetectorsResponse(M_Controller m_controller, M_Message message, int controllerCode) throws ServerException, Exception { message.reset(); try { while (message.readByte() != STX); } catch (Exception e) { throw new ServerException(Shared.getMessage("Message not start with 0x02")); } int controller = message.readByte() & 0x7F; if (controller != controllerCode) { throw new ServerException(Shared.getMessage("Message does not correspond to controller")); } int function = message.readByte() & 0x7F; if (function != M_ProtocolWriter.M_CURRENT_PRESENCE_DETECTORS) { return; } try { int status1 = message.readByte() & 0x7F; int status2 = message.readByte() & 0x7F; int status3 = message.readByte() & 0x7F; if (m_controller.getDeviceRealtime().lpresence.size() == 0) { for (int i=1; i<=21; i++) { M_ControllerRealtimePresenceDetector presenceDetector = new M_ControllerRealtimePresenceDetector(); presenceDetector.number = i; m_controller.getDeviceRealtime().lpresence.add(presenceDetector); } } for (M_ControllerRealtimePresenceDetector presenceDetector : m_controller.getDeviceRealtime().lpresence) { int presence = M_ControllerRealtimePresenceDetector.PRESENCE_NO; int byteStatus = status1; int bit = 0; if ((presenceDetector.number >= 1) && (presenceDetector.number <= 7)) { byteStatus = status1; bit = presenceDetector.number-1; } if ((presenceDetector.number >= 8) && (presenceDetector.number <= 14)) { byteStatus = status2; bit = presenceDetector.number-8; } if (presenceDetector.number > 14) { byteStatus = status3; bit = presenceDetector.number-14; } if ((byteStatus & (int)(Math.pow(2, bit))) != 0) presence = M_ControllerRealtimePresenceDetector.PRESENCE_YES; else presence = M_ControllerRealtimePresenceDetector.PRESENCE_NO; if (presenceDetector.presence != presence) presenceDetector.timestamp = System.currentTimeMillis(); presenceDetector.presence = presence; } } catch (Exception exception) { } } public static void analyseCurrentCountingDetectorsResponse(M_Controller m_controller, M_Message message, int controllerCode, EtdStatus etdStatus) throws ServerException, Exception { message.reset(); try { while (message.readByte() != STX); } catch (Exception e) { throw new ServerException(Shared.getMessage("Message not start with 0x02")); } int controller = message.readByte() & 0x7F; if (controller != controllerCode) { throw new ServerException(Shared.getMessage("Message does not correspond to controller")); } int function = message.readByte() & 0x7F; if (function != M_ProtocolWriter.M_CURRENT_COUNTING_DETECTORS) { return; } int length = message.length() - 4; int resto = (length % 8); boolean tieneHora = false; if (resto > 1) tieneHora = true; int hour = -1; int minute = -1; String sfecha = ""; String sfechahora = ""; // long timestamp = System.currentTimeMillis(); if (tieneHora == true) { hour = message.readByte() & 0x7F; minute = message.readByte() & 0x7F; // sfecha = sdf.format(new Date()); // sfechahora = sfecha + " " + String.format("%02d:%02d:00", hour, minute); // timestamp = sdfh.parse(sfechahora).getTime(); } long measurementTimestamp = TimeUtils.thisperiod(etdStatus.period); etdStatus.setMeasurementTimestamp(Device.getDate(measurementTimestamp)); int detector = 1; while (message.eof() == false) { int counting1 = message.readByte() & 0x0F; int counting2 = message.readByte() & 0x0F; int counting3 = message.readByte() & 0x0F; int counting4 = message.readByte() & 0x0F; int occupancy1 = message.readByte() & 0x0F; int occupancy2 = message.readByte() & 0x0F; int occupancy3 = message.readByte() & 0x0F; int occupancy4 = message.readByte() & 0x0F; int counting = (counting1 << 12) + (counting2 << 8) + (counting3 << 4) + counting4; int occupancy = (occupancy1 << 12) + (occupancy2 << 8) + (occupancy3 << 4) + occupancy4; boolean found = false; for (M_ControllerRealtimeCountingDetector countingDetector : m_controller.getDeviceRealtime().lcounting) { if (countingDetector.number == detector) { found = true; countingDetector.counting = counting; countingDetector.occupancy = occupancy; countingDetector.timestamp = measurementTimestamp; } } // for (M_ControllerRealtimeCountingDetector countingDetector : m_controller.getDeviceStatus().lcounting) // { // if (countingDetector.number == detector) // { // found = true; // countingDetector.counting = counting; // countingDetector.occupancy = occupancy; // countingDetector.timestamp = measurementTimestamp; // } // } if (found == false) { M_ControllerRealtimeCountingDetector countingDetector = new M_ControllerRealtimeCountingDetector(); countingDetector.number = detector; countingDetector.counting = counting; countingDetector.occupancy = occupancy; countingDetector.timestamp = measurementTimestamp; m_controller.getDeviceRealtime().lcounting.add(countingDetector); // m_controller.getDeviceStatus().lcounting.add(countingDetector); } // Put data in EtdStatus if (etdStatus.llane.size() >= detector) { // Exist EtdStatusLane EtdStatusLane laneStatus = etdStatus.llane.get(detector-1); laneStatus.status = DeviceStatus.STATUS_ONLINE; laneStatus.correctMeasurements++; laneStatus.totalMeasurements++; laneStatus.counting = counting; laneStatus.occupancy = occupancy; } else { // Not exist EtdStatusLane EtdStatusLane laneStatus = new EtdStatusLane(); laneStatus.correctMeasurements++; laneStatus.totalMeasurements++; laneStatus.counting = counting; laneStatus.occupancy = occupancy; etdStatus.llane.add(laneStatus); } detector++; } } public static void analyseEndRecordingResponse(M_Message message) throws ServerException, Exception { message.reset(); try { while (message.readByte() != STX); } catch (Exception e) { throw new ServerException(Shared.getMessage("Message not start with 0x02")); } int controller = message.readByte() & 0x7F; if (controller != 0x00) { throw new ServerException(Shared.getMessage("Message does not correspond to controller")); } int function = message.readByte() & 0x7F; int byteGeneral1 = message.readByte() & 0x7F; int byteGeneral2 = message.readByte() & 0x7F; int byteGeneral3 = message.readByte() & 0x7F; int byte1Subregulador1 = message.readByte() & 0x7F; int byte2Subregulador1 = message.readByte() & 0x7F; int byte3Subregulador1 = message.readByte() & 0x7F; int byte4Subregulador1 = message.readByte() & 0x7F; int byte1Subregulador2 = message.readByte() & 0x7F; int byte2Subregulador2 = message.readByte() & 0x7F; int byte3Subregulador2 = message.readByte() & 0x7F; int byte4Subregulador2 = message.readByte() & 0x7F; int byte1Subregulador3 = message.readByte() & 0x7F; int byte2Subregulador3 = message.readByte() & 0x7F; int byte3Subregulador3 = message.readByte() & 0x7F; int byte4Subregulador3 = message.readByte() & 0x7F; int byte1Subregulador4 = message.readByte() & 0x7F; int byte2Subregulador4 = message.readByte() & 0x7F; int byte3Subregulador4 = message.readByte() & 0x7F; int byte4Subregulador4 = message.readByte() & 0x7F; String msg = ""; int errorCode = byteGeneral1 & 0x0F; int subcontroller = (byteGeneral1 >> 4) & 0x07; // General Error Code if (errorCode == 1) { msg += Shared.getMessage("Error recording tables") + Shared.getMessage("Too much data received") + "\r\n"; } else if (errorCode == 2) { msg += Shared.getMessage("Error recording tables") + Shared.getMessage("Cannot record data") + "\r\n"; } else if (errorCode == 3) { msg += Shared.getMessage("Error recording tables") + Shared.getMessage("Failure recording data") + "\r\n"; } else if (errorCode == 4) { msg += Shared.getMessage("Error recording tables") + Shared.getMessage("Error deleting a table") + "\r\n"; } else if (errorCode == 5) { msg += Shared.getMessage("Error recording tables") + Shared.getMessage("Tables reception error") + "\r\n"; } // Error tables 1 to 7 of controller 0 if ((byteGeneral2 & 0x01) > 0) { msg += getMessageControllerTableError(M_Table.M_TABLE_A_GROUPS); } if ((byteGeneral2 & 0x02) > 0) { msg += getMessageControllerTableError(M_Table.M_TABLE_B_PHYSICAL_DETECTORS); } if ((byteGeneral2 & 0x04) > 0) { msg += getMessageControllerTableError(M_Table.M_TABLE_C_LOGICAL_DETECTORS); } if ((byteGeneral2 & 0x08) > 0) { msg += getMessageControllerTableError(M_Table.M_TABLE_D_DEMANDS); } if ((byteGeneral2 & 0x10) > 0) { msg += getMessageControllerTableError(M_Table.M_TABLE_E_INCOMPATIBILITIES); } if ((byteGeneral2 & 0x20) > 0) { msg += getMessageControllerTableError(M_Table.M_TABLE_F_SPECIAL_DAYS); } if ((byteGeneral2 & 0x40) > 0) { msg += getMessageControllerTableError(M_Table.M_TABLE_G_INTERMITENCES); } // Error tables 8 to 14 of controller 0 if ((byteGeneral3 & 0x01) > 0) { msg += getMessageControllerTableError(M_Table.M_TABLE_H_OPERATION_MODE); } if ((byteGeneral3 & 0x02) > 0) { msg += getMessageControllerTableError(M_Table.M_TABLE_I_ALARMS_MANAGEMENT); } if ((byteGeneral3 & 0x04) > 0) { msg += getMessageControllerTableError(M_Table.M_TABLE_DYNAMIC_LAMPS); } if ((byteGeneral3 & 0x08) > 0) { msg += getMessageControllerTableError(M_Table.M_TABLE_DYNAMIC_DETECTORS); } if ((byteGeneral3 & 0x10) > 0) { msg += getMessageControllerTableError(M_Table.M_TABLE_DYNAMIC_GENERAL_PARAMETERS); } if ((byteGeneral3 & 0x20) > 0) { msg += getMessageControllerTableError(M_Table.M_TABLE_DYNAMIC_BUTTON_GUARD); } if ((byteGeneral3 & 0x40) > 0) { msg += getMessageControllerTableError(M_Table.M_TABLE_DYNAMIC_ALARMS_CONFIGURATION); } msg += analyseSubcontrollerTablesErrors(1, byte1Subregulador1, byte2Subregulador1, byte3Subregulador1, byte4Subregulador1); msg += analyseSubcontrollerTablesErrors(2, byte1Subregulador2, byte2Subregulador2, byte3Subregulador2, byte4Subregulador2); msg += analyseSubcontrollerTablesErrors(3, byte1Subregulador3, byte2Subregulador3, byte3Subregulador3, byte4Subregulador3); msg += analyseSubcontrollerTablesErrors(4, byte1Subregulador4, byte2Subregulador4, byte3Subregulador4, byte4Subregulador4); if (msg.length() > 0) throw new Exception(msg); } public static List analyseReadTable(M_Message message, int tableCode, boolean hasCRC) throws ServerException, Exception { message.reset(); try { while (message.readByte() != STX); } catch (Exception e) { return(new ArrayList()); } int controller = message.readByte() & 0x7F; if (controller == 0x00) { return(analyseReadTableController(message, hasCRC)); } else { return(analyseReadTableSubcontroller(message, hasCRC)); } } private static List analyseReadTableController(M_Message message, boolean hasCRC) throws ServerException, Exception { int function = message.readByte() & 0x7F; switch (function) { case M_ProtocolWriter.M_READ_TABLE_FULL: { return(getTableDataController(message, hasCRC)); } } return(new ArrayList()); } private static List analyseReadTableSubcontroller(M_Message message, boolean hasCRC) throws ServerException, Exception { int function = message.readByte() & 0x7F; switch (function) { case M_ProtocolWriter.M_READ_TABLE_FULL: { return(getTableDataSubcontroller(message, hasCRC)); } case M_ProtocolWriter.M_READ_TABLE_PARTIAL: { int blockNumber = message.readByte() & 0x7F; return(getTableDataSubcontrollerPartial(message, hasCRC)); } } return(new ArrayList()); } public static M_Plan analyseGetRecordablePlanResponse(M_Message message, int subcontroller) throws ServerException, Exception { try { message.reset(); try { while (message.readByte() != STX); } catch (Exception e) { throw new ServerException(Shared.getMessage("Message not start with 0x02")); } int controller = message.readByte() & 0x7F; if (controller != subcontroller) { throw new ServerException(Shared.getMessage("Message does not correspond to controller")); } int function = message.readByte() & 0x7F; if (function != M_ProtocolWriter.M_RECORDABLE_PLAN_QUERY) { return null; } M_Plan recordablePlan = new M_Plan(); recordablePlan.structure = message.readByte() & 0x7F; recordablePlan.transitionsToTimesTable = message.readByte() & 0x7F; recordablePlan.minimumAndExtensionTimesTable = message.readByte() & 0x7F; int offset1 = message.readByte() & 0x7F; int offset2 = message.readByte() & 0x7F; recordablePlan.offset = (offset1 << 7) + offset2; recordablePlan.controlMode = message.readByte() & 0x7F; while (message.eof() == false) { recordablePlan.lPhaseTime.add(new Integer(message.readByte() & 0x7F)); } return(recordablePlan); } catch (ServerException exception) { throw exception; } catch (Exception exception) { return(null); } } private static List getTableDataController(M_Message message, boolean hasCRC) throws ServerException, Exception { int tableCode = message.readByte() & 0x7F; return(readTable(message, tableCode, hasCRC)); } private static List getTableDataSubcontroller(M_Message message, boolean hasCRC) throws ServerException, Exception { int tableCode = message.readByte() & 0x7F; return(readTable(message, tableCode, hasCRC)); } private static List getTableDataSubcontrollerPartial(M_Message message, boolean hasCRC) throws ServerException, Exception { int tableCode = message.readByte() & 0x7F; return(readTablePartial(message, tableCode, hasCRC)); } private static List readTable(M_Message message, int tableCode, boolean hasCRC) throws ServerException, Exception { List lContent = new ArrayList(); byte[] messageData = message.toByteArray(); int tabledatalen = messageData.length-1; if (hasCRC == true) tabledatalen = messageData.length-2; byte[] tableData = new byte[tabledatalen]; System.arraycopy(messageData, 0, tableData, 0, 2); System.arraycopy(messageData, 3, tableData, 2, tabledatalen-2); if (hasCRC == true) tableData[tableData.length-1] = ETX; for (int i=0; i readTablePartial(M_Message message, int tableCode, boolean hasCRC) throws ServerException, Exception { List lContent = new ArrayList(); byte[] messageData = message.toByteArray(); int tabledatalen = messageData.length-1; if (hasCRC == true) tabledatalen = messageData.length-2; byte[] tableData = new byte[tabledatalen]; System.arraycopy(messageData, 0, tableData, 0, 2); System.arraycopy(messageData, 3, tableData, 2, tabledatalen-2); if (hasCRC == true) tableData[tableData.length-1] = ETX; int indexSTX = 0; for (int i=0; i 0) { lContent.remove(lContent.size()-1); add = false; i += 3; } indexSTX++; } if (add == true) lContent.add(new Byte(tableData[i])); } return(lContent); } public static String analyseSubcontrollerTablesErrors(int subcontroller, int byte1, int byte2, int byte3, int byte4) throws ServerException, Exception { String msg = ""; // Error tables 15 to 21 of subcontroller if ((byte1 & 0x01) > 0) { msg += getMessageSubcontrollerTableError(subcontroller, M_Table.M_TABLE_A_PHASES_STABLES); } if ((byte1 & 0x02) > 0) { msg += getMessageSubcontrollerTableError(subcontroller, M_Table.M_TABLE_B_PHASES_TRANSITIONS); } if ((byte1 & 0x04) > 0) { msg += getMessageSubcontrollerTableError(subcontroller, M_Table.M_TABLE_C_TRANSITIONS); } if ((byte1 & 0x08) > 0) { msg += getMessageSubcontrollerTableError(subcontroller, M_Table.M_TABLE_D_STRUCTURES); } if ((byte1 & 0x10) > 0) { msg += getMessageSubcontrollerTableError(subcontroller, M_Table.M_TABLE_E_ACTION_ON_DEMAND); } if ((byte1 & 0x20) > 0) { msg += getMessageSubcontrollerTableError(subcontroller, M_Table.M_TABLE_F_TRANSITIONS_TO_TIMES); } if ((byte1 & 0x40) > 0) { msg += getMessageSubcontrollerTableError(subcontroller, M_Table.M_TABLE_G_TRANSITIONS_TIMES); } // Error tables 22 to 28 of subcontroller if ((byte2 & 0x01) > 0) { msg += getMessageSubcontrollerTableError(subcontroller, M_Table.M_TABLE_H_MINIMUM_EXTENSION_TIMES); } if ((byte2 & 0x02) > 0) { msg += getMessageSubcontrollerTableError(subcontroller, M_Table.M_TABLE_I_PLANS); } if ((byte2 & 0x04) > 0) { msg += getMessageSubcontrollerTableError(subcontroller, M_Table.M_TABLE_J_EMERGENCY); } if ((byte2 & 0x08) > 0) { msg += getMessageSubcontrollerTableError(subcontroller, M_Table.M_TABLE_K_STARTUP); } if ((byte2 & 0x10) > 0) { msg += getMessageSubcontrollerTableError(subcontroller, M_Table.M_TABLE_L_CONFLICTIVE_TRANSITIONS); } if ((byte2 & 0x20) > 0) { msg += getMessageSubcontrollerTableError(subcontroller, M_Table.M_TABLE_M_TIMETABLE); } if ((byte2 & 0x40) > 0) { msg += getMessageSubcontrollerTableError(subcontroller, M_Table.M_TABLE_N_SPARE_TIMES); } return(msg); } private static String getMessageControllerTableError(int tableCode) throws Exception { switch (tableCode) { case M_Table.M_TABLE_A_GROUPS: return(Shared.getMessage("Error recording controller table") + " : " + Shared.getMessage("Table A Groups") + "\r\n"); case M_Table.M_TABLE_B_PHYSICAL_DETECTORS: return(Shared.getMessage("Error recording controller table") + " : " + Shared.getMessage("Table B Physical Detectors") + "\r\n"); case M_Table.M_TABLE_C_LOGICAL_DETECTORS: return(Shared.getMessage("Error recording controller table") + " : " + Shared.getMessage("Table C Logical Detectors") + "\r\n"); case M_Table.M_TABLE_D_DEMANDS: return(Shared.getMessage("Error recording controller table") + " : " + Shared.getMessage("Table D Demands") + "\r\n"); case M_Table.M_TABLE_E_INCOMPATIBILITIES: return(Shared.getMessage("Error recording controller table") + " : " + Shared.getMessage("Table E Incompatibilities") + "\r\n"); case M_Table.M_TABLE_F_SPECIAL_DAYS: return(Shared.getMessage("Error recording controller table") + " : " + Shared.getMessage("Table F Special Days") + "\r\n"); case M_Table.M_TABLE_G_INTERMITENCES: return(Shared.getMessage("Error recording controller table") + " : " + Shared.getMessage("Table G Intermitences") + "\r\n"); case M_Table.M_TABLE_H_OPERATION_MODE: return(Shared.getMessage("Error recording controller table") + " : " + Shared.getMessage("Table H Operation Mode") + "\r\n"); case M_Table.M_TABLE_I_ALARMS_MANAGEMENT: return(Shared.getMessage("Error recording controller table") + " : " + Shared.getMessage("Table I Alarms Management") + "\r\n"); case M_Table.M_TABLE_DYNAMIC_LAMPS: return(Shared.getMessage("Error recording controller table") + " : " + Shared.getMessage("Table Dynamic Control Lamps") + "\r\n"); case M_Table.M_TABLE_DYNAMIC_DETECTORS: return(Shared.getMessage("Error recording controller table") + " : " + Shared.getMessage("Table Dynamic Control Detectors") + "\r\n"); case M_Table.M_TABLE_DYNAMIC_GENERAL_PARAMETERS: return(Shared.getMessage("Error recording controller table") + " : " + Shared.getMessage("Table Dynamic General Parameters") + "\r\n"); case M_Table.M_TABLE_DYNAMIC_BUTTON_GUARD: return(Shared.getMessage("Error recording controller table") + " : " + Shared.getMessage("Table Dynamic Button Guard Configuration") + "\r\n"); case M_Table.M_TABLE_DYNAMIC_ALARMS_CONFIGURATION: return(Shared.getMessage("Error recording controller table") + " : " + Shared.getMessage("Table Dynamic Alarms Configuration") + "\r\n"); } return(Shared.getMessage("Error recording controller table") + " : " + Shared.getMessage("Unknown table") + " : " + tableCode + "\r\n"); } private static String getMessageSubcontrollerTableError(int subcontroller, int tableCode) throws Exception { switch (tableCode) { case M_Table.M_TABLE_A_PHASES_STABLES: return(Shared.getMessage("Error recording subcontroller %1, table").replaceAll("%1", "" + subcontroller) + " " + Shared.getMessage("Table A Phases Stables") + "\r\n"); case M_Table.M_TABLE_B_PHASES_TRANSITIONS: return(Shared.getMessage("Error recording subcontroller %1, table").replaceAll("%1", "" + subcontroller) + " " + Shared.getMessage("Table B Phases Transitions") + "\r\n"); case M_Table.M_TABLE_C_TRANSITIONS: return(Shared.getMessage("Error recording subcontroller %1, table").replaceAll("%1", "" + subcontroller) + " " + Shared.getMessage("Table C Transitions")) + "\r\n"; case M_Table.M_TABLE_D_STRUCTURES: return(Shared.getMessage("Error recording subcontroller %1, table").replaceAll("%1", "" + subcontroller) + " " + Shared.getMessage("Table D Structures")) + "\r\n"; case M_Table.M_TABLE_E_ACTION_ON_DEMAND: return(Shared.getMessage("Error recording subcontroller %1, table").replaceAll("%1", "" + subcontroller) + " " + Shared.getMessage("Table E Action on Demand") + "\r\n"); case M_Table.M_TABLE_F_TRANSITIONS_TO_TIMES: return(Shared.getMessage("Error recording subcontroller %1, table").replaceAll("%1", "" + subcontroller) + " " + Shared.getMessage("Table F Transition to Times") + "\r\n"); case M_Table.M_TABLE_G_TRANSITIONS_TIMES: return(Shared.getMessage("Error recording subcontroller %1, table").replaceAll("%1", "" + subcontroller) + " " + Shared.getMessage("Table G Transitions Times") + "\r\n"); case M_Table.M_TABLE_H_MINIMUM_EXTENSION_TIMES: return(Shared.getMessage("Error recording subcontroller %1, table").replaceAll("%1", "" + subcontroller) + " " + Shared.getMessage("Table H Minimum Extension Times") + "\r\n"); case M_Table.M_TABLE_I_PLANS: return(Shared.getMessage("Error recording subcontroller %1, table").replaceAll("%1", "" + subcontroller) + " " + Shared.getMessage("Table I Plans") + "\r\n"); case M_Table.M_TABLE_J_EMERGENCY: return(Shared.getMessage("Error recording subcontroller %1, table").replaceAll("%1", "" + subcontroller) + " " + Shared.getMessage("Table J Emergency") + "\r\n"); case M_Table.M_TABLE_K_STARTUP: return(Shared.getMessage("Error recording subcontroller %1, table").replaceAll("%1", "" + subcontroller) + " " + Shared.getMessage("Table K Startup") + "\r\n"); case M_Table.M_TABLE_L_CONFLICTIVE_TRANSITIONS: return(Shared.getMessage("Error recording subcontroller %1, table").replaceAll("%1", "" + subcontroller) + " " + Shared.getMessage("Table L Conflictive Transitions") + "\r\n"); case M_Table.M_TABLE_M_TIMETABLE: return(Shared.getMessage("Error recording subcontroller %1, table").replaceAll("%1", "" + subcontroller) + " " + Shared.getMessage("Table M Timetable") + "\r\n"); case M_Table.M_TABLE_N_SPARE_TIMES: return(Shared.getMessage("Error recording subcontroller %1, table").replaceAll("%1", "" + subcontroller) + " " + Shared.getMessage("Table N Spare Times") + "\r\n"); } return(Shared.getMessage("Error recording subcontroller %1, table").replaceAll("%1", "" + subcontroller) + " " + Shared.getMessage("Unknown table") + " : " + tableCode + "\r\n"); } }