コード例 #1
0
        public static string SendParameters(string method, string[] request, Identity login)
        {
            if (method.Equals("createSonoff"))
            {
                if (!login.IsAdmin())
                {
                    return(new ReturnStatus(CommonStatus.ERROR_FORBIDDEN_REQUEST, "Insufficient permissions").Json());
                }
                string name    = null;
                string topic   = null;
                byte   channel = 1;
                Room   room    = null;

                foreach (string cmd in request)
                {
                    string[] command = cmd.Split('=');
                    switch (command[0])
                    {
                    case "objname":
                        name = command[1];
                        break;

                    case "topic":
                        topic = command[1];
                        break;

                    case "channel":
                        channel = byte.Parse(command[1]);
                        break;

                    case "room":
                        foreach (Room stanza in HomeAutomationServer.server.Rooms)
                        {
                            if (stanza.Name.ToLower().Equals(command[1].ToLower()))
                            {
                                room = stanza;
                            }
                        }
                        break;
                    }
                }
                if (room == null)
                {
                    return(new ReturnStatus(CommonStatus.ERROR_NOT_FOUND, "Room not found").Json());
                }
                if (ObjectFactory.FromString(name) != null)
                {
                    return(new ReturnStatus(CommonStatus.ERROR_BAD_REQUEST, name + " already exists").Json());
                }
                TasmotaSwitch relay = new TasmotaSwitch(name, topic);
                relay.Channel = channel;
                room.AddItem(relay);
                ReturnStatus data = new ReturnStatus(CommonStatus.SUCCESS);
                data.Object.device = relay;
                return(data.Json());
            }
            if (method.Equals("addAction/on"))
            {
                if (!login.IsAdmin())
                {
                    return(new ReturnStatus(CommonStatus.ERROR_FORBIDDEN_REQUEST, "Insufficient permissions").Json());
                }
                string name = null;
                string obj  = null;

                foreach (string cmd in request)
                {
                    string[] command = cmd.Split('=');
                    if (command[0].Equals("interface"))
                    {
                        continue;
                    }
                    switch (command[0])
                    {
                    case "objname":
                        name = command[1];
                        break;

                    case "action":
                        obj = command[1];
                        break;
                    }
                }
                if (name == null || obj == null)
                {
                    return(new ReturnStatus(CommonStatus.ERROR_BAD_REQUEST).Json());
                }

                TasmotaSwitch button = null;
                HomeAutomation.ObjectInterfaces.Action action = null;

                foreach (HomeAutomation.ObjectInterfaces.Action iobj in HomeAutomationServer.server.Actions)
                {
                    if (iobj.Name.Equals(obj))
                    {
                        action = iobj;
                    }
                }
                foreach (IObject iobj in HomeAutomationServer.server.Objects)
                {
                    if (iobj.GetName().Equals(name))
                    {
                        button = (TasmotaSwitch)iobj;
                    }
                }
                if (button == null)
                {
                    return(new ReturnStatus(CommonStatus.ERROR_NOT_FOUND, name + " not found").Json());
                }
                if (button == null)
                {
                    return(new ReturnStatus(CommonStatus.ERROR_NOT_FOUND, obj + " not found").Json());
                }

                button.AddActionOn(action);
                ReturnStatus data = new ReturnStatus(CommonStatus.SUCCESS);
                data.Object.button = button;
                return(data.Json());
            }
            if (method.Equals("addAction/off"))
            {
                if (!login.IsAdmin())
                {
                    return(new ReturnStatus(CommonStatus.ERROR_FORBIDDEN_REQUEST, "Insufficient permissions").Json());
                }
                string name = null;
                string obj  = null;

                foreach (string cmd in request)
                {
                    string[] command = cmd.Split('=');
                    if (command[0].Equals("interface"))
                    {
                        continue;
                    }
                    switch (command[0])
                    {
                    case "objname":
                        name = command[1];
                        break;

                    case "action":
                        obj = command[1];
                        break;
                    }
                }
                if (name == null || obj == null)
                {
                    return(new ReturnStatus(CommonStatus.ERROR_BAD_REQUEST).Json());
                }

                TasmotaSwitch button = null;
                HomeAutomation.ObjectInterfaces.Action action = null;

                foreach (HomeAutomation.ObjectInterfaces.Action iobj in HomeAutomationServer.server.Actions)
                {
                    if (iobj.Name.Equals(obj))
                    {
                        action = iobj;
                    }
                }
                foreach (IObject iobj in HomeAutomationServer.server.Objects)
                {
                    if (iobj.GetObjectType() == "SONOFF_TASMOTA_BUTTON")
                    {
                        if (iobj.GetName().Equals(name))
                        {
                            button = (TasmotaSwitch)iobj;
                        }
                    }
                }
                if (button == null)
                {
                    return(new ReturnStatus(CommonStatus.ERROR_NOT_FOUND, name + " not found").Json());
                }
                if (button == null)
                {
                    return(new ReturnStatus(CommonStatus.ERROR_NOT_FOUND, obj + " not found").Json());
                }

                button.AddActionOff(action);
                ReturnStatus data = new ReturnStatus(CommonStatus.SUCCESS);
                data.Object.button = button;
                return(data.Json());
            }
            if (method.Equals("removeAction/on"))
            {
                if (!login.IsAdmin())
                {
                    return(new ReturnStatus(CommonStatus.ERROR_FORBIDDEN_REQUEST, "Insufficient permissions").Json());
                }
                string name = null;
                string obj  = null;

                foreach (string cmd in request)
                {
                    string[] command = cmd.Split('=');
                    if (command[0].Equals("interface"))
                    {
                        continue;
                    }
                    switch (command[0])
                    {
                    case "objname":
                        name = command[1];
                        break;

                    case "action":
                        obj = command[1];
                        break;
                    }
                }
                if (name == null || obj == null)
                {
                    return(new ReturnStatus(CommonStatus.ERROR_BAD_REQUEST).Json());
                }

                TasmotaSwitch button = null;
                HomeAutomation.ObjectInterfaces.Action action = null;

                foreach (HomeAutomation.ObjectInterfaces.Action iobj in HomeAutomationServer.server.Actions)
                {
                    if (iobj.Name.Equals(obj))
                    {
                        action = iobj;
                    }
                }
                foreach (IObject iobj in HomeAutomationServer.server.Objects)
                {
                    if (iobj.GetObjectType() == "SONOFF_TASMOTA_BUTTON")
                    {
                        if (iobj.GetName().Equals(name))
                        {
                            button = (TasmotaSwitch)iobj;
                        }
                    }
                }
                if (button == null)
                {
                    return(new ReturnStatus(CommonStatus.ERROR_NOT_FOUND, name + " not found").Json());
                }
                if (button == null)
                {
                    return(new ReturnStatus(CommonStatus.ERROR_NOT_FOUND, obj + " not found").Json());
                }

                button.RemoveActionOn(action);
                ReturnStatus data = new ReturnStatus(CommonStatus.SUCCESS);
                data.Object.button = button;
                return(data.Json());
            }
            if (method.Equals("removeAction/off"))
            {
                if (!login.IsAdmin())
                {
                    return(new ReturnStatus(CommonStatus.ERROR_FORBIDDEN_REQUEST, "Insufficient permissions").Json());
                }
                string name = null;
                string obj  = null;

                foreach (string cmd in request)
                {
                    string[] command = cmd.Split('=');
                    if (command[0].Equals("interface"))
                    {
                        continue;
                    }
                    switch (command[0])
                    {
                    case "objname":
                        name = command[1];
                        break;

                    case "action":
                        obj = command[1];
                        break;
                    }
                }
                if (name == null || obj == null)
                {
                    return(new ReturnStatus(CommonStatus.ERROR_BAD_REQUEST).Json());
                }

                TasmotaSwitch button = null;
                HomeAutomation.ObjectInterfaces.Action action = null;

                foreach (HomeAutomation.ObjectInterfaces.Action iobj in HomeAutomationServer.server.Actions)
                {
                    if (iobj.Name.Equals(obj))
                    {
                        action = iobj;
                    }
                }
                foreach (IObject iobj in HomeAutomationServer.server.Objects)
                {
                    if (iobj.GetObjectType() == "SONOFF_TASMOTA_BUTTON")
                    {
                        if (iobj.GetName().Equals(name))
                        {
                            button = (TasmotaSwitch)iobj;
                        }
                    }
                }
                if (button == null)
                {
                    return(new ReturnStatus(CommonStatus.ERROR_NOT_FOUND, name + " not found").Json());
                }
                if (button == null)
                {
                    return(new ReturnStatus(CommonStatus.ERROR_NOT_FOUND, obj + " not found").Json());
                }

                button.RemoveActionOff(action);
                ReturnStatus data = new ReturnStatus(CommonStatus.SUCCESS);
                data.Object.button = button;
                return(data.Json());
            }
            return(new ReturnStatus(CommonStatus.ERROR_NOT_IMPLEMENTED).Json());
        }
コード例 #2
0
        public static string SendParameters(string method, string[] request, Identity login)
        {
            if (method.Equals("switch"))
            {
                SonoffTasmota relay  = null;
                bool          status = false;

                foreach (string cmd in request)
                {
                    string[] command = cmd.Split('=');
                    switch (command[0])
                    {
                    case "objname":
                        relay = FindSonoffFromName(command[1]);
                        break;

                    case "switch":
                        status = bool.Parse(command[1]);
                        break;
                    }
                }
                if (relay == null)
                {
                    return(new ReturnStatus(CommonStatus.ERROR_NOT_FOUND).Json());
                }
                if (!login.HasAccess(relay))
                {
                    return(new ReturnStatus(CommonStatus.ERROR_FORBIDDEN_REQUEST, "Insufficient permissions").Json());
                }
                if (status)
                {
                    relay.Start();
                }
                else
                {
                    relay.Stop();
                }
                return(new ReturnStatus(CommonStatus.SUCCESS).Json());
            }
            if (method.Equals("createSonoff"))
            {
                if (!login.IsAdmin())
                {
                    return(new ReturnStatus(CommonStatus.ERROR_FORBIDDEN_REQUEST, "Insufficient permissions").Json());
                }
                string   name          = null;
                string[] friendlyNames = null;
                string   description   = null;
                string   topic         = null;
                byte     channel       = 1;
                Room     room          = null;

                foreach (string cmd in request)
                {
                    string[] command = cmd.Split('=');
                    if (command[0].Equals("interface"))
                    {
                        continue;
                    }
                    switch (command[0])
                    {
                    case "objname":
                        name = command[1];
                        break;

                    case "description":
                        description = command[1];
                        break;

                    case "setfriendlynames":
                        string names = command[1];
                        friendlyNames = names.Split(',');
                        break;

                    case "topic":
                        topic = command[1];
                        break;

                    case "channel":
                        channel = byte.Parse(command[1]);
                        break;

                    /*case "client":
                     *  string clientName = command[1];
                     *  foreach (Client clnt in HomeAutomationServer.server.Clients)
                     *  {
                     *      if (clnt.Name.Equals(clientName))
                     *      {
                     *          client = clnt;
                     *      }
                     *  }
                     *  if (client == null) return new ReturnStatus(CommonStatus.ERROR_NOT_FOUND, "Raspi-Client not found").Json();
                     *  break;*/
                    case "room":
                        foreach (Room stanza in HomeAutomationServer.server.Rooms)
                        {
                            if (stanza.Name.ToLower().Equals(command[1].ToLower()))
                            {
                                room = stanza;
                            }
                        }
                        break;
                    }
                }
                if (room == null)
                {
                    return(new ReturnStatus(CommonStatus.ERROR_NOT_FOUND, "Room not found").Json());
                }
                SonoffTasmota relay = new SonoffTasmota(name, topic, description, friendlyNames);
                relay.RealDescription = description;
                relay.Channel         = channel;
                room.AddItem(relay);
                ReturnStatus data = new ReturnStatus(CommonStatus.SUCCESS);
                data.Object.device = relay;
                return(data.Json());
            }
            return(new ReturnStatus(CommonStatus.ERROR_NOT_IMPLEMENTED).Json());
        }