TKK_E32230371/UPSNet/UPSForm.cs

221 lines
6.2 KiB
C#

/*
* Created by SharpDevelop.
* User: EVXIO
* Date: 4/3/2025
* Time: 10:35 PM
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Text;
namespace UPSNet
{
/// <summary>
/// Description of UPSForm.
/// </summary>
public partial class UPSForm : Form {
ConfigForm cfgForm = new ConfigForm();
bool isBusy = false;
bool chargeTickTock = false;
public static bool closeJustHideForm = false;
public UPSForm(){
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
SetDefaultInfo();
if (UPSApp.IsRunningInDebuggerOrIDE()){
this.Text = "EVXIO UPS Net (Debug Mode)";
}
}
void SetDefaultInfo(){
labelConnection.Text = "No Connection";
labelBattLevel.Text = RepeatString("🔲", 4);
labelBattLevel.ForeColor = Color.LimeGreen;
labelStateEx.Text = "";
labelState.Text = "";
if (UPSApp.shutdownMode==0){
labelInfo.Text = "Shutdown: Use UPS Suggestion";
}else{
ShowShutdownMode(UPSApp.shutdownMode-1, true);
}
}
void UPSFormFormClosing(object sender, FormClosingEventArgs e) {
if (closeJustHideForm){
if (e.CloseReason == CloseReason.UserClosing){
e.Cancel = true; // Cancel the close operation
this.Hide(); // Hide the form instead
//trayIcon.Visible = true; // Show the tray icon
}
}else{
this.Hide();
UPSApp.upsForm = null;
e.Cancel = false; //accept close
}
}
void ButtonConfigClick(object sender, EventArgs e) {
UPSApp.dialogOk = false;
cfgForm.ShowDialog();
if (UPSApp.dialogOk){
if (!UPSApp.fetcher.IsReachable){
labelConnection.Text = "Connecting...";
labelConnection.ForeColor = Color.Black;
}else{
SetDefaultInfo();
}
}
}
void ShowShutdownMode(int mode, bool forced){
string s = "";
ShutdownMode x = (ShutdownMode) mode;
switch (x){
case ShutdownMode.Disabled: s="Disabled";break;
case ShutdownMode.OnBattery: s="When AC Loss";break;
case ShutdownMode.OnBattery75: s="When ≤75% Battery";break;
case ShutdownMode.OnBattery50: s="When ≤50% Battery";break;
case ShutdownMode.OnBattery25: s="When ≤25% Battery";break;
case ShutdownMode.OnBatteryCritical: s="When Battery Critical";break;
default:s="Mode Unknown";break;
}
labelInfo.Text = "Shutdown: " + s;
labelInfo.ForeColor = Color.Black;
}
void UpdateView(){
isBusy = true;
//if (UPSApp.fetcher.IsReachable && (this.WindowState == FormWindowState.Normal) && (this.Visible)){
if (UPSApp.fetcher.IsReachable){
if (UPSApp.fetcher.Success>0){
labelConnection.Text = "Connected";
labelConnection.ForeColor = Color.Green;
//information status:
//show information for shutdown flag
int realMode = 0;
bool isForced = UPSApp.shutdownMode>0;
if (!isForced){
realMode = UPSApp.fetcher.ShutdownSuggestMode;
}else{
realMode = UPSApp.shutdownMode-1;
}
ShowShutdownMode(realMode, isForced);
//Line and state info:
if (UPSApp.fetcher.OnState){
if (UPSApp.fetcher.UPSLineFrom==1 || UPSApp.fetcher.IsChargingOrBlink){
labelState.Text = "AC Line" + (UPSApp.fetcher.IsChargingOrBlink?" (Charging)":"");
labelStateEx.Text = "~";
labelStateEx.ForeColor = Color.DarkOrange;
}else if (UPSApp.fetcher.UPSLineFrom==2){
labelState.Text = "On Battery" + (UPSApp.fetcher.BatteryCritical?" (Critical)":"");
if (UPSApp.fetcher.BatteryCritical){
labelStateEx.Text = "r";
labelStateEx.ForeColor = Color.Red;
}else{
labelStateEx.Text = "";
}
}
}else{
labelState.Text = "OFF";
if (UPSApp.fetcher.UPSLineFrom==1){
labelStateEx.Text = "~";
labelStateEx.ForeColor = Color.DarkOrange;
if (UPSApp.fetcher.IsChargingOrBlink){
labelState.Text += " - Charging";
}else{
labelState.Text += " - AC Available";
}
}else{
labelStateEx.Text = "";
}
}
//batt visual meter
if (UPSApp.fetcher.IsChargingOrBlink){
chargeTickTock = !chargeTickTock;
int bar = chargeTickTock?Math.Min(UPSApp.fetcher.ChargeLevel+1, UPSApp.fetcher.BattMaxLevel):Math.Min(UPSApp.fetcher.ChargeLevel, UPSApp.fetcher.BattMaxLevel);
labelBattLevel.ForeColor = Color.LimeGreen;
labelBattLevel.Text = RepeatString("⬛", bar) + RepeatString("🔲", UPSApp.fetcher.BattMaxLevel-bar);
}else{
if (UPSApp.fetcher.BatteryCritical){
chargeTickTock = !chargeTickTock;
int bar = chargeTickTock?0:1;
labelBattLevel.ForeColor = Color.Red;
labelBattLevel.Text = RepeatString("⬛", bar) + RepeatString("🔲", UPSApp.fetcher.BattMaxLevel-bar);
}else{
chargeTickTock = false;
if (UPSApp.fetcher.BattMaxLevel==0){
labelBattLevel.ForeColor = Color.Red;
labelBattLevel.Text = "🔲🔲🔲🔲";
}else{
int bar = UPSApp.fetcher.ChargeLevel;
labelBattLevel.ForeColor = Color.LimeGreen;
labelBattLevel.Text = RepeatString("⬛", bar) + RepeatString("🔲", UPSApp.fetcher.BattMaxLevel-bar);
}
}
}
}
}else{
labelConnection.Text = "No Connection";
labelConnection.ForeColor = Color.Red;
}
isBusy = false;
}
void TimerUpdateTick(object sender, EventArgs e) {
if (isBusy) return;
UpdateView();
}
void UPSFormShown(object sender, EventArgs e){
UpdateView();
timerUpdate.Enabled = true;
}
void LabelBattClick(object sender, EventArgs e) {
}
public static string RepeatString(string str, int count){
if (count <= 0) return "";//string.Empty;
if (count == 1) return str;
var sb = new StringBuilder(str.Length * count);
for (int i = 0; i < count; i++) {
sb.Append(str);
}
return sb.ToString();
}
void UPSFormVisibleChanged(object sender, EventArgs e){
if (!this.Visible){
timerUpdate.Enabled = false;
}else{
UpdateView();
timerUpdate.Enabled = true;
}
}
}
}