package it.cnr.isti.energia.localization; /** * * @author Giancarlo Riolo * */ import java.util.Observable; import java.util.concurrent.ConcurrentHashMap; 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 AlgoritmoSoglia implements IntefaceAlgorithm { private Logger log = LoggerFactory.getLogger(AlgoritmoSoglia.class); private Boolean result = false; 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 ConcurrentHashMap threshold = new ConcurrentHashMap(); private String room; public AlgoritmoSoglia(LiveAlgorithmManager liveAlgorithmManager) { this.manager = liveAlgorithmManager; threshold.put("C62", 170); threshold.put("C63", 55); threshold.put("C64", 30); threshold.put("C66", 45); threshold.put("C69", 70); threshold.put("C70", 40); threshold.put("C70a", 50); threshold.put("C71", 200); threshold.put("C73", 60); threshold.put("IIT1", 55); threshold.put("IIT2", 95); // 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 {}, SOGLIA, presenza nella stanza {} rilevata", user, room); } else { // log.warn("Richiesta dall'utente {}, SOGLIA, presenza nella stanza {} non rilevata", user, room); } return result; } @Override public void update(Observable o, Object arg) { switch (((SensedValue) arg).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"))); } break; case "Noise": if (manager.noiseList.size() > 0) { lastNoiseValue = (Integer.parseInt( (String) (manager.noiseList.get(manager.noiseList.size() - 1).getValue()).get("Noise"))); } break; case "Humidity": if (manager.humidityList.size() > 0) { lastHumidityValue = (Integer .parseInt((String) (manager.humidityList.get(manager.humidityList.size() - 1).getValue()) .get("Measure"))); } 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; } try { Subject currentUser = SecurityUtils.getSubject(); String user = currentUser.getPrincipal().toString(); } catch (UnavailableSecurityManagerException exception) { } room = manager.getRoom(); // log.info("Threshold {} for room {}", threshold.get(room), room); if (lastPowerValue >= threshold.get(room) || lastPresenceValue > 1 || lastPresenceValue > 1) { result = true; } else { result = false; } } }