示例#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 static HomeDevice GetOrCreateDevice(string ID, string Name)
        {
            string[]   ModelID = ID.Split(new[] { '-' });
            HomeDevice Device  = GetDevice(ID);

            if (Device == null)
            {
                switch (ModelID[0])
                {
                case "shelly1":
                    Device = new HomeDeviceRelay(ID, Name);
                    break;

                case "shellydw2":
                    Device = new HomeDeviceDoor(ID, Name);
                    break;

                case "shelly25":
                case "shellyswitch25":
                    Device = new HomeDeviceRelay2(ID, Name);
                    break;

                default:
                    throw new NotImplementedException();
                }

                RegisterDevice(Device);
            }

            return(Device);
        }
示例#3
0
 public static void BroadcastChange(HomeDevice Dev, string Name)
 {
     WSS.WebSocketServices["/ws"].Sessions.Broadcast(
         JSON.Serialize(new {
         EventName = "ws_set_inner",
         ID        = string.Format("sd-{0}", Dev.ID),
         ClassName = "id-value",
         Inner     = Dev.Value.ToString()
     })
         );
 }
示例#4
0
        public static void RegisterDevice(HomeDevice Device)
        {
            Devices.Add(Device);

            if (WSS != null && WSS.IsListening)
            {
                WSS.WebSocketServices["/ws"].Sessions.Broadcast(
                    JSON.Serialize(new {
                    EventName = "ws_refresh_page"
                })
                    );
            }
        }
示例#5
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();
            }
        }
示例#6
0
        public static DevProperty[] GetAllDeviceProperties(HomeDevice Dev)
        {
            List <DevProperty> DevProps = new List <DevProperty>();

            IEnumerable <PropertyInfo> Props = Dev.GetType().GetProperties().Where(P => P.GetCustomAttribute <DevicePropertyAttribute>() != null);

            foreach (PropertyInfo Prop in Props)
            {
                DevProperty DevProp = new DevProperty()
                {
                    Owner        = Dev,
                    Prop         = Prop,
                    PropertyName = Prop.GetCustomAttribute <DevicePropertyAttribute>().Name
                };

                DevProps.Add(DevProp);
            }

            return(DevProps.ToArray());
        }
示例#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);
                        }
                    }
                });
            }
        }