private void btnOk_Click(object sender, EventArgs e) { btnOk.Enabled = false; btnCancel.Enabled = true; this.btnCancel.Text = Resources.Resource1.Form_Stop; this.timer1.Enabled = true; this.timer1.Interval = 1000; _Begin = DateTime.Now; HardwareTree hardwareTree = GetHardwareTree(); if (hardwareTree != null) { this.progressBar1.Maximum = hardwareTree.GetSelectedEntrances().Count; } else { this.progressBar1.Maximum = 0; } this.progressBar1.Value = 0; this.progressBar1.Visible = true; btnOk_Handler(); Sync_Thread = new Thread(SyncMethod); Sync_Thread.Start(); }
private List <ContainerDTO> ConvertToList(HardwareTree tree, Guid?parentId) { logger.Trace($"Entering {new StackTrace().GetFrame(0).GetMethod().Name}"); logger.Debug($"Parent id = '{parentId}'"); List <ContainerDTO> containers = new List <ContainerDTO>(); var container = new ContainerDTO() { DeviceName = tree.DeviceName, ContainerId = Guid.NewGuid(), ParentId = parentId, Sensors = tree.Sensors }; containers.Add(container); logger.Info($"Adding container '{container.ContainerId}' to list"); logger.Debug($"\twith sensors '{string.Join(",", container.Sensors.Select(s => s.Id))}'"); foreach (var subhardware in tree.Subhardware) { containers.AddRange(ConvertToList(subhardware, container.ContainerId)); } logger.Trace($"Exiting {new StackTrace().GetFrame(0).GetMethod().Name}"); return(containers); }
public void WriteContainer(HardwareTree tree, IDataContext ctx, Guid agentId, Guid parentId) { foreach (var container in ctx.Containers) { if (container.AgentId == agentId && container.ParentContainerId == parentId) { if (!CompareNodes(ctx, container, tree)) { var newContainer = new Container() { Id = Guid.NewGuid(), AgentId = agentId, ParentContainerId = parentId }; ctx.Containers.Add(newContainer); foreach (var sh in tree.Subhardware) { WriteContainer(sh, ctx, agentId, newContainer.Id); } } } foreach (var sh in tree.Subhardware) { WriteContainer(sh, ctx, agentId, container.Id); } } }
public void CommonParts() { defaultHwTree = new HardwareTree(); defaultHwTree.Sensors.Add(new SensorHW("1", "cpu_temp", 35.4f)); defaultHwTree.Sensors.Add(new SensorHW("2", "gpu_temp", 83.2f)); defaultHwTree.Sensors.Add(new SensorHW("3", "mb_temp", 32.4f)); }
public void WatchProc() { var tree = new HardwareTree(); tree.Sensors.Add(new SensorHW("1", "cpu_temp", 35.4f)); tree.Sensors.Add(new SensorHW("2", "gpu_temp", 83.2f)); tree.Sensors.Add(new SensorHW("3", "mb_temp", 32.4f)); lock (sync) lastCatchedData.Enqueue(tree); }
private bool CompareNodes(IDataContext ctx, Container container, HardwareTree tree) { var sensors = ctx.Sensors.Where(p => p.ContainerId == container.Id); if (sensors.Count() != tree.Sensors.Count) { return(false); } return(true); }
private void WriteSensorsData(HardwareTree tree, Agent agent, Session session, Guid containerParentId) { if (tree == null) { return; } var CurrContainer = new Container() { AgentId = agent.Id, Id = Guid.NewGuid(), ParentContainerId = containerParentId }; foreach (var sensor in tree.Sensors) { Agents = DbContext.Agents.ToList(); foreach (var container in DbContext.Containers) { DbContext.Containers.Remove(container); } foreach (var snsr in DbContext.Sensors) { DbContext.Sensors.Remove(snsr); } var curr_sensor = new Sensor() { Id = sensor.Id, ContainerId = CurrContainer.Id, Type = sensor.Type }; DbContext.Sensors.Add(curr_sensor); var curr_metric = new Metric() { SessionId = session.Id, SensorId = Guid.NewGuid(), Valued = sensor.Value }; DbContext.Metrics.Add(curr_metric); } if (tree.Subhardware.Count != 0) { foreach (var sh in tree.Subhardware) { WriteSensorsData(sh, agent, session, CurrContainer.Id); } } }
private List <NewSensorDTO> GetNewSensors(HardwareTree tree, Guid containerId) { logger.Trace($"Entering {nameof(GetNewSensors)}"); logger.Debug($"Current parent id = '{containerId}'"); var sensors = dbContext.Sensors.Where(p => p.ContainerId == containerId).ToArray(); foreach (var sensor in sensors) { logger.Debug($"Found sensor '{sensor.ContainerId}:{sensor.Id}' type: {sensor.Type} in database"); } var buffTree = (from element in tree.Sensors select element.Type); var enumerable = buffTree as string[] ?? buffTree.ToArray(); foreach (var sensor in enumerable) { logger.Debug($"Found sensor '{sensor}' in tree"); } var buffDb = (from element in sensors select element.Type).AsEnumerable(); var items = enumerable.Except(buffDb).AsEnumerable(); foreach (var sensor in enumerable) { logger.Debug($"Found new sensor '{sensor}' in tree"); } List <NewSensorDTO> newSensors = new List <NewSensorDTO>(); foreach (var item in tree.Sensors) { if (items.Contains(item.Type)) { logger.Info($"New item '{item.Id}' will be added to DB"); newSensors.Add(new NewSensorDTO() { ContainerId = containerId, Sensor = item }); } else { logger.Debug($"Item '{item.Id}' already exists"); } } logger.Trace($"Exiting {nameof(GetNewSensors)}"); return(newSensors); }
public void OneNodeDifference_NewNode() { var expected = new List <SensorHW>(); expected.Add(new SensorHW("4", "winchester", 45.4f)); var anotherTree = new HardwareTree(); anotherTree.Sensors.Add(new SensorHW("4", "winchester", 45.4f)); defaultHwTree.Subhardware.Add(anotherTree); var containers = new List <Container>(); containers.Add(new Container() { AgentId = new Guid(), Id = new Guid(), ParentContainerId = null }); var sensors = new List <Sensor>(); sensors.Add(new Sensor() { Id = "1", Type = "cpu_temp", ContainerId = new Guid() }); sensors.Add(new Sensor() { Id = "2", Type = "gpu_temp", ContainerId = new Guid() }); sensors.Add(new Sensor() { Id = "3", Type = "mb_temp", ContainerId = new Guid() }); var contextMock = new Mock <IReadOnlyDataContext>(MockBehavior.Strict); contextMock.Setup(a => a.Containers).Returns(containers.AsQueryable()); contextMock.Setup(a => a.Sensors).Returns(sensors.AsQueryable()); contextMock.Setup(a => a.Agents).Returns((new Agent[0]).AsQueryable()); sut = new DataProvider(contextMock.Object); List <SensorHW> result = new List <SensorHW>(); foreach (var item in sut.GetNewData(defaultHwTree, new Guid(), null).NewContainers) { result.AddRange(item.Sensors); } Assert.That(result, Is.EqualTo(expected)); }
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 HardwareTree GetSystemInfo() { HardwareTree tree = new HardwareTree(); tree.DeviceName = "Computer"; tree.Sensors = new List <SensorHW>(); tree.Subhardware = new List <HardwareTree>(); foreach (var hardware in computer.Hardware) { tree.Subhardware.Add(GetInfoRecursive(hardware)); } return(tree); }
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}"); }
private bool NotNeedNewContainer(Guid id, HardwareTree tree) { var result = false; var container = dbContext.Containers.First(p => p.Id == id); if (container.DeviceName == tree.DeviceName) { result = true; } logger.Debug($"{nameof(NotNeedNewContainer)} result is {result}"); return(result); }
public void WriteMetrics(HardwareTree tree, Guid sessionId) { foreach (var sensor in tree.Sensors) { var metric = new Metric() { SessionId = sessionId, SensorId = sensor.Id, Value = sensor.Value }; dataContext.Metrics.Add(metric); } foreach (var sh in tree.Subhardware) { WriteMetrics(sh, sessionId); } dataContext.SaveChanges(); }
private Envelope Func() { Random rand = new Random(); var tree = new HardwareTree(); tree.Sensors.Add(new SensorHW("1", "PCH", rand.Next(30, 50))); var cpuTree = new HardwareTree(); cpuTree.Sensors.Add(new SensorHW("2", "cpu_temp", rand.Next(30, 80))); var gpuTree = new HardwareTree(); gpuTree.Sensors.Add(new SensorHW("3", "gpu_temp", rand.Next(40, 88))); var hddTree = new HardwareTree(); hddTree.Sensors.Add(new SensorHW("4", "hdd_temp", rand.Next(25, 35))); var hdddTree = new HardwareTree(); hdddTree.Sensors.Add(new SensorHW("66", "hd2d_temp", rand.Next(25, 35))); tree.Subhardware.Add(cpuTree); tree.Subhardware.Add(gpuTree); tree.Subhardware.Add(hddTree); tree.Subhardware.Add(hdddTree); var envelope = new Envelope { Header = new Header { AgentId = Guid.NewGuid(), AgentTime = DateTime.Now, ErrorMsg = tree == null ? "No data available" : "" }, HardwareTree = tree }; return(envelope); }
private HardwareTree GetInfoRecursive(IHardware hardware) { HardwareTree tree = new HardwareTree(); tree.DeviceName = hardware.Name; tree.Sensors = new List <SensorHW>(); tree.Subhardware = new List <HardwareTree>(); foreach (var sensor in hardware.Sensors) { tree.Sensors.Add(new SensorHW(sensor.Identifier.ToString(), sensor.SensorType.ToString(), (float)sensor.Value)); } foreach (var subhardware in hardware.SubHardware) { tree.Subhardware.Add(GetInfoRecursive(subhardware)); } return(tree); }