Exemple #1
0
        public NewDataDTO GetNewData(HardwareTree tree, Guid agentId, Guid?parentId)
        {
            logger.Trace($"Entering {new StackTrace().GetFrame(0).GetMethod().Name}");

            NewDataDTO data = new NewDataDTO()
            {
                NewSensors    = new List <NewSensorDTO>(),
                NewContainers = new List <ContainerDTO>()
            };

            GetNewContainers(tree, agentId, parentId, data);

            data.NewContainers = data.NewContainers.Distinct().ToList();
            data.NewSensors    = data.NewSensors.Distinct().ToList();

            foreach (var item in data.NewContainers)
            {
                logger.Info("New Container id {0}, pareint id {1}", item.ContainerId, item.ParentId);

                foreach (var sensor in item.Sensors)
                {
                    logger.Info("Sensor id {0}, type {1}, value {2}",
                                sensor.Id,
                                sensor.Type,
                                sensor.Value);
                }
            }

            foreach (var sensor in data.NewSensors)
            {
                logger.Info("Sensor id {0}, pareint id {1}, value {2}, container id {3}",
                            sensor.Sensor.Id,
                            sensor.Sensor.Type,
                            sensor.Sensor.Value,
                            sensor.ContainerId);
            }

            if (data.NewContainers.Count == 0)
            {
                logger.Info("No new containers");
            }

            if (data.NewSensors.Count == 0)
            {
                logger.Info("No new sensors");
            }

            logger.Trace($"Exiting {new StackTrace().GetFrame(0).GetMethod().Name}");

            return(data);
        }
        public void Write(NewDataDTO data, Guid agentId)
        {
            foreach (var item in data.NewSensors)
            {
                var currentSensor = new Sensor // naming!
                {
                    Id          = item.Sensor.Id,
                    ContainerId = item.ContainerId,
                    Type        = item.Sensor.Type
                };
                context.Sensors.Add(currentSensor);
            }

            foreach (var container in data.NewContainers)
            {
                var currentContainer = new Container // naming!
                {
                    DeviceName        = container.DeviceName,
                    AgentId           = agentId,
                    Id                = container.ContainerId,
                    ParentContainerId = container.ParentId
                };

                context.Containers.Add(currentContainer);

                foreach (var sensor in container.Sensors)
                {
                    var currentSensor = new Sensor // naming!
                    {
                        Id          = sensor.Id,
                        ContainerId = currentContainer.Id,
                        Type        = sensor.Type
                    };
                    context.Sensors.Add(currentSensor);
                }
            }
            context.SaveChanges();
        }
Exemple #3
0
        private void GetNewContainers(HardwareTree tree, Guid agentId, Guid?parentId, NewDataDTO data)
        {
            logger.Trace($"Entering {new StackTrace().GetFrame(0).GetMethod().Name}");

            var dbcontainers = dbContext.Containers.Where(p => p.AgentId == agentId && p.ParentContainerId == parentId);

            if (dbcontainers.Count() == 0)
            {
                logger.Info("Empty DbSet Containers, creating new containers ");

                data.NewContainers.AddRange(ConvertToList(tree, parentId));

                logger.Trace($"Exiting {new StackTrace().GetFrame(0).GetMethod().Name}");

                return;
            }


            Guid resultId         = Guid.Empty;
            bool needNewContainer = true;

            foreach (var container in dbcontainers)
            {
                if (container.AgentId == agentId && container.ParentContainerId == parentId)
                {
                    if (NotNeedNewContainer(container.Id, tree))
                    {
                        data.NewSensors.AddRange(GetNewSensors(tree, container.Id));
                        resultId         = container.Id;
                        needNewContainer = false;
                        break;
                    }
                }
            }

            if (needNewContainer)
            {
                var buff = ConvertToList(tree, parentId);
                data.NewContainers.AddRange(buff);
                resultId = buff.First().ContainerId;
            }

            foreach (var sh in tree.Subhardware)
            {
                GetNewContainers(sh, agentId, resultId, data);
            }

            logger.Trace($"Exiting {new StackTrace().GetFrame(0).GetMethod().Name}");
        }