执行我的 java 程序后,从 pubnub 获取正确的发布数据但后来不正确

After execution of my java program getting correct published data from pubnub but later on incorrect

我创建了一个 java 文件,它在按下 IoT 调制解调器上的按钮后获取数据。 IoT 设备通过 PubNub 发布协议数据,订阅后我从 PubNub 的 java 程序中获取数据。

接收到的数据是 ASCII 表示的十六进制值。

我将协议数据作为单个字符串获取并转换为 char 数组,然后从数组中获取数组的一部分并转换为十进制值。

程序执行后我得到了正确的数据,但后来数据不正确。

这是我的全部代码:

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:--5747
***Received data from device***

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

第二次执行后

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

此处,167303080003063001020001 是从设备获取的协议数据对于两个输出都是相同的。

Callback()有什么问题吗?

Device id 和 Event counter 的值取的是前值和当前值,经过进程转换后被赋予了一个巨大的值。

因此,如果我在 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
    }
    .   .   .   .
    .   .   .   .
    .   .   .   .
}