예제 #1
0
        public void CreateDevice(Device device)
        {
            if (device == null)
                throw new WebFaultException(HttpStatusCode.BadRequest);

            PhysicalAddress mac;
            try
            {
                mac = PhysicalAddress.Parse(device.MACAddress);
            }
            catch (FormatException ex)
            {
                throw new WebFaultException<FormatException>(ex, HttpStatusCode.BadRequest);
            }

            //test if device already exists
            if (Manager.Instance.Current.Devices.Any(p => Object.Equals(p.MACAddress, mac)))
                throw new WebFaultException(HttpStatusCode.Conflict);
            if (String.IsNullOrWhiteSpace(device.Name))
                throw new WebFaultException<ArgumentNullException>(new ArgumentNullException("Name can not be empty"), HttpStatusCode.BadRequest);

            Logic.Device logicDevice = new Logic.Device();
            logicDevice.Name = device.Name;
            logicDevice.MACAddress = mac;

            logicDevice.State = Logic.DeviceState.Offline;
            if (!String.IsNullOrWhiteSpace(device.IPAddress))
            {
                IPAddress ip = null;
                if (IPAddress.TryParse(device.IPAddress, out ip))
                {
                    if (IPAddress.None.Equals(ip) || !Manager.Instance.Current.Devices.Any(p => Object.Equals(ip, p.IPAddress)))
                    {
                        logicDevice.IPAddress = ip;
                    }
                    else
                    {
                        throw new WebFaultException<ArgumentException>(new ArgumentException("ip already used by another device"), HttpStatusCode.Conflict);
                    }
                }
                else
                {
                    throw new WebFaultException<ArgumentException>(new ArgumentException("invalid ip"), HttpStatusCode.BadRequest);
                }
            }
            else
            {
                logicDevice.IPAddress = IPAddress.None;
            }

            Manager.Instance.Current.AddDevice(logicDevice);
        }
예제 #2
0
        public void SaveDevice(string mac, Device device)
        {
            if (device == null)
                throw new WebFaultException(HttpStatusCode.BadRequest);

            PhysicalAddress macAddress;
            try
            {
                macAddress = PhysicalAddress.Parse(mac);
            }
            catch (FormatException ex)
            {
                throw new WebFaultException<FormatException>(ex, HttpStatusCode.BadRequest);
            }
            PhysicalAddress macAddressNew;
            try
            {
                macAddressNew = PhysicalAddress.Parse(device.MACAddress);
            }
            catch (FormatException ex)
            {
                throw new WebFaultException<FormatException>(ex, HttpStatusCode.BadRequest);
            }

            Logic.Device logicDevice = Manager.Instance.Current.Devices.FirstOrDefault(p => Object.Equals(p.MACAddress, macAddress));
            if (logicDevice == null)
                throw new WebFaultException(HttpStatusCode.NotFound);
            if (String.IsNullOrWhiteSpace(device.Name))
                throw new WebFaultException<ArgumentNullException>(new ArgumentNullException("Name can not be empty"), HttpStatusCode.BadRequest);

            if (!Object.Equals(macAddressNew, logicDevice.MACAddress)) //do we allow this?
            {
                if (Manager.Instance.Current.Devices.Any(p => Object.Equals(p.MACAddress, macAddressNew)))
                {
                    throw new WebFaultException<ArgumentException>(new ArgumentException("mac-address is already used by another device"), HttpStatusCode.Conflict);
                }
            }

            logicDevice.Name = device.Name;
            logicDevice.MACAddress = macAddressNew;
            //TODO: ip-address modification
        }