package it.cnr.isti.energia.localization; import java.sql.SQLException; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Observable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import it.cnr.isti.energia.model.DataReader; import it.cnr.isti.energia.util.SensedValue; public class LiveAlgorithmManager extends Observable { private Logger log = LoggerFactory.getLogger(LiveAlgorithmManager.class); public List socketPowerList = new ArrayList(); public List lightPowerList = new ArrayList(); public List peoplePresenceList = new ArrayList(); public List noiseList = new ArrayList(); public List temperatureList = new ArrayList(); public List humidityList = new ArrayList(); private boolean isCapped = true; DataReader dr = new DataReader(isCapped); private String[] sensors; private String[] sensorsType; Map corrispondenze = new HashMap(); Map algorithmList = new HashMap(); IntefaceAlgorithm stigma; IntefaceAlgorithm threshold; private HashMap algorithmResults = new HashMap(); private String room; private Map lastValuesFromPowerSensorsIds = new HashMap(); public LiveAlgorithmManager(String room) { this.room = room; try { this.sensors = dr.getSensorbyRoom(room, "id"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { sensorsType = dr.getSensorbyRoom(room, "tipo"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } // prendo liste id per ogni tipo sensore diverso for (int i = 0; i < sensors.length; i++) { corrispondenze.put(sensors[i], sensorsType[i]); // log.warn("sensorsType {}", sensorsType[i]); // log.warn("sensor {}", sensors[i]); if (sensorsType[i].equals("SocketPower")) { lastValuesFromPowerSensorsIds.put(sensors[i], 0d); } } algorithmList.put("stigma", new AlgoritmoStigma(this)); algorithmList.put("threshold", new AlgoritmoSoglia(this)); // log.warn("powerSensorsIds {}", Arrays // .toString(lastValuesFromPowerSensorsIds.entrySet().toArray())); } public String getRoom() { return room; } public void addCurrentData(SensedValue value) { String tipoSensore = corrispondenze.get(value.getId()); switch (tipoSensore) { case "SocketPower": lastValuesFromPowerSensorsIds.put(value.getId(), value.getRealValue()); double powerValuesSum = 0; for (Entry entry : lastValuesFromPowerSensorsIds .entrySet()) { powerValuesSum += entry.getValue(); } value.setRealValue(powerValuesSum); // log.warn("Somma delle due componenti energia {}", powerValuesSum); socketPowerList.add(value); // log.warn("Adding to SocketPower List the value {}", // value.getRealValue()); break; case "LightPower": lightPowerList.add(value); // log.debug("Adding to LightPower List the value {}", value // .getValue().get("Power")); // log.debug(lightPowerList.toString()); break; case "PeoplePresence": peoplePresenceList.add(value); // log.debug("Adding to PeoplePresence List the value {}", value // .getValue().get("Presence")); // log.debug(peoplePresenceList.toString()); break; case "Noise": noiseList.add(value); // log.debug("Adding to Noise List the value {}", value.getValue() // .get("Noise")); // log.debug(noiseList.toString()); break; case "Humidity": humidityList.add(value); // log.debug("Adding to Humidity List the value {}", value.getValue() // .get("Measure")); // log.debug(humidityList.toString()); break; case "Temperature": temperatureList.add(value); // log.debug("Adding to Temperature List the value {}", value // .getValue().get("Measure")); // log.debug(temperatureList.toString()); break; default: log.debug("Unsupported type"); break; } value.addType(tipoSensore); setChanged(); notifyObservers(value); } public HashMap getResults() { for (Entry entry : algorithmList.entrySet()) { String key = entry.getKey(); IntefaceAlgorithm algorithm = entry.getValue(); algorithmResults.put(key, algorithm.getResult()); } return algorithmResults; } }