[Bprocessor-commit] model/src/net/sourceforge/bprocessor/model Project.java, 1.195, 1.196
Status: Pre-Alpha
Brought to you by:
henryml
From: Michael L. <he...@us...> - 2010-08-31 13:48:12
|
Update of /cvsroot/bprocessor/model/src/net/sourceforge/bprocessor/model In directory sfp-cvsdas-2.v30.ch3.sourceforge.com:/tmp/cvs-serv20256/src/net/sourceforge/bprocessor/model Modified Files: Project.java Log Message: Index: Project.java =================================================================== RCS file: /cvsroot/bprocessor/model/src/net/sourceforge/bprocessor/model/Project.java,v retrieving revision 1.195 retrieving revision 1.196 diff -C2 -d -r1.195 -r1.196 *** Project.java 13 Jul 2010 12:33:52 -0000 1.195 --- Project.java 31 Aug 2010 13:48:03 -0000 1.196 *************** *** 8,13 **** package net.sourceforge.bprocessor.model; - import java.io.ByteArrayInputStream; - import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; --- 8,11 ---- *************** *** 16,30 **** import java.io.OutputStream; import java.io.PrintStream; - - import java.net.InetAddress; - import java.net.InetSocketAddress; - import java.net.Socket; - import java.net.SocketAddress; import java.net.URL; - import java.net.URLConnection; - import java.text.DateFormat; - import java.text.DecimalFormat; - import java.text.ParseException; - import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collection; --- 14,18 ---- *************** *** 43,64 **** import javax.swing.SwingUtilities; ! import net.sourceforge.bprocessor.model.bridge.Connection; ! import net.sourceforge.bprocessor.model.bridge.DataItem; ! import net.sourceforge.bprocessor.model.bridge.DeSerializer; ! import net.sourceforge.bprocessor.model.bridge.Sensor; ! import net.sourceforge.bprocessor.model.bridge.Serializer; ! import net.sourceforge.bprocessor.model.bridge.UnSerializer; ! import net.sourceforge.bprocessor.model.brunata.io.BPersistence; ! import net.sourceforge.bprocessor.model.brunata.io.XMLDocument; ! import net.sourceforge.bprocessor.model.brunata.io.DocumentParser; ! import net.sourceforge.bprocessor.model.sense.SensorItem; ! import net.sourceforge.bprocessor.model.sense.SensorStorage; import net.sourceforge.bprocessor.model.xml.Bmodel; import org.apache.log4j.Logger; - import dk.au.perpos.spatialsupport.routefinding.Boundary; import dk.au.perpos.spatialsupport.locationmodel.Location; import dk.au.perpos.spatialsupport.routefinding.Area; import dk.au.perpos.spatialsupport.routefinding.LocationModel; import dk.au.perpos.spatialsupport.routefinding.Opening; --- 31,47 ---- import javax.swing.SwingUtilities; ! import net.sourceforge.bprocessor.model.sense.brunata.TargetManager; ! import net.sourceforge.bprocessor.model.sense.core.Target; ! import net.sourceforge.bprocessor.model.sense.monitor.DataSet; ! import net.sourceforge.bprocessor.model.sense.monitor.Graph; ! import net.sourceforge.bprocessor.model.sense.monitor.MonitorManager; ! import net.sourceforge.bprocessor.model.sense.monitor.TargetMonitor; import net.sourceforge.bprocessor.model.xml.Bmodel; import org.apache.log4j.Logger; import dk.au.perpos.spatialsupport.locationmodel.Location; import dk.au.perpos.spatialsupport.routefinding.Area; + import dk.au.perpos.spatialsupport.routefinding.Boundary; import dk.au.perpos.spatialsupport.routefinding.LocationModel; import dk.au.perpos.spatialsupport.routefinding.Opening; *************** *** 82,91 **** /** */ ! public static final boolean SENSE = false; - /** */ - public static final boolean BRUN = false; - private static final String HOST = "localhost"; - private static final int PORT = 8800; private static boolean brunata; --- 65,70 ---- /** */ ! public static final boolean SENSE = true; private static boolean brunata; *************** *** 96,102 **** private Tesselator tesselator; - private List<SensorItem> measures; - private int index; - /** The observers */ private List<Observer> observers; --- 75,78 ---- *************** *** 131,137 **** private ParameterBlock globals; - /** */ - private Sensor sensor; - /** Has changes been made */ private boolean dirty; --- 107,110 ---- *************** *** 180,194 **** private List<Component> catalog; - - - /** - * - * @param data data - */ - public void setMeasures(List<SensorItem> data) { - measures = data; - } - - private Space findIn(Item item, String name) { if (item instanceof Space) { --- 153,156 ---- *************** *** 235,277 **** */ public void updateSensorData() { - if (measures != null) { - if (index < measures.size()) { - SensorItem data = measures.get(index); - if (data != null) { - String name = "Sensor - " + data.id(); - Space sensor = find(name); - if (sensor != null) { - DecimalFormat format = new DecimalFormat("0.000"); - String t = format.format(data.temperature()); - String h = format.format(data.humimdity()); - - String id = ""; - if (data.id().equals("2501858")) { - id = "3134"; - } else if (data.id().equals("2501861")) { - id = "3132"; - } else if (data.id().equals("2501863")) { - id = "3119"; - } else if (data.id().equals("2501864")) { - id = "3138"; - } else if (data.id().equals("2501865")) { - id = "3127"; - } - - String header = "Internal ID: " + data.id() + "\nExternal ID: " + id + "\n"; - - String info = "Temperatur: " + t + " C\n" + "Relativ luftfugtighed: " + h + "ä\n"; - sensor.setDescription(header + info); - - if (data.temperature() < 18 && data.humimdity() > 70) { - sensor.setMaterial(findMaterial("Red")); - } else { - sensor.setMaterial(findMaterial("Green")); - } - changed(); - } - } - } - } } --- 197,200 ---- *************** *** 280,289 **** */ public void forward() { - if (measures != null) { - if ((index + 1) < measures.size()) { - index++; - updateSensorData(); - } - } } --- 203,206 ---- *************** *** 292,301 **** */ public void backward() { - if (measures != null) { - if (index > 0) { - index--; - updateSensorData(); - } - } } --- 209,212 ---- *************** *** 305,423 **** */ public Date getDate() { ! if (measures != null) { ! SensorItem item = measures.get(index); ! return item.date(); ! } ! return null; ! } ! ! /** ! * ! * @return motes ! * @throws IOException error ! */ ! public static List<Integer> motes() throws IOException { ! InetAddress address = InetAddress.getByName(HOST); ! SocketAddress endpoint = new InetSocketAddress(address, PORT); ! Socket socket = new Socket(); ! socket.connect(endpoint, 2000); ! Connection connection = new Connection(socket); ! ByteArrayOutputStream output = new ByteArrayOutputStream(); ! Serializer serializer = new Serializer(output); ! serializer.writeString("motes"); ! connection.write(output.toByteArray()); ! byte[] reply = connection.read(); ! ByteArrayInputStream input = new ByteArrayInputStream(reply); ! DeSerializer deserializer = new DeSerializer(input); ! int n = deserializer.readInt(); ! List<Integer> result = new LinkedList(); ! for (int i = 0; i < n; i++) { ! int id = deserializer.readInt(); ! result.add(new Integer(id)); ! } ! return result; ! } ! ! /** ! * ! * @return List of sensor-ids ! * @throws IOException IOException ! */ ! public static List<Integer> sensors() throws IOException { ! List<Integer> result = new LinkedList(); ! result.add(15198069); ! result.add(15198041); ! result.add(15198042); ! result.add(15198043); ! result.add(15198044); ! result.add(15198028); ! return result; ! } ! ! /** ! * ! * @param id moteid ! * @return data ! * @throws IOException error ! */ ! public static List<DataItem> data(int id) throws IOException { ! InetAddress address = InetAddress.getByName(HOST); ! SocketAddress endpoint = new InetSocketAddress(address, PORT); ! Socket socket = new Socket(); ! socket.connect(endpoint, 2000); ! Connection connection = new Connection(socket); ! ByteArrayOutputStream output = new ByteArrayOutputStream(); ! Serializer serializer = new Serializer(output); ! serializer.writeString("latest"); ! serializer.writeInt(id); ! connection.write(output.toByteArray()); ! byte[] reply = connection.read(); ! ByteArrayInputStream input = new ByteArrayInputStream(reply); ! DeSerializer deserializer = new DeSerializer(input); ! int n = deserializer.readInt(); ! List<DataItem> items = new LinkedList(); ! for (int i = 0; i < n; i++) { ! int moteid = deserializer.readInt(); ! long timestamp = Long.valueOf(deserializer.readString()); ! short type = (short) deserializer.readInt(); ! long data = deserializer.readInt(); ! DataItem item = new DataItem(moteid, timestamp, type, data); ! items.add(item); ! } ! return items; ! } ! ! /** ! * ! * @param id moteid ! * @return data ! * @throws IOException error ! */ ! public static DataItem latest(int id) throws IOException { ! InetAddress address = InetAddress.getByName(HOST); ! SocketAddress endpoint = new InetSocketAddress(address, PORT); ! Socket socket = new Socket(); ! socket.connect(endpoint, 2000); ! OutputStream output = socket.getOutputStream(); ! Serializer serializer = new Serializer(output); ! serializer.writeString("latest"); ! serializer.writeInt(id); ! ! InputStream input = socket.getInputStream(); ! UnSerializer deserializer = new UnSerializer(input); ! String result = deserializer.readString(); ! ! if (result.equals("data")) { ! String stamp = deserializer.readString(); ! long timestamp = Long.valueOf(stamp); ! long data = deserializer.readInt(); ! DataItem item = new DataItem(id, timestamp, (short) 1, data); ! return item; ! } else { ! return null; ! } } - /** * --- 216,222 ---- */ public Date getDate() { ! return new Date(); } /** * *************** *** 528,539 **** globals.putDouble("brick", 0.12); globals.putDouble("roof", 0.07); - sensor = new Sensor(); - if (SENSE) { - try { - sensor.init(sensors()); - } catch (IOException e) { - // empty - } - } makeClean(); } --- 327,330 ---- *************** *** 570,581 **** } - - /** - * - * @return sensor - */ - public Sensor sensor() { - return sensor; - } /** * Get the world --- 361,364 ---- *************** *** 1211,1217 **** res.add(new Attribute("Export", Boolean.valueOf(doExport))); res.add(new Attribute("Export Path", getExportPath())); ! if (BRUN) { ! res.add(new Attribute("Brunata Server", Boolean.valueOf(brunata))); ! res.add(new Attribute("Initial Date", initialDate)); } res.add(new Attribute("Script", script)); --- 994,1011 ---- res.add(new Attribute("Export", Boolean.valueOf(doExport))); res.add(new Attribute("Export Path", getExportPath())); ! if (SENSE) { ! List<DataSet> lst = new LinkedList<DataSet>(); ! for (Instance current : world.collectInstances()) { ! Object obj = current.getParameters().get("target"); ! if (obj instanceof String) { ! Target target = TargetManager.instance().getTarget((String) obj); ! TargetMonitor monitor = MonitorManager.instance().get(target); ! monitor.collect(lst); ! } ! } ! if (!lst.isEmpty()) { ! Graph graph = new Graph(lst); ! res.add(new Attribute("graph", graph)); ! } } res.add(new Attribute("Script", script)); *************** *** 2185,2191 **** */ public void updateSensors() { - if (SENSE) { - sensor.update(); - } if (brunata) { fetch(); --- 1979,1982 ---- *************** *** 2200,2360 **** } } ! private void fetch() { System.out.println("fetching..."); ! List<Instance> sensors = getSensors(); ! if (initialized) { ! // ! } else { ! for (Instance current : sensors) { ! String value = (String) current.getParameters().get("Intern ID"); ! System.out.println(value); ! long id = Long.valueOf(value); ! SensorStorage storage = new SensorStorage(id); ! current.setStorage(storage); ! } ! DateFormat format = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); ! try { ! Date date = format.parse(initialDate); ! stamp = date.getTime(); ! } catch (ParseException e) { ! // TODO Auto-generated catch block ! e.printStackTrace(); ! } ! for (Instance current : sensors) { ! SensorStorage storage = (SensorStorage) current.getStorage(); ! if (storage != null) { ! long id = storage.getId(); ! try { ! List<SensorItem> items = process(id, new Date(stamp)); ! storage.update(items); ! } catch (Exception e) { ! e.printStackTrace(); ! } ! } ! } ! stamp = System.currentTimeMillis(); ! initialized = true; ! Project.getInstance().changed(); ! } ! } ! ! private void fetch1() { ! try { ! List<SensorItem> items1 = process(2501858); ! List<SensorItem> items2 = process(2501861); ! List<SensorItem> items3 = process(2501863); ! List<SensorItem> items4 = process(2501864); ! List<SensorItem> items5 = process(2501865); ! ! List<SensorItem> result = new LinkedList(); ! ! Iterator<SensorItem> iter1 = items1.iterator(); ! Iterator<SensorItem> iter2 = items2.iterator(); ! Iterator<SensorItem> iter3 = items3.iterator(); ! Iterator<SensorItem> iter4 = items4.iterator(); ! Iterator<SensorItem> iter5 = items5.iterator(); ! for (int i = 0; i < items1.size(); i++) { ! if (iter1.hasNext()) { ! result.add(iter1.next()); ! } ! if (iter2.hasNext()) { ! result.add(iter2.next()); ! } ! if (iter3.hasNext()) { ! result.add(iter3.next()); ! } ! if (iter4.hasNext()) { ! result.add(iter4.next()); ! } ! if (iter5.hasNext()) { ! result.add(iter5.next()); ! } ! } ! setMeasures(result); ! } catch (Exception e) { ! // TODO Auto-generated catch block ! e.printStackTrace(); ! } ! } ! ! ! ! /** ! * ! * @param url URL ! * @return XMLDocument ! * @throws Exception Error ! */ ! public static XMLDocument fetch(URL url) throws Exception { ! URLConnection connection = url.openConnection(); ! DocumentParser parser = new DocumentParser(); ! InputStream input = connection.getInputStream(); ! XMLDocument document = parser.parse(input); ! input.close(); ! return document; ! } ! ! private URL address(long id) throws Exception { ! String www = "https://ext.brunata.com/ws-webmon/v1/da/"; ! String login = "040985:b35c52662aee64d6b12faa2a2eb7013a/"; ! String what = "meters/"; ! String type = ":(DEWPOINT,HUMIDITY,TEMPERATURE).xml"; ! String address = www + login + what + id + type; ! return new URL(address); ! } ! ! private static URL address(long id, Date from) throws Exception { ! String www = "https://ext.brunata.com/ws-webmon/v1/da/"; ! String login = "040985:b35c52662aee64d6b12faa2a2eb7013a/"; ! String what = "meters/"; ! String type = ":(DEWPOINT,HUMIDITY,TEMPERATURE).xml"; ! String parameter = "?period=" + from.getTime(); ! String address = www + login + what + id + type + parameter; ! return new URL(address); ! } ! ! ! private List<SensorItem> process(long id) throws Exception { ! URL url = address(id); ! return process(id, url); ! } ! ! private List<SensorItem> process(long id, Date from) throws Exception { ! URL url = address(id, from); ! return process(id, url); } - private List<SensorItem> process(long id, URL url) throws Exception { - XMLDocument document = fetch(url); - BPersistence persistence = new BPersistence(); - List<BPersistence.Serie> series = persistence.internalize(document); - BPersistence.Serie leader = series.get(0); - List<SensorItem> items = new ArrayList(); - for (int i = 0; i < leader.size(); i++) { - SensorItem item = new SensorItem(); - item.setId(String.valueOf(id)); - items.add(item); - } - for (BPersistence.Serie current : series) { - String type = current.getType(); - int i = 0; - for (BPersistence.DataItem data : current.items()) { - SensorItem item = items.get(i); - if (type.equals("TEMPERATURE")) { - item.setTemparature(data.value); - item.setTime(data.timestamp); - } else if (type.equals("HUMIDITY")) { - item.setHumidity(data.value); - } - i++; - } - } - for (SensorItem current : items) { - System.out.println(current); - } - return items; - } - /** * --- 1991,2003 ---- } } ! private void fetch() { System.out.println("fetching..."); ! stamp = System.currentTimeMillis(); ! initialized = true; ! Project.getInstance().changed(); ! } /** * |