Exemplo n.º 1
0
        public double?GetTemperature()
        {
            double?temperature = null;

            // Connects to the onewire adapter
            DeviceContainerList owSensors = adapter.GetDeviceContainers();

            if (owSensors.Count > 0)
            {
                OneWireContainer owc     = owSensors[0];
                string           owcType = owc.GetType().ToString();
                if (owcType.CompareTo("DalSemi.OneWire.Container.OneWireContainer10") == 0) // Read DS1820 and DS18S20
                {
                    OneWireContainer10 owc10 = (OneWireContainer10)owc;
                    byte[]             state = owc10.ReadDevice();
                    owc10.DoTemperatureConvert(state);

                    //Changes the resultion that that will be read
                    owc10.SetTemperatureResolution(OneWireContainer10.RESOLUTION_MAXIMUM, state);

                    //Reads the temperature
                    temperature = owc10.GetTemperature(state);
                }
            }
            return(temperature);
        }
Exemplo n.º 2
0
        private void poll(object nothing)
        {
            threadCount++;
            logging.AddToLog("Thread Started.  Threads running: " + threadCount, false);
            if (threadCount < 2)
            {
                lock (thisLock)
                {
                    try
                    {
                        adapter.beginExclusive(true);
                        logging.AddToLog("polling...", false);
                        double           temp;
                        OneWireContainer owc = null;

                        owc = adapter.getFirstDeviceContainer();

                        while (owc != null)
                        {
                            sbyte[]    state;
                            OSAEObject obj = null;
                            string     addr;
                            string     desc;

                            string[] ct      = owc.GetType().ToString().Split('.');
                            string   owcType = ct[ct.Length - 1];

                            addr = owc.getAddressAsString();
                            desc = owc.getDescription();

                            logging.AddToLog("- Device Name: " + owc.getName(), false);
                            logging.AddToLog(" - Type: " + owcType, false);
                            logging.AddToLog(" - Address: " + addr, false);
                            logging.AddToLog(" - Description: " + desc, false);

                            switch (owcType)
                            {
                            case "OneWireContainer10":
                                //DS1920 or DS18S20 - iButton family type 10

                                try
                                {
                                    OneWireContainer10 owc10 = (OneWireContainer10)owc;
                                    state = owc10.readDevice();
                                    owc10.doTemperatureConvert(state);

                                    //Changes the resultion that that will be read
                                    owc10.setTemperatureResolution(OneWireContainer10.RESOLUTION_NORMAL, state);

                                    obj = OSAEObjectManager.GetObjectByAddress(owc10.getAddressAsString());
                                    if (obj == null)
                                    {
                                        OSAEObjectManager.ObjectAdd("Temp Sensor-" + addr, "Temp Sensor-" + addr, "1WIRE TEMP SENSOR", addr, "", true);
                                        obj = OSAEObjectManager.GetObjectByAddress(addr);
                                    }



                                    //Reads the temperature
                                    temp = tempConvert(owc10.getTemperature(state));



                                    logging.AddToLog(" - Temperature: " + temp, false);
                                    OSAEObjectPropertyManager.ObjectPropertySet(obj.Name, "Temperature", temp.ToString(), pName);
                                }
                                catch (Exception ex)
                                {
                                    logging.AddToLog("Container type 10 poll error: " + ex.Message, true);
                                }
                                break;

                            case "OneWireContainer22":
                                //DS1822 - iButton family type 22

                                try
                                {
                                }
                                catch (Exception ex)
                                {
                                    logging.AddToLog("Container type 22 poll error: " + ex.Message, true);
                                }
                                break;

                            case "OneWireContainer28":
                                //DS18B20 - iButton family type 28

                                try
                                {
                                    OneWireContainer28 owc28 = (OneWireContainer28)owc;
                                    state = owc28.readDevice();

                                    ////Changes the resolution that that will be read
                                    owc28.setTemperatureResolution(OneWireContainer28.RESOLUTION_12_BIT, state);

                                    obj = OSAEObjectManager.GetObjectByAddress(addr);
                                    if (obj == null)
                                    {
                                        OSAEObjectManager.ObjectAdd("Temp Sensor-" + addr, "Temp Sensor-" + addr, "1WIRE TEMP SENSOR", addr, "", true);
                                        obj = OSAEObjectManager.GetObjectByAddress(addr);
                                    }

                                    //Reads the temperature
                                    owc28.doTemperatureConvert(state);
                                    temp = tempConvert(owc28.getTemperature(state));



                                    logging.AddToLog(" - Temperature: " + temp, false);
                                    OSAEObjectPropertyManager.ObjectPropertySet(obj.Name, "Temperature", temp.ToString(), pName);
                                }
                                catch (Exception ex)
                                {
                                    logging.AddToLog("Container type 28 poll error: " + ex.Message, true);
                                }
                                break;
                            }
                            owc = adapter.getNextDeviceContainer();
                        }
                        adapter.endExclusive();
                    }
                    catch (Exception ex)
                    {
                        logging.AddToLog("Cannot get exclusive use of the 1-wire adapter: " + ex.Message, true);
                    }
                }

                threadCount--;
                logging.AddToLog("Thread Ended.  Threads running: " + threadCount, false);
                abortCount = 0;
            }
            else
            {
                threadCount--;
                logging.AddToLog("Thread aborted.  Threads running: " + threadCount, false);
                abortCount++;
                if (abortCount > 2 && !restarting)
                {
                    logging.AddToLog("Restarting plugin", false);
                    restarting = true;
                    Shutdown();
                    RunInterface(pName);
                }
            }
        }