001/* 002 * Copyright (c) 2010 The Regents of the University of California. 003 * All rights reserved. 004 * 005 * '$Author: crawl $' 006 * '$Date: 2015-08-24 22:48:48 +0000 (Mon, 24 Aug 2015) $' 007 * '$Revision: 33634 $' 008 * 009 * Permission is hereby granted, without written agreement and without 010 * license or royalty fees, to use, copy, modify, and distribute this 011 * software and its documentation for any purpose, provided that the above 012 * copyright notice and the following two paragraphs appear in all copies 013 * of this software. 014 * 015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 019 * SUCH DAMAGE. 020 * 021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 026 * ENHANCEMENTS, OR MODIFICATIONS. 027 * 028 */ 029 030package org.ROADnet; 031 032import java.io.BufferedReader; 033import java.io.File; 034import java.io.FileOutputStream; 035import java.io.FileReader; 036import java.io.PrintStream; 037import java.util.HashMap; 038import java.util.StringTokenizer; 039 040import com.brtt.antelope.Orb; 041 042import ptolemy.actor.TypedAtomicActor; 043import ptolemy.actor.TypedIOPort; 044import ptolemy.data.ObjectToken; 045import ptolemy.data.expr.StringParameter; 046import ptolemy.data.type.BaseType; 047import ptolemy.kernel.CompositeEntity; 048import ptolemy.kernel.util.IllegalActionException; 049import ptolemy.kernel.util.NameDuplicationException; 050 051/** 052 * This actor connects to an Antelope ORB and collects packets matching the 053 * given sourcename. Packets are reaped from the Orb and unpacked assuming that 054 * they're OrbImagePackets. The actor outputs ImageTokens to Ptolemy. 055 * 056 * @author Tobin Fricke (tobin@splorg.org), University of California 057 * @version $Id: OrbSensorDataSource.java 33634 2015-08-24 22:48:48Z crawl $ 058 * @Pt.ProposedRating Red (tobin) 059 */ 060 061public class OrbSensorDataSource extends TypedAtomicActor { 062 /** 063 * @param container 064 * The container. 065 * @param name 066 * The name of this actor. 067 * @exception IllegalActionException 068 * If the actor cannot be contained by the proposed 069 * container. 070 * @exception NameDuplicationException 071 * If the container already has an actor with this name. 072 */ 073 public OrbSensorDataSource(CompositeEntity container, String name) 074 throws NameDuplicationException, IllegalActionException { 075 super(container, name); 076 077 output = new TypedIOPort(this, "output", false, true); 078 output.setMultiport(false); 079 output.setTypeEquals(BaseType.OBJECT); 080 081 orbname = new StringParameter(this, "orbname"); 082 srcname = new StringParameter(this, "srcname"); 083 location = new StringParameter(this, "location"); 084 085 orbname.setTypeEquals(BaseType.STRING); 086 srcname.setTypeEquals(BaseType.STRING); 087 location.setTypeEquals(BaseType.STRING); 088 addAvailableLocationChoices(); 089 } 090 091 /** 092 * Initialize the component and connect to the ORB. 093 * 094 * public void initialize() throws IllegalActionException { 095 * 096 * 097 * try { 098 * 099 * // _orb = new 100 * Orb(StringToken.convert(orbname.getToken()).stringValue(),"r"); 101 * //_orb.select(StringToken.convert(srcname.getToken()).stringValue()); 102 * //_orb.after(0); 103 * 104 * 105 * 106 * } catch (Exception e) { throw new IllegalActionException(this, 107 * "Couldn't connect to Orb."+ " (" + e.getMessage() + ")"); } 108 * 109 * } 110 */ 111 /** 112 * Reap one packet from the ORB, unstuff it as an OrbImagePacket, and output 113 * the resulting ImageToken (containing a java.awt.Image object). Note that 114 * this whole actor can be implemented as a composite actor utilizing 115 * ObjectToRecord, RecordDisassembler, and something that forms ImageTokens 116 * from java.awt.Image. 117 */ 118 119 public void fire() throws IllegalActionException { 120 121 // get status.txt file now from the above ORB. 122 // get sites.txt file now from the ORB also (?) 123 status = new File(System.getProperty("KEPLER") 124 + "//lib//testdata//ROADNet//status.txt"); 125 sites = new File(System.getProperty("KEPLER") 126 + "//lib//testdata//ROADNet//sites.txt"); 127 128 output.broadcast(new ObjectToken(computeSparseMatrices())); 129 /* 130 * try { OrbRawPacket pkt = (OrbRawPacket)(_orb.reap(false)); 131 * OrbImagePacket imgPkt = 132 * (OrbImagePacket)(OrbImagePacket.unstuff(pkt)); output.broadcast(new 133 * AWTImageToken(imgPkt.image)); 134 * 135 * } catch (Exception e) { throw new IllegalActionException(this, 136 * e.getMessage()); } 137 */ 138 } 139 140 // ///////////////////////////////////////////////////////// 141 // /// private methods //// 142 143 private void addAvailableLocationChoices() { 144 // get available location choices 145 location.addChoice("San Diego"); 146 147 } 148 149 private HashMap computeSparseMatrices() { 150 151 HashMap dataMatrices = new HashMap(); 152 153 try { 154 155 FileOutputStream out; 156 PrintStream p; 157 out = new FileOutputStream(System.getProperty("KEPLER") 158 + "//lib//testdata//ROADNet//WeatherSensors.data"); 159 p = new PrintStream(out); 160 161 int weatherChannels = countSiteDataVariables() - 4; // (e.g 162 // temperature,Pressure,humidity 163 // ..) 164 System.err.println(weatherChannels); 165 166 for (int channelCounter = 0; channelCounter < weatherChannels; channelCounter++) { 167 String[][] outputMatrix = new String[countSites()][countSiteDataVariables()]; 168 int rowCounter = 0; 169 170 // using sites,status files make a string array containing 171 // sites, 172 173 BufferedReader sitesInput = new BufferedReader(new FileReader( 174 sites)); 175 String strSites; 176 while ((strSites = sitesInput.readLine()) != null) { 177 int i = 0; 178 // System.err.println(strSites); 179 String[] resultSites = new String[countSiteDataVariables()]; 180 StringTokenizer lineTok = new StringTokenizer(strSites); 181 while (lineTok.hasMoreTokens()) { 182 String word = lineTok.nextToken(); 183 resultSites[i] = word; 184 i++; 185 } 186 187 String net = resultSites[0]; 188 String sta = resultSites[1]; 189 /* 190 * String chan = result[2]; String chan2 = result[3]; 191 */ 192 String lat = resultSites[countSiteDataVariables() - 2]; 193 String longitude = resultSites[countSiteDataVariables() - 1]; 194 String currentChannel = resultSites[channelCounter + 2]; 195 196 // now search through status for entries with the same name 197 // as net,sta,chan 198 BufferedReader statusInput = new BufferedReader( 199 new FileReader(status)); 200 String strStatus; 201 while ((strStatus = statusInput.readLine()) != null) { 202 i = 0; 203 String[] resultStatus = new String[30]; // current 204 // status file 205 // 10 data 206 // variables. 207 StringTokenizer lineTok2 = new StringTokenizer( 208 strStatus); 209 while (lineTok2.hasMoreTokens()) { 210 String word2 = lineTok2.nextToken(); 211 resultStatus[i] = word2; 212 i++; 213 } 214 215 if (resultStatus[0].equals(net) 216 && resultStatus[1].equals(sta) 217 && resultStatus[2].equals(currentChannel)) { 218 System.out.println("****" + strStatus + "****\n\n"); 219 String sample = resultStatus[8]; 220 221 outputMatrix[rowCounter][0] = lat; 222 outputMatrix[rowCounter][1] = longitude; 223 outputMatrix[rowCounter][2] = sample; 224 outputMatrix[rowCounter][3] = net; 225 outputMatrix[rowCounter][4] = sta; 226 outputMatrix[rowCounter][5] = currentChannel; 227 228 System.err.println(outputMatrix[rowCounter][0] 229 + " " + outputMatrix[rowCounter][1] + " " 230 + outputMatrix[rowCounter][2] + " " 231 + outputMatrix[rowCounter][3] + " " 232 + outputMatrix[rowCounter][4] + " " 233 + outputMatrix[rowCounter][5]); 234 p.println(new String(outputMatrix[rowCounter][0] 235 + " " + outputMatrix[rowCounter][1] + " " 236 + outputMatrix[rowCounter][2] + " " 237 + outputMatrix[rowCounter][3] + " " 238 + outputMatrix[rowCounter][4] + " " 239 + outputMatrix[rowCounter][5])); 240 rowCounter++; 241 } 242 243 }// end of status while 244 245 }// end of sites while 246 247 p.close(); 248 String keySparseMatrix = ""; 249 if (channelCounter == 0) 250 keySparseMatrix = "temperature"; 251 else if (channelCounter == 1) 252 keySparseMatrix = "humidity"; 253 else if (channelCounter == 2) 254 keySparseMatrix = "pressure"; 255 else if (channelCounter == 3) 256 keySparseMatrix = "wind"; 257 258 dataMatrices.put(keySparseMatrix, outputMatrix); 259 260 }// end of for 261 262 } catch (Exception e) { 263 System.err.println(e.toString()); 264 } 265 266 return dataMatrices; 267 268 } 269 270 public int countSites() throws Exception { 271 int siteCounter = 0; 272 BufferedReader sitesInput = new BufferedReader(new FileReader(sites)); 273 while ((sitesInput.readLine()) != null) 274 siteCounter++; 275 276 return siteCounter; 277 } 278 279 public int countSiteDataVariables() throws Exception { 280 BufferedReader sitesInput = new BufferedReader(new FileReader(sites)); 281 String siteStr = sitesInput.readLine(); 282 283 int dataVariablesCounter = 0; 284 String[] result2 = new String[30]; 285 286 StringTokenizer lineTok2 = new StringTokenizer(siteStr); 287 while (lineTok2.hasMoreTokens()) { 288 String word2 = lineTok2.nextToken(); 289 dataVariablesCounter++; 290 } 291 return dataVariablesCounter; 292 } 293 294 /** 295 * The name of the orb to connect to, in the format "hostname:port". Note 296 * that orbnames.pf-style names are not supported -- you have to use a valid 297 * IP address or resolvable DNS name, and you have to use a numeric port 298 * number. 299 */ 300 301 public StringParameter orbname; 302 303 /** 304 * The source name to request from the Orb. When this actor is initialized, 305 * orb.select() is called with the value of this parameter. 306 */ 307 308 public StringParameter srcname; 309 310 /** Samples from incoming waveform packets appear on this port. */ 311 312 public TypedIOPort output; 313 314 /** The location to be mapped */ 315 316 public StringParameter location; 317 318 /** 319 * This is our orb handle. Maybe one day it will also come on an input 320 * channel. 321 */ 322 323 private Orb _orb; 324 private File status; 325 private File sites; 326 327}