/// <summary> /// Method endDocument /// /// </summary> public virtual void endDocument() { // Iterate through deviceList and make all the // OWPaths from the TaggedDevice's vector of Branches. TaggedDevice device; OWPath branchPath; Stack <TaggedDevice> singleBranchVector; for (int i = 0; i < deviceList.Count; i++) { device = (TaggedDevice)deviceList[i]; device.setOWPath(adapter, device.Branches); } // Now, iterate through branchVectors and make all the // OWPaths for the Vector of OWPaths for (int i = 0; i < branchVectors.Count; i++) { singleBranchVector = new Stack <TaggedDevice>(branchVectors[i]); branchPath = new OWPath(adapter); for (int j = 0; j < singleBranchVector.Count; j++) { device = singleBranchVector.ToArray()[j]; branchPath.add(device.DeviceContainer, device.Channel); } branchPaths.Add(branchPath); } }
/// <summary> /// Returns the OWPath of the device with the given address. /// </summary> /// <param name="address"> a Long object representing the address of the device </param> /// <returns> The OWPath representing the network path to the device. </returns> public override OWPath getDevicePath(long address) { lock (devicePathHash) { OWPath val = null; devicePathHash.TryGetValue(address, out val); return(val); } }
/// <summary> /// Parse the provided XML input stream with the provided parser. /// Gather the new TaggedDevices and OWPaths into the global vectors /// 'taggedDevices' and 'paths'. /// </summary> /// <param name="parser"> parser to parse 1-Wire XML files </param> /// <param name="stream"> XML file stream </param> /// <param name="currentPath"> OWPath that was opened to get to this file </param> /// <param name="autoSpawnFrames"> true if new DeviceFrames are spawned with /// new taggedDevices discovered </param> /// <returns> true an XML file was successfully parsed. </returns> public static bool parseStream(TAGParser parser, Stream stream, OWPath currentPath, bool autoSpawnFrames) { bool rslt = false; OWPath tempPath; Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); try { // parse the file List <TaggedDevice> new_devices = parser.parse(stream); // get the new paths List <OWPath> new_paths = parser.OWPaths; Debug.WriteLine("Success, XML parsed with " + new_devices.Count + " devices " + new_paths.Count + " paths"); // add the new devices to the old list for (int i = 0; i < new_devices.Count; i++) { TaggedDevice current_device = new_devices[i]; // update this devices OWPath depending on where we got it if its OWPath is empty tempPath = current_device.OWPath; if (!tempPath.AllOWPathElements.MoveNext()) { // replace this devices path with the current path tempPath.copy(currentPath); current_device.OWPath = tempPath; } // add the new device to the device list taggedDevices.Add(current_device); } // add the new paths for (int i = 0; i < new_paths.Count; i++) { paths.Add(new_paths[i]); } rslt = true; } catch (Exception e) { Debug.WriteLine("Error: " + e); } stopWatch.Stop(); Debug.WriteLine("Parsed in " + stopWatch.ElapsedMilliseconds + "ms"); return(rslt); }
/// <summary> /// Sets the OWPath for the tagged device. An /// OWPath is a description of how to /// physically get to a 1-Wire device through a /// set of nested 1-Wire switches. /// </summary> /// <param name="adapter"> </param> /// <param name="Branches"> </param> public virtual void setOWPath(DSPortAdapter adapter, List <TaggedDevice> Branches) { OWPath = new OWPath(adapter); TaggedDevice TDevice; for (int i = 0; i < Branches.Count; i++) { TDevice = Branches[i]; OWPath.add(TDevice.DeviceContainer, TDevice.Channel); } }
/// <summary> /// Performs a search of the 1-Wire network, with branch searching /// </summary> /// <param name="arrivals"> A vector of Long objects, represent new arrival addresses. </param> /// <param name="departures"> A vector of Long objects, represent departed addresses. </param> public override void search(List <long> arrivals, List <long> departures) { lock (sync_flag) { try { // aquire the adapter adapter.beginExclusive(true); // setup the search adapter.setSearchAllDevices(); adapter.targetAllFamilies(); adapter.Speed = DSPortAdapter.SPEED_REGULAR; // close any opened branches for (int j = 0; j < paths.Count; j++) { try { ((OWPath)paths[j]).close(); } catch (System.Exception) { ; } } // search through all of the paths for (int i = 0; i < paths.Count; i++) { // set searches to not use reset adapter.setNoResetSearch(); // find the first device on this branch bool search_result = false; OWPath path = (OWPath)paths[i]; try { // try to open the current path path.open(); } catch (System.Exception) { // if opening the path failed, continue on to the next path continue; } search_result = adapter.findFirstDevice(); // loop while devices found while (search_result) { // get the 1-Wire address long longAddress = adapter.AddressAsLong; // check if the device allready exists in our hashtable if (!deviceAddressHash.ContainsKey(longAddress)) { OneWireContainer owc = getDeviceContainer(adapter, longAddress); // check to see if it's a switch and if we are supposed // to automatically search down branches if (this.branchAutoSearching && (owc is SwitchContainer)) { SwitchContainer sc = (SwitchContainer)owc; byte[] state = sc.readDevice(); for (int j = 0; j < sc.getNumberChannels(state); j++) { OWPath tmp = new OWPath(adapter, path); tmp.add(owc, j); if (!paths.Contains(tmp)) { paths.Add(tmp); } } } lock (devicePathHash) { devicePathHash[longAddress] = path; } if (arrivals != null) { arrivals.Add(longAddress); } } // check if the existing device moved else if (!path.Equals(devicePathHash[longAddress])) { lock (devicePathHash) { devicePathHash[longAddress] = path; } if (departures != null) { departures.Add(longAddress); } if (arrivals != null) { arrivals.Add(longAddress); } } // update count deviceAddressHash[longAddress] = max_state_count; // find the next device on this branch path.open(); search_result = adapter.findNextDevice(); } } } finally { adapter.endExclusive(); } // remove any devices that have not been seen foreach (var address in deviceAddressHash.Keys.Where(kv => deviceAddressHash[kv] <= 0).ToList()) { // device entry is stale, should be removed deviceAddressHash.Remove(address); if (departures != null) { departures.Add(address); } } foreach (var address in deviceAddressHash.Keys.Where(kv => deviceAddressHash[kv] > 0).ToList()) { // device entry isn't stale, it stays deviceAddressHash[address] -= 1; } // fire notification events if (departures != null && departures.Count > 0) { fireDepartureEvent(adapter, departures); } if (arrivals != null && arrivals.Count > 0) { fireArrivalEvent(adapter, arrivals); } } }
/// <summary> /// Adds a branch for searching. Must be used to traverse branches if /// auto-searching is disabled. /// </summary> /// <param name="path"> A branch to be searched during the next search routine </param> public virtual void addBranch(OWPath path) { paths.Add(path); }
/// <summary> /// Parse the provided XML input stream with the provided parser. /// Gather the new TaggedDevices and OWPaths into the global vectors /// 'taggedDevices' and 'paths'. /// </summary> /// <param name="parser"> parser to parse 1-Wire XML files </param> /// <param name="stream"> XML file stream </param> /// <param name="currentPath"> OWPath that was opened to get to this file </param> /// <param name="autoSpawnFrames"> true if new DeviceFrames are spawned with /// new taggedDevices discovered </param> /// <returns> true an XML file was successfully parsed. </returns> public static bool parseStream(TAGParser parser, Stream stream, OWPath currentPath, bool autoSpawnFrames) { bool rslt = false; OWPath tempPath; try { // parse the file List <TaggedDevice> new_devices = parser.parse(stream); // get the new paths List <OWPath> new_paths = parser.OWPaths; main_Renamed.Status = "Success, XML parsed with " + new_devices.Count + " devices " + new_paths.Count + " paths"; // add the new devices to the old list for (int i = 0; i < new_devices.Count; i++) { TaggedDevice current_device = (TaggedDevice)new_devices[i]; // update this devices OWPath depending on where we got it if its OWPath is empty tempPath = current_device.OWPath; if (!tempPath.AllOWPathElements.MoveNext()) { // replace this devices path with the current path tempPath.copy(currentPath); current_device.OWPath = tempPath; } // check if spawning frames if (autoSpawnFrames) { if (current_device is TaggedSensor) { main_Renamed.Status = "Spawning Sensor: " + current_device.Label; deviceFrames.Add(new DeviceMonitorSensor(current_device, logFile)); } else if (current_device is TaggedActuator) { main_Renamed.Status = "Spawning Actuator: " + current_device.Label; deviceFrames.Add(new DeviceMonitorActuator(current_device, logFile)); } } // add the new device to the device list taggedDevices.Add(current_device); } // add the new paths for (int i = 0; i < new_paths.Count; i++) { paths.Add(new_paths[i]); } rslt = true; } catch (org.xml.sax.SAXException se) { main_Renamed.Status = "XML error: " + se; } catch (IOException ioe) { main_Renamed.Status = "IO error: " + ioe; } return(rslt); }
/// <summary> /// Search a given path for an XML file. /// </summary> /// <param name="currentPathIndex"> index into the 'paths' Vector that /// indicates what 1-Wire path to search for XML files </param> /// <returns> true if the current path provided has been /// completely checked for XML files. false if /// if this current path should be searched further </returns> public static bool pathXMLSearchComplete(int currentPathIndex) { OneWireContainer owd = null, check_owd = null; ParseContainer pc = null, check_pc = null; OWFileInputStream file_stream = null; bool rslt = true; bool xml_parsed = false; try { main_Renamed.Status = "Waiting for 1-Wire available"; // get exclusive use of adapter adapter.beginExclusive(true); main_Renamed.Status = "Exclusive 1-Wire aquired"; // open the current path to the device OWPath owpath = (OWPath)paths[currentPathIndex]; owpath.open(); main_Renamed.Status = "Path opened: " + owpath.ToString(); // setup search adapter.setSearchAllDevices(); adapter.targetAllFamilies(); adapter.Speed = DSPortAdapter.SPEED_REGULAR; // find all devices, update parseLog and get a device filesystem to check for (System.Collections.IEnumerator owd_enum = adapter.AllDeviceContainers; owd_enum.MoveNext();) { owd = (OneWireContainer)owd_enum.Current; long key = owd.AddressAsLong; main_Renamed.Status = "Device Found: " + owd.AddressAsString; // check to see if this is in the parseLog, add if not there pc = null; parseLog.TryGetValue(key, out pc); if (pc == null) { main_Renamed.Status = "Device is new to parse: " + owd.AddressAsString; pc = new ParseContainer(owd); parseLog.Add(key, pc); } else { main_Renamed.Status = "Device " + owd.AddressAsString + " with count " + pc.attemptCount; } // if attemptCount is low then check it later for XML files if (pc.attemptCount < ParseContainer.MAX_ATTEMPT) { check_owd = owd; check_pc = pc; } } // check if there is anything to open if (check_owd != null) { // result is false because found something to try and open rslt = false; main_Renamed.Status = "Attempt to open file TAGX.000"; // attempt to open a 'TAGX.000' file, (if fail update parse_log) try { file_stream = new OWFileInputStream(check_owd, "TAGX.0"); main_Renamed.Status = "Success file TAGX.000 opened"; } catch (OWFileNotFoundException) { file_stream = null; check_pc.attemptCount++; main_Renamed.Status = "Could not open TAGX.000 file"; } } // try to parse the file, (if fail update parse_log) if (file_stream != null) { // create the tagParser // (this should not be necessary but the parser currently holds state) parser = new TAGParser(adapter); // attempt to parse it, on success max out the attempt if (parseStream(parser, file_stream, owpath, true)) { xml_parsed = true; check_pc.attemptCount = ParseContainer.MAX_ATTEMPT; } else { check_pc.attemptCount++; } // close the file try { file_stream.close(); } catch (IOException) { main_Renamed.Status = "Could not close TAGX.000 file"; } } // close the path owpath.close(); main_Renamed.Status = "Path closed"; // update the main paths listbox if XML file found if (xml_parsed) { // add the paths to the main window main_Renamed.clearPathList(); for (int p = 0; p < paths.Count; p++) { main_Renamed.addToPathList(((OWPath)paths[p]).ToString()); } } } catch (OneWireException e) { Debug.WriteLine(e); main_Renamed.Status = e.ToString(); } finally { // end exclusive use of adapter adapter.endExclusive(); main_Renamed.Status = "Exclusive 1-Wire aquired released"; } return(rslt); }