Beispiel #1
0
        public void DeviceClassTest()
        {
            var deviceClass = new DeviceClass("Test", "V1");
            DeviceClassRepository.Save(deviceClass);

            // test GetAll
            var deviceClasses = DeviceClassRepository.GetAll();
            Assert.Greater(deviceClasses.Count, 0);

            // test Get(id)
            var deviceClass1 = DeviceClassRepository.Get(deviceClass.ID);
            Assert.IsNotNull(deviceClass1);
            Assert.AreEqual("Test", deviceClass1.Name);

            // test Get(name, version)
            var deviceClass2 = DeviceClassRepository.Get("Test", "V1");
            Assert.IsNotNull(deviceClass2);

            // test Save
            deviceClass.Name = "Test2";
            deviceClass.Version = "V2";
            deviceClass.IsPermanent = true;
            DeviceClassRepository.Save(deviceClass);
            var deviceClass3 = DeviceClassRepository.Get(deviceClass.ID);
            Assert.AreEqual("Test2", deviceClass3.Name);
            Assert.AreEqual("V2", deviceClass3.Version);
            Assert.AreEqual(true, deviceClass3.IsPermanent);

            // test Delete
            DeviceClassRepository.Delete(deviceClass.ID);
            var deviceClass4 = DeviceClassRepository.Get(deviceClass.ID);
            Assert.IsNull(deviceClass4);
        }
        public void Save(DeviceClass deviceClass)
        {
            if (deviceClass == null)
                throw new ArgumentNullException("deviceClass");

            using (var context = new DeviceHiveContext())
            {
                context.DeviceClasses.Add(deviceClass);
                if (deviceClass.ID > 0)
                {
                    context.Entry(deviceClass).State = EntityState.Modified;

                    foreach (var equipment in deviceClass.Equipment.Where(e => e.ID > 0))
                    {
                        context.Entry(equipment).State = EntityState.Modified;
                    }
                    foreach (var equipment in context.Equipments.Where(e => e.DeviceClassID == deviceClass.ID))
                    {
                        if (context.Entry(equipment).State == EntityState.Unchanged)
                            context.Equipments.Remove(equipment);
                    }
                }
                
                context.SaveChanges();
            }
        }
Beispiel #3
0
        /// <summary>
        /// Initializes all required properties
        /// </summary>
        /// <param name="name">Equipment name</param>
        /// <param name="code">Equipment code</param>
        /// <param name="type">Equipment type</param>
        /// <param name="deviceClass">Associated device class object</param>
        public Equipment(string name, string code, string type, DeviceClass deviceClass)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException("Name is null or empty!", "name");
            }
            if (string.IsNullOrEmpty(code))
            {
                throw new ArgumentException("Code is null or empty!", "code");
            }
            if (string.IsNullOrEmpty(type))
            {
                throw new ArgumentException("Type is null or empty!", "type");
            }
            if (deviceClass == null)
            {
                throw new ArgumentNullException("deviceClass");
            }

            this.Name          = name;
            this.Code          = code;
            this.Type          = type;
            this.DeviceClass   = deviceClass;
            this.DeviceClassID = deviceClass.ID;
        }
        private DeviceClass GetDeviceClass(DataContext dataContext)
        {
            var deviceClass = dataContext.DeviceClass.Get("device class", "1");
            if (deviceClass != null)
                return deviceClass;

            deviceClass = new DeviceClass("device class", "1");
            dataContext.DeviceClass.Save(deviceClass);
            return deviceClass;
        }
        /// <summary>
        /// Initializes all required properties
        /// </summary>
        /// <param name="guid">Device global identifier</param>
        /// <param name="name">Device name</param>
        /// <param name="network">Associated network object</param>
        /// <param name="deviceClass">Associated device class object</param>
        public Device(string guid, string name, Network network, DeviceClass deviceClass)
            : this(guid)
        {
            if (string.IsNullOrEmpty(name))
                throw new ArgumentException("Name is null or empty!", "name");
            if (deviceClass == null)
                throw new ArgumentNullException("deviceClass");

            this.Name = name;
            this.Network = network;
            this.DeviceClass = deviceClass;
        }
        public void Save(DeviceClass deviceClass)
        {
            if (deviceClass == null)
                throw new ArgumentNullException("deviceClass");

            using (var context = new DeviceHiveContext())
            {
                context.DeviceClasses.Add(deviceClass);
                if (deviceClass.ID > 0)
                {
                    context.Entry(deviceClass).State = EntityState.Modified;
                }
                context.SaveChanges();
            }
        }
        public void Save(DeviceClass deviceClass)
        {
            if (deviceClass == null)
                throw new ArgumentNullException("deviceClass");

            _mongo.EnsureIdentity(deviceClass);

            if (deviceClass.Equipment == null)
                deviceClass.Equipment = new List<Equipment>();
            deviceClass.Equipment.ForEach(e => _mongo.EnsureIdentity(e));

            _mongo.DeviceClasses.Save(deviceClass);

            _mongo.Devices.Update(Query<Device>.EQ(e => e.DeviceClassID, deviceClass.ID),
                Update<Device>.Set(d => d.DeviceClass, deviceClass), new MongoUpdateOptions { Flags = UpdateFlags.Multi });
        }
Beispiel #8
0
        /// <summary>
        /// Initializes all required properties
        /// </summary>
        /// <param name="guid">Device global identifier</param>
        /// <param name="name">Device name</param>
        /// <param name="network">Associated network object</param>
        /// <param name="deviceClass">Associated device class object</param>
        public Device(string guid, string name, Network network, DeviceClass deviceClass)
            : this(guid)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException("Name is null or empty!", "name");
            }
            if (deviceClass == null)
            {
                throw new ArgumentNullException("deviceClass");
            }

            this.Name        = name;
            this.Network     = network;
            this.DeviceClass = deviceClass;
        }
Beispiel #9
0
        /// <summary>
        /// Initializes all required properties
        /// </summary>
        /// <param name="name">Equipment name</param>
        /// <param name="code">Equipment code</param>
        /// <param name="type">Equipment type</param>
        /// <param name="deviceClass">Associated device class object</param>
        public Equipment(string name, string code, string type, DeviceClass deviceClass)
        {
            if (string.IsNullOrEmpty(name))
                throw new ArgumentException("Name is null or empty!", "name");
            if (string.IsNullOrEmpty(code))
                throw new ArgumentException("Code is null or empty!", "code");
            if (string.IsNullOrEmpty(type))
                throw new ArgumentException("Type is null or empty!", "type");
            if (deviceClass == null)
                throw new ArgumentNullException("deviceClass");

            this.Name = name;
            this.Code = code;
            this.Type = type;
            this.DeviceClass = deviceClass;
            this.DeviceClassID = deviceClass.ID;
        }
Beispiel #10
0
        /// <summary>
        /// Default constructor
        /// </summary>
        /// <param name="guid">Device global identifier</param>
        /// <param name="key">Device key</param>
        /// <param name="name">Device name</param>
        /// <param name="network">Associated network object</param>
        /// <param name="deviceClass">Associated device class object</param>
        public Device(Guid guid, string key, string name, Network network, DeviceClass deviceClass)
        {
            if (guid == Guid.Empty)
                throw new ArgumentException("GUID is empty!", "guid");
            if (string.IsNullOrEmpty(key))
                throw new ArgumentException("Key is null or empty!", "key");
            if (string.IsNullOrEmpty(name))
                throw new ArgumentException("Name is null or empty!", "name");
            if (network == null)
                throw new ArgumentNullException("network");
            if (deviceClass == null)
                throw new ArgumentNullException("deviceClass");

            this.GUID = guid;
            this.Key = key;
            this.Name = name;
            this.Network = network;
            this.DeviceClass = deviceClass;
        }
        public void Device()
        {
            var network = new Network("N1");
            DataContext.Network.Save(network);
            RegisterTearDown(() => DataContext.Network.Delete(network.ID));

            var deviceClass = new DeviceClass("D1", "V1");
            DataContext.DeviceClass.Save(deviceClass);
            RegisterTearDown(() => DataContext.DeviceClass.Delete(deviceClass.ID));

            var device = new Device(Guid.NewGuid().ToString(), "Test", network, deviceClass);
            DataContext.Device.Save(device);
            RegisterTearDown(() => DataContext.Device.Delete(device.ID));

            // test GetByNetwork
            var devices = DataContext.Device.GetByNetwork(network.ID);
            Assert.Greater(devices.Count, 0);

            // test Get(id)
            var device1 = DataContext.Device.Get(device.ID);
            Assert.IsNotNull(device1);
            Assert.AreEqual(device.GUID, device1.GUID);
            Assert.IsNull(device1.Key);
            Assert.AreEqual("Test", device1.Name);
            Assert.AreEqual(network.ID, device1.NetworkID);
            Assert.AreEqual(deviceClass.ID, device1.DeviceClassID);
            Assert.IsNotNull(device1.Network);
            Assert.IsNotNull(device1.DeviceClass);

            // test Get(guid)
            var device2 = DataContext.Device.Get(device.GUID);
            Assert.IsNotNull(device2);
            Assert.AreEqual(device.GUID, device2.GUID);
            Assert.AreEqual("Test", device2.Name);
            Assert.AreEqual(network.ID, device2.NetworkID);
            Assert.AreEqual(deviceClass.ID, device2.DeviceClassID);
            Assert.IsNotNull(device2.Network);
            Assert.IsNotNull(device2.DeviceClass);

            // test Save
            device.Key = "key";
            device.Name = "Test2";
            device.Status = "Status";
            device.Data = "{ }";
            device.Network = null;
            device.NetworkID = null;
            DataContext.Device.Save(device);
            var device3 = DataContext.Device.Get(device.ID);
            Assert.AreEqual("key", device3.Key);
            Assert.AreEqual("Test2", device3.Name);
            Assert.AreEqual("Status", device3.Status);
            Assert.AreEqual("{ }", device3.Data);
            Assert.IsNull(device3.Network);
            Assert.IsNull(device3.NetworkID);

            // test update relationship
            var deviceClass2 = new DeviceClass("D2", "V2");
            DataContext.DeviceClass.Save(deviceClass2);
            RegisterTearDown(() => DataContext.DeviceClass.Delete(deviceClass2.ID));
            device.DeviceClass = deviceClass2;
            DataContext.Device.Save(device);
            var device4 = DataContext.Device.Get(device.ID);
            Assert.AreEqual(deviceClass2.ID, device4.DeviceClassID);
            Assert.IsNotNull(device4.DeviceClass);

            // test Delete
            DataContext.Device.Delete(device.ID);
            var device5 = DataContext.Device.Get(device.ID);
            Assert.IsNull(device5);
        }
Beispiel #12
0
        public void DeviceEquipmentTest()
        {
            var network = new Network("N1");
            NetworkRepository.Save(network);

            var deviceClass = new DeviceClass("D1", "V1");
            DeviceClassRepository.Save(deviceClass);

            var device = new Device(Guid.NewGuid(), "key", "Test", network, deviceClass);
            DeviceRepository.Save(device);

            var equipment = new DeviceEquipment("Test", DateTime.UtcNow, device);
            DeviceEquipmentRepository.Save(equipment);

            // test GetByDevice
            var equipments = DeviceEquipmentRepository.GetByDevice(device.ID);
            Assert.Greater(equipments.Count, 0);

            // test GetByDeviceAndCode
            var equipment0 = DeviceEquipmentRepository.GetByDeviceAndCode(device.ID, "Test");
            Assert.IsNotNull(equipment0);

            // test Get(id)
            var equipment1 = DeviceEquipmentRepository.Get(equipment.ID);
            Assert.IsNotNull(equipment1);
            Assert.AreEqual("Test", equipment1.Code);
            Assert.AreEqual(device.ID, equipment1.DeviceID);

            // test Save
            equipment.Code = "Test2";
            equipment.Parameters = "{}";
            DeviceEquipmentRepository.Save(equipment);
            var equipment2 = DeviceEquipmentRepository.Get(equipment.ID);
            Assert.AreEqual("Test2", equipment2.Code);
            Assert.AreEqual("{}", equipment2.Parameters);

            // test Delete
            DeviceEquipmentRepository.Delete(equipment.ID);
            var equipment3 = DeviceEquipmentRepository.Get(equipment.ID);
            Assert.IsNull(equipment3);
        }
Beispiel #13
0
        public void DeviceCommandTest()
        {
            var network = new Network("N1");
            NetworkRepository.Save(network);

            var deviceClass = new DeviceClass("D1", "V1");
            DeviceClassRepository.Save(deviceClass);

            var device = new Device(Guid.NewGuid(), "key", "Test", network, deviceClass);
            DeviceRepository.Save(device);

            var command = new DeviceCommand("Test", device);
            DeviceCommandRepository.Save(command);

            // test GetByDevice
            var commands = DeviceCommandRepository.GetByDevice(device.ID, null, null);
            Assert.Greater(commands.Count, 0);

            // test Get(id)
            var command1 = DeviceCommandRepository.Get(command.ID);
            Assert.IsNotNull(command1);
            Assert.AreEqual("Test", command1.Command);
            Assert.AreEqual(device.ID, command1.DeviceID);

            // test Save
            command.Command = "Test2";
            command.Parameters = "{}";
            command.Status = "OK";
            command.Result = "Success";
            DeviceCommandRepository.Save(command);
            var command2 = DeviceCommandRepository.Get(command.ID);
            Assert.AreEqual("Test2", command2.Command);
            Assert.AreEqual("{}", command2.Parameters);
            Assert.AreEqual("OK", command2.Status);
            Assert.AreEqual("Success", command2.Result);

            // test Delete
            DeviceCommandRepository.Delete(command.ID);
            var command3 = DeviceCommandRepository.Get(command.ID);
            Assert.IsNull(command3);
        }
Beispiel #14
0
        public void DeviceTest()
        {
            var network = new Network("N1");
            NetworkRepository.Save(network);

            var deviceClass = new DeviceClass("D1", "V1");
            DeviceClassRepository.Save(deviceClass);

            var device = new Device(Guid.NewGuid(), "key", "Test", network, deviceClass);
            DeviceRepository.Save(device);

            // test GetByNetwork
            var devices = DeviceRepository.GetByNetwork(network.ID);
            Assert.Greater(devices.Count, 0);

            // test Get(id)
            var device1 = DeviceRepository.Get(device.ID);
            Assert.IsNotNull(device1);
            Assert.AreEqual(device.GUID, device1.GUID);
            Assert.AreEqual("Test", device1.Name);
            Assert.AreEqual(network.ID, device1.NetworkID);
            Assert.AreEqual(deviceClass.ID, device1.DeviceClassID);
            Assert.IsNotNull(device1.Network);
            Assert.IsNotNull(device1.DeviceClass);

            // test Get(guid)
            var device2 = DeviceRepository.Get(device.GUID);
            Assert.IsNotNull(device2);
            Assert.AreEqual(device.GUID, device2.GUID);
            Assert.AreEqual("Test", device2.Name);
            Assert.AreEqual(network.ID, device2.NetworkID);
            Assert.AreEqual(deviceClass.ID, device2.DeviceClassID);
            Assert.IsNotNull(device2.Network);
            Assert.IsNotNull(device2.DeviceClass);

            // test Save
            device.Name = "Test2";
            device.Status = "Status";
            DeviceRepository.Save(device);
            var device3 = DeviceRepository.Get(device.ID);
            Assert.AreEqual("Test2", device3.Name);
            Assert.AreEqual("Status", device3.Status);

            // test update relationship
            var deviceClass2 = new DeviceClass("D2", "V2");
            DeviceClassRepository.Save(deviceClass2);
            device.DeviceClass = deviceClass2;
            DeviceRepository.Save(device);
            var device4 = DeviceRepository.Get(device.ID);
            Assert.AreEqual(deviceClass2.ID, device4.DeviceClassID);
            Assert.IsNotNull(device4.DeviceClass);

            // test Delete
            DeviceRepository.Delete(device.ID);
            var device5 = DeviceRepository.Get(device.ID);
            Assert.IsNull(device5);
        }
Beispiel #15
0
        public void DeviceNotificationTest()
        {
            var network = new Network("N1");
            NetworkRepository.Save(network);

            var deviceClass = new DeviceClass("D1", "V1");
            DeviceClassRepository.Save(deviceClass);

            var device = new Device(Guid.NewGuid(), "key", "Test", network, deviceClass);
            DeviceRepository.Save(device);

            var notification = new DeviceNotification("Test", device);
            DeviceNotificationRepository.Save(notification);

            // test GetByDevice
            var notifications = DeviceNotificationRepository.GetByDevice(device.ID, null, null);
            Assert.Greater(notifications.Count, 0);

            // test Get(id)
            var notification1 = DeviceNotificationRepository.Get(notification.ID);
            Assert.IsNotNull(notification1);
            Assert.AreEqual("Test", notification1.Notification);
            Assert.AreEqual(device.ID, notification1.DeviceID);

            // test Save
            notification.Notification = "Test2";
            notification.Parameters = "{}";
            DeviceNotificationRepository.Save(notification);
            var notification2 = DeviceNotificationRepository.Get(notification.ID);
            Assert.AreEqual("Test2", notification2.Notification);
            Assert.AreEqual("{}", notification2.Parameters);

            // test Delete
            DeviceNotificationRepository.Delete(notification.ID);
            var notification3 = DeviceNotificationRepository.Get(notification.ID);
            Assert.IsNull(notification3);
        }
Beispiel #16
0
        private void ResolveDeviceClass(JObject json, bool isRequired)
        {
            var jDeviceClass = json.Property("deviceClass");
            if (isRequired && jDeviceClass == null)
                throw new InvalidDataException("Required 'deviceClass' property was not specified!");

            if (jDeviceClass != null && jDeviceClass.Value is JObject)
            {
                // search device class by name/version or auto-create if it does not exist
                var jDeviceClassObj = (JObject)jDeviceClass.Value;
                if (jDeviceClassObj["name"] == null)
                    throw new InvalidDataException("Specified 'deviceClass' object must include 'name' property!");
                if (jDeviceClassObj["version"] == null)
                    throw new InvalidDataException("Specified 'deviceClass' object must include 'version' property!");

                var deviceClass = DataContext.DeviceClass.Get((string)jDeviceClassObj["name"], (string)jDeviceClassObj["version"]);
                if (deviceClass == null)
                {
                    // auto-create device class
                    deviceClass = new DeviceClass();
                }
                if (deviceClass.ID == 0 || !deviceClass.IsPermanent)
                {
                    // auto-update device class if it's not set as permanent
                    GetMapper<DeviceClass>().Apply(deviceClass, jDeviceClassObj);
                    Validate(deviceClass);
                    DataContext.DeviceClass.Save(deviceClass);
                }
                jDeviceClass.Value = (long)deviceClass.ID;
            }
        }
Beispiel #17
0
        public void EquipmentTest()
        {
            var deviceClass = new DeviceClass("D1", "V1");
            DeviceClassRepository.Save(deviceClass);

            var equipment = new Equipment("Test", "Code", "Type", deviceClass);
            EquipmentRepository.Save(equipment);

            // test GetByDeviceClass
            var equipments = EquipmentRepository.GetByDeviceClass(deviceClass.ID);
            Assert.Greater(equipments.Count, 0);

            // test Get(id)
            var equipment1 = EquipmentRepository.Get(equipment.ID);
            Assert.IsNotNull(equipment1);
            Assert.AreEqual("Test", equipment1.Name);
            Assert.AreEqual("Code", equipment1.Code);
            Assert.AreEqual("Type", equipment1.Type);
            Assert.AreEqual(deviceClass.ID, equipment1.DeviceClassID);
            Assert.IsNotNull(equipment1.DeviceClass);

            // test Save
            equipment.Name = "Test2";
            equipment.Code = "Code2";
            equipment.Type = "Type2";
            EquipmentRepository.Save(equipment);
            var equipment2 = EquipmentRepository.Get(equipment.ID);
            Assert.AreEqual("Test2", equipment2.Name);
            Assert.AreEqual("Code2", equipment2.Code);
            Assert.AreEqual("Type2", equipment2.Type);

            // test update relationship
            var deviceClass2 = new DeviceClass("D2", "V2");
            DeviceClassRepository.Save(deviceClass2);
            equipment.DeviceClass = deviceClass2;
            EquipmentRepository.Save(equipment);
            var equipment3 = EquipmentRepository.Get(equipment.ID);
            Assert.AreEqual(deviceClass2.ID, equipment3.DeviceClassID);
            Assert.IsNotNull(equipment3.DeviceClass);

            // test Delete
            EquipmentRepository.Delete(equipment.ID);
            var equipment4 = EquipmentRepository.Get(equipment.ID);
            Assert.IsNull(equipment4);
        }
        public void DeviceEquipment()
        {
            var network = new Network("N1");
            DataContext.Network.Save(network);
            RegisterTearDown(() => DataContext.Network.Delete(network.ID));

            var deviceClass = new DeviceClass("D1", "V1");
            DataContext.DeviceClass.Save(deviceClass);
            RegisterTearDown(() => DataContext.DeviceClass.Delete(deviceClass.ID));

            var device = new Device(Guid.NewGuid().ToString(), "Test", network, deviceClass);
            DataContext.Device.Save(device);
            RegisterTearDown(() => DataContext.Device.Delete(device.ID));

            var equipment = new DeviceEquipment("Test", DateTime.UtcNow, device);
            DataContext.DeviceEquipment.Save(equipment);
            RegisterTearDown(() => DataContext.DeviceEquipment.Delete(equipment.ID));

            // test GetByDevice
            var equipments = DataContext.DeviceEquipment.GetByDevice(device.ID);
            Assert.Greater(equipments.Count, 0);

            // test GetByDeviceAndCode
            var equipment0 = DataContext.DeviceEquipment.GetByDeviceAndCode(device.ID, "Test");
            Assert.IsNotNull(equipment0);

            // test Get(id)
            var equipment1 = DataContext.DeviceEquipment.Get(equipment.ID);
            Assert.IsNotNull(equipment1);
            Assert.AreEqual("Test", equipment1.Code);
            Assert.AreEqual(device.ID, equipment1.DeviceID);

            // test Save
            equipment.Code = "Test2";
            equipment.Parameters = "{ }";
            DataContext.DeviceEquipment.Save(equipment);
            var equipment2 = DataContext.DeviceEquipment.Get(equipment.ID);
            Assert.AreEqual("Test2", equipment2.Code);
            Assert.AreEqual("{ }", equipment2.Parameters);

            // test Delete
            DataContext.DeviceEquipment.Delete(equipment.ID);
            var equipment3 = DataContext.DeviceEquipment.Get(equipment.ID);
            Assert.IsNull(equipment3);
        }
        public void DeviceCommand()
        {
            var network = new Network("N1");
            DataContext.Network.Save(network);
            RegisterTearDown(() => DataContext.Network.Delete(network.ID));

            var deviceClass = new DeviceClass("D1", "V1");
            DataContext.DeviceClass.Save(deviceClass);
            RegisterTearDown(() => DataContext.DeviceClass.Delete(deviceClass.ID));

            var device = new Device(Guid.NewGuid().ToString(), "Test", network, deviceClass);
            DataContext.Device.Save(device);
            RegisterTearDown(() => DataContext.Device.Delete(device.ID));

            var command = new DeviceCommand("Test", device);
            DataContext.DeviceCommand.Save(command);
            RegisterTearDown(() => DataContext.DeviceCommand.Delete(command.ID));

            // test GetByDevice
            var commands = DataContext.DeviceCommand.GetByDevice(device.ID);
            Assert.Greater(commands.Count, 0);

            // test Get(id)
            var command1 = DataContext.DeviceCommand.Get(command.ID);
            Assert.IsNotNull(command1);
            Assert.AreEqual("Test", command1.Command);
            Assert.AreEqual(device.ID, command1.DeviceID);

            // test Save
            command.Command = "Test2";
            command.Parameters = "{ }";
            command.Status = "OK";
            command.Result = "\"Success\"";
            command.UserID = 1;
            DataContext.DeviceCommand.Save(command);
            var command2 = DataContext.DeviceCommand.Get(command.ID);
            Assert.AreEqual("Test2", command2.Command);
            Assert.AreEqual("{ }", command2.Parameters);
            Assert.AreEqual("OK", command2.Status);
            Assert.AreEqual("\"Success\"", command2.Result);
            Assert.AreEqual(1, command2.UserID);

            // test Delete
            DataContext.DeviceCommand.Delete(command.ID);
            var command3 = DataContext.DeviceCommand.Get(command.ID);
            Assert.IsNull(command3);
        }
        public void DeviceNotification()
        {
            var network = new Network("N1");
            DataContext.Network.Save(network);
            RegisterTearDown(() => DataContext.Network.Delete(network.ID));

            var deviceClass = new DeviceClass("D1", "V1");
            DataContext.DeviceClass.Save(deviceClass);
            RegisterTearDown(() => DataContext.DeviceClass.Delete(deviceClass.ID));

            var device = new Device(Guid.NewGuid().ToString(), "Test", network, deviceClass);
            DataContext.Device.Save(device);
            RegisterTearDown(() => DataContext.Device.Delete(device.ID));

            var notification = new DeviceNotification("Test", device);
            DataContext.DeviceNotification.Save(notification);
            RegisterTearDown(() => DataContext.DeviceNotification.Delete(notification.ID));

            // test GetByDevice
            var notifications = DataContext.DeviceNotification.GetByDevice(device.ID);
            Assert.Greater(notifications.Count, 0);

            // test Get(id)
            var notification1 = DataContext.DeviceNotification.Get(notification.ID);
            Assert.IsNotNull(notification1);
            Assert.AreEqual("Test", notification1.Notification);
            Assert.AreEqual(device.ID, notification1.DeviceID);

            // test Save
            notification.Notification = "Test2";
            notification.Parameters = "{ }";
            DataContext.DeviceNotification.Save(notification);
            var notification2 = DataContext.DeviceNotification.Get(notification.ID);
            Assert.AreEqual("Test2", notification2.Notification);
            Assert.AreEqual("{ }", notification2.Parameters);

            // test Delete
            DataContext.DeviceNotification.Delete(notification.ID);
            var notification3 = DataContext.DeviceNotification.Get(notification.ID);
            Assert.IsNull(notification3);
        }
Beispiel #21
0
        private void ResolveDeviceClass(JObject json, bool isRequired)
        {
            var jDeviceClass = json.Property("deviceClass");
            if (isRequired && jDeviceClass == null)
                ThrowHttpResponse(HttpStatusCode.BadRequest, "Required 'deviceClass' property was not specified!");

            if (jDeviceClass != null && jDeviceClass.Value is JObject)
            {
                // search device class by name/version or auto-create
                var jDeviceClassObj = (JObject)jDeviceClass.Value;
                if (jDeviceClassObj["name"] == null)
                    ThrowHttpResponse(HttpStatusCode.BadRequest, "Specified 'deviceClass' object must include 'name' property!");
                if (jDeviceClassObj["version"] == null)
                    ThrowHttpResponse(HttpStatusCode.BadRequest, "Specified 'deviceClass' object must include 'version' property!");

                var deviceClass = DataContext.DeviceClass.Get((string)jDeviceClassObj["name"], (string)jDeviceClassObj["version"]);
                if (deviceClass == null)
                {
                    // auto-create device class if it does not exist
                    deviceClass = new DeviceClass();
                    GetMapper<DeviceClass>().Apply(deviceClass, jDeviceClassObj);
                    DataContext.DeviceClass.Save(deviceClass);
                }
                jDeviceClass.Value = (long)deviceClass.ID;
            }
        }
        public void DeviceClass()
        {
            var deviceClass = new DeviceClass("Test", "V1");
            deviceClass.Equipment.Add(new Equipment("name1", "code1", "type1"));
            deviceClass.Equipment.Add(new Equipment("name2", "code2", "type2"));
            DataContext.DeviceClass.Save(deviceClass);
            RegisterTearDown(() => DataContext.DeviceClass.Delete(deviceClass.ID));

            // test GetAll
            var deviceClasses = DataContext.DeviceClass.GetAll();
            Assert.Greater(deviceClasses.Count, 0);

            // test Get(id)
            var deviceClass1 = DataContext.DeviceClass.Get(deviceClass.ID);
            Assert.IsNotNull(deviceClass1);
            Assert.AreEqual("Test", deviceClass1.Name);
            Assert.IsNotNull(deviceClass1.Equipment);
            Assert.AreEqual(2, deviceClass1.Equipment.Count);
            Assert.AreEqual("name1", deviceClass1.Equipment[0].Name);
            Assert.AreEqual("name2", deviceClass1.Equipment[1].Name);

            // test Get(name, version)
            var deviceClass2 = DataContext.DeviceClass.Get("Test", "V1");
            Assert.IsNotNull(deviceClass2);

            // test Save
            deviceClass.Name = "Test2";
            deviceClass.Version = "V2";
            deviceClass.IsPermanent = true;
            deviceClass.OfflineTimeout = 10;
            deviceClass.Data = "{ }";
            deviceClass.Equipment.RemoveAt(1);
            deviceClass.Equipment.Add(new Equipment("name3", "code3", "type3"));
            DataContext.DeviceClass.Save(deviceClass);
            var deviceClass3 = DataContext.DeviceClass.Get(deviceClass.ID);
            Assert.AreEqual("Test2", deviceClass3.Name);
            Assert.AreEqual("V2", deviceClass3.Version);
            Assert.AreEqual(true, deviceClass3.IsPermanent);
            Assert.AreEqual(10, deviceClass3.OfflineTimeout);
            Assert.AreEqual("{ }", deviceClass3.Data);
            Assert.AreEqual(2, deviceClass3.Equipment.Count);
            Assert.AreEqual("name1", deviceClass3.Equipment[0].Name);
            Assert.AreEqual("name3", deviceClass3.Equipment[1].Name);

            // test Delete
            DataContext.DeviceClass.Delete(deviceClass.ID);
            var deviceClass4 = DataContext.DeviceClass.Get(deviceClass.ID);
            Assert.IsNull(deviceClass4);
        }