package com.ffii.towngas.config;

import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;

import com.ffii.towngas.pojo.MqttRawData;
import com.google.gson.Gson;

public class MqttConsumerCallBack implements MqttCallback {

  private Gson gson = new Gson();

  @Override
  public void connectionLost(Throwable throwable) {
    System.out.println("Disconnected with server");
  }

  @Override
  public void messageArrived(String topic, MqttMessage message) throws Exception {
    String payload = new String(message.getPayload());

    MqttRawData mqttRawData = gson.fromJson(payload, MqttRawData.class);
    mqttRawData.setTopic(topic);
    mqttRawData.setQos(message.getQos());
    mqttRawData.setPayload(payload);
    System.out.println(mqttRawData);
    MqttDataWriter.insertMqttRawData(mqttRawData);
  }

  @Override
  public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {

  }
}