Esempio n. 1
0
        protected override void OnMessage(MessageEventArgs e)
        {
            JSONMsgBase Msg = JSON.Deserialize <JSONMsgBase>(e.Data);

            switch (Msg.EventName)
            {
            case "Command":
                Msg = JSON.Deserialize <JSONMsgCommand>(e.Data);
                break;

            default:
                throw new NotImplementedException();
            }

            if (Msg is JSONMsgCommand Command)
            {
                HomeDevice Dev = SmartHome.GetDevice(Command.Args.ID);

                switch (Command.Args.Value)
                {
                case "ToggleOn":
                    Dev.Toggle(true);
                    break;

                case "ToggleOff":
                    Dev.Toggle(false);
                    break;

                case "Open":
                    ((HomeDeviceRelay2)Dev).Open();
                    break;

                case "Stop":
                    ((HomeDeviceRelay2)Dev).Stop();
                    break;

                case "Close":
                    ((HomeDeviceRelay2)Dev).Close();
                    break;

                default:
                    throw new NotImplementedException();
                }
            }
            else
            {
                throw new NotImplementedException();
            }
        }
Esempio n. 2
0
        static void ParseAction(XmlElement E)
        {
            if (E.Name == "DeviceName")
            {
                string DeviceID = E.GetAttribute("ID");
                string IsRoller = E.GetAttribute("IsRoller");

                /*if (DeviceNames.ContainsKey(DeviceID))
                 *      DeviceNames.Remove(DeviceID);
                 *
                 * DeviceNames.Add(DeviceID, E.InnerText.Trim());*/

                HomeDevice Dev = SmartHome.GetOrCreateDevice(DeviceID, E.InnerText.Trim());

                if (!string.IsNullOrEmpty(IsRoller) && IsRoller == "true" && Dev is HomeDeviceRelay2 Rel2)
                {
                    Rel2.Stop();
                }
            }

            if (E.Name == "Event")
            {
                ActionEvent Event = new ActionEvent(E.GetAttribute("MQTT"), E.GetAttribute("Value"));
                Event.Nodes = E.ChildNodes;
                Events.Add(Event);
            }

            if (E.Name == "Toggle")
            {
                HomeDevice Dev   = SmartHome.GetDeviceByName(E.GetAttribute("Name"));
                bool       Value = E.GetAttribute("Value") == "On";

                if (Dev != null)
                {
                    Dev.Toggle(Value);
                }
            }

            if (E.Name == "WaitSeconds")
            {
                string      Name     = "WaitSeconds_" + E.GetAttribute("Name");
                int         Value    = int.Parse(E.GetAttribute("Value"));
                XmlNodeList Children = E.ChildNodes;

                Tasks.Register(Name, (This) => {
                    foreach (XmlElement Child in Children)
                    {
                        ParseAction(Child);
                    }
                }).ScheduleAfter(Value);
            }

            if (E.Name == "If")
            {
                HomeDevice Dev      = SmartHome.GetDeviceByName(E.GetAttribute("Name"));
                string     Value    = E.GetAttribute("Value");
                string     DevValue = "";

                if (Dev != null && Dev.Value != null)
                {
                    DevValue = Dev.Value.ToString();
                }

                if (DevValue == Value)
                {
                    XmlNodeList Children = E.ChildNodes;
                    foreach (XmlElement Child in Children)
                    {
                        ParseAction(Child);
                    }
                }
            }

            if (E.Name == "SunShutter")
            {
                string MaxPercentStr = E.GetAttribute("Max").Trim();

                if (string.IsNullOrEmpty(MaxPercentStr))
                {
                    MaxPercentStr = "100";
                }

                int  MaxPercent = int.Parse(MaxPercentStr);
                bool Invert     = E.GetAttribute("Invert").Trim() == "True";

                string IntervalStr = E.GetAttribute("Interval").Trim();
                if (string.IsNullOrEmpty(IntervalStr))
                {
                    IntervalStr = "120";
                }

                int    Interval = int.Parse(IntervalStr);
                string DevName  = E.GetAttribute("Name");

                Tasks.Register("SunShutter_" + DevName, (This) => {
                    This.ScheduleAfter(Interval);

                    HomeDevice Dev = SmartHome.GetDeviceByName(DevName);
                    if (Dev != null && Dev is HomeDeviceRelay2 Dev25)
                    {
                        DateTime Now = DateTime.Now;
                        SunPosition.Update();

                        int Percent = 0;
                        if (SunPosition.IsSunsetToDusk(out Percent) || SunPosition.IsDawnToSunrise(out Percent))
                        {
                            Percent = (int)Utils.Lerp(0, MaxPercent, Percent / 100.0f);

                            if (Invert)
                            {
                                Percent = 100 - Percent;
                            }

                            Dev25.SetRollerPosition(Percent);
                        }
                    }
                });
            }
        }