Esempio n. 1
0
        // Selections for the Switch actuator:
        // element 0 -> Means "disconnected" or "open circuit" (init = 0) and is
        //              associated with the "min" message.
        // element 1 -> Means "connect" or "close the circuit", (init = 1) and is
        //              associated with the "max" message.

        /// <summary>
        /// Initializes the actuator </summary>
        /// <param name="Init"> The initialization string.
        /// </param>
        /// <exception cref="OneWireException">
        ///  </exception>
        public virtual void initActuator()
        {
            SwitchContainer switchcontainer = DeviceContainer as SwitchContainer;

            // initialize the ActuatorSelections Vector
            ActuatorSelections.Add(Min); // for switch, use min and max
            ActuatorSelections.Add(Max);
            // Now, initialize the switch to the desired condition.
            // This condition is in the <init> tag and, of course, the
            // <channel> tag is also needed to know which channel to
            // to open or close.
            int channelValue;
            int switchStateIntValue = 0;
            int initValue           = Int32.Parse(Init);

            channelValue = Channel;

            byte[] state        = switchcontainer.readDevice();
            bool   switch_state = switchcontainer.getLatchState(channelValue, state);

            if (switch_state)
            {
                switchStateIntValue = 1;
            }
            else
            {
                switchStateIntValue = 0;
            }
            if (initValue != switchStateIntValue)
            {
                // set the switch's state to the value specified in XML file
                switchcontainer.setLatchState(channelValue, !switch_state, false, state);
                switchcontainer.writeDevice(state);
            }
        }
Esempio n. 2
0
    /// <summary>
    /// Display information about the Switch device
    /// </summary>
    /// <param name="owd"> OneWireContainer device </param>
    internal static void printSwitchInfo(SwitchContainer swd)
    {
        try
        {
            byte[] state = swd.readDevice();

            Debug.WriteLine("");
            Debug.WriteLine("-----------------------------------------------------------------------");
            Debug.WriteLine("| Number of channels: " + swd.getNumberChannels(state));
            Debug.WriteLine("| Is high-side switch: " + swd.HighSideSwitch);
            Debug.WriteLine("| Has Activity Sensing: " + swd.hasActivitySensing());
            Debug.WriteLine("| Has Level Sensing: " + swd.hasLevelSensing());
            Debug.WriteLine("| Has Smart-on: " + swd.hasSmartOn());
            Debug.WriteLine("| Only 1 channel on at a time: " + swd.onlySingleChannelOn());
            Debug.WriteLine("");

            Debug.Write("    Channel          ");
            for (int ch = 0; ch < swd.getNumberChannels(state); ch++)
            {
                Debug.Write(ch + "      ");
            }
            Debug.WriteLine("");

            Debug.WriteLine("    -----------------------------------");

            Debug.Write("    Latch State      ");
            for (int ch = 0; ch < swd.getNumberChannels(state); ch++)
            {
                Debug.Write(((swd.getLatchState(ch, state) == true) ? "ON     " : "OFF    "));
            }
            Debug.WriteLine("");

            if (swd.hasLevelSensing())
            {
                Debug.Write("    Sensed Level     ");
                for (int ch = 0; ch < swd.getNumberChannels(state); ch++)
                {
                    Debug.Write(((swd.getLevel(ch, state) == true) ? "HIGH   " : "LOW    "));
                }
                Debug.WriteLine("");
            }

            if (swd.hasActivitySensing())
            {
                Debug.Write("    Sensed Activity  ");
                for (int ch = 0; ch < swd.getNumberChannels(state); ch++)
                {
                    Debug.Write(((swd.getSensedActivity(ch, state) == true) ? "SET    " : "CLEAR  "));
                }
                Debug.WriteLine("");
            }
        }
        catch (OneWireIOException e)
        {
            Debug.WriteLine(e);
        }
    }
        /// <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);
                }
            }
        }
Esempio n. 4
0
    /// <summary>
    /// Main for 1-Wire Memory utility
    /// </summary>
    public static void Main1(string[] args)
    {
        List <OneWireContainer> owd_vect = new List <OneWireContainer>(5);
        SwitchContainer         sw       = null;
        int           ch;
        bool          done    = false;
        DSPortAdapter adapter = null;

        byte[] state;

        Stream stream = loadResourceFile("Switch.input.txt");

        dis = new StreamReader(stream);

        Debug.WriteLine("");
        Debug.WriteLine("1-Wire Switch utility console application: Version 0.00");
        Debug.WriteLine("");

        try
        {
            // get the default adapter
            adapter = OneWireAccessProvider.DefaultAdapter;

            // adapter driver info
            Debug.WriteLine("=========================================================================");
            Debug.WriteLine("== Adapter Name: " + adapter.AdapterName);
            Debug.WriteLine("== Adapter Port description: " + adapter.PortTypeDescription);
            Debug.WriteLine("== Adapter Version: " + adapter.AdapterVersion);
            Debug.WriteLine("== Adapter support overdrive: " + adapter.canOverdrive());
            Debug.WriteLine("== Adapter support hyperdrive: " + adapter.canHyperdrive());
            Debug.WriteLine("== Adapter support EPROM programming: " + adapter.canProgram());
            Debug.WriteLine("== Adapter support power: " + adapter.canDeliverPower());
            Debug.WriteLine("== Adapter support smart power: " + adapter.canDeliverSmartPower());
            Debug.WriteLine("== Adapter Class Version: " + adapter.ClassVersion);

            // get exclusive use of adapter
            adapter.beginExclusive(true);

            // force first select device
            int main_selection = MAIN_SELECT_DEVICE;

            // loop to do menu
            do
            {
                // Main menu
                switch (main_selection)
                {
                case MAIN_DISPLAY_INFO:
                    // display Switch info
                    printSwitchInfo(sw);
                    break;

                case MAIN_CLEAR_ACTIVITY:
                    sw.clearActivity();
                    state = sw.readDevice();
                    sw.writeDevice(state);
                    // display Switch info
                    printSwitchInfo(sw);
                    break;

                case MAIN_SET_LATCH:
                    state = sw.readDevice();
                    Debug.Write("Enter the channel number: ");
                    ch = getNumber(0, sw.getNumberChannels(state) - 1);
                    if (menuSelect(stateMenu) == STATE_ON)
                    {
                        sw.setLatchState(ch, true, false, state);
                    }
                    else
                    {
                        sw.setLatchState(ch, false, false, state);
                    }
                    sw.writeDevice(state);
                    // display Switch info
                    printSwitchInfo(sw);
                    break;

                case MAIN_SELECT_DEVICE:
                    // find all parts
                    owd_vect = findAllSwitchDevices(adapter);
                    // select a device
                    sw = (SwitchContainer)selectDevice(owd_vect);
                    // display device info
                    printDeviceInfo((OneWireContainer)sw);
                    // display the switch info
                    printSwitchInfo(sw);
                    break;

                case MAIN_QUIT:
                    done = true;
                    break;
                }

                if (!done)
                {
                    main_selection = menuSelect(mainMenu);
                }
            } while (!done);
        }
        catch (Exception e)
        {
            Debug.WriteLine(e);
        }
        finally
        {
            if (adapter != null)
            {
                // end exclusive use of adapter
                adapter.endExclusive();

                // free the port used by the adapter
                Debug.WriteLine("Releasing adapter port");
                try
                {
                    adapter.freePort();
                }
                catch (OneWireException e)
                {
                    Debug.WriteLine(e);
                }
            }
        }

        Debug.WriteLine("");
        return;
    }
Esempio n. 5
0
    /// <summary>
    /// Method main
    ///
    /// </summary>
    /// <param name="args">
    /// </param>
    /// <exception cref="OneWireException"> </exception>
    /// <exception cref="OneWireIOException">
    ///  </exception>
    public static void Main1(string[] args)
    {
        bool          usedefault   = false;
        DSPortAdapter access       = null;
        string        adapter_name = null;
        string        port_name    = null;

        if ((args == null) || (args.Length < 1))
        {
            try
            {
                access = OneWireAccessProvider.DefaultAdapter;

                if (access == null)
                {
                    throw new Exception();
                }
            }
            catch (Exception)
            {
                Debug.WriteLine("Couldn't get default adapter!");
                printUsageString();

                return;
            }

            usedefault = true;
        }

        if (!usedefault)
        {
            string[] st = args[0].Split(new char[] { '_' });


            if (st.Length != 2)
            {
                printUsageString();

                return;
            }

            adapter_name = st[0];
            port_name    = st[1];

            Debug.WriteLine("Adapter Name: " + adapter_name);
            Debug.WriteLine("Port Name: " + port_name);
        }

        if (access == null)
        {
            try
            {
                access = OneWireAccessProvider.getAdapter(adapter_name, port_name);
            }
            catch (Exception)
            {
                Debug.WriteLine("That is not a valid adapter/port combination.");

                System.Collections.IEnumerator en = OneWireAccessProvider.enumerateAllAdapters();

                while (en.MoveNext())
                {
                    DSPortAdapter temp = (DSPortAdapter)en.Current;

                    Debug.WriteLine("Adapter: " + temp.AdapterName);

                    System.Collections.IEnumerator f = temp.PortNames;

                    while (f.MoveNext())
                    {
                        Debug.WriteLine("   Port name : " + ((string)f.Current));
                    }
                }

                return;
            }
        }

        access.adapterDetected();
        access.targetAllFamilies();
        access.beginExclusive(true);
        access.reset();
        access.setSearchAllDevices();

        bool next = access.findFirstDevice();

        if (!next)
        {
            Debug.WriteLine("Could not find any iButtons!");

            return;
        }

        while (next)
        {
            OneWireContainer owc = access.DeviceContainer;

            Debug.WriteLine("====================================================");
            Debug.WriteLine("= Found One Wire Device: " + owc.AddressAsString + "          =");
            Debug.WriteLine("====================================================");
            Debug.WriteLine("=");

            bool            isSwitchContainer = false;
            SwitchContainer sc = null;

            try
            {
                sc = (SwitchContainer)owc;
                isSwitchContainer = true;
            }
            catch (InvalidCastException)
            {
                sc = null;
                isSwitchContainer = false;         //just to reiterate
            }

            if (isSwitchContainer)
            {
                Debug.WriteLine("= This device is a " + owc.Name);
                Debug.WriteLine("= Also known as a " + owc.AlternateNames);
                Debug.WriteLine("=");
                Debug.WriteLine("= It is a Switch Container");
                if (sc.hasActivitySensing())
                {
                    sc.clearActivity();
                }

                byte[] state    = sc.readDevice();
                int    channels = sc.getNumberChannels(state);
                bool   activity = sc.hasActivitySensing();
                bool   level    = sc.hasLevelSensing();
                bool   smart    = sc.hasSmartOn();

                Debug.WriteLine("= This device has " + channels + " channel" + (channels > 1 ? "s" : ""));
                Debug.WriteLine("= It " + (activity ? "has" : "does not have") + " activity sensing abilities");
                Debug.WriteLine("= It " + (level ? "has" : "does not have") + " level sensing abilities");
                Debug.WriteLine("= It " + (smart ? "is" : "is not") + " smart-on capable");

                for (int ch = 0; ch < channels; ch++)
                {
                    Debug.WriteLine("======================");
                    Debug.WriteLine("=   Channel " + ch + "        =");
                    Debug.WriteLine("=--------------------=");

                    bool latchstate = sc.getLatchState(ch, state);

                    Debug.WriteLine("= State " + (latchstate ? "ON " : "OFF") + "          =");

                    if (level)
                    {
                        bool sensedLevel = sc.getLevel(ch, state);

                        Debug.WriteLine("= Level " + (sensedLevel ? "HIGH" : "LOW ") + "         =");
                    }

                    if (activity)
                    {
                        bool sensedActivity = sc.getSensedActivity(ch, state);

                        Debug.WriteLine("= Activity " + (sensedActivity ? "YES" : "NO ") + "       =");
                    }

                    Debug.WriteLine("= Toggling switch... =");

                    try
                    {
                        Thread.Sleep(500);
                    }
                    catch (Exception)
                    {
                        /*drain it*/
                    }

                    sc.setLatchState(ch, !latchstate, smart, state);
                    sc.writeDevice(state);

                    state = sc.readDevice();

                    if (latchstate == sc.getLatchState(ch, state))
                    {
                        Debug.WriteLine("= Toggle Failed      =");
                    }
                    else
                    {
                        try
                        {
                            Thread.Sleep(500);
                        }
                        catch (Exception)
                        {
                            /*drain it*/
                        }

                        Debug.WriteLine("= Toggling back...   =");
                        sc.setLatchState(ch, latchstate, smart, state);
                        sc.writeDevice(state);
                    }
                }
            }
            else
            {
                Debug.WriteLine("= This device is not a Switch device.");
                Debug.WriteLine("=");
                Debug.WriteLine("=");
            }

            next = access.findNextDevice();
        }
    }