Пример #1
0
        public DataModel JObjectToDataModel(JToken node, DataModel dataModel)
        {
            switch (node.Type)
            {
            case JTokenType.Object:
            {
                JsonDataModel dynamicDataModel = new();
                dataModel.AddDynamicChild(node.Path.Split('.').LastOrDefault() ?? string.Empty, dynamicDataModel);
                foreach (var child in node.Children <JProperty>())
                {
                    JObjectToDataModel(child.Value, dynamicDataModel);
                }
                return(dynamicDataModel);
            }

            case JTokenType.Array:
            {
                JsonDataModel dynamicDataModel = new();
                dataModel.AddDynamicChild(node.Path.Split('.').LastOrDefault() ?? string.Empty, dynamicDataModel);
                foreach (var child in node.Children())
                {
                    JObjectToDataModel(child, dynamicDataModel);
                }
                return(dynamicDataModel);
            }

            default:
                dataModel.AddDynamicChild(node.Path.Split('.').LastOrDefault() ?? string.Empty, ((JValue)node).Value);
                return(dataModel);
            }
        }
        private void UpdateDataModels()
        {
            foreach (var item in _aidaElements)
            {
                AidaElementDataModel dm = DataModel.DynamicChild <AidaElementDataModel>(item.Id)
                                          ?? DataModel.AddDynamicChild(new AidaElementDataModel(), item.Id, item.Label);

                dm.Value = item.Value;
            }
        }
        private void UpdateProfilesDataDataModel()
        {
            // Cleaning categories

            DataModel.DynamicChildren.Where(child => !_profileService.ProfileCategories.Any(cat => cat.Name == child.Key)).ToList().ForEach(k => DataModel.RemoveDynamicChild(k.Value));

            foreach (ProfileCategory profileCategory in _profileService.ProfileCategories)
            {
                // Set Category values
                string profileCategoryKey = profileCategory.Name;

                if (DataModel.TryGetDynamicChild(profileCategoryKey, out DynamicChild <ProfileCategoryDataModel> profileCategoryDataModelchild))
                {
                    // Update
                    profileCategoryDataModelchild.Value = new ProfileCategoryDataModel(profileCategory);
                }
                else
                {
                    // Create
                    profileCategoryDataModelchild = DataModel.AddDynamicChild
                                                    (
                        profileCategoryKey,
                        new ProfileCategoryDataModel(profileCategory)
                                                    );
                }

                // Cleaning categories profiles
                profileCategoryDataModelchild.Value.Profiles.DynamicChildren.Where(child => !profileCategory.ProfileConfigurations.Any(prof => prof.ProfileId.ToString() == child.Key)).ToList().ForEach(k => profileCategoryDataModelchild.Value.Profiles.RemoveDynamicChild(k.Value));

                // Set Profiles values
                foreach (ProfileConfiguration profileConfiguration in profileCategory.ProfileConfigurations)
                {
                    string profileConfigurationKey = profileConfiguration.ProfileId.ToString();

                    if (profileCategoryDataModelchild.Value.Profiles.TryGetDynamicChild(profileConfigurationKey, out DynamicChild <ProfileInformationDataModel> profileInformationDataModelChild))
                    {
                        // Update
                        profileInformationDataModelChild.Value = new ProfileInformationDataModel(profileConfiguration);
                    }
                    else
                    {
                        // Create
                        profileCategoryDataModelchild.Value.Profiles.AddDynamicChild
                        (
                            profileConfigurationKey,
                            new ProfileInformationDataModel(profileConfiguration),
                            profileConfiguration.Name
                        );
                    }
                }
            }
        }
        public override void Enable()
        {
            foreach (string scope in Scopes)
            {
                try
                {
                    HardwareMonitorScope = new ManagementScope(scope, null);
                    HardwareMonitorScope.Connect();
                }
                catch
                {
                    _logger.Warning($"Could not connect to WMI scope: {scope}");
                    //if the connection to one of the scopes fails,
                    //ignore the exception and try the other one.
                    //this way both Open and Libre HardwareMonitors
                    //can be supported since only the name of
                    //scope differs
                    continue;
                }

                SensorSearcher   = new ManagementObjectSearcher(HardwareMonitorScope, SensorQuery);
                HardwareSearcher = new ManagementObjectSearcher(HardwareMonitorScope, HardwareQuery);

                System.Collections.Generic.List <Sensor>   sensors   = Sensor.FromCollection(SensorSearcher.Get());
                System.Collections.Generic.List <Hardware> hardwares = Hardware.FromCollection(HardwareSearcher.Get());

                if (sensors.Count == 0 || hardwares.Count == 0)
                {
                    _logger.Warning($"Connected to WMI scope \"{scope}\" but it did not contain any data.");
                    continue;
                }

                int hardwareIdCounter = 0;
                foreach (Hardware hw in hardwares.OrderBy(hw => hw.HardwareType))
                {
                    //loop through the hardware,
                    //and find all the sensors that hardware has
                    System.Collections.Generic.IEnumerable <Sensor> children = sensors.Where(s => s.Parent == hw.Identifier);

                    //if we don't find any sensors, skip and do the next hardware
                    if (!children.Any())
                    {
                        continue;
                    }

                    HardwareDynamicDataModel hwDataModel = DataModel.AddDynamicChild(
                        new HardwareDynamicDataModel(),
                        $"{hw.HardwareType}{hardwareIdCounter++}",
                        hw.Name,
                        hw.HardwareType.ToString()
                        );

                    //group sensors by type for easier UI navigation.
                    //this is also the way the UI of the HardwareMonitor
                    //programs displays the sensors, so let's keep that consistent
                    foreach (IGrouping <SensorType, Sensor> sensorsOfType in children.GroupBy(s => s.SensorType))
                    {
                        SensorTypeDynamicDataModel sensorTypeDataModel = hwDataModel.AddDynamicChild(
                            new SensorTypeDynamicDataModel(),
                            sensorsOfType.Key.ToString()
                            );

                        int sensorIdCounter = 0;
                        //for each type of sensor, we add all the sensors we found
                        foreach (Sensor sensorOfType in sensorsOfType.OrderBy(s => s.Name))
                        {
                            //this switch is only useful for the unit of each sensor
                            SensorDynamicDataModel dataModel = sensorsOfType.Key switch
                            {
                                SensorType.Temperature => new TemperatureDynamicDataModel(sensorOfType.Identifier),
                                SensorType.Load => new PercentageDynamicDataModel(sensorOfType.Identifier),
                                SensorType.Level => new PercentageDynamicDataModel(sensorOfType.Identifier),
                                SensorType.Voltage => new VoltageDynamicDataModel(sensorOfType.Identifier),
                                SensorType.SmallData => new SmallDataDynamicDataModel(sensorOfType.Identifier),
                                SensorType.Data => new BigDataDynamicDataModel(sensorOfType.Identifier),
                                SensorType.Power => new PowerDynamicDataModel(sensorOfType.Identifier),
                                SensorType.Fan => new FanDynamicDataModel(sensorOfType.Identifier),
                                SensorType.Throughput => new ThroughputDynamicDataModel(sensorOfType.Identifier),
                                SensorType.Clock => new ClockDynamicDataModel(sensorOfType.Identifier),
                                _ => new SensorDynamicDataModel(sensorOfType.Identifier),
                            };

                            sensorTypeDataModel.AddDynamicChild(
                                dataModel,
                                (sensorIdCounter++).ToString(),
                                sensorOfType.Name
                                );
                        }
                    }
                }
                AddTimedUpdate(TimeSpan.FromMilliseconds(500), UpdateData);
                _logger.Information($"Successfully connected to WMI scope: {scope}");
                return;
                //success!
            }
            throw new ArtemisPluginException(Plugin, "Could not find hardware monitor WMI scope with data");
        }