public override IValidationResult IsValid(UpdateFanTaskProcessorRequestVersionOne record) { var existingRelays = _relayClient.GetRelays(); var jobExists = JobStateHelper.GetJobByName(record.JobName) != null; if (record?.RelayGpioPin != null && !existingRelays.Any(r => r.GpioPin == record.RelayGpioPin)) { return(new ValidationResult { Message = $"Could not find a relay with the gpio pin: {record.RelayGpioPin}", StatusCode = System.Net.HttpStatusCode.NotFound }); } if (!jobExists) { return(new ValidationResult { Message = $"Could not find any jobs with the name: {record.JobName}", StatusCode = System.Net.HttpStatusCode.NotFound }); } return(null); }
public override IActionResult ProcessRequest(GetTaskByNameProcessorRequestVersionOne record) { var task = JobStateHelper.GetJobByName(record.TaskName); if (task == null) { return(new NotFoundObjectResult(record)); } return(new OkObjectResult(task)); }
public override IActionResult ProcessRequest(UpdateFanTaskProcessorRequestVersionOne record) { var jobToUpdate = JobStateHelper.GetJobByName(record.JobName); var currentJobOptions = (FanJobOptions)jobToUpdate.JobOptions; var newJobState = new JobState() { JobOptions = new FanJobOptions { RunInterval = record?.RunInterval ?? currentJobOptions.RunInterval, RunDuration = record?.RunDuration ?? currentJobOptions.RunDuration, RelayGpioPin = record?.RelayGpioPin ?? currentJobOptions.RelayGpioPin }, LastRunTime = jobToUpdate.LastRunTime, NextRunTime = jobToUpdate.NextRunTime }; JobStateHelper.AddOrUpdateJobState(newJobState, record.JobName); return(new OkObjectResult(newJobState)); }
public DerivedProgressionManager(IObservableList <IProgressionViewer> subProgs, ILoggerFactory loggerFactory) { this.logger = loggerFactory.CreateLogger <DerivedProgressionManager>(); subProgs .Connect() .ToCollection() .Select(progs => progs.Select(prog => prog.WhenAnyValue( vm => vm.Title, vm => vm.State, vm => vm.Target, vm => vm.Current, vm => vm.Details, vm => vm.Weight, ProgressionSnapshot.Factory))) .Select(Observable.CombineLatest) .Switch() .Subscribe(progs => { this.State = JobStateHelper.MergeStates(progs.Select(prog => prog.State)); Target = progs.Sum(prog => prog.Weight) * Resolution; Current = (int)progs.Sum(prog => (decimal)prog.Current / (decimal)prog.Target * Weight * Resolution); var newDetails = progs.FirstOrDefault(x => x.State == JobState.InProgress).Details; if (State.IsIn(JobState.ToDo, JobState.Done)) { newDetails = string.Empty; } if (State == JobState.Failed) { newDetails = $"Job Failed.{Environment.NewLine + progs.FirstOrDefault(x => x.State == JobState.Failed).Details}"; logger.LogError("job failed: {0}", newDetails); } Details = newDetails; logger.LogTrace("extra info:" + subProgs.Count + Environment.NewLine + string.Join(Environment.NewLine, progs.Select(prog => $" {prog.Title} | {prog.State} | {prog.Current}/{prog.Target} : {prog.Details}")) ); logger.LogTrace("updating progress - {0}subs: {1}/{2} ({3}): {4}", progs.Count(), Current, Target, State, Details); }); }
public override IActionResult ProcessRequest(UpdateHumidifierTaskProcessorRequestVersionOne record) { var jobToUpdate = JobStateHelper.GetJobByName(record.JobName); var currentJobOptions = (HumidifierPressureAltitudeTemperatureJobOptions)jobToUpdate.JobOptions; var newJobState = new JobState() { JobOptions = new HumidifierPressureAltitudeTemperatureJobOptions { CheckInterval = record?.CheckInterval ?? currentJobOptions.CheckInterval, HumiditySensorGpio = record?.HumiditySensorGpio ?? currentJobOptions.HumiditySensorGpio, TargetHumidity = record?.TargetHumidity ?? currentJobOptions.TargetHumidity, RelayGpio = record?.RelayGpio ?? currentJobOptions.RelayGpio }, LastRunTime = jobToUpdate.LastRunTime, NextRunTime = jobToUpdate.NextRunTime }; JobStateHelper.AddOrUpdateJobState(newJobState, record.JobName); return(new OkObjectResult(newJobState)); }
public IActionResult GetAllTasks() { return(new OkObjectResult(JobStateHelper.GetJobs())); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { var devMachineNames = Configuration.GetSection("DevMachineNames").Get <List <string> >(); var relays = Configuration.GetSection("Relays").Get <List <Relay> >(); var relayOptions = new RelayClientOptions { Relays = relays }; var sensors = Configuration.GetSection("Sensors").Get <List <Sensor> >(); var sensorOptions = new SensorClientOptions { Sensors = sensors }; var humidifierJobOptions = Configuration.GetSection("HumidifierJobOptions") .Get <HumidifierPressureAltitudeTemperatureJobOptions>(); var fanJobOptions = Configuration.GetSection("FanJobOptions") .Get <FanJobOptions>(); JobStateHelper.AddOrUpdateJobState(new JobState() { JobOptions = humidifierJobOptions }, "HumidifierPressureAltitudeTemperatureJob"); JobStateHelper.AddOrUpdateJobState(new JobState() { JobOptions = fanJobOptions }, "FanJob"); //TODO: use a real logger eventually var loggerFactory = new LoggerFactory(); services.AddTransient(provider => loggerFactory); if (devMachineNames.Contains(Environment.MachineName)) { services.AddSingleton <IGpioController, MockGpioDriver>(); } else { services.AddSingleton <IGpioController, GpioController>(); } services.AddSingleton <HumidifierPressureAltitudeTemperatureJobOptions>(provider => humidifierJobOptions); services.AddSingleton <FanJobOptions>(provider => fanJobOptions); services.AddSingleton <IRelayClientOptions>(provider => relayOptions); services.AddSingleton <ISensorClientOptions>(provider => sensorOptions); services.AddTransient <ISensorReadingClientFactory, SensorReadingClientFactory>(); services.AddSingleton <IRelayClient, RelayClient.RelayClient>(); services.AddSingleton <ISensorClient, SensorClient.SensorClient>(); services.AddTransient <IProcessorFactory, ProcessorFactory>(); services.AddSingleton <IHostedService, HumidifierPressureAltitudeTemperatureJob>(); services.AddSingleton <IHostedService, FanJob>(); services.AddMvc() .SetCompatibilityVersion(CompatibilityVersion.Version_2_2); }