Example #1
0
        internal static PlugInformations CreateFromSystemInfosAndOutputState(TPLink.SmartHome.SystemInfo sysInfo, TPLink.SmartHome.OutputState state)
        {
            var infos = new PlugInformations();

            infos.FillWithSystemInfosAndOutputState(sysInfo, state);

            return(infos);
        }
Example #2
0
        /// <summary>
        /// OnStart
        /// </summary>
        public override void OnStart()
        {
            PackageHost.WriteInfo("Package starting - IsRunning: {0} - IsConnected: {1}", PackageHost.IsRunning, PackageHost.IsConnected);

            if (PackageHost.TryGetSettingAsJsonObject <IEnumerable <TPLinkConfig> >("devices", out IEnumerable <TPLinkConfig> configs))
            {
                Task.Factory.StartNew(async() =>
                {
                    //pool to get devices informations
                    while (PackageHost.IsRunning)
                    {
                        int soLifeTime = Math.Max(PackageHost.GetSettingValue <int>("poolingInterval") * 2, 30000) / 1000;

                        foreach (TPLinkConfig config in configs)
                        {
                            try
                            {
                                if (config.Type == TPLink.SmartHome.SystemType.PlugWithEnergyMeter)
                                {
                                    TPLink.SmartHome.PlugWithEnergyMeterClient plug = new TPLink.SmartHome.PlugWithEnergyMeterClient(config.HostName);
                                    TPLink.SmartHome.ConsumptionInfo consumption    = await plug.GetConsumptionAsync();
                                    TPLink.SmartHome.SystemInfo systemInfos         = await plug.GetSystemInfoAsync();
                                    TPLink.SmartHome.OutputState state = await plug.GetOutputAsync();

                                    PlugWithEnergyMeterInformations plugInfos = PlugWithEnergyMeterInformations.CreateFromSystemInfosAndOutputStateAndConsumption(systemInfos, state, consumption);

                                    PackageHost.PushStateObject($"TPLink-{config.HostName}", plugInfos, lifetime: soLifeTime);
                                }
                                else if (config.Type == TPLink.SmartHome.SystemType.Plug)
                                {
                                    TPLink.SmartHome.PlugClient plug        = new TPLink.SmartHome.PlugClient(config.HostName);
                                    TPLink.SmartHome.SystemInfo systemInfos = await plug.GetSystemInfoAsync();
                                    TPLink.SmartHome.OutputState state      = await plug.GetOutputAsync();

                                    PlugInformations plugInfos = PlugInformations.CreateFromSystemInfosAndOutputState(systemInfos, state);

                                    PackageHost.PushStateObject($"TPLink-{config.HostName}", plugInfos, lifetime: soLifeTime);
                                }
                            }
                            catch (TimeoutException ex)
                            {
                                PackageHost.WriteError($"Connection timeout for TPLink device '{config?.HostName}' : {ex.Message}");
                            }
                            catch (Exception ex)
                            {
                                PackageHost.WriteError($"An unknown error has occurred for TPLink device '{config?.HostName}' : {ex}");
                            }
                        }

                        await Task.Delay(PackageHost.GetSettingValue <int>("poolingInterval"));
                    }
                }, TaskCreationOptions.LongRunning);
            }
        }