Esempio n. 1
0
        private (List <OpenHardwareMonitor.Hardware> Hardware, List <SensorHttp> Sensors) CreateSensorTree(ComputerJson rootNode)
        {
            var newHardware = new List <OpenHardwareMonitor.Hardware>();
            var newSensors  = new List <SensorHttp>();

            foreach (var hardware in rootNode.Hardware)
            {
                var hw = new OpenHardwareMonitor.Hardware(hardware.Name, hardware.NodeId, hardware.Parent, hardware.HardwareType);
                newHardware.Add(hw);

                foreach (var sensor in hardware.Sensors)
                {
                    SensorType sensorType;
                    if (Enum.TryParse <SensorType>(sensor.Type, true, out sensorType))
                    {
                        var s = new SensorHttp(this, sensor.Name, sensor.NodeId, sensor.Parent, sensorType, sensor.Value);
                        newSensors.Add(s);
                    }
                }
            }

            return(newHardware, newSensors);
        }
Esempio n. 2
0
        private bool TryUpdateSensor(SensorHttp sensor, out double value)
        {
            value = 0;
            if (UpdateStrategy == SensorUpdateStrategy.PerSensor || UpdateStrategy == SensorUpdateStrategy.Unspecified)
            {
                try
                {
                    var apiUrl = Combine(_uri, "/api/nodes/", sensor.Identifier);

                    string fullJson = _httpClient.GetStringAsync(apiUrl).Result;
                    lock (_lock)
                    {
                        SensorJson?rootNode = JsonSerializer.Deserialize <SensorJson>(fullJson);
                        if (rootNode == null)
                        {
                            _logger.LogWarning($"Unable to parse json: {fullJson}");
                            return(false);
                        }

                        value = rootNode.Value;
                        return(true);
                    }
                }
                catch (AggregateException ex)
                {
                    _logger.LogWarning(ex, $"Unable to read update value for node {sensor.Identifier}: {ex.Message}");
                    return(false);
                }
            }
            else if (UpdateStrategy == SensorUpdateStrategy.SynchronousAfterTimeout && (DateTime.UtcNow - _lastUpdateTime).Duration() > UpdateInterval)
            {
                UpdateSensors(false);
            }

            value = sensor.Value;
            return(true);
        }