Exemplo n.º 1
0
        public ActionResult <GroupResponseModel> Get(long id)
        {
            GroupResponseModel result = _procedure.Get(id);

            return(result);
        }
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            //return;
            using (IServiceScope scope = scopeFactory.CreateScope()) {
                var valveService = scope.ServiceProvider.GetRequiredService <IValveProcedure>();
                IProfileProcedure profileService = scope.ServiceProvider.GetRequiredService <IProfileProcedure>();
                IGroupProcedure   groupService   = scope.ServiceProvider.GetRequiredService <IGroupProcedure>();
                //PinController pinController = scope.ServiceProvider.GetRequiredService<PinController>();
                MainController settingsController = scope.ServiceProvider.GetRequiredService <MainController>();

                valveService.GetList().ToList().ForEach(
                    valve => {
                    EasyRequest($"http://localhost:5000/api/v1/Pin/Close/{valve.Id}");
                });
                Console.Clear();

                while (!stoppingToken.IsCancellationRequested)
                {
                    //todo check for is whole system is active
                    //if not, turn off all open valves

                    GgSettings settings = settingsController.Get().Value;

                    if (settings.IsStarted)
                    {
                        List <ProfileResponseModel> profiles = profileService.GetList().ToList();

                        List <ProfileResponseModel> todaysProfiles                      = GetTodaysProfiles(profiles);
                        List <ProfileResponseModel> startTimePassedProfiles             = GetStartTimePassedProfiles(todaysProfiles);
                        List <ProfileResponseModel> totalTimeIsNotPassedProfiles        = GetTotalTimeIsNotPassedProfiles(startTimePassedProfiles);
                        IOrderedEnumerable <ProfileResponseModel> orderedTodaysProfiles = totalTimeIsNotPassedProfiles.OrderBy(o => o.StartHour).ThenBy(o => o.StartMinute);

                        List <Valve> valvesToBeOpen = new List <Valve>();

                        foreach (ProfileResponseModel profile in orderedTodaysProfiles)
                        {
                            //DateTime profileEndTime = GetProfileEndTime(profile);
                            //if (DateTime.Now.CompareTo(profileEndTime) >= 0) {
                            //    continue;
                            //}

                            List <Group> profileGroups = profile.Groups.ToList();
                            int          passedTime    = (DateTime.Now.Hour * 60 + DateTime.Now.Minute - profile.StartHour * 60 - profile.StartMinute);

                            for (int i = profileGroups.Count - 1; i >= 0; i--)
                            {
                                GroupResponseModel g = groupService.Get(profileGroups[i].Id);
                                if (passedTime < profileGroups[i].Duration)
                                {
                                    valvesToBeOpen.AddRange(g.Valves);
                                    break;
                                }
                                else
                                {
                                    //foreach (Valve valve in g.Valves) {
                                    //    //EasyRequest($"http://localhost:5000/api/v1/Pin/Close/{valve.Id}");
                                    //    //pinController.Close(valve.Id);
                                    //}
                                    passedTime -= profileGroups[i].Duration;
                                }
                            }
                        }

                        valvesToBeOpen.ForEach(valve => EasyRequest($"http://localhost:5000/api/v1/Pin/Open/{valve.Id}"));
                        await Task.Delay(1000 * 5, stoppingToken);

                        List <ValveResponseModel> valves = valveService.GetList().ToList();
                        foreach (ValveResponseModel valve in valves)
                        {
                            if (!valvesToBeOpen.Exists(o => o.Id == valve.Id))
                            {
                                EasyRequest($"http://localhost:5000/api/v1/Pin/Close/{valve.Id}");
                            }
                        }
                    }

                    await Task.Delay(1000 * 10, stoppingToken);
                }
            }
        }