[Bprocessor-commit] model/src/net/sourceforge/bprocessor/model/sense/monitor TargetMonitor.java, N
Status: Pre-Alpha
Brought to you by:
henryml
From: Michael L. <he...@us...> - 2010-08-31 13:48:11
|
Update of /cvsroot/bprocessor/model/src/net/sourceforge/bprocessor/model/sense/monitor In directory sfp-cvsdas-2.v30.ch3.sourceforge.com:/tmp/cvs-serv20256/src/net/sourceforge/bprocessor/model/sense/monitor Added Files: TargetMonitor.java SensorMonitor.java Graph.java DataSet.java DataItem.java MonitorManager.java SensorHistory.java Log Message: --- NEW FILE: Graph.java --- package net.sourceforge.bprocessor.model.sense.monitor; import java.util.HashMap; import java.util.List; import java.util.Map; public class Graph { public List<DataSet> data; public class Range { public double min; public double max; } public class Period { public long from; public long to; } public Map<String, Range> ranges; public Period period; public Graph(List<DataSet> data) { this.data = data; ranges = new HashMap<String, Range>(); period = periodOf(data); Range t = new Range(); t.min = 10; t.max = 30; ranges.put("temperature", t); Range h = new Range(); h.min = 0; h.max = 100; ranges.put("humidity", h); } private Period periodOf(List<DataSet> data) { Period result = new Period(); result.to = Long.MIN_VALUE; result.from = Long.MAX_VALUE; for (DataSet current : data) { Period period = periodOf(current); result.from = Math.min(result.from, period.from); result.to = Math.max(result.to, period.to); } return result; } private Period periodOf(DataSet data) { Period result = new Period(); result.to = Long.MIN_VALUE; result.from = Long.MAX_VALUE; for (DataItem current : data.getItems()) { result.from = Math.min(result.from, current.getMeasureMent().getTime()); result.to = Math.max(result.to, current.getMeasureMent().getTime()); } return result; } } --- NEW FILE: TargetMonitor.java --- package net.sourceforge.bprocessor.model.sense.monitor; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import net.sourceforge.bprocessor.model.Attribute; import net.sourceforge.bprocessor.model.Entity; import net.sourceforge.bprocessor.model.Parametric; import net.sourceforge.bprocessor.model.sense.core.Sensor; import net.sourceforge.bprocessor.model.sense.core.Target; public class TargetMonitor extends Entity implements Parametric { private Target target; private List<SensorMonitor> monitors; public TargetMonitor(Target target) { this.target = target; monitors = new LinkedList<SensorMonitor>(); for (Sensor current : target.getSensors()) { monitors.add(new SensorMonitor(target, current)); } } public List<SensorMonitor> getMonitors() { return monitors; } public void delete() { } public List<Attribute> getAttributes() { List<Attribute> attributes = new LinkedList<Attribute>(); attributes.add(new Attribute("Name", target.getId(), false)); return attributes; } public void setAttributes(List<Attribute> attributes) { } public String title() { return "Target"; } public String getName() { return "Target " + target.getId(); } public void collect(List<DataSet> lst) { for (SensorMonitor current : monitors) { current.collect(lst); } } } --- NEW FILE: SensorMonitor.java --- package net.sourceforge.bprocessor.model.sense.monitor; import java.util.LinkedList; import java.util.List; import net.sourceforge.bprocessor.model.Attribute; import net.sourceforge.bprocessor.model.Entity; import net.sourceforge.bprocessor.model.Parametric; import net.sourceforge.bprocessor.model.sense.core.Sensor; import net.sourceforge.bprocessor.model.sense.core.Target; public class SensorMonitor extends Entity implements Parametric { private Target target; private Sensor sensor; private SensorHistory sensorHistory; public SensorMonitor(Target target, Sensor sensor) { this.target = target; this.sensor = sensor; sensorHistory = new SensorHistory(target, sensor); } public SensorHistory getSensorHistory() { return sensorHistory; } public void delete() { } public List<Attribute> getAttributes() { List<Attribute> attributes = new LinkedList<Attribute>(); attributes.add(new Attribute("Name", sensor.getId(), false)); return attributes; } public void setAttributes(List<Attribute> attributes) { } public String title() { return "Sensor"; } public String getName() { return sensor.getType() + " sensor"; } public void collect(List<DataSet> lst) { sensorHistory.collect(lst); } } --- NEW FILE: MonitorManager.java --- package net.sourceforge.bprocessor.model.sense.monitor; import java.util.HashMap; import java.util.Map; import net.sourceforge.bprocessor.model.sense.core.Target; public class MonitorManager { private static MonitorManager instance; public static MonitorManager instance() { if (instance == null) { instance = new MonitorManager(); } return instance; } private Map<Target, TargetMonitor> targetMap; public MonitorManager() { targetMap = new HashMap<Target, TargetMonitor>(); } public TargetMonitor get(Target target) { TargetMonitor monitor = targetMap.get(target); if (monitor == null) { monitor = new TargetMonitor(target); targetMap.put(target, monitor); } return monitor; } } --- NEW FILE: DataItem.java --- package net.sourceforge.bprocessor.model.sense.monitor; import java.util.LinkedList; import java.util.List; import net.sourceforge.bprocessor.model.Attribute; import net.sourceforge.bprocessor.model.Entity; import net.sourceforge.bprocessor.model.Parametric; import net.sourceforge.bprocessor.model.sense.core.Measurement; import net.sourceforge.bprocessor.model.sense.core.Sensor; public class DataItem extends Entity implements Parametric { private Sensor sensor; private Measurement measurement; public DataItem(Sensor sensor, Measurement measurement) { this.sensor = sensor; this.measurement = measurement; } public Measurement getMeasureMent() { return measurement; } public void delete() { } public List<Attribute> getAttributes() { List<Attribute> attributes = new LinkedList<Attribute>(); attributes.add(new Attribute(sensor.getType(), measurement.getValue(), false)); return attributes; } public void setAttributes(List<Attribute> attributes) { } public String title() { return "Measurement"; } public String getName() { return measurement.toString(); } } --- NEW FILE: SensorHistory.java --- package net.sourceforge.bprocessor.model.sense.monitor; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.LinkedList; import java.util.List; import net.sourceforge.bprocessor.model.Attribute; import net.sourceforge.bprocessor.model.Entity; import net.sourceforge.bprocessor.model.Operation; import net.sourceforge.bprocessor.model.Parametric; import net.sourceforge.bprocessor.model.sense.brunata.TargetManager; import net.sourceforge.bprocessor.model.sense.core.History; import net.sourceforge.bprocessor.model.sense.core.Measurement; import net.sourceforge.bprocessor.model.sense.core.Sensor; import net.sourceforge.bprocessor.model.sense.core.Target; public class SensorHistory extends Entity implements Parametric { private Target target; private Sensor sensor; private String from; private String to; private DataSet data; public SensorHistory(Target target, Sensor sensor) { this.target = target; this.sensor = sensor; from = "2010.04.23 08:00:00"; to = "2010.04.24 08:00:00"; data = new DataSet(sensor, new LinkedList<Measurement>()); } public DataSet getData() { return data; } public void fetch() { try { DateFormat format = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); Date f = format.parse(from); Date t = format.parse(to); System.out.println("fetch " + from + " => " + to); History history = TargetManager.instance().getHistory(); List<Measurement> result = history.fetch(target, sensor, f, t); data = new DataSet(sensor, result); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void delete() { } public List<Attribute> getAttributes() { List<Attribute> attributes = new LinkedList<Attribute>(); attributes.add(new Attribute("from", from, true)); attributes.add(new Attribute("to", to, true)); attributes.add(new Attribute("Fetch", new Operation() { public void perform() { fetch(); } })); return attributes; } public void setAttributes(List<Attribute> attributes) { for (Attribute current : attributes) { if (current.getName().equals("from")) { from = (String) current.getValue(); } else if (current.getName().equals("to")) { to = (String) current.getValue(); } } } public String title() { return "History"; } public String getName() { return sensor.getType() + " history"; } public void collect(List<DataSet> lst) { if (!data.isEmpty()) { lst.add(data); } } } --- NEW FILE: DataSet.java --- package net.sourceforge.bprocessor.model.sense.monitor; import java.util.LinkedList; import java.util.List; import net.sourceforge.bprocessor.model.Attribute; import net.sourceforge.bprocessor.model.Entity; import net.sourceforge.bprocessor.model.Parametric; import net.sourceforge.bprocessor.model.sense.core.Measurement; import net.sourceforge.bprocessor.model.sense.core.Sensor; public class DataSet extends Entity implements Parametric { private Sensor sensor; private List<DataItem> items; public DataSet(Sensor sensor, List<Measurement> measurements) { this.sensor = sensor; items = new LinkedList<DataItem>(); for (Measurement current : measurements) { items.add(new DataItem(sensor, current)); } } public Sensor getSensor() { return sensor; } public List<DataItem> getItems() { return items; } public void delete() { } public List<Attribute> getAttributes() { List<Attribute> attributes = new LinkedList<Attribute>(); List<DataSet> lst = new LinkedList<DataSet>(); lst.add(this); attributes.add(new Attribute("graph", new Graph(lst))); return attributes; } public void setAttributes(List<Attribute> attributes) { } public String title() { return "Data Set"; } public String getName() { return sensor.getType() + " data"; } public boolean isEmpty() { return items.isEmpty(); } } |