private void SimulateBatteryDecay()
        {
            if (txtSimBatteryRuntime.Text.Equals(""))
            {
                SimulatorPopulate();
            }

            double BattValue            = Convert.ToDouble(NUT_Processor.SearchNUTData("battery.charge"));
            int    BattRuntimeMax       = Convert.ToInt32(txtSimBatteryRuntime.Text);
            int    BattRuntimeBreakdown = (BattRuntimeMax / 100);
            int    BattRuntime          = Convert.ToInt32(NUT_Processor.SearchNUTData("battery.runtime"));

            // Since it would be impossible for a battery to have a negative charge, it will set the charge to 0
            if ((BattValue - SimUPSDecayRate) <= 0)
            {
                Backend.Background.WriteNUTLog("[SIMULATOR] Battery decayed to empty");
                NUT_Processor.ModifySimNUTData("battery.charge", "0");
                NUT_Processor.ModifySimNUTData("battery.runtime", "0");
            }
            else
            {
                Backend.Background.WriteNUTLog("[SIMULATOR] Battery decayed by " + SimUPSDecayRate + " from " + BattValue + " to " + (BattValue - SimUPSDecayRate));
                NUT_Processor.ModifySimNUTData("battery.charge", Convert.ToString(BattValue - SimUPSDecayRate));
                NUT_Processor.ModifySimNUTData("battery.runtime", Convert.ToString(BattRuntime - (BattRuntimeBreakdown * SimUPSDecayRate)));
            }

            // Refresh information
            //UPSPoll();
        }
 private void SimulatorPopulate()
 {
     txtSimBatteryCharge.Text         = NUT_Processor.SearchNUTData("battery.charge");
     txtSimBatteryChargeLow.Text      = NUT_Processor.SearchNUTData("battery.charge.low");
     txtSimBatteryChargeWarn.Text     = NUT_Processor.SearchNUTData("battery.charge.warning");
     txtSimBatteryRuntime.Text        = NUT_Processor.SearchNUTData("battery.runtime");
     txtSimBatteryRuntimeLow.Text     = NUT_Processor.SearchNUTData("battery.runtime.low");
     txtSimBatteryVoltage.Text        = NUT_Processor.SearchNUTData("battery.voltage");
     txtSimBatteryVoltageNominal.Text = NUT_Processor.SearchNUTData("battery.voltage.nominal");
     txtSimInputVoltage.Text          = NUT_Processor.SearchNUTData("input.voltage");
     txtSimInputVoltageNominal.Text   = NUT_Processor.SearchNUTData("input.voltage.nominal");
     txtSimOutputVoltage.Text         = NUT_Processor.SearchNUTData("output.voltage");
     cmbSimUPSStatus.Text             = NUT_Processor.SearchNUTData("ups.status");
     lblUPSModel.Text = NUT_Processor.UPSStatistics();
 }
        private void btnSimApply_Click(object sender, EventArgs e)
        {
            NUT_Processor.ModifySimNUTData("battery.charge", txtSimBatteryCharge.Text);
            NUT_Processor.ModifySimNUTData("battery.charge.low", txtSimBatteryChargeLow.Text);
            NUT_Processor.ModifySimNUTData("battery.charge.warning", txtSimBatteryChargeWarn.Text);
            NUT_Processor.ModifySimNUTData("battery.runtime", txtSimBatteryRuntime.Text);
            NUT_Processor.ModifySimNUTData("battery.runtime.low", txtSimBatteryRuntimeLow.Text);
            NUT_Processor.ModifySimNUTData("battery.voltage", txtSimBatteryVoltage.Text);
            NUT_Processor.ModifySimNUTData("battery.voltage.nominal", txtSimBatteryVoltageNominal.Text);
            NUT_Processor.ModifySimNUTData("input.voltage", txtSimInputVoltage.Text);
            NUT_Processor.ModifySimNUTData("input.voltage.nominal", txtSimInputVoltageNominal.Text);
            NUT_Processor.ModifySimNUTData("output.voltage", txtSimOutputVoltage.Text);
            NUT_Processor.ModifySimNUTData("ups.status", cmbSimUPSStatus.Text);

            SimUPSDecayRate = Convert.ToDouble(txtSimBatteryDecay.Text);

            IsSimulatingDecay = chkSimulate.AutoCheck;

            UPSPoll();
        }
        private void UPSPoll()
        {
            // Pulls latest data from UPSVariables
            short nutPort = Convert.ToInt16(txtPort.Text);

            // Determines whether it is in simulation mode (isSimulated) and whether it has already been polled
            if (Backend.Background.isSimulated && isPolled)
            {
                // I have a feeling I'm eventually going to need to put something here.
            }
            else
            {
                // If simulation mode is not active, it will poll the NUT server
                try
                {
                    Tuple <string, bool> TupNutOutput = NUT_Poller.PollNUTServer(txtIPAddress.Text, nutPort);
                    isPolled = TupNutOutput.Item2;
                    string nutOutput = TupNutOutput.Item1;
                    NUT_Processor.ParseNUTOutput(nutOutput);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Exception occured: " + e);
                }
            }

            // Error condition, unable to get data from NUT server for a variety of reasons
            if (!isPolled)
            {
                // Checks to see if this is the first time this program has been run
                string TempValue = Backend.NUT_Config.GetConfig("IP Address");
                if (TempValue == null)
                {
                    return;
                }

                Console.WriteLine("Did not get data successfully");
                MessageBox.Show("Was unable to retrieve data from NUT sever. Got an \"Access Denied\" message.\n\nA common cause of this is the IP address of this client not being whitelisted on the NUT server.");
                isPollingUPS = false;
                return;
            }
            else
            {
                isPollingUPS = true;
            }

            ushort BatteryPercentage = Convert.ToUInt16(NUT_Processor.SearchNUTData("battery.charge"));

            if (BatteryPercentage <= AlarmPercentage)
            {
                PerformAlarmAction();
            }

            if (Backend.Background.isSimulated && IsSimulatingDecay)
            {
                SimulateBatteryDecay();
            }

            // Refreshes the data on the form
            Tuple <string, double, int> UPSBatteryStatus = NUT_Processor.GetBatteryStatus();

            lblUPSStatus.Invoke((MethodInvoker)(() => UpdateUPSStatus(UPSBatteryStatus.Item1, UPSBatteryStatus.Item3)));
            lblUPSModel.Invoke((MethodInvoker)(() => lblUPSModel.Text = (NUT_Processor.UPSStatistics())));
        }