2017-04-27 1 views
1

IoT 모뎀의 버튼을 누른 후 데이터를 가져 오는 Java 파일을 만들었습니다. IoT 장치는 PubNub을 통해 프로토콜 데이터를 게시하고이 구독을 신청 한 후 PubNub의 Java 프로그램에서 데이터를 가져옵니다.내 Java 프로그램을 실행 한 후 pubnub에서 올바로 게시 된 데이터를 가져온 후 올바르지 않음

수신 된 데이터는 ASCII 표현의 16 진수 값입니다.

하나의 문자열로 프로토콜 데이터를 가져오고 char 배열로 변환하면 배열에서 배열의 일부를 가져 와서 10 진수 값으로 변환합니다.

프로그램을 실행 한 직후 올바른 데이터를 얻었지만 나중에 데이터가 올바르지 않습니다. 나는 이것을 실행하면

import com.pubnub.api.Callback; 
import com.pubnub.api.Pubnub; 
import com.pubnub.api.PubnubException; 

public class SubscribeTest { 
    String PUB_KEY = "pub-c-a2a1d4ab..."; 
    String SUB_KEY = "sub-c-0ce5f84a..."; 
    String CHANNEL = "IOT1"; 
    Pubnub pn = new Pubnub(PUB_KEY,SUB_KEY); 

    String receivedData = new String(); //to store the subscribed data 
    char[] receivedDataInArray = new char[50]; //to store the converted string 

    int deviceId = 0; 
    int functionCode = 0; 
    int productId =0; 
    int timeHour = 0; 
    int timeMin = 0; 
    int dateDay = 0; 
    int dateMonth = 0; 
    int eventCount = 0; 


    public void subTest() throws PubnubException { 
     try { 

      System.out.println("Subscribed"); 
      pn.subscribe(CHANNEL, new Callback() { 

       @Override 
       public void successCallback(String arg0, Object arg1) { 


        System.out.println(arg1); 
        receivedData = (String)arg1; //subscribed data stored 
        System.out.println("DATA :-" + receivedData); 
        System.out.println("Message length : "+receivedData.length()); 

        for(int i=0; i<receivedData.length(); i++) { 
         receivedDataInArray[i] = receivedData.charAt(i);  //Received data converted into array 
        } 

        System.out.println(receivedDataInArray); 

        int tmp = 0; 
        for (int j = 0; j < 4 ; j++) { 

         if (j < 3) { 
          if(receivedDataInArray[j] > 0x39) { 
           if(receivedDataInArray[j] == 'A') { 
            tmp = 0x0A; 
           }else if(receivedDataInArray[j] == 'B') { 
            tmp = 0x0B; 
           }else if(receivedDataInArray[j] == 'C') { 
            tmp = 0x0C; 
           }else if(receivedDataInArray[j] == 'D') { 
            tmp = 0x0D; 
           }else if(receivedDataInArray[j] == 'E') { 
            tmp = 0x0E; 
           }else if(receivedDataInArray[j] == 'F') { 
            tmp = 0x0F; 
           } 

           deviceId |= tmp; 
           deviceId <<= 4; 

          } else { 
           tmp=receivedDataInArray[j]-0x30; 
           deviceId |= tmp; 
           deviceId <<= 4; 
          } 
         }else { 
          if (receivedDataInArray[j] > 0x39) { 
           if (receivedDataInArray[j] == 'A') { 
            tmp = 0x0A; 
           }else if(receivedDataInArray[j] == 'B') { 
            tmp = 0x0B; 
           }else if(receivedDataInArray[j] == 'C') { 
            tmp = 0x0C; 
           }else if(receivedDataInArray[j] == 'D') { 
            tmp = 0x0D; 
           }else if(receivedDataInArray[j] == 'E') { 
            tmp = 0x0E; 
           }else if(receivedDataInArray[j] == 'F') { 
            tmp = 0x0F; 
           } 

           deviceId |= tmp; 
          } else { 
           tmp = receivedDataInArray[j]-0x30; 
           deviceId |= tmp; 
          } 
         } 
        } 
        System.out.println("device ID:--"+deviceId); 

        //for Product Id 
        tmp = receivedDataInArray[10]-0x30; 
        productId = tmp; 
        productId <<= 4; 
        tmp = receivedDataInArray[11]-0x30; 
        productId |= tmp; 

        //for Time Hour 
        tmp = receivedDataInArray[12]-0x30; 
        timeHour = tmp*10; 
        //timeHour <<= 4; 
        tmp = receivedDataInArray[13]-0x30; 
        timeHour += tmp; 

        //for Time Minute 
        tmp = receivedDataInArray[14]-0x30; 
        timeMin = tmp*10; 
        //timeMin <<= 4; 
        tmp = receivedDataInArray[15]-0x30; 
        timeMin += tmp; 

        //for Date Day 
        tmp = receivedDataInArray[16]-0x30; 
        dateDay = tmp*10; 
        //date <<= 4; 
        tmp = receivedDataInArray[17]-0x30; 
        dateDay += tmp; 

        //for month 
        tmp = receivedDataInArray[18]-0x30; 
        dateMonth = tmp*10; 
        //dateMonth <<= 4; 
        tmp = receivedDataInArray[19]-0x30; 
        dateMonth += tmp; 

        //for event count 
        for (int j = 20; j < 24 ; j++) { 

         if (j < 23) { 
          if(receivedDataInArray[j] > 0x39) { 
           if(receivedDataInArray[j] == 'A') { 
            tmp=0x0A; 
           }else if(receivedDataInArray[j] == 'B') { 
            tmp=0x0B; 
           }else if(receivedDataInArray[j] == 'C') { 
            tmp=0x0C; 
           }else if(receivedDataInArray[j] == 'D') { 
            tmp =0x0D; 
           }else if(receivedDataInArray[j] == 'E') { 
            tmp=0x0E; 
           }else if(receivedDataInArray[j] == 'F') { 
            tmp=0x0F; 
           } 

           eventCount |= tmp; 
           eventCount <<= 4; 

          } else { 
           tmp=receivedDataInArray[j]-0x30; 
           eventCount |= tmp; 
           eventCount <<= 4; 
          } 
         }else { 
          if (receivedDataInArray[j] > 0x39) { 
           if (receivedDataInArray[j] == 'A') { 
            tmp = 0x0A; 
           }else if(receivedDataInArray[j] == 'B') { 
            tmp = 0x0B; 
           }else if(receivedDataInArray[j] == 'C') { 
            tmp = 0x0C; 
           }else if(receivedDataInArray[j] == 'D') { 
            tmp = 0x0D; 
           }else if(receivedDataInArray[j] == 'E') { 
            tmp = 0x0E; 
           }else if(receivedDataInArray[j] == 'F') { 
            tmp = 0x0F; 
           } 
           eventCount |= tmp; 
          } else { 
           tmp = receivedDataInArray[j]-0x30; 
           eventCount |= tmp; 
          } 
         } 

        } 


        System.out.println("***Received data from device***"+"\n\n"+ 
          "Device id : "+deviceId+ 
          "\nProduct Id: "+productId+ 
          "\nTime Hour: "+timeHour+ 
          "\nTime Minutes: "+timeMin+ 
          "\nDate Day: "+dateDay+ 
          "\nDate Month: "+dateMonth+ 
          "\nEvent Count: "+eventCount); 
       } 

      }); 

     }catch (PubnubException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
    public static void main(String[] args) throws PubnubException { 

     new SubscribeTest().subTest(); 
    } 

} 

는, 만약 IoT 모뎀의 버튼을 누른 후 나는 정확한 데이터를 얻고 그러나 나는 다시 누르면 만 소자 (Id)이벤트가를 계산 : 여기

내 전체 코드입니다 달랐다. 여기

내 출력 : 실행 후

1 시간

2 시간 후 실행 여기에

167303080003063001020001 
DATA :-167303080003063001020001 
Message length : 24 
167303080003063001020001 
device ID:--23541363 
***Received data from device*** 

Device id : 23541363 
Product Id: 3 
Time Hour: 6 
Time Minutes: 30 
Date Day: 1 
Date Month: 2 
Event Count: 4097 

, 167,303080003063001020001부터 점점 프로토콜 데이터입니다 장치가 두 출력 모두 동일합니다.

콜백()에 문제가 있습니까?

+1

귀하의 pub/sub 키 세트가 수정되었습니다. 키 세트에서 Access Manager를 사용 가능하게 설정하지 않으면 절대 해당 항목을 세계에 공개해서는 안됩니다. 모든 코드를 검토하기 전에 PubNub Java SDK의 v4를 사용하지 않는 이유를 묻습니다. v3가 곧 EOL로갑니다. –

+1

다음 질문 ... 당신은 단지 장치 ID와 이벤트 개수가 다르다고 말합니다. 기대는 무엇입니까? 데이터 값이 더 많아 지거나 차이가 없어야합니까? 또한 게시중인 데이터가 구독시 수신하는 데이터와 동일하면 PubNub는 더 이상 관련되지 않으며 앱의 비즈니스 로직을 사용해야합니다. 귀하가 귀하의 데이터에서 기대하고있는 것을 알려주십시오. 계속 드릴 다운하겠습니다. –

+1

나는 PubNub에 익숙하지 않은데 pubnub의 github 테스트 자바 파일을 보았지만 v4 SDK에 설명 된 방법을 사용할 수 없었다. – Shambhu

답변

0

장치 ID 및 이벤트 카운터의 값이 이전 값과 현재 값을 취하고 프로세스 변환 후 큰 값이 할당되었습니다.

successCallback() 안에 모든 변수를 선언하면이 블록을 호출 할 때마다 전체 변수를 모두 실행 한 후에 모든 변수 값이 0이됩니다. 모두 올바른 값을 갖게됩니다.

public void successCallback(String arg0, Object arg1) { 

    int deviceId = 0; 
    int functionCode = 0; 
    int productId =0; 
    int timeHour = 0; 
    int timeMin = 0; 
    int dateDay = 0; 
    int dateMonth = 0; 
    int eventCount = 0; 

    System.out.println(arg1); 
    receivedData = (String)arg1; //subscribed data stored 
    System.out.println("DATA :-" + receivedData); 
    System.out.println("Message length : "+receivedData.length()); 

    for(int i=0; i<receivedData.length(); i++) { 
     receivedDataInArray[i] = receivedData.charAt(i);  //Received data converted into array 
    } 
    . . . . 
    . . . . 
    . . . . 
} 
관련 문제