You are on page 1of 5

/*

* To change this license header, choose License Headers in Project Properties.


* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.mycompany.meaven;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;

import java.io.FileReader;
import java.io.IOException;
import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import org.json.JSONObject;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

@JsonIgnoreProperties(ignoreUnknown = true)

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"topic",
"clientId",
"broker",
"senzor"
})

public class simulator {

public static String topic;


public static MqttMessage m;

public String broker;

public static String clientId = "josip";


public boolean status;

public ArrayList<senzor> senzori;


public static MqttClient clients;
public static MqttConnectOptions conOpts;
public static MemoryPersistence persistence;
public simulator()
{
}

@JsonCreator
public simulator(@JsonProperty("broker") String broker,
@JsonProperty("client") String clientId,
@JsonProperty("topic") String topic,
@JsonProperty("senzor") ArrayList<senzor> senzori
)
{

this.broker=broker;
this.clientId =clientId;
this.topic = topic;
this.senzori=senzori;
}

public simulator( String broker,String client, String topic) {


topic = topic;
this.broker = broker;
clientId = client;

this.senzori = new ArrayList<>();


try {
MqttConnectOptions conOpts = new MqttConnectOptions();
clients = new MqttClient(broker, clientId, persistence);
conOpts.setCleanSession(true);
System.out.println("Connecting to broker: " + broker);
clients.connect(conOpts);
System.out.println("Connected");
} catch (MqttException me) {
System.out.println("reason " + me.getReasonCode());
System.out.println("msg " + me.getMessage());
System.out.println("loc " + me.getLocalizedMessage());
System.out.println("cause " + me.getCause());
System.out.println("excep " + me);
me.printStackTrace();
}

}
@JsonProperty("senzor")

public void setSenzor(senzor s) {


this.senzori.add(s);

}
@JsonProperty("senzor")

public ArrayList<senzor> getSenzor() {


return senzori;
}
@JsonProperty("broker")
public String getBroker() {
return broker;
}
@JsonProperty("broker")

public void setBroker(String broker)


{
this.broker=broker;
}

@JsonProperty("client")

public static String getClient() {


return clientId;
}
@JsonProperty("client")

public static void setClient(String client)


{
clientId=client;
}

@JsonProperty("topic")

public static void setTopic(String top)


{
topic=top;
}

@JsonProperty("topic")

public static String getTopic() {


return topic;
}

@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("broker: ")
.append(this.broker).append("\n")
.append("client: ")
.append(clientId).append("\n")
.append("topic")
.append(topic).append("\n");
for (senzor sen: this.senzori) {
stringBuilder.append(sen.toString());
}

return stringBuilder.toString();
}
public static void main(String[] args) throws IOException {

// ObjectMapper m=new ObjectMapper();


// simulator sim=m.readValue(new
File("C:\\Users\\icoric\\Desktop\\j.json"),simulator.class);
/*/
simulator simul = new simulator("tcp://localhost:1883", "josip", "test");
senzor s1 = new senzor("Temperatura", "C", -3210, 32000, true);
senzor s2 = new senzor("Tlak", "Bar", 0, 65336, true);
senzor s3 = new senzor("Potrosnja_minuta", "litra", 0, 65000, true);
senzor s4 = new senzor("Potrosnja 10 minuta", "litra", 0, 65000, true);
senzor s5 = new senzor("potrosnja 1 mjesec", "m3", 0, (int) 65533.6, true);
senzor s6 = new senzor("potrosnja 1 godinu", "m3", 0, (int) 65533.6, true);

simul.setSenzor(s1);
simul.setSenzor(s2);
simul.setSenzor(s3);
simul.setSenzor(s4);
simul.setSenzor(s5);
simul.setSenzor(s6);

/*/

/*/
JSONObject json=new JSONObject(simul);
String myjson=json.toString();
System.out.println(myjson);
ObjectMapper mapper = new ObjectMapper();
/*/

simulator sim=new simulator();


ObjectMapper mapper = new ObjectMapper();

sim= mapper.readValue(new File("C:\\Users\\icoric\\Desktop\\j3.json"),


simulator.class);
simulator s=new
simulator(sim.getBroker().toString(),sim.getClient().toString(),sim.getTopic().toSt
ring());

System.out.println(sim.toString());

ArrayList<Thread>t=new ArrayList<>();

for(int i=0;i<sim.getSenzor().size();i++)
{
s.setSenzor(sim.getSenzor().get(i));
t.add(new Thread(sim.getSenzor().get(i)));
}
for(int j=0;j<t.size();j++)
{
t.get(j).start();

ButtonFrame frame = new ButtonFrame(s); //radi novi okvir


frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

}
}