Пример #1
0
 private static SonoffTasmota FindSonoffFromTopic(string topic)
 {
     foreach (IObject obj in HomeAutomationServer.server.Objects)
     {
         if (obj is SonoffTasmota)
         {
             SonoffTasmota sonoff = (SonoffTasmota)obj;
             if (sonoff.TasmotaTopic.Equals(topic))
             {
                 return(sonoff);
             }
         }
     }
     return(null);
 }
Пример #2
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);
        }
Пример #3
0
        private static SonoffTasmota FindSonoffFromName(string name)
        {
            SonoffTasmota relay = null;

            foreach (IObject obj in HomeAutomationServer.server.Objects)
            {
                if (obj.GetName().ToLower().Equals(name.ToLower()))
                {
                    relay = (SonoffTasmota)obj;
                    break;
                }
                if (obj.GetFriendlyNames() == null)
                {
                    continue;
                }
                if (Array.IndexOf(obj.GetFriendlyNames(), name.ToLower()) > -1)
                {
                    relay = (SonoffTasmota)obj;
                    break;
                }
            }
            return(relay);
        }
Пример #4
0
 public static void OnMQTTMessage(MqttClient sender, MqttMsgPublishEventArgs e)
 {
     if (e.Topic.StartsWith("tele/") && e.Topic.EndsWith("/STATE"))
     {
         string id = e.Topic.Substring("tele/".Length);
         id = id.Substring(0, id.Length - "/STATE".Length);
         SonoffTasmota sonoff = FindSonoffFromTopic(id);
         if (sonoff == null)
         {
             return;
         }
         string  message = Encoding.UTF8.GetString(e.Message);
         dynamic update  = JsonConvert.DeserializeObject <ExpandoObject>(message);
         string  state   = update.POWER;
         if (sonoff.Connected == true)
         {
             if (state.Equals("ON"))
             {
                 sonoff.Switch = true;
             }
             else
             {
                 sonoff.Switch = false;
             }
         }
         else
         {
             if (sonoff.Switch)
             {
                 HomeAutomationServer.server.MQTTClient.Publish("cmnd/" + sonoff.TasmotaTopic + "/power" + sonoff.Channel, "on");
             }
             else
             {
                 HomeAutomationServer.server.MQTTClient.Publish("cmnd/" + sonoff.TasmotaTopic + "/power" + sonoff.Channel, "off");
             }
             sonoff.Connected = true;
         }
     }
     if (e.Topic.StartsWith("stat/") && e.Topic.EndsWith("/POWER"))
     {
         string id = e.Topic.Substring("stat/".Length);
         id = id.Substring(0, id.Length - "/POWER".Length);
         SonoffTasmota sonoff = FindSonoffFromTopic(id);
         if (sonoff == null)
         {
             return;
         }
         string message = Encoding.UTF8.GetString(e.Message);
         if (sonoff.Connected == true)
         {
             if (message.Equals("ON"))
             {
                 sonoff.Switch = true;
             }
             else
             {
                 sonoff.Switch = false;
             }
         }
         else
         {
             if (sonoff.Switch)
             {
                 HomeAutomationServer.server.MQTTClient.Publish("cmnd/" + sonoff.TasmotaTopic + "/power" + sonoff.Channel, "on");
             }
             else
             {
                 HomeAutomationServer.server.MQTTClient.Publish("cmnd/" + sonoff.TasmotaTopic + "/power" + sonoff.Channel, "off");
             }
             sonoff.Connected = true;
         }
     }
 }
Пример #5
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());
        }