Skip to content

Commit

Permalink
update repository
Browse files Browse the repository at this point in the history
  • Loading branch information
KelCarmo committed Dec 20, 2020
1 parent 7a5a79b commit 465b9a2
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 72 deletions.
2 changes: 1 addition & 1 deletion .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
Expand Down
6 changes: 3 additions & 3 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
org.eclipse.jdt.core.compiler.compliance=1.6
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.6
org.eclipse.jdt.core.compiler.source=1.7
4 changes: 4 additions & 0 deletions .settings/org.eclipse.m2e.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
11 changes: 7 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>7</maven.compiler.source>
<maven.compiler.target>7</maven.compiler.target>
</properties>

<!-- Build specifications -->
Expand Down Expand Up @@ -89,11 +91,12 @@
<version>4.3.0</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.eclipse.paho/org.eclipse.paho.client.mqttv3 -->
<dependency>
<groupId>org.eclipse.paho</groupId>
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
<version>1.2.0</version>
</dependency>
<groupId>org.eclipse.paho</groupId>
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public String getApiIot(String url_Api) {
return null;
}

public static void main(String[] args) throws JAXBException {
ClientIotService novo = new ClientIotService();
System.out.println(novo.getApiIot("http://localhost:8181/cxf/iot-service/devices"));

}
// public static void main(String[] args) throws JAXBException {
// ClientIotService novo = new ClientIotService();
// System.out.println(novo.getApiIot("http://localhost:8181/cxf/iot-service/devices"));
//
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void unsubscribe(String... topicos) {
public void iniciar() {
try {
System.out.println("Conectando no broker MQTT em " + serverURI);
client = new MqttClient(serverURI, String.format("cliente_java_%d", System.currentTimeMillis()), new MqttDefaultFilePersistence(System.getProperty("java.io.tmpdir")));
client = new MqttClient(serverURI, String.format("bottom_broker_%d", System.currentTimeMillis()), new MqttDefaultFilePersistence(System.getProperty("java.io.tmpdir")));
client.setCallback(this);
client.connect(mqttOptions);
} catch (MqttException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,44 +15,54 @@

import br.ufba.dcc.wiser.soft_iot.entities.*;

public class ControllerImpl implements Controller{
public class ControllerImpl implements Controller {

private List<Device> listDevices;
private String ip;
private String port;
private String user;
private String pass;
private boolean debugModeValue;
private ClientMQTT clienteMQTT;
private ClientIotService clienteIot;
private ClientMQTT clienteMQTTFogGateway;
private ClientIotService clienteIot;


public void start(){
//printlnDebug("Starting mapping of connected devices...");
public void start() {
printlnDebug("Sincronizando os dados dos dispositivos ...");

// TODO Auto-generated method stub
this.clienteIot = new ClientIotService();
clienteMQTT = new ClientMQTT("tcp://" + this.ip + ":" + this.port, this.user, this.pass);
clienteMQTT.iniciar();
String devices = clienteIot.getApiIot("http://localhost:8181/cxf/iot-service/devices");
// if(devices != null) System.out.println("Conectado com Broker de FOG com sucesso!!!");
this.loadConnectedDevices(devices);
new Listener(this, clienteMQTT, "TOP_K_HEALTH/#", 1);


this.clienteIot = new ClientIotService();

printlnDebug("BROKER_URL FOR CONNECT: " + "tcp://" + this.ip + ":" + this.port);

clienteMQTTFogGateway = new ClientMQTT("tcp://" + this.ip + ":" + this.port, this.user, this.pass);
clienteMQTTFogGateway.iniciar();

String devices = clienteIot.getApiIot("http://localhost:8181/cxf/iot-service/devices");

this.loadConnectedDevices(devices);

new Listener(this, clienteMQTTFogGateway, "TOP_K_HEALTH/#", 1);
}

public static void main(String[] args) throws JAXBException {
ControllerImpl ctrl= new ControllerImpl();
ctrl.start();
// ctrl.updateValuesSensors();
// System.out.print(ctrl.getListDevices().get(0).getSensors().get(0).getValue());

}
// /**
// * Métodos para testar o bundle FORA do Service Mix. AO fazer o build, comente-os.
// * @param args
// * @throws JAXBException
// */
// public static void main(String[] args) throws JAXBException {
// ControllerImpl ctrl= new ControllerImpl("localhost", "1884");
//
// ctrl.start();
// }
//
// public ControllerImpl(String ip, String port) {
// this.ip = ip;
// this.port = port;
// }

public void stop(){

this.clienteMQTT.finalizar();

public void stop() {
this.clienteMQTTFogGateway.finalizar();
}

public void updateValuesSensors() {
Expand All @@ -61,26 +71,30 @@ public void updateValuesSensors() {
}
}


private void loadConnectedDevices(String strDevices){
private void loadConnectedDevices(String strDevices) {
List<Device> listDevices = new ArrayList<Device>();

try {
printlnDebug("JSON load:");
printlnDebug(strDevices);
JSONArray jsonArrayDevices = new JSONArray(strDevices);
for (int i = 0; i < jsonArrayDevices.length(); i++){

for (int i = 0; i < jsonArrayDevices.length(); i++) {
JSONObject jsonDevice = jsonArrayDevices.getJSONObject(i);
ObjectMapper mapper = new ObjectMapper();
Device device = mapper.readValue(jsonDevice.toString(), Device.class);

listDevices.add(device);

List<Sensor> listSensors = new ArrayList<Sensor>();
JSONArray jsonArraySensors = jsonDevice.getJSONArray("sensors");

for (int j = 0; j < jsonArraySensors.length(); j++){
JSONObject jsonSensor = jsonArraySensors.getJSONObject(j);
Sensor sensor = mapper.readValue(jsonSensor.toString(), Sensor.class);
listSensors.add(sensor);
}

device.setSensors(listSensors);
}

Expand All @@ -91,8 +105,10 @@ private void loadConnectedDevices(String strDevices){
} catch (IOException e) {
e.printStackTrace();
}

this.listDevices = listDevices;
System.out.println("Qtd from devices: " + this.listDevices.size());

printlnDebug("Qtd from devices: " + this.listDevices.size());
}

public Device getDeviceById(String deviceId){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,69 +41,69 @@ public Listener(ControllerImpl impl, ClientMQTT clienteMQTT, String topico, int
public Listener(ControllerImpl impl) {
this.impl = impl;
}

public static void main(String[] args) throws Exception {
ControllerImpl ctrl= new ControllerImpl();
Listener novo = new Listener(ctrl);
novo.messageArrived("TOP_K_HEALTH/1/1", new MqttMessage());
}

@Override
public synchronized void messageArrived( final String topic, final MqttMessage message) throws Exception {
// System.out.println("Mensagem recebida:");
// System.out.println("\tTopico: " + topico);
// System.out.println("\tMensagem: " + new String(mm.getPayload()));
// System.out.println("");

final String [] params = topic.split("/");
final int k = Integer.valueOf(params[2]);
final int k = Integer.valueOf(params[2]); // K valores TOP

System.out.println("=========================================");

this.topk_k_scoresByIdrequi.put(params[1], null);

new Thread(new Runnable() {
public void run() {
String messageContent = new String(message.getPayload());
printlnDebug("topic: " + topic + "message: " + messageContent);

if(params[0].equals("TOP_K_HEALTH")) {
System.out.println("REQUI: TOP_K_HEALTH | " + " ID = " + params[1] + " | k = " + k);
// int qtdDevices = impl.getListDevices().size();
Map<String,Integer> scores = new HashMap<String,Integer>();
System.out.println("CALCULATING SCORES FROM DEVICES ..");
System.out.println("CALCULATING SCORES FROM DEVICES..");

// Consumindo apiIot para pegar os valores mais atualizados dos dispositivos
impl.updateValuesSensors();

for(Device d: impl.getListDevices()) {
// Lógica de usar função personalizável aqui
// Lógica de usar função personalizável aqui. Porém foi usado uma função aleatória para gerar scores randômicos.
Random random = new Random();
int x = random.nextInt(51);
scores.put(d.getId(), x);
}

Object[] a = scores.entrySet().toArray();

Arrays.sort(a, new Comparator<Object>() {
@SuppressWarnings("unchecked")
public int compare(Object o1, Object o2) {
return ((Map.Entry<String, Integer>) o2).getValue()
.compareTo(((Map.Entry<String, Integer>) o1).getValue());
}
});

for (Object e : a) {
System.out.println(((Map.Entry<String, Integer>) e).getKey() + " : "
+ ((Map.Entry<String, Integer>) e).getValue());
}

Map<String,Integer> top_k = new HashMap<String,Integer>();

// Pegando os k piores ...
for (int i =0; i< k; i++) {
for (int i = 0; i< k; i++) {
Map.Entry<String, Integer> e = (Map.Entry<String, Integer>) a[i];
top_k.put(e.getKey(), e.getValue());

}

topk_k_scoresByIdrequi.put(params[1], top_k);

System.out.println("TOP_K => " + top_k.toString());
System.out.println("=========================================");
byte[] b = top_k.toString().getBytes();
clienteMQTT.publicar("TOP_K_HEALTH_RES/" + params[1], b, 1);
}


}
}).start();
}
Expand All @@ -115,11 +115,6 @@ public void calcScores() {
}
}

public void saveMsg() {


}

private void printlnDebug(String str){
if (debugModeValue)
System.out.println(str);
Expand Down
8 changes: 4 additions & 4 deletions src/main/resources/OSGI-INF/blueprint/blueprint.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@
<property name="ip" value="${ip}" />
<property name="port" value="${port}" />
<property name="user" value="${user}" />
<property name="pass" value="${pass}" />
<property name="debugModeValue" value="${debugMode}" />
<property name="pass" value="${pass}" ></property>
<property name="debugModeValue" value="${debugModeValue}" ></property>
</bean>

<cm:property-placeholder
persistent-id="br.ufba.dcc.wiser.soft_iot.broker.bottom.config"
persistent-id="br.ufba.dcc.wiser.soft_iot.broker_bottom"
update-strategy="reload">
<cm:default-properties>
<cm:property name="ip" value="localhost" />
<cm:property name="port" value="1883" />
<cm:property name="user" value="" />
<cm:property name="pass" value="" />
<cm:property name="debugMode" value="false" ></cm:property>
<cm:property name="debugModeValue" value="true" ></cm:property>
</cm:default-properties>
</cm:property-placeholder>

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ip=localhost
port=1884
user=
pass=

debugModeValue=true

0 comments on commit 465b9a2

Please sign in to comment.