예제 #1
0
        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;

            default:
                Log.Logger.Error("Unknown sensortype");
                break;
            }
            if (sensorToCreate != null)
            {
                this._configurationService.AddConfiguredSensor(sensorToCreate);
            }
        }
        public async void ReadSensorSettings(MqttPublisher publisher)
        {
            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 "WMIQuerySensor":
                    sensor = new WMIQuerySensor(publisher, configuredSensor.Query, configuredSensor.UpdateInterval, configuredSensor.Name, configuredSensor.Id);
                    break;

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

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

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

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