Пример #1
0
        public LocalizationResponse Localize(LocalizationRequest request)
        {
            try
            {
                var deviceService = new DeviceService(Context);
                var device = new Device
                {
                    Name = request.Name,
                    IpAddress = request.IpAddress,
                    MacAddress = request.MacAddress,
                    IsLocalized = request.IsLocalized,
                    Room = new Room
                    {
                        Department = new Department
                        {
                            Name = request.Department
                        },
                        Name = request.Room
                    },
                    Location = new Location
                    {
                        Name = request.Location
                    }
                };

                var message = deviceService.Localize(device);

                return new LocalizationResponse(message);
            }
            catch (Exception ex)
            {
                return new LocalizationResponse(ex);
            }
        }
Пример #2
0
        public void AssignDeviceToRoom(Device device, string roomName)
        {
            if (device == null) throw new ArgumentNullException("device");
            if (string.IsNullOrEmpty(roomName)) throw new ArgumentNullException("roomName");

            var room = Context.Rooms.FirstOrDefault(x => x.Name == roomName);
            if (room == null)
            {
                throw new InvalidOperationException(string.Format("Room {0} was not found", roomName));
            }

            var roomService = new RoomService(Context);
            roomService.AssignDevice(room, device);
        }
Пример #3
0
        public string Localize(Device device)
        {
            if (device == null) throw new ArgumentNullException("device");
            if (device.Room == null) return "Unknown device room";
            if (device.Room.Department == null) return "Unknown department";
            if (device.Location == null) return "Unknown device location";

            var department = Context.Departments.FirstOrDefault(x => x.Name == device.Room.Department.Name);
            if (department == null)
            {
                return string.Format("Department {0} does not exist.", device.Room.Department.Name);
            }

            var room = department.Rooms.FirstOrDefault(x => x.Name == device.Room.Name);
            if (room == null)
            {
                return string.Format("Room {0} was not found in department {1}", device.Room.Name, department.Name);
            }

            var location = room.Locations.FirstOrDefault(x => x.Name == device.Location.Name);
            if (location == null)
            {
                return string.Format("Location {0} was not found in room {1}", device.Location.Name, room.Name);
            }

            var existingDevice = Context.Devices.FirstOrDefault(x => x.IpAddress == device.IpAddress && x.MacAddress == device.MacAddress);
            if (existingDevice != null)
            {
                existingDevice.Name = device.Name;
                existingDevice.IsLocalized = true;
                existingDevice.Location = location;
                existingDevice.Room = room;

                Context.SaveChanges();

                return string.Format("Device {0} was successfully localized in department {1}, room {2}, location {3}", device.Name, department.Name, room.Name, location.Name);
            }

            var newDevice = Create(device.Name, device.IpAddress, device.MacAddress, true);

            newDevice.Location = location;
            room.Devices.Add(newDevice);

            Context.SaveChanges();

            return string.Format("Device {0} was successfully localized in department {1}, room {2}, location {3}", device.Name, department.Name, room.Name, location.Name);
        }
Пример #4
0
        public void AssignDevice(Room room, Device device)
        {
            if (room == null) throw new ArgumentNullException("room");
            if (device == null) throw new ArgumentNullException("device");

            var existingRoom = Context.Rooms.FirstOrDefault(x => x.Id == room.Id);
            if (existingRoom == null)
            {
                throw new InvalidOperationException(string.Format("Room with id {0} does not exist", room.Id));
            }

            if (device.Location == null)
            {
                throw new ArgumentException("Device is not assigned to a location");
            }

            if (!existingRoom.Locations.Any(x => x.Name == device.Location.Name))
            {
                throw new InvalidOperationException(string.Format("Location {0} is not available in room {1}", device.Location.Name, room.Name));
            }

            existingRoom.Devices.Add(device);
            Context.SaveChanges();
        }
Пример #5
0
        public void Unlocalize(Device device)
        {
            if (device == null) throw new ArgumentNullException();

            device.Location = null;
            device.Room = null;
            device.IsLocalized = false;

            Context.SaveChanges();
        }