Ejemplo n.º 1
0
        public void EditSmartThing(SmartThing input)
        {
            var sensor = Things.Where(x => x.Id == input.Id).FirstOrDefault();

            if (sensor == null)
            {
                throw new Exception("Вещь с таким Id не обнаружен");
            }
            if (input.Name != null)
            {
                sensor.Name = input.Name;
            }
            if (input.ControllerId.HasValue)
            {
                sensor.ControllerId = input.ControllerId;
            }
            if (input.Description != null)
            {
                sensor.Description = input.Description;
            }
            if (input.Type != -1)
            {
                sensor.Type = input.Type;
            }
            if (input.UserGroupId != -1)
            {
                sensor.UserGroupId = input.UserGroupId;
            }
        }
Ejemplo n.º 2
0
        public void Listen(IPAddress address, int port, SmartThing smartThing)
        {
            try
            {
                TcpListener server = new TcpListener(address, port);
                server.Start();

                byte[] bytes = new Byte[256];
                string data  = null;

                Console.Clear();

                while (true)
                {
                    Console.WriteLine("Waiting for an action...");

                    TcpClient client = server.AcceptTcpClient();
                    Console.WriteLine("Finally! An Action!");

                    data = null;

                    NetworkStream stream = client.GetStream();

                    int i;

                    while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                    {
                        data = Encoding.ASCII.GetString(bytes, 0, i);
                        ESmartCommands command = JsonConvert.DeserializeObject <ESmartCommands>(data);

                        string response = smartThing.Execute(command);

                        byte[] msg = Encoding.ASCII.GetBytes(response);

                        stream.Write(msg, 0, msg.Length);
                    }

                    client.Close();
                }
            }
            catch
            {
                Console.WriteLine("something went wrong...");
            }
        }
 public void EditThing([FromBody] SmartThing input)
 {
     _thingRepository.EditSmartThing(input);
     _thingRepository.SaveChanges();
 }
Ejemplo n.º 4
0
 public SmartThing CreateSmartThing(SmartThing input)
 {
     _context.Add(input);
     return(input);
 }
Ejemplo n.º 5
0
        public void Start()
        {
            Console.WriteLine("Starting smartobject...");

            Console.WriteLine("What is my Address?");

            Console.Write("-> ");
            string address = Console.ReadLine();

            IPAddress ipAddress;

            if (!string.IsNullOrEmpty(address) && IPAddress.TryParse(address, out ipAddress))
            {
                Console.WriteLine("What is my port?");

                Console.Write("-> ");
                string portStr = Console.ReadLine();

                int port = 0;

                if (!string.IsNullOrEmpty(portStr) && int.TryParse(portStr, out port))
                {
                    if (SmartManagerService.Register(address, port))
                    {
                        Console.WriteLine("Choose my type:");
                        Console.WriteLine("1 - TV");
                        Console.WriteLine("2 - Air Conditioner");
                        Console.WriteLine("3 - Light");
                        Console.WriteLine("4 - Water heater");
                        Console.WriteLine("5 - Lock");

                        Console.Write("->");
                        string type = Console.ReadLine();

                        SmartThing smartThing = null;

                        switch (type)
                        {
                        case "1":
                            smartThing = new SmartTV();

                            break;

                        case "2":
                            smartThing = new SmartAirConfitioner();

                            break;

                        case "3":
                            smartThing = new SmartLight();

                            break;

                        case "4":
                            smartThing = new SmartWaterHeater();

                            break;

                        case "5":
                            smartThing = new SmartLock();

                            break;

                        default:
                            Console.WriteLine("Invalid choice...");

                            break;
                        }

                        if (smartThing != null)
                        {
                            new Listener().Listen(ipAddress, port, smartThing);
                        }
                    }

                    else
                    {
                        Console.WriteLine("Sorry... Could not register myself to the manager...");
                    }
                }

                else
                {
                    Console.WriteLine("Port not valid...");
                }
            }

            else
            {
                Console.WriteLine("Invalid address...");
            }

            Console.WriteLine("Smart object shutting down...");
        }