public ActionResult OnGetToggleOutletState(int id, Outlet.PowerState currentPowerState)
        {
            switch (currentPowerState)
            {
            case Outlet.PowerState.On:
                _pdu.TurnOutletOff(id);
                break;

            case Outlet.PowerState.Off:
                _pdu.TurnOutletOn(id);
                break;
            }

            var outletTableViewModel = new OutletTableViewModel();

            outletTableViewModel.Outlets            = _pdu.GetOutletsWaitForPending(true, false);
            outletTableViewModel.WebRootPath        = _environment.WebRootPath;
            outletTableViewModel.OutletConfirmation = _configuration.GetSection("OutletConfirmation").Get <string[]>();
            outletTableViewModel.Phases             = _pdu.GetPhases();

            var viewData = new ViewDataDictionary(new Microsoft.AspNetCore.Mvc.ModelBinding.EmptyModelMetadataProvider(), new Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary())
            {
                { "OutletTableViewModel", outletTableViewModel }
            };

            viewData.Model = outletTableViewModel;

            PartialViewResult result = new PartialViewResult()
            {
                ViewName = "_OutletTable",
                ViewData = viewData,
            };

            return(result);
        }
Exemple #2
0
        public IEnumerable <Outlet> GetOutlets(bool getPower = false, bool getCurrent = false)
        {
            Dictionary <int, Outlet> output = new Dictionary <int, Outlet>();

            int    id;
            string name;
            string tail;

            var olStatusAll = _sshDevice.ExecuteCommand("olStatus all");

            // Added asserts as ExecuteCommand has been oberved to return null
            Debug.Assert(olStatusAll != null);

            // Initially populate Outlet dictionary with information parsed from olStatus all
            foreach (string line in olStatusAll)
            {
                bool success = ParseOlReadingCommonLine(line, out id, out name, out tail);
                if (success)
                {
                    Match match = Regex.Match(tail, @"^([A-Za-z]+)([*]*) *$");
                    if (match.Success)
                    {
                        Outlet.PowerState state = (match.Groups[1].Value == "On") ? Outlet.PowerState.On : Outlet.PowerState.Off;
                        bool pending            = (match.Groups[2].Value == "*");

                        output.Add(id, new Outlet()
                        {
                            Id = id, Name = name, State = state, Pending = pending
                        });
                    }
                }
            }

            if (getPower)
            {
                var olReadingAllPower = _sshDevice.ExecuteCommand("olReading all power");
                Debug.Assert(olReadingAllPower != null);

                // Populate additional information parsed from power command
                foreach (string line in olReadingAllPower)
                {
                    bool success = ParseOlReadingCommonLine(line, out id, out name, out tail);
                    if (success)
                    {
                        Match match = Regex.Match(tail, @"^([0-9.]+) W *$");
                        if (match.Success)
                        {
                            Outlet foundOutlet;
                            if (output.TryGetValue(id, out foundOutlet))
                            {
                                foundOutlet.Watts = float.Parse(match.Groups[1].Value);
                            }
                        }
                    }
                }
            }

            if (getCurrent)
            {
                var olReadingAllCurrent = _sshDevice.ExecuteCommand("olReading all current");
                Debug.Assert(olReadingAllCurrent != null);

                // Populate additional information parsed from current command
                foreach (string line in olReadingAllCurrent)
                {
                    bool success = ParseOlReadingCommonLine(line, out id, out name, out tail);
                    if (success)
                    {
                        Match match = Regex.Match(tail, @"^([0-9.]+) A *$");
                        if (match.Success)
                        {
                            Outlet foundOutlet;
                            if (output.TryGetValue(id, out foundOutlet))
                            {
                                foundOutlet.Amps = float.Parse(match.Groups[1].Value);
                            }
                        }
                    }
                }
            }

            if (output.Values.Count > 0)
            {
                return(output.Values);
            }
            else
            {
                return(null);
            }
        }