From e357aa4d8dd705e1c90c728120cf2a309f60eb1a Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rendek=20R=C3=B3bert?= Date: Thu, 5 Oct 2017 12:08:41 +0000 Subject: [PATCH] git-tfs-id: [http://tfs.userrendszerhaz.hu:8080/tfs/DefaultCollection]$/MediaCube;C30539 --- .../user/commons/nexio/api/MediaListener.java | 131 ++++++++++++------ 1 file changed, 85 insertions(+), 46 deletions(-) diff --git a/server/user.jobengine.osgi.commons/src/user/commons/nexio/api/MediaListener.java b/server/user.jobengine.osgi.commons/src/user/commons/nexio/api/MediaListener.java index e04d2017..84356e03 100644 --- a/server/user.jobengine.osgi.commons/src/user/commons/nexio/api/MediaListener.java +++ b/server/user.jobengine.osgi.commons/src/user/commons/nexio/api/MediaListener.java @@ -2,6 +2,7 @@ package user.commons.nexio.api; import java.util.ArrayList; +import org.apache.commons.io.output.ByteArrayOutputStream; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -31,6 +32,7 @@ public class MediaListener implements Runnable { static private final byte[] ACK = { (byte) 0x10, (byte) 0x01 }; //CF A6 BC NN DATA NN=0 -> IDs are in added list, NN=1 -> IDs are in deleted list static private final byte[] CHANGE_NOTIFICATION_RESP = { (byte) 0xCF, (byte) 0xA6 }; + static private final String CHANGE_NOTIFICATION_RESP_S = "CFA6"; //List First ID List (C1 4C) static private final byte[] LIST_FIRST_ID_LIST_ADDED = { (byte) 0xC1, (byte) 0x4C, (byte) 0x02 }; //2 added, 3 deleted static private final byte[] LIST_FIRST_ID_LIST_DELETED = { (byte) 0xC1, (byte) 0x4C, (byte) 0x03 }; //2 added, 3 deleted @@ -96,7 +98,7 @@ public class MediaListener implements Runnable { } } - private byte[] readBytes(int numberOfBytes) throws Exception { + private byte[] readBytesOLD(int numberOfBytes) throws Exception { try { byte[] ret = new byte[numberOfBytes]; this.connection.read(ret, 0, numberOfBytes); @@ -107,6 +109,20 @@ public class MediaListener implements Runnable { } } + private byte[] readBytes(int numberOfBytes) throws Exception { + byte[] ret = new byte[numberOfBytes]; + try { + for(int c=0; c notificationList = new ArrayList(); + + @Override public void run() { try { @@ -138,52 +157,19 @@ public class MediaListener implements Runnable { byte[] buffer = readBytes(2); logger.info("Nexio MediaListener is waiting for events.."); + while (isRunning) { try { - //CHANGE_NOTIFICATION_RESP (BLOCKING READ!) - buffer = readBytes(4); //CFA6 BC NN->0=added, 1=deleted - int notificationNumber = buffer[3]; - logger.debug("Nexio MediaListener notification: " + bytesToHex(buffer)); - - //SEND ACK - // writeBytes(ACK); - - //LIST_FIRST_ID_LIST - /* - 0x01 Main ID Handle List D8 4C ID - 0x02 ID Handles Added List D8 4C ID - 0x03 ID Handles Deleted List D8 4C ID - 0x11 Main Extended ID List DF 4C BC XID * - 0x12 Extended IDs Added List DF 4C BC XID * - 0x13 Extended IDs Deleted List DF 4C BC XID * - 0x21 Main ID Handle/Extended ID List DF 4C BC ID XID * - 0x22 ID Handles/Extended IDs Added List DF 4C BC ID XID * - 0x23 ID Handles/Extended IDs Deleted List DF 4C BC ID XID * - */ - - boolean handled = false; - if (notificationNumber == NN_ID_ADDED) { - writeBytes(LIST_FIRST_ID_LIST_ADDED); - handled = true; - } - - if (notificationNumber == NN_ID_DELETED) { - //SEND ACK - writeBytes(ACK); - writeBytes(LIST_FIRST_ID_LIST_DELETED); - handled = true; - } - - if (!handled) { - writeBytes(ACK); - logger.debug("Unhandled notification: {}", bytesToHex(buffer)); - continue; - } + //BLOKKOLVA addig olvassa fel es gyujti a notification-oket amig mas adat nem jon. + //az elso alkalommal amikor nem notification-t kap, visszater. + buffer = collectNotifications(false); + //LIST_FIRST_ID_LIST response - buffer = readBytes(2); if (LIST_FIRST_ID_LIST_FOUND.equals(bytesToHex(buffer))) { - + int notificationNumber = notificationList.get(0); + notificationList.remove(0); + //read first id buffer = readBytes(8); String stringId = new String(buffer); @@ -192,7 +178,8 @@ public class MediaListener implements Runnable { //read next ids writeBytes(LIST_NEXT_ID_LIST); - for (buffer = readBytes(2); LIST_NEXT_ID_LIST_FOUND.equals(bytesToHex(buffer)) && isRunning; buffer = readBytes(2)) { + //for (buffer = readBytes(2); LIST_NEXT_ID_LIST_FOUND.equals(bytesToHex(buffer)) && isRunning; buffer = readBytes(2)) { + for (buffer = collectNotifications(true); LIST_NEXT_ID_LIST_FOUND.equals(bytesToHex(buffer)) && isRunning; buffer = collectNotifications(true)) { byte[] idBuffer = readBytes(8); stringId = new String(idBuffer); @@ -204,10 +191,12 @@ public class MediaListener implements Runnable { logger.debug("Nexio MediaListener end of id list: " + bytesToHex(buffer)); //SEND ACK writeBytes(ACK); - - } else if (LIST_FIRST_ID_LIST_NOT_FOUND.equals(bytesToHex(buffer))) { + + }else + if (LIST_FIRST_ID_LIST_NOT_FOUND.equals(bytesToHex(buffer))) { + notificationList.remove(0); logger.debug("Nexio MediaListener LIST_FIRST_ID_LIST ID not found!"); - } else { + }else{ logger.debug("Nexio MediaListener unknown answer has arrived for LIST_FIRST_ID_LIST: " + bytesToHex(buffer)); } } catch (Exception exc) { @@ -220,6 +209,56 @@ public class MediaListener implements Runnable { } } + + private byte[] collectNotifications(boolean dontAnswer) throws Exception{ + + //kov. notificationnek megfelelo listazas kerese + if(notificationList.size() > 0 && !dontAnswer){ + handleNotification(notificationList.get(0)); + } + + //tovabbi notification-ok gyujtese + //CHANGE_NOTIFICATION_RESP (BLOCKING READ!) ******** + byte[] buffer = null; + for(buffer = readBytes(2); CHANGE_NOTIFICATION_RESP_S.equals(bytesToHex(buffer)); buffer = readBytes(2)){ + buffer = readBytes(2); + int notNumber = buffer[1]; + notificationList.add(notNumber); + logger.debug("Nexio MediaListener notification: CFA6" + bytesToHex(buffer)); + + //csak igy jon meg a kesobb kezelt LIST_FIRST_ID_LIST + if(notificationList.size() == 1 && !dontAnswer){ + handleNotification(notNumber); + } + } + return buffer; + } + + + private void handleNotification(int notificationNumber) throws Exception{ + //LIST_FIRST_ID_LIST + /* + 0x01 Main ID Handle List D8 4C ID + 0x02 ID Handles Added List D8 4C ID + 0x03 ID Handles Deleted List D8 4C ID + 0x11 Main Extended ID List DF 4C BC XID * + 0x12 Extended IDs Added List DF 4C BC XID * + 0x13 Extended IDs Deleted List DF 4C BC XID * + 0x21 Main ID Handle/Extended ID List DF 4C BC ID XID * + 0x22 ID Handles/Extended IDs Added List DF 4C BC ID XID * + 0x23 ID Handles/Extended IDs Deleted List DF 4C BC ID XID * + */ + if (notificationNumber == NN_ID_ADDED) { + writeBytes(LIST_FIRST_ID_LIST_ADDED); + }else + if (notificationNumber == NN_ID_DELETED) { + //SEND ACK + writeBytes(ACK); + writeBytes(LIST_FIRST_ID_LIST_DELETED); + } + + } + public void startListener() { try { createNexioConnection(); -- 2.54.0