示例#1
0
        static void ParseShelly(string Topic, string Payload)
        {
            string[] Top      = Topic.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
            string   ID       = Top[1];
            string   SubTopic = "";

            if (ID == "announce")
            {
                return;
            }

            if (Topic.Count(C => C == '/') >= 2)
            {
                SubTopic = Topic.Substring(Topic.IndexOf('/', Topic.IndexOf('/') + 1) + 1);
            }

            HomeDevice Dev = SmartHome.GetOrCreateDevice(ID, ID);

            if (Dev != null)
            {
                Dev.ReceiveUpdateProperty(SubTopic, Payload);
            }

            ActionsParser.TriggerEvent(Topic, Payload);
        }
示例#2
0
        public override void Toggle(bool On)
        {
            MQTT.Publish(string.Format("shellies/{0}/relay/0/command", ID), On ? "on" : "off");

            if (Program.TEST)
            {
                RelayValue = On ? "on" : "off";
                SmartHome.BroadcastChange(this, "relay/0");
            }
        }
示例#3
0
        public void Stop()
        {
            IsRoller = true;
            MQTT.Publish(string.Format("shellies/{0}/roller/0/command", ID), "stop");

            if (Program.TEST)
            {
                RollerState = "stop";
                SmartHome.BroadcastChange(this, "roller/0");
            }
        }
示例#4
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();
            }
        }
示例#5
0
        public virtual void ReceiveUpdateProperty(string Name, string StrValue)
        {
            LastUpdate = DateTime.Now;

            for (int i = 0; i < Properties.Length; i++)
            {
                if (Properties[i].PropertyName == Name)
                {
                    object Value = Utils.ParseType(Properties[i].PropertyType, StrValue);

                    if (Properties[i].Get() != Value)
                    {
                        Properties[i].Set(Value);

                        SmartHome.BroadcastChange(this, Name);
                    }
                }
            }
        }
示例#6
0
        static void Main(string[] args)
        {
            //Test();
            //return;

            Logging.Init();
            Tasks.Init();
            SmartHome.Init();
            ActionsParser.Init();
            MQTT.Init();

            NancyHost Host = new NancyHost(new Uri("http://localhost:8080"));

            Host.Start();

            //Application.EnableVisualStyles();
            //Application.Run(new MainForm());

            while (true)
            {
                Tasks.Tick();
                Thread.Sleep(250);
            }
        }
示例#7
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);
                        }
                    }
                });
            }
        }