コード例 #1
0
ファイル: Program.cs プロジェクト: nibax/SmartPlugSP2101W
        static void Main(string[] args)
        {
            List <IPAddress> ipAdresses = GetLoacalIPAddresses();

            if (ipAdresses.Count() <= 0)
            {
                System.Console.WriteLine("No valid IP address found, please check your network seetings!");
            }
            else
            {
                if (ipAdresses.Count() > 1)
                {
                    System.Console.WriteLine("More than one IP address found!");
                    for (int i = 0; i < ipAdresses.Count(); i++)
                    {
                        System.Console.WriteLine(ipAdresses[i].ToString());
                    }
                }
                System.Console.WriteLine(string.Format("Local IP address used for this demo: {0}", ipAdresses[0].ToString()));

                SmartPlug plugToPlayWith = DeviceFinderDemo(ipAdresses[0]);
                plugToPlayWith.UserName = "******";
                plugToPlayWith.PassWord = "******";
                System.Console.WriteLine(string.Format("SmartPlug used for further demo: Name: {0}  IP: {1}", plugToPlayWith.Name, plugToPlayWith.IpAddress.ToString()));

                RenamingDemo(plugToPlayWith);

                PlugPlayDemo(plugToPlayWith);
            }
            int j = ipAdresses.Count();
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: nibax/SmartPlugSP2101W
        private static void RenamingDemo(SmartPlug plugToPlayWith)
        {
            System.Console.WriteLine(string.Format("First let's give the smart plug \"{0}\"", plugToPlayWith.Name));
            System.Console.Write("Please enter a new name and hit return: ");
            string newPlugName = System.Console.ReadLine();

            plugToPlayWith.Name = newPlugName;
            System.Console.WriteLine(string.Format("The new name of the plug is now: \"{0}\". Hit Enter key to continue!", plugToPlayWith.Name));
            System.Console.ReadLine();
        }
コード例 #3
0
        private void createDevices()
        {
            //Console.WriteLine("Creating Device Objects...");
            List <Device> new_devices = new List <Device>();

            foreach (var dev in _devices)
            {
                switch (dev.module_type)
                {
                case 0:     //unknown
                    break;

                case 1:     // *** Smartplug ***
                    SmartPlug smartplug = new SmartPlug(dev);
                    new_devices.Add(smartplug);
                    break;

                case 2:     // *** Bluetooth ***
                    Bluetooth bluetooth = new Bluetooth(dev);
                    new_devices.Add(bluetooth);
                    break;

                case 3:     // *** USB ***
                    USB usb = new USB(dev);
                    new_devices.Add(usb);
                    break;

                case 4:     // *** Infrared ***
                    Infrared infrared = new Infrared(dev);
                    new_devices.Add(infrared);
                    break;

                case 5:     // *** Industrial ***
                    Industrial industrial = new Industrial(dev);
                    new_devices.Add(industrial);
                    break;

                case 6:     // *** Multiboard ***
                    Multiboard multiboard = new Multiboard(dev);
                    new_devices.Add(multiboard);
                    break;

                case 7:     // *** Audio ***
                    Audio audio = new Audio(dev);
                    new_devices.Add(audio);
                    break;

                default:
                    break;
                }
            }
            _devices.Clear();
            _devices.AddRange(new_devices);
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: nibax/SmartPlugSP2101W
        static void ShowSchedule(DayOfWeek dayOfWeek, SmartPlug smartPlug)
        {
            IEnumerable <ScheduledEntry> entriesForDay = smartPlug.GetScheduleForWeekDay(dayOfWeek);

            foreach (ScheduledEntry entry in entriesForDay)
            {
                System.Console.WriteLine(string.Format("Entry\tStart:{0}:{1}\tEnd:{2}:{3}\t{4}",
                                                       entry.Period.Begin.Hour.ToString("D2"), entry.Period.Begin.Minute.ToString("D2"),
                                                       entry.Period.End.Hour.ToString("D2"), entry.Period.End.Minute.ToString("D2"),
                                                       entry.Enabled ? "enabled" : "disabled"));
            }
        }
コード例 #5
0
ファイル: Program.cs プロジェクト: nibax/SmartPlugSP2101W
        static void PlugPlayDemo(SmartPlug smartPlug)
        {
            // initializing adapter
            //SmartPlug smartPlug = new SmartPlug(new IPAddress(new byte[] {a, b, c, d}), "lala", "9999"); //Please use your IP, UserName, PassWord...

            //displaying current state on the
            System.Console.WriteLine(string.Format("Current state of the smartplug is: {0}. Hit Enter key to continue!", smartPlug.Status.ToString()));
            System.Console.ReadLine();
            if (smartPlug.Status == SmartPlugState.Off)
            {
                smartPlug.Status = SmartPlugState.On;
            }
            else
            {
                smartPlug.Status = SmartPlugState.Off;
            }
            System.Console.WriteLine(string.Format("Now the state of the smartplug is: {0}. Hit Enter key to continue!", smartPlug.Status.ToString()));
            System.Console.ReadLine();

            //smartPlug.GetEntireSchedule();

            System.Console.WriteLine("Old schedule for Monday:");
            ShowSchedule(DayOfWeek.Monday, smartPlug);
            System.Console.WriteLine("Hit Enter key to continue!");
            System.Console.ReadLine();

            List <ScheduledEntry> newSchedule = new List <ScheduledEntry>();

            newSchedule.Add(new ScheduledEntry(new TimePeriod(new PointInTime(2, 0), new PointInTime(3, 0)), true));
            newSchedule.Add(new ScheduledEntry(new TimePeriod(new PointInTime(12, 0), new PointInTime(14, 30)), true));
            newSchedule.Add(new ScheduledEntry(new TimePeriod(new PointInTime(17, 10), new PointInTime(17, 45)), false));
            newSchedule.Add(new ScheduledEntry(new TimePeriod(new PointInTime(20, 20), new PointInTime(23, 13)), true));

            smartPlug.SetScheduleForWeekDay(DayOfWeek.Monday, newSchedule);

            System.Console.WriteLine("New schedule for Monday:");
            ShowSchedule(DayOfWeek.Monday, smartPlug);
            System.Console.WriteLine("Hit Enter key to continue!");
            System.Console.ReadLine();

            decimal thisMonthPowerConsumption = smartPlug.GetCummulatedPowerConsumption(EnergyPeriods.Month);

            System.Console.WriteLine(string.Format("The power consumption during this month has been {0} [kWh]. Hit Enter key to continue!", thisMonthPowerConsumption));
            System.Console.ReadLine();
        }
コード例 #6
0
        public SmartPlugSmall(SmartPlug sm_device)
        {
            InitializeComponent();

            _device = sm_device;
        }
コード例 #7
0
        private void powerOffAndOn(double Power)
        {
            try
            {
                var eventArgs = new SmartPlugPoweredOffAndOnEventArgs(Settings.PlugType, Settings.IpAddress, PlugFriendlyName, Power, DateTime.Now, DateTime.Now);

                Log.AddToLog("Powering off.");

                var result = SmartPlug.SetPlugState(RpmSmartPlugState.off);

                Log.AddToLog($"{PlugFriendlyName} succesfully switched off.");

                var sw = new Stopwatch();
                sw.Start();
                var timeToWait = new TimeSpan(0, 0, Settings.SecondsToWaitBeforePoweringBackOn);

                while (timeToWait.TotalMilliseconds > 0 && !quit)
                {
                    Log.AddToLog($"Powering back on in {timeToWait.Minutes}:{timeToWait.Seconds.ToString().PadLeft(2, '0')}.", false);

                    for (int i = 0; i < 4; i++)
                    {
                        Thread.Sleep(250);

                        if (Console.KeyAvailable)
                        {
                            var keyInfo = Console.ReadKey(true);
                            if (keyInfo.KeyChar == 'q')
                            {
                                quit = true;
                                break;
                            }
                        }
                    }
                    if (quit)
                    {
                        return;
                    }

                    timeToWait = new TimeSpan(0, 0, Settings.SecondsToWaitBeforePoweringBackOn).Subtract(sw.Elapsed);
                }
                sw.Stop();
                sw.Reset();

                Log.AddToLog("Powering on.");
                result = SmartPlug.SetPlugState(RpmSmartPlugState.on);
                Log.AddToLog($"{PlugFriendlyName} succesfully switched on.");
                eventArgs.PoweredOn = DateTime.Now;

                sw.Start();
                timeToWait = new TimeSpan(0, 0, Settings.SecondsToWaitAfterPowerDecline);
                while (timeToWait.TotalMilliseconds > 0 && !quit)
                {
                    Log.AddToLog($"Resuming monitoring in {timeToWait.Minutes}:{timeToWait.Seconds.ToString().PadLeft(2, '0')}.", false);

                    for (int i = 0; i < 4; i++)
                    {
                        Thread.Sleep(250);

                        if (Console.KeyAvailable)
                        {
                            var keyInfo = Console.ReadKey(true);
                            if (keyInfo.KeyChar == 'q')
                            {
                                quit = true;
                                break;
                            }
                        }
                    }
                    if (quit)
                    {
                        return;
                    }

                    timeToWait = new TimeSpan(0, 0, Settings.SecondsToWaitAfterPowerDecline).Subtract(sw.Elapsed);
                }

                OnSmartPlugPoweredOffAndOn?.Invoke(this, eventArgs);

                Log.AddToLog($"The plug was powered off at {eventArgs.PoweredOff.ToShortTimeString()} due to power consumption being lower than {Settings.PowerConsumptionThreshold.ToString()} W for {Settings.SecondsToWaitAfterPowerDecline} seconds. Power consumption at the time of powering off was {Power.ToString()} W. The plug was powered back on at {eventArgs.PoweredOn.ToShortTimeString()}", true, RpmLogEntryItemType.WARNING);

                if (Settings.TextNotificationEnabled)
                {
                    try
                    {
                        var tbHandler        = new TextbeltHandler(Settings.TextbeltApiKey, Settings.MobileNumber);
                        var tbQuotaRemaining = tbHandler.SendText($"{PlugFriendlyName} was powered off at {eventArgs.PoweredOff.ToShortTimeString()} due to power consumption being lower than {Settings.PowerConsumptionThreshold.ToString()} W for {Settings.SecondsToWaitAfterPowerDecline} seconds. Power consumption at the time of powering off was {Power.ToString()} W. The plug was powered back on at {eventArgs.PoweredOn.ToShortTimeString()}.");

                        Log.AddToLog($"Text notification sent via Textbelt.com. Quota remaining : {tbQuotaRemaining}");
                    }
                    catch (RpmApiException ex)
                    {
                        Log.AddToLog(ex);
                    }
                }
            }
            catch (RpmSmartPlugCommunicationException ex)
            {
                Log.AddToLog($"ERROR: Plug communication failed (IP:{ex.IpAddress}, type: {ex.PlugType}): {ex.Message}", true, RpmLogEntryItemType.ERROR, ex);
                throw new RpmApiException("Powering off and on failed in MonitorHandler", ".powerOffAndOn", RpmExceptionType.Exception, ex);
            }
            catch (RpmApiException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw new RpmApiException("Powering off and on failed in MonitorHandler", "powerOffAndOn", RpmExceptionType.Exception, ex);
            }
        }