Exemple #1
0
        /// <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>
        /// 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);
                }
            }
        }