Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
0
        public void Save(DeviceEquipment equipment)
        {
            if (equipment == null)
            {
                throw new ArgumentNullException("equipment");
            }

            if (equipment.Device != null)
            {
                equipment.DeviceID = equipment.Device.ID;
            }

            _mongo.EnsureIdentity(equipment);
            _mongo.DeviceEquipment.Save(equipment);
        }
Ejemplo n.º 4
0
        public void Save(DeviceEquipment equipment)
        {
            if (equipment == null)
            {
                throw new ArgumentNullException("equipment");
            }

            using (var context = new DeviceHiveContext())
            {
                context.Devices.Attach(equipment.Device);
                context.DeviceEquipments.Add(equipment);
                if (equipment.ID > 0)
                {
                    context.Entry(equipment).State = EntityState.Modified;
                }
                context.SaveChanges();
            }
        }
        /// <summary>
        /// Invoked when a new notification is inserted.
        /// </summary>
        /// <param name="context">MessageHandlerContext object.</param>
        public override void NotificationInserted(MessageHandlerContext context)
        {
            var parameters    = JObject.Parse(context.Notification.Parameters);
            var equipmentCode = (string)parameters["equipment"];

            if (string.IsNullOrEmpty(equipmentCode))
            {
                throw new Exception("Equipment notification is missing required 'equipment' parameter");
            }

            parameters.Remove("equipment");
            var equipment = _deviceEquipmentRepository.GetByDeviceAndCode(context.Device.ID, equipmentCode);

            if (equipment == null)
            {
                equipment = new DeviceEquipment(equipmentCode, context.Notification.Timestamp, context.Device);
            }
            equipment.Timestamp  = context.Notification.Timestamp;
            equipment.Device     = context.Device;
            equipment.Parameters = parameters.ToString();
            _deviceEquipmentRepository.Save(equipment);
        }
        /// <summary>
        /// Handles a device notification
        /// </summary>
        /// <param name="notification">DeviceNotification object</param>
        public void Handle(DeviceNotification notification)
        {
            var parameters    = JObject.Parse(notification.Parameters);
            var equipmentCode = (string)parameters["equipment"];

            if (string.IsNullOrEmpty(equipmentCode))
            {
                throw new Exception("Equipment notification is missing required 'equipment' parameter");
            }

            parameters.Remove("equipment");
            var equipment = _deviceEquipmentRepository.GetByDeviceAndCode(notification.Device.ID, equipmentCode);

            if (equipment == null)
            {
                equipment = new DeviceEquipment(equipmentCode, notification.Timestamp, notification.Device);
            }
            equipment.Timestamp  = notification.Timestamp;
            equipment.Device     = notification.Device;
            equipment.Parameters = parameters.ToString();
            _deviceEquipmentRepository.Save(equipment);
        }