繁体   English   中英

无法在Azure IoT中心Java上创建模拟的设备应用程序

[英]Unable to Create a simulated device app on Azure IoT hub Java

我是Azure IoT中心的新手。 我正在使用Java进行开发,并且首先要学习教程。 我能够创建设备身份,并且我从云模块接收的消息也可以正常执行。 但是,当我尝试执行设备仿真模块时,我得到:

错误:错误{condition = amqp:connection:framing-error,description ='org.apache.qpid.proton.engine.TransportException:连接已终止',info = null}

错误:org.apache.qpid.proton.engine.TransportException:连接异常终止

我认为这与AMQPS配置有关,但不确定在这里出什么问题。

有人遇到过这样的问题吗?

@ UmerF92,感谢您的耐心配合。 您能否在您的环境中尝试以下代码? 该示例遵循了教程( https://azure.microsoft.com/zh-cn/documentation/articles/iot-hub-java-java-getstarted/ ),并且在我这一方面工作正常。

private static String connString = "HostName=<your hub>.azure-devices.net;"
            + "DeviceId=< your device >;"
            + "SharedAccessKey=<your share key >";
    private static IotHubClientProtocol protocol = IotHubClientProtocol.AMQPS;//or HTTPS
    private static boolean stopThread = false;


private static String DEVICEID="<your device id>";

private static class TelemetryDataPoint {
    public String deviceId;
    public double windSpeed;

    public String serialize() {
        Gson gson = new Gson();
        return gson.toJson(this);
    }
}

protected static class EventCallback implements IotHubEventCallback {
    public void execute(IotHubStatusCode statusCode, Object context) {
        System.out.println("IoT Hub responded to message with status " + statusCode.name());

        if (context != null) {
            synchronized (context) {
                context.notify();
            }
        }
    }
}

private static class MessageSender implements Runnable {
    public volatile boolean stopThread = false;

    public void run() {
        try {
            double avgWindSpeed = 10;
            Random rand = new Random();
            DeviceClient client;

            client = new DeviceClient(connString, protocol);
            client.open();

            while (!stopThread) {
                double currentWindSpeed = avgWindSpeed + rand.nextDouble() * 4 - 2;
                TelemetryDataPoint telemetryDataPoint = new TelemetryDataPoint();

                telemetryDataPoint.deviceId = DEVICEID;
                telemetryDataPoint.windSpeed = currentWindSpeed;

                String msgStr = telemetryDataPoint.serialize();
                Message msg = new Message(msgStr);

                System.out.println(msgStr);

                Object lockobj = new Object();
                EventCallback callback = new EventCallback();

                client.sendEventAsync(msg, callback, lockobj);

                synchronized (lockobj) {
                    lockobj.wait();
                }

                Thread.sleep(1000);
            }
            client.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

public static void main(String[] args) throws IOException, URISyntaxException, InterruptedException {
    MessageSender messageSender = new MessageSender();
    Thread t0 = new Thread(messageSender);

    t0.start();

    System.out.println("Press ENTER to exit...");
    System.in.read();
    messageSender.stopThread = true;
    t0.join();
} 

请尝试一下。

任何更新或疑虑,请让我知道。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM