Ejemplo n.º 1
0
        public static void Setup(Room room, dynamic device)
        {
            string[]      friendlyNames = Array.ConvertAll(((List <object>)device.FriendlyNames).ToArray(), x => x.ToString());
            SonoffTasmota relay         = new SonoffTasmota(device.Name, device.TasmotaTopic, device.Description, friendlyNames);

            relay.RealDescription = device.RealDescription;
            relay.Channel         = (byte)device.Channel;
            if (device.Switch)
            {
                relay.Start();
            }
            else
            {
                relay.Stop();
            }
            room.AddItem(relay);
        }
Ejemplo n.º 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());
        }