// EMF 5/30/03: never clear cache, just don't write if older than // what's there... public void put(ChannelMap cm) { try { if (debug > 3) System.err.println("BRCache.put: cm " + cm); if (debug > 3) for (int i = 0; i < cm.NumberOfChannels(); i++) { System.err.println(i + ": " + cm.GetTimeStart(i) + " " + cm.GetData(i)); } // check must be on per channel basis ChannelMap cmCache = new ChannelMap(); Double[] lastTimeD = sink.getEndTimes(cm); for (int i = 0; i < cm.NumberOfChannels(); i++) { double thisTime = cm.GetTimeStart(i); if (lastTimeD[i] == null || lastTimeD[i].doubleValue() < thisTime) { int idx = cmCache.Add(cm.GetName(i)); cmCache.PutTimeRef(cm, i); cmCache.PutDataRef(idx, cm, i); } } source.Flush(cmCache); /* //if time goes backwards, reset ring buffer double thisTime=Double.MAX_VALUE; for (int i=0;i<cm.NumberOfChannels();i++) { double time=cm.GetTimeStart(i)+cm.GetTimeDuration(i); if (time<thisTime) thisTime=time; } if (thisTime<lastTime) { //reset ring buffer and clear endTimes if (debug>3) System.err.println("BRCache.put: reset ring buffer"); source.SetRingBuffer(1000,"none",0); endTimes.clear(); } if (thisTime<Double.MAX_VALUE) lastTime=thisTime; //put end times into cache for (int i=0;i<cm.NumberOfChannels();i++) { endTimes.put(cm.GetName(i),new Double(cm.GetTimeStart(i)+cm.GetTimeDuration(i))); } //put data into server source.Flush(cm); */ } catch (Exception e) { System.err.println("BRCache.put exception: " + e.getMessage()); e.printStackTrace(); } }
private ChannelTree getChannelTree(String pattern) { ChannelTree tr = null; try { sMap = new ChannelMap(); if ((pattern != null) && (pattern.length() > 0)) sMap.Add(pattern); sink.RequestRegistration(sMap); sMap = sink.Fetch(-1, sMap); tr = ChannelTree.createFromChannelMap(sMap); } catch (SAPIException se) { se.printStackTrace(); } return tr; }
/** @param args the command line arguments */ @SuppressWarnings("static-access") public static void main(String[] args) { String hostname = "localhost:3333"; String portName = "/dev/tty.KeySerial1"; String srcName = "ADXL-RBNB Accelerometer"; String[] chanNames = {"X", "Y"}; int[] chanIdx = {0, 1}; double[] vals = {0, 0}; String unitsMetadata = "units=G,scale=1,offset=0"; Source source; ChannelMap cmap; int cacheSize = 10240; // ! @todo parse from command line int archiveSize = cacheSize * 10; // ! @todo parse from command line int idx; int chanCount = 2; ADXL chip; // Setup interrupt handler DoHook(); System.out.println("Opening serial port"); chip = new ADXL(); if (chip.initialize(portName) == 0) { portConnected = true; System.out.println("Serial port initialized OK"); } else { System.out.println("Error opening serial port"); return; } // RBNB connection setup try { System.out.println("Opening connection to RBNB on " + hostname); // Create both a source and a sink, connect both: source = new Source(cacheSize, "append", archiveSize); source.OpenRBNBConnection(hostname, srcName); DTconnected = true; System.out.println("OK."); // Setup channel map - names of channels, units cmap = new ChannelMap(); for (idx = 0; idx < chanCount; idx++) { chanIdx[idx] = cmap.Add(chanNames[idx]); // Hardwired units (G) for all three sources cmap.PutUserInfo(chanIdx[idx], unitsMetadata); // Not sure if we still need the MIME type or not cmap.PutMime(chanIdx[idx], "application/octet-stream"); } source.Register(cmap); source.Flush(cmap); } // We don't distinguish between errors in setup phase, just bail out catch (SAPIException se) { System.out.println("Error on Turbine - not connected"); DTconnected = false; return; } // ******************************************************************** // Main data loop: read, scale, write. try { System.out.println("Turbine connected, running. Press control-c to end"); // Loop - runs until control-c or error. do { for (idx = 0; idx < chanCount; idx++) chanIdx[idx] = cmap.Add(chanNames[idx]); // Read the data from the accelerometer vals = chip.chipRead(); // Timestamp all channels with client-side time cmap.PutTimeAuto("timeofday"); for (idx = 0; idx < chanCount; idx++) { double valBuf[] = {vals[idx]}; cmap.PutDataAsFloat64(cmap.GetIndex(chanNames[idx]), valBuf); } source.Flush(cmap); cmap.Clear(); Thread.sleep(20); } while (!ctrlC); } catch (SAPIException mse) { System.out.println("Error saving data!"); mse.printStackTrace(); } catch (InterruptedException ie) { System.out.println("Interrupted, exiting."); } // Shutdown and exit if (portConnected) { chip.closePort(chip.serialPort); } if (DTconnected) { System.out.println("Closing RBNB connection"); // Tell RBNB to keep the data once we close source.Detach(); source.CloseRBNBConnection(); } System.out.println("Done, exiting."); return; }
/** * Get the metadata channel tree for the given <code>path</code>. This will populate the channel * map with channel objects derived from the metadata. This will recursively make requests for * child servers and plugins up to the maximum request depth of {@value #MAX_REQUEST_DEPTH}. * * @param sink sink the sink connection to the RBNB server * @param path the path for the desired metadata * @param channels the map to populate with channel objects * @param depth the depth of the request * @return the metadata channel tree for the given path * @throws SAPIException if a server error occurs * @see #MAX_REQUEST_DEPTH */ private ChannelTree getChannelTree( Sink sink, String path, Map<String, Channel> channels, int depth) throws SAPIException { depth++; ChannelTree ctree = ChannelTree.EMPTY_TREE; ChannelMap markerChannelMap = new ChannelMap(); ChannelMap cmap = new ChannelMap(); if (path == null) { path = ""; cmap.Add("..."); } else { cmap.Add(path + "/..."); } sink.RequestRegistration(cmap); cmap = sink.Fetch(FETCH_TIMEOUT, cmap); if (cmap.GetIfFetchTimedOut()) { log.error("Failed to get metadata. Fetch timed out."); return ctree; } ctree = ChannelTree.createFromChannelMap(cmap); // store user metadata in channel objects String[] channelList = cmap.GetChannelList(); for (int i = 0; i < channelList.length; i++) { int channelIndex = cmap.GetIndex(channelList[i]); if (channelIndex != -1) { ChannelTree.Node node = ctree.findNode(channelList[i]); String userMetadata = cmap.GetUserInfo(channelIndex); Channel channel = new RBNBChannel(node, userMetadata); channels.put(channelList[i], channel); // look for marker channels String mimeType = channel.getMetadata("mime"); if (mimeType != null && mimeType.compareToIgnoreCase(EventMarker.MIME_TYPE) == 0) { markerChannelMap.Add(node.getFullName()); } } } Iterator<?> it = ctree.iterator(); while (it.hasNext()) { ChannelTree.Node node = (ChannelTree.Node) it.next(); NodeTypeEnum type = node.getType(); // look for child servers or plugins and get their channels if ((type == ChannelTree.SERVER || type == ChannelTree.PLUGIN) && !path.startsWith(node.getFullName()) && depth < MAX_REQUEST_DEPTH) { ChannelTree childChannelTree = getChannelTree(sink, node.getFullName(), channels, depth); ctree = childChannelTree.merge(ctree); } } if (markerChannelMap.NumberOfChannels() > 0) { double markersDuration = System.currentTimeMillis() / 1000d; // request from start of marker channels to now sink.Request(markerChannelMap, 0, markersDuration, "absolute"); markerChannelMap = sink.Fetch(FETCH_TIMEOUT, markerChannelMap); if (!markerChannelMap.GetIfFetchTimedOut()) { // notify marker listeners fireMarkersUpdated(markerChannelMap); } else { log.error("Failed to get event markers. Fetched timed out."); } } return ctree; }
/** Main data push from SCRAMNet */ private boolean execute() { if (!connected) return false; SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); sdf.setTimeZone(TimeZone.getTimeZone("UTC")); // Connect to SCRAMNet ScramNetIO scr = new ScramNetIO(); scr.initScramnet(); // Create a new Channel map ChannelMap cmap = new ChannelMap(); // Gather all the channels int channelId[] = new int[rbnbChannelNames.length]; for (int i = 0; i < rbnbChannelNames.length; i++) { try { // add the channels channelId[i] = cmap.Add(rbnbChannelNames[i]); } catch (SAPIException e) { System.err.println( "Failed to add SCRAMNet channel to channel map; name = " + rbnbChannelNames[i]); disconnect(); return false; } try { cmap.PutUserInfo(channelId[i], "Channel_Name=" + rbnbChannelNames[i]); } catch (SAPIException e) { System.err.println("Failed to register SCRAMnet channel metadata."); } } try { source.Register(cmap); } catch (SAPIException e) { System.err.println("Failed to register SCRAMnet channel metadata."); } while (true) { // Read SCRAMNet channel depending on if its DAQ or other for (int i = 0; i < rbnbChannelNames.length; i++) { float scrdata[] = new float[1]; if (xml.getxPCReadisDAQ(i).equals("true")) { scrdata[0] = (float) scr.readDAQ( xml.getxPCReadLocation(i), xml.getxPCReadGain(i), xml.getxPCReadVoffset(i), xml.getxPCReadVslope(i), xml.getxPCReadEUoffset(i), xml.getxPCReadEUslope(i)); } else { scrdata[0] = scr.readFloat(Integer.parseInt(xml.getxPCReadLocation(i))) * (float) xml.getxPCReadGain(i); } // Push data to turbine try { cmap.PutTimeAuto("timeofday"); cmap.PutDataAsFloat32(channelId[i], scrdata); } catch (SAPIException e) { System.err.println("Failed to put SCRAMNet data into channel map."); scr.unmapScramnet(); return false; } } // Flush output try { source.Flush(cmap, true); } catch (SAPIException e) { e.printStackTrace(); System.err.println("Failed to flush channel output data to server."); scr.unmapScramnet(); return false; } try { // Delay between Thread.sleep(delay); } catch (InterruptedException e) { } } }