Example #1
0
        /// <summary>
        /// Gets the current devices for plugin from Homeseer
        /// </summary>
        /// <returns>Current devices for plugin</returns>
        /// <exception cref="HspiException"></exception>
        private IDictionary <string, DeviceClass> GetCurrentDevices()
        {
            var deviceEnumerator = HS.GetDeviceEnumerator() as clsDeviceEnumeration;

            if (deviceEnumerator == null)
            {
                throw new HspiException(Invariant($"{Name} failed to get a device enumerator from HomeSeer."));
            }

            var currentDevices = new Dictionary <string, DeviceClass>();

            do
            {
                CancellationToken.ThrowIfCancellationRequested();
                DeviceClass device = deviceEnumerator.GetNext();
                if ((device != null) &&
                    (device.get_Interface(HS) != null) &&
                    (device.get_Interface(HS).Trim() == Name))
                {
                    string address = device.get_Address(HS);
                    currentDevices.Add(address, device);
                }
            } while (!deviceEnumerator.Finished);
            return(currentDevices);
        }
        private NameValueCollection GetCurrentDeviceImportDevices()
        {
            HSHelper hsHelper         = new HSHelper(HS);
            var      deviceEnumerator = HS.GetDeviceEnumerator() as clsDeviceEnumeration;

            var currentDevices    = new NameValueCollection();
            var importDevicesData = pluginConfig.ImportDevicesData;

            do
            {
                DeviceClass device = deviceEnumerator.GetNext();
                if ((device != null) &&
                    (device.get_Interface(HS) != null) &&
                    (device.get_Interface(HS).Trim() == PlugInData.PlugInName))
                {
                    string address = device.get_Address(HS);

                    var childDeviceData = DeviceIdentifier.Identify(device);
                    if (childDeviceData != null)
                    {
                        if (pluginConfig.ImportDevicesData.TryGetValue(childDeviceData.DeviceId, out var importDeviceData))
                        {
                            currentDevices.Add(device.get_Ref(HS).ToString(CultureInfo.CurrentCulture), hsHelper.GetName(device));
                        }
                    }
                }
            } while (!deviceEnumerator.Finished);

            return(currentDevices);
        }
Example #3
0
        private void GetCurrentDevices()
        {
            if (!(HS.GetDeviceEnumerator() is clsDeviceEnumeration deviceEnumerator))
            {
                throw new HspiException(Invariant($"{PluginData.PlugInName} failed to get a device enumerator from HomeSeer."));
            }

            string baseAddress = DeviceIdentifier.CreateDeviceIdSpecficAddress(CameraSettings.Id);

            do
            {
                DeviceClass device = deviceEnumerator.GetNext();
                if ((device != null) &&
                    (device.get_Interface(HS) != null) &&
                    string.Equals(device.get_Interface(HS).Trim(), PluginData.PlugInName, StringComparison.Ordinal))
                {
                    string address = device.get_Address(HS);
                    if (address.StartsWith(baseAddress, StringComparison.Ordinal))
                    {
                        var deviceData = GetDeviceData(device);
                        if (deviceData != null)
                        {
                            devices.Add(address, deviceData);
                            deviceData.OnPlugInLoad(HS, CameraSettings);

                            if (deviceData.IsRootDevice)
                            {
                                parentRefId = device.get_Ref(HS);
                            }
                        }
                    }
                }
            } while (!deviceEnumerator.Finished);
        }
Example #4
0
        private void GetCurrentDevices()
        {
            var deviceEnumerator = HS.GetDeviceEnumerator() as clsDeviceEnumeration;

            if (deviceEnumerator == null)
            {
                throw new HspiException(Invariant($"{PluginData.PlugInName} failed to get a device enumerator from HomeSeer."));
            }

            string parentAddress = DeviceIdentifier.CreateRootAddress(rootDeviceId);

            do
            {
                DeviceClass device = deviceEnumerator.GetNext();
                if ((device != null) &&
                    (device.get_Interface(HS) != null) &&
                    (device.get_Interface(HS).Trim() == PluginData.PlugInName))
                {
                    string address = device.get_Address(HS);
                    if (address == parentAddress)
                    {
                        parentRefId = device.get_Ref(HS);
                    }
                    else if (address.StartsWith(parentAddress, StringComparison.Ordinal))
                    {
                        DeviceData childDeviceData = GetDeviceData(device);
                        if (childDeviceData != null)
                        {
                            currentChildDevices.Add(address, childDeviceData);
                        }
                    }
                }
            } while (!deviceEnumerator.Finished);
        }
Example #5
0
        public override string ConfigDevice(int deviceId, [AllowNull] string user, int userRights, bool newDevice)
        {
            if (newDevice)
            {
                return(string.Empty);
            }

            try
            {
                DeviceClass deviceClass = (DeviceClass)HS.GetDeviceByRef(deviceId);

                if (deviceClass.get_Interface(HS) == PlugInData.PlugInName)
                {
                    var deviceIdentifier = DeviceIdentifier.Identify(deviceClass);
                    if (deviceIdentifier != null)
                    {
                        return(configPage.GetDeviceImportTab(deviceIdentifier));
                    }
                }
                else
                {
                    return(configPage.GetDeviceHistoryTab(deviceClass));
                }
                return(string.Empty);
            }
            catch (Exception ex)
            {
                LogError(Invariant($"ConfigDevice for {deviceId} With {ex.Message}"));
                return(string.Empty);
            }
        }
        private HSDevices GetCurrentDevices()
        {
            var deviceEnumerator = HS.GetDeviceEnumerator() as clsDeviceEnumeration;

            if (deviceEnumerator == null)
            {
                throw new HspiException(Invariant($"{PlugInData.PlugInName} failed to get a device enumerator from HomeSeer."));
            }

            int?parentRefId         = null;
            var currentChildDevices = new Dictionary <int, DeviceData>();

            string parentAddress = DeviceIdentifier.CreateRootAddress();

            do
            {
                DeviceClass device = deviceEnumerator.GetNext();
                if ((device != null) &&
                    (device.get_Interface(HS) != null) &&
                    (device.get_Interface(HS).Trim() == PlugInData.PlugInName))
                {
                    string address = device.get_Address(HS);
                    if (address == parentAddress)
                    {
                        parentRefId = device.get_Ref(HS);
                    }
                    else
                    {
                        var childDeviceData = DeviceIdentifier.Identify(device);
                        if (childDeviceData != null)
                        {
                            if (importDevicesData.TryGetValue(childDeviceData.DeviceId, out var importDeviceData))
                            {
                                device.set_Status_Support(HS, true);
                                currentChildDevices.Add(device.get_Ref(HS), new NumberDeviceData(importDeviceData));
                            }
                        }
                    }
                }
            } while (!deviceEnumerator.Finished);

            return(new HSDevices()
            {
                ParentRefId = parentRefId,
                Children = currentChildDevices,
            });
        }
Example #7
0
        static internal List <DeviceDataPoint> Get_Device_List(List <DeviceDataPoint> deviceList)
        {
            // Gets relevant devices from HomeSeer
            DeviceClass dv = new DeviceClass();

            try
            {
                Scheduler.Classes.clsDeviceEnumeration EN = default(Scheduler.Classes.clsDeviceEnumeration);
                EN = (Scheduler.Classes.clsDeviceEnumeration)Util.hs.GetDeviceEnumerator();
                if (EN == null)
                {
                    throw new Exception(IFACE_NAME + " failed to get a device enumerator from HomeSeer.");
                }
                int dvRef;

                do
                {
                    dv = EN.GetNext();
                    if (dv == null)
                    {
                        continue;
                    }
                    if (dv.get_Interface(null) != IFACE_NAME)
                    {
                        continue;
                    }
                    dvRef = dv.get_Ref(null);

                    var ddp = new DeviceDataPoint(dvRef, dv);
                    deviceList.Add(ddp);
                } while (!(EN.Finished));
            }
            catch (Exception ex)
            {
                Log("Exception in Get_Device_List: " + ex.Message, LogType.LOG_TYPE_ERROR);
            }

            return(deviceList);
        }
        public override void HSEvent(Enums.HSEvent eventType, object[] parameters)
        {
            if (!IsAnyWebHookConfigured())
            {
                Program.WriteLog(LogType.Debug, "Ignoring event " + eventType + " because no webhook endpoint is configured.");
                return;
            }

            Dictionary <string, object> dict = new Dictionary <string, object> {
                { "eventType", eventType.ToString() }
            };

            try {
                int devRef;

                switch (eventType)
                {
                case Enums.HSEvent.VALUE_SET:
                case Enums.HSEvent.VALUE_CHANGE:
                    devRef = (int)parameters[4];
                    if (ignoreUnchangedEvents && (double)parameters[2] == (double)parameters[3])
                    {
                        Program.WriteLog(LogType.Verbose, $"Suppressing {eventType} for device {devRef} because its value did not change ({(double)parameters[2]} == {(double)parameters[3]})");
                        return;
                    }
                    dict.Add("address", (string)parameters[1]);
                    dict.Add("newValue", ((double)parameters[2]).ToString(CultureInfo.InvariantCulture));
                    dict.Add("oldValue", ((double)parameters[3]).ToString(CultureInfo.InvariantCulture));
                    dict.Add("ref", devRef);
                    break;

                case Enums.HSEvent.STRING_CHANGE:
                    devRef = (int)parameters[3];
                    dict.Add("address", (string)parameters[1]);
                    dict.Add("newValue", (string)parameters[2]);
                    dict.Add("ref", devRef);
                    break;

                default:
                    Program.WriteLog(LogType.Warn, "Unknown event type " + eventType);
                    return;
                }

                if (ignoredDeviceRefs.Contains(devRef))
                {
                    Program.WriteLog(LogType.Verbose, $"Suppressing {eventType} for device {devRef} because it is ignored.");
                    return;
                }

                if (ignoreTimerEvents)
                {
                    if (!deviceRefTimerState.ContainsKey(devRef))
                    {
                        // We need to check if this is a timer
                        DeviceClass device = (DeviceClass)hs.GetDeviceByRef(devRef);
                        PlugExtraData.clsPlugExtraData deviceData = device.get_PlugExtraData_Get(hs);
                        deviceRefTimerState[devRef] = deviceData.GetNamed("timername") != null && device.get_Interface(hs) == "";
                    }

                    if (deviceRefTimerState[devRef])
                    {
                        // This is a timer.
                        Program.WriteLog(LogType.Verbose, $"Suppressing {eventType} for device {devRef} because it's a timer.");
                        return;
                    }
                }

                string json = jsonSerializer.Serialize(dict);
                Program.WriteLog(LogType.Verbose, json);

                for (byte i = 0; i < TOTAL_WEBHOOK_SLOTS; i++)
                {
                    if (webHooks[i] == null)
                    {
                        continue;
                    }

                    WebHook webHook = webHooks[i];

                    webHook.Execute(new StringContent(json, Encoding.UTF8, "application/json")).ContinueWith((task) => {
                        Program.WriteLog(LogType.Verbose, "Sent WebHook " + webHook + " with status code " + task.Result.StatusCode);
                        if (!task.Result.IsSuccessStatusCode)
                        {
                            Program.WriteLog(LogType.Warn, "Got non-successful response code from WebHook " + webHook + ": " + task.Result.StatusCode);
                        }

                        task.Result.Dispose();
                    }).ContinueWith((task) => {
                        if (task.Exception?.InnerException != null)
                        {
                            Program.WriteLog(LogType.Error, $"Unable to send WebHook {webHook}: {getInnerExceptionMessage(task.Exception)}");
                        }
                    }, TaskContinuationOptions.OnlyOnFaulted);
                }
            }
            catch (Exception ex) {
                Program.WriteLog(LogType.Error, ex.ToString());
            }
        }
Example #9
0
        public override string InitIO(string port)
        {
            Program.WriteLog(LogType.Verbose, "InitIO");

            dimmersByRef          = new Dictionary <int, DimmerDevice>();
            haveDoneInitialUpdate = false;

            Dictionary <byte, DimmerDevice> dict       = new Dictionary <byte, DimmerDevice>();
            clsDeviceEnumeration            enumerator = (clsDeviceEnumeration)hs.GetDeviceEnumerator();

            do
            {
                DeviceClass device = enumerator.GetNext();
                if (device != null)
                {
                    if (device.get_Interface(hs) != "Z-Wave")
                    {
                        continue;
                    }

                    // It's a Z-Wave device
                    PlugExtraData.clsPlugExtraData extraData = device.get_PlugExtraData_Get(hs);
                    string[] addressParts = device.get_Address(hs).Split('-');
                    byte     nodeId       = byte.Parse(addressParts[1]);
                    if (dict.ContainsKey(nodeId))
                    {
                        continue;
                    }

                    if (DeviceIsDimmer(extraData))
                    {
                        DimmerDevice dimmerDevice = new DimmerDevice {
                            HomeID = addressParts[0],
                            NodeID = nodeId,
                            SwitchMultiLevelDeviceRef = device.get_Ref(hs)
                        };

                        dict[nodeId] = dimmerDevice;
                        dimmersByRef[dimmerDevice.SwitchMultiLevelDeviceRef] = dimmerDevice;
                    }
                }
            } while (!enumerator.Finished);

            callbacks.RegisterEventCB(HomeSeerAPI.Enums.HSEvent.VALUE_SET, Name, InstanceFriendlyName());
            callbacks.RegisterEventCB(HomeSeerAPI.Enums.HSEvent.VALUE_CHANGE, Name, InstanceFriendlyName());

            hs.RegisterPage("IdleLightColorsSettings", Name, InstanceFriendlyName());
            WebPageDesc configLink = new WebPageDesc {
                plugInName     = Name,
                link           = "IdleLightColorsSettings",
                linktext       = "Settings",
                order          = 1,
                page_title     = "HS-WD200+ Idle Light Colors Settings",
                plugInInstance = InstanceFriendlyName()
            };

            callbacks.RegisterConfigLink(configLink);
            callbacks.RegisterLink(configLink);

            offColor = (WD200NormalModeColor)int.Parse(hs.GetINISetting("Colors", "idle_color",
                                                                        ((int)WD200NormalModeColor.Blue).ToString(), IniFilename));
            onColor = (WD200NormalModeColor)int.Parse(hs.GetINISetting("Colors", "active_color",
                                                                       ((int)WD200NormalModeColor.White).ToString(), IniFilename));

            Program.WriteLog(LogType.Info, string.Format(
                                 "Init complete. Active color: {0}. Idle color: {1}. Found {2} dimmers with node IDs: {3}",
                                 onColor,
                                 offColor,
                                 dimmersByRef.Keys.Count,
                                 string.Join(", ", dimmersByRef.Values.Select(dimmerDevice => dimmerDevice.NodeID))
                                 ));

            return("");
        }