Esempio n. 1
0
        private static bool DeviceIsDimmer(PlugExtraData.clsPlugExtraData extraData)
        {
            int?   manufacturerId = (int?)extraData.GetNamed("manufacturer_id");
            ushort?prodId         = (ushort?)extraData.GetNamed("manufacturer_prod_id");
            ushort?prodType       = (ushort?)extraData.GetNamed("manufacturer_prod_type");
            int?   relationship   = (int?)extraData.GetNamed("relationship");
            byte?  commandClass   = (byte?)extraData.GetNamed("commandclass");

            return(manufacturerId == 12 && prodId == 12342 && prodType == 17479 && relationship == 4 && commandClass == 38);
        }
Esempio n. 2
0
        static internal string GetDeviceKeys(DeviceClass dev, out string name)
        {
            string id = "";

            name = "";
            PlugExtraData.clsPlugExtraData pData = dev.get_PlugExtraData_Get(hs);
            if (pData != null)
            {
                id   = (string)pData.GetNamed("id");
                name = (string)pData.GetNamed("name");
            }
            return(id);
        }
        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());
            }
        }