コード例 #1
0
        /// <summary>
        /// Adds a command to the configured commands. This properly initializes the class and writes it to the config file.
        /// </summary>
        /// <param name="sensorType"></param>
        /// <param name="json"></param>
        public void AddSensor(AvailableSensors sensorType, string json)
        {
            var serializerOptions = new JsonSerializerOptions
            {
                Converters = { new DynamicJsonConverter() }
            };
            dynamic model = JsonSerializer.Deserialize <dynamic>(json, serializerOptions);

            AbstractSensor sensorToCreate = null;

            switch (sensorType)
            {
            case AvailableSensors.UserNotificationStateSensor:
                sensorToCreate = new UserNotificationStateSensor(this._publisher, (int)model.UpdateInterval, model.Name);
                break;

            case AvailableSensors.DummySensor:
                sensorToCreate = new DummySensor(this._publisher, (int)model.UpdateInterval, model.Name);
                break;

            case AvailableSensors.CurrentClockSpeedSensor:
                sensorToCreate = new CurrentClockSpeedSensor(this._publisher, (int)model.UpdateInterval, model.Name);
                break;

            case AvailableSensors.CPULoadSensor:
                sensorToCreate = new CPULoadSensor(this._publisher, (int)model.UpdateInterval, model.Name);
                break;

            case AvailableSensors.WMIQuerySensor:
                sensorToCreate = new WMIQuerySensor(this._publisher, model.Query, (int)model.UpdateInterval, model.Name);
                break;

            case AvailableSensors.MemoryUsageSensor:
                sensorToCreate = new MemoryUsageSensor(this._publisher, (int)model.UpdateInterval, model.Name);
                break;

            case AvailableSensors.ActiveWindowSensor:
                sensorToCreate = new ActiveWindowSensor(this._publisher, (int)model.UpdateInterval, model.Name);
                break;

            case AvailableSensors.WebcamActiveSensor:
                sensorToCreate = new WebcamActiveSensor(this._publisher, (int)model.UpdateInterval, model.Name);
                break;

            case AvailableSensors.MicrophoneActiveSensor:
                sensorToCreate = new MicrophoneActiveSensor(this._publisher, (int)model.UpdateInterval, model.Name);
                break;

            case AvailableSensors.NamedWindowSensor:
                sensorToCreate = new NamedWindowSensor(this._publisher, model.WindowName, model.Name, (int)model.UpdateInterval);
                break;

            case AvailableSensors.LastActiveSensor:
                sensorToCreate = new LastActiveSensor(this._publisher, (int)model.UpdateInterval, model.Name);
                break;

            case AvailableSensors.LastBootSensor:
                sensorToCreate = new LastBootSensor(this._publisher, (int)model.UpdateInterval, model.Name);
                break;

            case AvailableSensors.SessionStateSensor:
                sensorToCreate = new SessionStateSensor(this._publisher, (int)model.UpdateInterval, model.Name);
                break;

            case AvailableSensors.CurrentVolumeSensor:
                sensorToCreate = new CurrentVolumeSensor(this._publisher, (int)model.UpdateInterval, model.Name);
                break;

            case AvailableSensors.GPUTemperatureSensor:
                sensorToCreate = new GpuTemperatureSensor(this._publisher, (int)model.UpdateInterval, model.Name);
                break;

            case AvailableSensors.GPULoadSensor:
                sensorToCreate = new GpuLoadSensor(this._publisher, (int)model.UpdateInterval, model.Name);
                break;

            default:
                Log.Logger.Error("Unknown sensortype");
                break;
            }
            if (sensorToCreate != null)
            {
                this._configurationService.AddConfiguredSensor(sensorToCreate);
            }
        }
コード例 #2
0
        public async void ReadSensorSettings(MqttPublisher publisher)
        {
            this._sensorsLoading = true;
            while (this.SensorsSettingsFileLocked)
            {
                await Task.Delay(500);
            }
            this.SensorsSettingsFileLocked = true;
            List <ConfiguredSensor> sensors = new List <ConfiguredSensor>();

            using (var stream = new FileStream(Path.Combine(path, "configured-sensors.json"), FileMode.Open))
            {
                Log.Logger.Information($"reading configured sensors from: {stream.Name}");
                if (stream.Length > 0)
                {
                    sensors = await JsonSerializer.DeserializeAsync <List <ConfiguredSensor> >(stream);
                }
                stream.Close();
                this.SensorsSettingsFileLocked = false;
            }

            foreach (ConfiguredSensor configuredSensor in sensors)
            {
                AbstractSensor sensor = null;
                switch (configuredSensor.Type)
                {
                case "UserNotificationStateSensor":
                    sensor = new UserNotificationStateSensor(publisher, configuredSensor.UpdateInterval, configuredSensor.Name, configuredSensor.Id);
                    break;

                case "DummySensor":
                    sensor = new DummySensor(publisher, configuredSensor.UpdateInterval, configuredSensor.Name, configuredSensor.Id);
                    break;

                case "CurrentClockSpeedSensor":
                    sensor = new CurrentClockSpeedSensor(publisher, configuredSensor.UpdateInterval, configuredSensor.Name, configuredSensor.Id);
                    break;

                case "CPULoadSensor":
                    sensor = new CPULoadSensor(publisher, configuredSensor.UpdateInterval, configuredSensor.Name, configuredSensor.Id);
                    break;

                case "MemoryUsageSensor":
                    if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                    {
                        sensor = new MemoryUsageSensor(publisher, configuredSensor.UpdateInterval, configuredSensor.Name, configuredSensor.Id);
                    }
                    break;

                case "ActiveWindowSensor":
                    sensor = new ActiveWindowSensor(publisher, configuredSensor.UpdateInterval, configuredSensor.Name, configuredSensor.Id);
                    break;

                case "NamedWindowSensor":
                    sensor = new NamedWindowSensor(publisher, configuredSensor.WindowName, configuredSensor.Name, configuredSensor.UpdateInterval, configuredSensor.Id);
                    break;

                case "LastActiveSensor":
                    sensor = new LastActiveSensor(publisher, configuredSensor.UpdateInterval, configuredSensor.Name, configuredSensor.Id);
                    break;

                case "LastBootSensor":
                    sensor = new LastBootSensor(publisher, configuredSensor.UpdateInterval, configuredSensor.Name, configuredSensor.Id);
                    break;

                case "WebcamActiveSensor":
                    sensor = new WebcamActiveSensor(publisher, configuredSensor.UpdateInterval, configuredSensor.Name, configuredSensor.Id);
                    break;

                case "MicrophoneActiveSensor":
                    sensor = new MicrophoneActiveSensor(publisher, configuredSensor.UpdateInterval, configuredSensor.Name, configuredSensor.Id);
                    break;

                case "SessionStateSensor":
                    sensor = new SessionStateSensor(publisher, configuredSensor.UpdateInterval, configuredSensor.Name, configuredSensor.Id);
                    break;

                case "CurrentVolumeSensor":
                    sensor = new CurrentVolumeSensor(publisher, configuredSensor.UpdateInterval, configuredSensor.Name, configuredSensor.Id);
                    break;

                case "GpuTemperatureSensor":
                    sensor = new GpuTemperatureSensor(publisher, configuredSensor.UpdateInterval, configuredSensor.Name, configuredSensor.Id);
                    break;

                case "GpuLoadSensor":
                    sensor = new GpuLoadSensor(publisher, configuredSensor.UpdateInterval, configuredSensor.Name, configuredSensor.Id);
                    break;

                // keep this one last!
                case "WMIQuerySensor":
                    sensor = new WMIQuerySensor(publisher, configuredSensor.Query, configuredSensor.UpdateInterval, configuredSensor.Name, configuredSensor.Id);
                    break;

                default:
                    Log.Logger.Error("unsupported sensor type in config");
                    break;
                }
                if (sensor != null)
                {
                    this.ConfiguredSensors.Add(sensor);
                }
                this._sensorsLoading = false;
            }
        }