Example #1
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 #2
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 #3
0
        private void UpdateSensorValue(MPowerDevice device, [AllowNull] string label, int port, DeviceType deviceType, double value)
        {
            if (!device.EnabledTypes.Contains(deviceType))
            {
                return;
            }

            if (!device.Resolution.TryGetValue(deviceType, out var resolutionValue))
            {
                resolutionValue = PluginConfig.GetDefaultResolution(deviceType);
            }

            var deviceIdentifier = new DeviceIdentifier(rootDeviceId, port, deviceType);

            string address = deviceIdentifier.Address;

            if (!currentChildDevices.ContainsKey(address))
            {
                CreateDevice(label, deviceIdentifier);
            }

            var childDevice = currentChildDevices[address];

            value /= childDevice.Denominator;

            double roundedValue = Math.Round(value / resolutionValue, 0, MidpointRounding.AwayFromZero) * resolutionValue;

            childDevice.Update(HS, roundedValue);
        }
        /// <summary>
        /// Creates the devices based on configuration.
        /// </summary>
        private void CreateDevices(HSDevices hsDevices)
        {
            try
            {
                var existingDevices = hsDevices.Children.ToDictionary(x => x.Value.Data.Id);
                foreach (var deviceImport in importDevicesData)
                {
                    combinedToken.Token.ThrowIfCancellationRequested();

                    if (!existingDevices.TryGetValue(deviceImport.Key, out var device))
                    {
                        DeviceIdentifier deviceIdentifier = new DeviceIdentifier(deviceImport.Value.Id);
                        // lazy creation of parent device when child is created
                        if (!hsDevices.ParentRefId.HasValue)
                        {
                            var parentDeviceClass = CreateDevice(null, Invariant($"{PlugInData.PlugInName} Root"),
                                                                 deviceIdentifier.RootDeviceAddress, new RootDeviceData());
                            hsDevices.ParentRefId = parentDeviceClass.get_Ref(HS);
                        }

                        string address     = deviceIdentifier.Address;
                        var    childDevice = new NumberDeviceData(deviceImport.Value);

                        var childHSDevice = CreateDevice(hsDevices.ParentRefId.Value, deviceImport.Value.Name, address, childDevice);
                        hsDevices.Children[childHSDevice.get_Ref(HS)] = childDevice;
                    }
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError(Invariant($"Failed to Create Devices For PlugIn With {ex.GetFullMessage()}"));
            }
        }
Example #5
0
        private void CreateDevice(DeviceIdentifier deviceIdentifier)
        {
            CreateParentDevice();

            string address         = deviceIdentifier.Address;
            var    childDevice     = GetDevice(deviceIdentifier);
            string childDeviceName = Invariant($"{CameraSettings.Name} - {childDevice.DefaultName}");
            var    childHSDevice   = CreateDevice(parentRefId.Value, childDeviceName, address, childDevice);

            childDevice.RefId = childHSDevice.get_Ref(HS);
            devices[address]  = childDevice;
        }
Example #6
0
        private DeviceData GetDeviceData(DeviceClass hsDevice)
        {
            var id = DeviceIdentifier.Identify(hsDevice);

            if (id == null)
            {
                return(null);
            }

            var device = GetDevice(id.Port, id.DeviceType);

            device.RefId = hsDevice.get_Ref(HS);
            return(device);
        }
Example #7
0
        public async Task HandleCommand(DeviceIdentifier deviceIdentifier, CancellationToken token,
                                        MPowerConnector connector, double value, ePairControlUse control)
        {
            if (deviceIdentifier.DeviceId != rootDeviceId)
            {
                throw new ArgumentException("Invalid Device Identifier");
            }

            if (!currentChildDevices.TryGetValue(deviceIdentifier.Address, out var deviceData))
            {
                throw new HspiException(Invariant($"{deviceIdentifier.Address} Not Found."));
            }

            await deviceData.HandleCommand(connector, token, value, control).ConfigureAwait(false);
        }
Example #8
0
        private void UpdateSensorValue(DeviceType deviceType, double value, DateTime updateTime)
        {
            var deviceIdentifier = new DeviceIdentifier(rootDeviceId, deviceType);

            string address = deviceIdentifier.Address;

            if (!currentChildDevices.ContainsKey(address))
            {
                CreateDevice(deviceIdentifier);
            }

            var childDevice = currentChildDevices[address];

            childDevice.Update(HS, value, updateTime);
        }
Example #9
0
        public async Task ProcessUpdate(ICameraContruct value)
        {
            using (var sync = await dataLock.WriterLockAsync().ConfigureAwait(false))
            {
                var deviceIdentifier = new DeviceIdentifier(CameraSettings.Id, value.DeviceType, value.Id);

                string address = deviceIdentifier.Address;
                if (!devices.ContainsKey(address))
                {
                    CreateDevice(deviceIdentifier);
                }

                var childDevice = devices[address];
                childDevice.Update(HS, value.Value);
            }
        }
        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 #11
0
        private DeviceDataBase GetDeviceData(DeviceClass hsDevice)
        {
            var id = DeviceIdentifier.Identify(hsDevice);

            if (id == null)
            {
                return(null);
            }

            var device = GetDevice(id);

            if (device != null)
            {
                device.RefId = hsDevice.get_Ref(HS);
            }
            return(device);
        }
Example #12
0
        private void CreateDevice([AllowNull] string label, DeviceIdentifier deviceIdentifier)
        {
            if (!parentRefId.HasValue)
            {
                string parentAddress  = deviceIdentifier.RootDeviceAddress;
                var    parentHSDevice = CreateDevice(null, deviceName, parentAddress, new RootDeviceData());
                parentRefId = parentHSDevice.get_Ref(HS);
            }

            string address         = deviceIdentifier.Address;
            var    childDevice     = GetDevice(deviceIdentifier.Port, deviceIdentifier.DeviceType);
            string childDeviceName = Invariant($"{ label ?? Invariant($"{deviceName} Port {deviceIdentifier.Port}")} {EnumHelper.GetDescription(childDevice.DeviceType)}");
            var    childHSDevice   = CreateDevice(parentRefId.Value, childDeviceName, address, childDevice);

            childDevice.RefId            = childHSDevice.get_Ref(HS);
            currentChildDevices[address] = childDevice;
        }
Example #13
0
        public async Task HandleCommand(DeviceIdentifier deviceIdentifier, CameraBase camera, string stringValue, double value, ePairControlUse control)
        {
            if (deviceIdentifier.CameraId != CameraSettings.Id)
            {
                throw new ArgumentException("Invalid Device Identifier");
            }

            DeviceDataBase deviceData;

            using (var sync = await dataLock.ReaderLockAsync().ConfigureAwait(false))
            {
                if (!devices.TryGetValue(deviceIdentifier.Address, out deviceData))
                {
                    throw new HspiException(Invariant($"{deviceIdentifier.Address} Not Found."));
                }
            }

            await deviceData.HandleCommand(HS, camera, stringValue, value, control, cancellationToken).ConfigureAwait(false);
        }
Example #14
0
 private DeviceDataBase GetDevice(DeviceIdentifier deviceIdentifier)
 {
     return(CameraSettings.GetDevice(deviceIdentifier));
 }