public Main(string[] files) { Stopwatch stopWatch = new Stopwatch(); try { DSPortAdapter adapter; adapter = OneWireAccessProvider.DefaultAdapter; Debug.WriteLine("starting..."); taggedDevices = new List <TaggedDevice>(); paths = new List <OWPath>(); TAGParser p = new TAGParser(adapter); foreach (var file in files) { // attempt to parse it parseStream(p, loadResourceFile(file), new OWPath(adapter), true); } Debug.WriteLine("done..."); } catch (Exception ex) { Debug.WriteLine("Exception " + ex.GetType().FullName + ": " + ex.Message); Debug.WriteLine(ex.ToString()); Debug.Write(ex.StackTrace); } }
/// <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> /// 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); }
/// <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); }
//-------- //-------- Methods //-------- /// <summary> /// Method main that creates the main window and then polls for /// for XML files. /// </summary> /// <param name="args"> command line arguments </param> public static void Main1(string[] args) { int path_count = 0; try { // attempt to get the default adapter adapter = OneWireAccessProvider.DefaultAdapter; // create the state instances parseLog = new Dictionary <long, ParseContainer>(); taggedDevices = new List <TaggedDevice>(); paths = new List <OWPath>(); deviceFrames = new ArrayList(); // create dummy 'trunk' search path paths.Add(new OWPath(adapter)); // create the main frame main_Renamed = new TagMainFrame(); main_Renamed.AdapterLabel = adapter.AdapterName + "_" + adapter.PortName; // get the initial log file logFile = main_Renamed.LogFile; // check for XML files on the command line for (int i = 0; i < args.Length; i++) { main_Renamed.Status = "File being parsed: " + args[i]; FileStream file_stream = new FileStream(args[i], FileMode.Open, FileAccess.Read); // create the tagParser parser = new TAGParser(adapter); // attempt to parse it parseStream(parser, file_stream, new OWPath(adapter), true); } // 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()); } // turn off all branches allBranchesOff(); // run loop for (;;) { // check if scanning 1-Wire for XML files enabled if (main_Renamed.ScanChecked) { // check if there is a path to search if (path_count < paths.Count) { // only increment if there is nothing else to search for if (pathXMLSearchComplete(path_count)) { path_count++; } } else { path_count = 0; } } // sleep for 1 second main_Renamed.Status = "sleeping"; Thread.Sleep(1000); } } catch (Exception ex) { Debug.WriteLine(ex.ToString()); Debug.Write(ex.StackTrace); } }