package it.cnr.isti.energia.localization; /** * * @author Giancarlo Riolo * */ import java.util.Observable; import org.apache.shiro.SecurityUtils; import org.apache.shiro.UnavailableSecurityManagerException; import org.apache.shiro.subject.Subject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import it.cnr.isti.energia.util.SensedValue; public class AlgoritmoStigma implements IntefaceAlgorithm { private static final int MAX = 6; private Logger log = LoggerFactory.getLogger(AlgoritmoStigma.class); private LiveAlgorithmManager manager; private double lastLightValue = 0; private double lastPowerValue = 0; private double lastTemperatureValue = 0; private double lastPresenceValue = 0; private double lastNoiseValue = 0; private double lastHumidityValue = 0; private int weightPresence = 1; private int weightNoise = 0; private int weightPower = 0; private int timer = MAX; private String room; private boolean result = false; public AlgoritmoStigma(LiveAlgorithmManager liveAlgorithmManager) { this.manager = liveAlgorithmManager; // log.warn("registering observer for {}", this); manager.addObserver(this); } public boolean getResult() { String user = "nobody"; try { Subject currentUser = SecurityUtils.getSubject(); user = currentUser.getPrincipal().toString(); } catch (UnavailableSecurityManagerException exception) { user = "unattended"; } room = manager.getRoom(); if (result) { // log.warn("Richiesta dall'utente {}, STIGMA, presenza nella stanza {} rilevata", user, room); } else { // log.warn("Richiesta dall'utente {}, STIGMA, presenza nella stanza {} non rilevata", user, room); } return result; } @Override public void update(Observable o, Object arg) { // log.warn("Tipo di dato arrivato {}", ((SensedValue) arg).getType()); // log.warn("Peso {}", weightPresence); // log.warn("Peso {}", weightNoise); // log.warn("Peso {}", weightPower); // log.warn("Timer {}", timer); SensedValue valore = ((SensedValue) arg); switch ((valore).getType()) { case "SocketPower": if (manager.socketPowerList.size() > 0) { lastPowerValue = manager.socketPowerList.get(manager.socketPowerList.size() - 1).getRealValue(); } break; case "LightPower": if (manager.lightPowerList.size() > 0) { lastLightValue = (Integer.parseInt((String) manager.lightPowerList .get(manager.lightPowerList.size() - 1).getValue().get("Power"))); } break; case "PeoplePresence": if (manager.peoplePresenceList.size() > 0) { lastPresenceValue = (Integer.parseInt( (String) (manager.peoplePresenceList.get(manager.peoplePresenceList.size() - 1).getValue()) .get("Presence"))); } // log.warn("PeoplePresenceValue {}", lastPresenceValue); if (lastPresenceValue > 0) { timer = MAX; weightNoise = 1; weightPower = 1; } break; case "Noise": if (manager.noiseList.size() > 0) { lastNoiseValue = (Integer.parseInt( (String) (manager.noiseList.get(manager.noiseList.size() - 1).getValue()).get("Noise"))); } if (result && lastNoiseValue > 0) { timer = MAX; weightNoise = 1; weightPower = 1; } break; case "Humidity": if (manager.humidityList.size() > 0) { lastHumidityValue = (Integer .parseInt((String) (manager.humidityList.get(manager.humidityList.size() - 1).getValue()) .get("Measure"))); } if (timer > 0) { timer--; } if (timer == 0) { weightNoise = 0; weightPower = 0; } break; case "Temperature": if (manager.temperatureList.size() > 0) { lastTemperatureValue = (Integer .parseInt((String) (manager.temperatureList.get(manager.temperatureList.size() - 1).getValue()) .get("Measure"))); } break; default: break; } if (weightPresence * lastPresenceValue + weightNoise * lastNoiseValue + weightPower * lastPowerValue > 0) { result = true; } else { result = false; } } }