Exemple #1
0
        public void DeviceBase_TestCopyState()
        {
            var device = CreateTestDevice();

            var copyState = device.CopyState();

            var testState = new DeviceBase.DeviceState();
            testState.Name = "ProperDeviceName";
            testState.DisplayName = "ProperDisplayName";
            testState.Archetype = "ProperArchetype";
            testState.Type = device.GetType().ToString();

            Assert.AreEqual(testState.Name, copyState.Name);
            Assert.AreEqual(testState.DisplayName, copyState.DisplayName);
            Assert.AreEqual(testState.Archetype, copyState.Archetype);
            Assert.AreEqual(testState.Type, copyState.Type);
        }
Exemple #2
0
 public ProxyDevice(DeviceBase.DeviceState state, DeviceCreationInfo creationInfo)
     : base(state, creationInfo)
 {
 }
        public object OnUpdateDeviceStatus(string deviceName, dynamic body)
        {
            DeviceBase device = mDeviceManager.GetDevice(deviceName);

            if (device == null)
            {
                throw new ServiceBase.RequestException(string.Format("Unknown device: {0}", deviceName));
            }

            DeviceBase.DeviceState state = device.CopyState();
            foreach (var property in state.GetType().GetProperties())
            {
                if (!body.ContainsKey(property.Name))
                {
                    continue;
                }

                // Make sure we can actually set the value to begin with.
                if (property.SetMethod == null)
                {
                    continue;
                }

                Type type  = property.PropertyType;
                var  value = body[property.Name];

                if (type == typeof(System.Int32))
                {
                    property.SetValue(state, Int32.Parse((string)value));
                }
                else if (type == typeof(string))
                {
                    property.SetValue(state, (string)value);
                }
                else if (type == typeof(bool))
                {
                    property.SetValue(state, bool.Parse((string)value));
                }
                else if (type == typeof(float))
                {
                    property.SetValue(state, float.Parse((string)value));
                }
                else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List <>))
                {
                    // TODO - Add proper support for lists.
                }
                else if (type.IsEnum)
                {
                    var intValue = Int32.Parse((string)value);
                    property.SetValue(state, Enum.ToObject(type, intValue));
                }
                else
                {
                    throw new ArgumentException("Unhandled type: " + type.Name);
                }
            }

            lock (device)
            {
                device.ApplyState(state);

                return(device.CopyState());
            }
        }