Exemple #1
0
 internal void Connect()
 {
     try
     {
         upsd.Connect();
         this.Status            &= ~UPSMonStatus.COMMBAD;
         this.Status            |= UPSMonStatus.COMMOK;
         this.Device.Description = upsd.GetUPSDescription(this.Device.Name);
         upsd.SetUsername(this.Username);
         upsd.SetPassword(this.Password);
         upsd.Login(this.Device.Name);
     }
     catch (SocketException sockex)
     {
         UPSMonThreads.AppendLog("Could not connect to UPS " + this.Device.ToString() + "! " + sockex.Message);
         if (this.Status != UPSMonStatus.NOCOMMS)
         {
             this.Status &= ~UPSMonStatus.COMMOK;
             this.Status |= UPSMonStatus.COMMBAD;
         }
         upsd.Disconnect();
     }
     catch (UPSException upsex)
     {
         UPSMonThreads.AppendLog("Error on UPS " + this.Device.ToString() + "! " + upsex.Code.ToString() + ":" + upsex.Description
                                 + (String.IsNullOrEmpty(upsex.Message) ? "" : " (" + upsex.Message + ")"));
         if (this.Status != UPSMonStatus.NOCOMMS)
         {
             this.Status &= ~UPSMonStatus.COMMOK;
             this.Status |= UPSMonStatus.COMMBAD;
         }
     }
 }
Exemple #2
0
        internal void Initialize()
        {
            Parallel.ForEach <MonitoredUPS>(this, monups =>
            {
                monups.Connect();
                if (monups.Connected == true)
                {
                    monups.UpdateStatus();
                }
            });

            foreach (MonitoredUPS monups in this)
            {
                UPSMonThreads.Debug("UPS " + monups.Device.ToString() + "(" + monups.Device.Description + ") Status:" + monups.Status);
            }
        }
Exemple #3
0
 internal void Update()
 {
     Parallel.ForEach <MonitoredUPS>(this, monups =>
     {
         if (monups.Connected == false)
         {
             monups.Connect();
         }
         if (monups.Connected == true)
         {
             TimeSpan pollInterval = (monups.Alert == true ? UPSMonThreads.Settings.PollFrequencyAlert : UPSMonThreads.Settings.PollFrequency);
             if (monups.LastUpdate + pollInterval < DateTime.Now)
             {
                 //UPSMonThreads.Debug("Polling status of " + monups.Device.ToString());
                 UPSMonStatus curStatus = monups.Status;
                 monups.UpdateStatus();
                 if (monups.Status != curStatus)
                 {
                     UPSMonThreads.AppendLog(monups.Device.ToString() + " : New Status=" + monups.Status);
                 }
             }
         }
     });
 }
Exemple #4
0
        public void UpdateStatus()
        {
            try
            {
                if (this.Connected == false)
                {
                    this.Status = UPSMonStatus.NOCOMMS;
                    return;
                }

                string upsdStatus = upsd.GetUPSVar(this.Device.Name, "ups.status");
                this.Status &= ~UPSMonStatus.ALLUPSD;

                string[] parts = upsdStatus.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string status in parts)
                {
                    switch (status.ToUpper())
                    {
                    case "OL":
                        this.Status |= UPSMonStatus.ONLINE;
                        break;

                    case "OB":
                        this.Status |= UPSMonStatus.ONBATT;
                        break;

                    case "LB":
                        this.Status |= UPSMonStatus.LOWBATT;
                        break;

                    case "HB":
                        this.Status |= UPSMonStatus.HIGHBATT;
                        break;

                    case "RB":
                        this.Status |= UPSMonStatus.REPLACEBATT;
                        break;

                    case "CHRG":
                        this.Status |= UPSMonStatus.CHARGING;
                        break;

                    case "DISCHRG":
                        this.Status |= UPSMonStatus.DISCHARGING;
                        break;

                    case "BYPASS":
                        this.Status |= UPSMonStatus.BYPASS;
                        break;

                    case "CAL":
                        this.Status |= UPSMonStatus.CALIBRATION;
                        break;

                    case "OFF":
                        this.Status |= UPSMonStatus.OFF;
                        break;

                    case "OVER":
                        this.Status |= UPSMonStatus.OVERLOADED;
                        break;

                    case "TRIM":
                        this.Status |= UPSMonStatus.TRIM;
                        break;

                    case "BOOST":
                        this.Status |= UPSMonStatus.BOOST;
                        break;

                    case "FSD":
                        this.Status |= UPSMonStatus.FSD;
                        break;
                    }
                }

                this.LastUpdate = DateTime.Now;
            }
            catch (SocketException sockex)
            {
                UPSMonThreads.AppendLog("Network error communicating to UPS " + this.Device.ToString() + "! " + sockex.Message);
                if (this.Status != UPSMonStatus.NOCOMMS)
                {
                    this.Status &= ~UPSMonStatus.COMMOK;
                    this.Status |= UPSMonStatus.COMMBAD;
                }
                upsd.Disconnect();
            }
            catch (UPSException upsex)
            {
                UPSMonThreads.AppendLog("Error on UPS " + this.Device.ToString() + "! " + upsex.Message);
                if (this.Status != UPSMonStatus.NOCOMMS)
                {
                    this.Status &= ~UPSMonStatus.COMMOK;
                    this.Status |= UPSMonStatus.COMMBAD;
                }
            }
        }