examples/iot-nirvana/client/src/main/java/com/google/cloud/demo/iot/nirvana/client/IotDevice.java
/**
   * Generate and publish a temperature messages to Cloud IoT Core with a 10% variance around a
   * given temperature value and with a frequency of 1 message/second.
   */
Generate and publish a temperature messages to Cloud IoT Core with a 10% variance around a
given temperature value and with a frequency of 1 message/second.
Generate and publish a temperature messages to Cloud IoT Core with a 10% variance around a given temperature value and with a frequency of 1 message / second .
private void publishMessages(City city) throws ClientException, FormatException {
    try {
      String mqttTopic = generateMqttTopic();
      MqttClient client = createAndConnectMqttClient();
      while (true) {
        double variance = MIN_DELTA + (Math.random() * MAX_DELTA);
        Message msg =
            Message.newBuilder()
                .setId(TemperatureUtils.generateCityKey(city))
                .setTemperature(city.getAvgTemperature() + variance)
                .setTimestamp(new java.util.Date().getTime())
                .build();
        String payload = GSON.toJson(msg);
        MqttMessage mqttMessage = new MqttMessage(payload.getBytes());
        mqttMessage.setQos(DEFAULT_QOS);
        client.publish(mqttTopic, mqttMessage);
        LogUtils.logDebug(
            LOGGER,
            getDeviceId(),
            getGcpProjectId(),
            String.format("Message published: %s", mqttMessage));
        try {
          Thread.sleep(1000);
        } catch (InterruptedException e) {;
        }
      }
    } catch (MqttException e) {
      String message = String.format("Failed to send messages. Cause: %s", e.getMessage());
      LogUtils.logDebug(LOGGER, getDeviceId(), getGcpProjectId(), message);
      throw new ClientException(message, e);
    }
  }
private void publishMessages ( City city ) throws ClientException , FormatException { try { String mqttTopic = generateMqttTopic ( ) ; MqttClient client = createAndConnectMqttClient ( ) ; while ( true ) { double variance = MIN_DELTA + ( Math . random ( ) * MAX_DELTA ) ; Message msg = Message . newBuilder ( ) . setId ( TemperatureUtils . generateCityKey ( city ) ) . setTemperature ( city . getAvgTemperature ( ) + variance ) . setTimestamp ( new java . util . Date ( ) . getTime ( ) ) . build ( ) ; String payload = GSON . toJson ( msg ) ; MqttMessage mqttMessage = new MqttMessage ( payload . getBytes ( ) ) ; mqttMessage . setQos ( DEFAULT_QOS ) ; client . publish ( mqttTopic , mqttMessage ) ; LogUtils . logDebug ( LOGGER , getDeviceId ( ) , getGcpProjectId ( ) , String . format ( "Message published: %s" , mqttMessage ) ) ; try { Thread . sleep ( 1000 ) ; } catch ( InterruptedException e ) { ; } } } catch ( MqttException e ) { String message = String . format ( "Failed to send messages. Cause: %s" , e . getMessage ( ) ) ; LogUtils . logDebug ( LOGGER , getDeviceId ( ) , getGcpProjectId ( ) , message ) ; throw new ClientException ( message , e ) ; } }
Generate and publish a temperature messages to Cloud IoT Core with a 10% variance around a
 given temperature value and with a frequency of 1 message/second.
Generate and publish a temperature messages to Cloud IoT Core with a 10% variance around a given temperature value and with a frequency of 1 message / second .
// Create the temperature message to publish // Create the MQTT message (in this case QoS=1 --> delivered at least once) // Publish the message and sleep for 1 second