2017-04-27 4 views
1

IoTモデムのボタンを押してデータを取得しているjavaファイルを作成しました。 IoTデバイスはPubNub上にプロトコルデータを公開しており、これを購読した後でPubNubからデータを取得しています。私の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をイベントカウントを押した場合:ここで

は私の全体のコードです異なってくる。ここで

私の出力です:プロトコル・データから実行ここで

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 

、後の第二の時間を取得している

1回目

実行後デバイスは両方の出力に対して同じです。

コールバック()に問題はありますか?

+1

あなたのpub/subキーセットを編集しました。あなたのキーセットでAccess Managerを有効にしない限り、それらを世界に公開するべきではありません。すべてのコードを確認する前に、なぜPubNub Java SDKのv4を使用していないのか尋ねてください。 v3はすぐにEOLに向かいます。 –

+1

次の質問...あなたは、デバイスIDとイベント数だけが異なると言っています。あなたは何を期待していますか?データ値のより多くのものが異なっていなければならないか、あるいは何も変わらないはずですか?また、パブリッシュするデータがサブスクライブで受け取ったデータと同じ場合、PubNubはもはや関与しておらず、アプリケーションのビジネスロジックを持つものでなければなりません。あなたのデータであなたが期待していることを私に教えてください。私はこれを掘り下げて検討していきます。 –

+1

私はPubNubを初めて使っていますが、pubnubのgithubテスト用Javaファイルを読んでいますが、v4 SDKに記載されているメソッドを使用することができませんでした。 – Shambhu

答えて

0

デバイスIDとイベントカウンタの値が以前の値と現在の値を使用していて、プロセス変換後に大きな値が割り当てられていました。

successCallback()内のすべての変数を宣言すると、このブロックを呼び出すたびに、プロセス全体の実行後にすべての変数の値がゼロになります。すべて正しい値を持ちます。

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 
    } 
    . . . . 
    . . . . 
    . . . . 
} 
関連する問題