예제 #1
0
        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 "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 "IdleTimeSensor":
                    sensor = new IdleTimeSensor(publisher, configuredSensor.UpdateInterval, configuredSensor.Name, configuredSensor.Id);
                    break;

                case "UpTimeSensor":
                    sensor = new UpTimeSensor(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;

                // 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);
                }
            }
        }
예제 #2
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;

            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.IdleTimeSensor:
                sensorToCreate = new IdleTimeSensor(this._publisher, (int)model.UpdateInterval, model.Name);
                break;

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

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

            default:
                Log.Logger.Error("Unknown sensortype");
                break;
            }
            if (sensorToCreate != null)
            {
                this._configurationService.AddConfiguredSensor(sensorToCreate);
            }
        }