gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 11:32:20 24 4
gpt4 key购买 nike

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

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

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

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

这是我的完整代码:

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是从设备获取的协议(protocol)数据对于两个输出都是相同的。

Callback()有什么问题吗?

最佳答案

设备 ID 和事件计数器的值采用先前值和当前值,并且在进程转换后,它被分配一个巨大的值。

因此,如果我在 successCallback() 中声明所有变量,那么每次调用此 block 后,所有变量的值将在整个过程执行后变为零,所有变量都将具有正确的值。

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
}
. . . .
. . . .
. . . .
}

关于java - 执行我的 java 程序后,从 pubnub 获取正确的发布数据,但后来不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43657235/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com