예제 #1
0
    /// <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);
    }
예제 #2
0
    //--------
    //-------- 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);
        }
    }