\r
import java.util.ArrayList;\r
\r
+import org.apache.commons.io.output.ByteArrayOutputStream;\r
import org.apache.logging.log4j.LogManager;\r
import org.apache.logging.log4j.Logger;\r
\r
static private final byte[] ACK = { (byte) 0x10, (byte) 0x01 };\r
//CF A6 BC NN DATA NN=0 -> IDs are in added list, NN=1 -> IDs are in deleted list\r
static private final byte[] CHANGE_NOTIFICATION_RESP = { (byte) 0xCF, (byte) 0xA6 };\r
+ static private final String CHANGE_NOTIFICATION_RESP_S = "CFA6";\r
//List First ID List (C1 4C)\r
static private final byte[] LIST_FIRST_ID_LIST_ADDED = { (byte) 0xC1, (byte) 0x4C, (byte) 0x02 }; //2 added, 3 deleted\r
static private final byte[] LIST_FIRST_ID_LIST_DELETED = { (byte) 0xC1, (byte) 0x4C, (byte) 0x03 }; //2 added, 3 deleted\r
}\r
}\r
\r
- private byte[] readBytes(int numberOfBytes) throws Exception {\r
+ private byte[] readBytesOLD(int numberOfBytes) throws Exception {\r
try {\r
byte[] ret = new byte[numberOfBytes];\r
this.connection.read(ret, 0, numberOfBytes);\r
}\r
}\r
\r
+ private byte[] readBytes(int numberOfBytes) throws Exception {\r
+ byte[] ret = new byte[numberOfBytes];\r
+ try {\r
+ for(int c=0; c<numberOfBytes; c++){\r
+ ret[c] = (byte)this.connection.read();\r
+ }\r
+ } catch (Exception e) {\r
+ restartListener();\r
+ throw e;\r
+ }\r
+ return ret;\r
+ }\r
+ \r
+ \r
public void removeClipEventListener(ClipEventListener listener) {\r
this.clipEventListeners.remove(listener);\r
}\r
startListener();\r
}\r
\r
+ private ArrayList<Integer> notificationList = new ArrayList<Integer>();\r
+ \r
+ \r
@Override\r
public void run() {\r
try {\r
byte[] buffer = readBytes(2);\r
logger.info("Nexio MediaListener is waiting for events..");\r
\r
+ \r
while (isRunning) {\r
try {\r
- //CHANGE_NOTIFICATION_RESP (BLOCKING READ!)\r
- buffer = readBytes(4); //CFA6 BC NN->0=added, 1=deleted\r
- int notificationNumber = buffer[3];\r
- logger.debug("Nexio MediaListener notification: " + bytesToHex(buffer));\r
-\r
- //SEND ACK\r
- // writeBytes(ACK);\r
-\r
- //LIST_FIRST_ID_LIST\r
- /*\r
- 0x01 Main ID Handle List D8 4C ID\r
- 0x02 ID Handles Added List D8 4C ID\r
- 0x03 ID Handles Deleted List D8 4C ID\r
- 0x11 Main Extended ID List DF 4C BC XID *\r
- 0x12 Extended IDs Added List DF 4C BC XID *\r
- 0x13 Extended IDs Deleted List DF 4C BC XID *\r
- 0x21 Main ID Handle/Extended ID List DF 4C BC ID XID *\r
- 0x22 ID Handles/Extended IDs Added List DF 4C BC ID XID *\r
- 0x23 ID Handles/Extended IDs Deleted List DF 4C BC ID XID *\r
- */\r
-\r
- boolean handled = false;\r
- if (notificationNumber == NN_ID_ADDED) {\r
- writeBytes(LIST_FIRST_ID_LIST_ADDED);\r
- handled = true;\r
- }\r
-\r
- if (notificationNumber == NN_ID_DELETED) {\r
- //SEND ACK\r
- writeBytes(ACK);\r
- writeBytes(LIST_FIRST_ID_LIST_DELETED);\r
- handled = true;\r
- }\r
-\r
- if (!handled) {\r
- writeBytes(ACK);\r
- logger.debug("Unhandled notification: {}", bytesToHex(buffer));\r
- continue;\r
- }\r
\r
+ //BLOKKOLVA addig olvassa fel es gyujti a notification-oket amig mas adat nem jon.\r
+ //az elso alkalommal amikor nem notification-t kap, visszater.\r
+ buffer = collectNotifications(false);\r
+ \r
//LIST_FIRST_ID_LIST response\r
- buffer = readBytes(2);\r
if (LIST_FIRST_ID_LIST_FOUND.equals(bytesToHex(buffer))) {\r
-\r
+ int notificationNumber = notificationList.get(0);\r
+ notificationList.remove(0);\r
+ \r
//read first id\r
buffer = readBytes(8);\r
String stringId = new String(buffer);\r
\r
//read next ids\r
writeBytes(LIST_NEXT_ID_LIST);\r
- for (buffer = readBytes(2); LIST_NEXT_ID_LIST_FOUND.equals(bytesToHex(buffer)) && isRunning; buffer = readBytes(2)) {\r
+ //for (buffer = readBytes(2); LIST_NEXT_ID_LIST_FOUND.equals(bytesToHex(buffer)) && isRunning; buffer = readBytes(2)) {\r
+ for (buffer = collectNotifications(true); LIST_NEXT_ID_LIST_FOUND.equals(bytesToHex(buffer)) && isRunning; buffer = collectNotifications(true)) {\r
\r
byte[] idBuffer = readBytes(8);\r
stringId = new String(idBuffer);\r
logger.debug("Nexio MediaListener end of id list: " + bytesToHex(buffer));\r
//SEND ACK\r
writeBytes(ACK);\r
-\r
- } else if (LIST_FIRST_ID_LIST_NOT_FOUND.equals(bytesToHex(buffer))) {\r
+ \r
+ }else\r
+ if (LIST_FIRST_ID_LIST_NOT_FOUND.equals(bytesToHex(buffer))) {\r
+ notificationList.remove(0);\r
logger.debug("Nexio MediaListener LIST_FIRST_ID_LIST ID not found!");\r
- } else {\r
+ }else{\r
logger.debug("Nexio MediaListener unknown answer has arrived for LIST_FIRST_ID_LIST: " + bytesToHex(buffer));\r
}\r
} catch (Exception exc) {\r
}\r
}\r
\r
+ \r
+ private byte[] collectNotifications(boolean dontAnswer) throws Exception{\r
+\r
+ //kov. notificationnek megfelelo listazas kerese\r
+ if(notificationList.size() > 0 && !dontAnswer){\r
+ handleNotification(notificationList.get(0));\r
+ }\r
+ \r
+ //tovabbi notification-ok gyujtese\r
+ //CHANGE_NOTIFICATION_RESP (BLOCKING READ!) ********\r
+ byte[] buffer = null;\r
+ for(buffer = readBytes(2); CHANGE_NOTIFICATION_RESP_S.equals(bytesToHex(buffer)); buffer = readBytes(2)){\r
+ buffer = readBytes(2);\r
+ int notNumber = buffer[1];\r
+ notificationList.add(notNumber);\r
+ logger.debug("Nexio MediaListener notification: CFA6" + bytesToHex(buffer));\r
+ \r
+ //csak igy jon meg a kesobb kezelt LIST_FIRST_ID_LIST\r
+ if(notificationList.size() == 1 && !dontAnswer){\r
+ handleNotification(notNumber);\r
+ }\r
+ }\r
+ return buffer;\r
+ }\r
+ \r
+ \r
+ private void handleNotification(int notificationNumber) throws Exception{\r
+ //LIST_FIRST_ID_LIST\r
+ /*\r
+ 0x01 Main ID Handle List D8 4C ID\r
+ 0x02 ID Handles Added List D8 4C ID\r
+ 0x03 ID Handles Deleted List D8 4C ID\r
+ 0x11 Main Extended ID List DF 4C BC XID *\r
+ 0x12 Extended IDs Added List DF 4C BC XID *\r
+ 0x13 Extended IDs Deleted List DF 4C BC XID *\r
+ 0x21 Main ID Handle/Extended ID List DF 4C BC ID XID *\r
+ 0x22 ID Handles/Extended IDs Added List DF 4C BC ID XID *\r
+ 0x23 ID Handles/Extended IDs Deleted List DF 4C BC ID XID *\r
+ */\r
+ if (notificationNumber == NN_ID_ADDED) {\r
+ writeBytes(LIST_FIRST_ID_LIST_ADDED);\r
+ }else\r
+ if (notificationNumber == NN_ID_DELETED) {\r
+ //SEND ACK\r
+ writeBytes(ACK);\r
+ writeBytes(LIST_FIRST_ID_LIST_DELETED);\r
+ }\r
+ \r
+ }\r
+ \r
public void startListener() {\r
try {\r
createNexioConnection();\r