-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDevices.cs
221 lines (179 loc) · 6.69 KB
/
Devices.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
using ReaLTaiizor.Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ReaLTaiizor.Extension;
using YeelightAPI;
using YeelightAPI.Models;
namespace light_controller_app
{
public partial class Devices : Form
{
private StorageController storageController = new StorageController("devices.xml");
private List<Device> deviceList = new List<Device>();
private List<DeviceModel> deviceModelList = new List<DeviceModel>();
public Devices()
{
InitializeComponent();
}
private void Devices_FormClosing(object sender, FormClosingEventArgs e)
{
// Save the devices to the XML file
_SaveDevices();
}
private void _SaveDevices()
{
// Save the devices to the XML file
storageController.SaveDevices(deviceList);
}
private void _LoadDevices()
{
deviceModelList = storageController.LoadDevices();
deviceListBox.Items.Clear();
// Add the devices to the list box
foreach (DeviceModel deviceModel in deviceModelList)
{
deviceListBox.Items.Add(deviceModel.Hostname);
}
;
}
private async Task DiscoverDevicesAsync()
{
try
{
// Discover devices on the network
IEnumerable<Device> devices = await DeviceLocator.DiscoverAsync();
// if deviceList doesn't contain the devices, add them to deviceList
foreach (Device device in devices)
{
if (!deviceList.Contains(device))
{
deviceList.Add(device);
}
}
}
catch (DeviceDiscoveryException ex)
{
// Handle any exceptions that occur during device discovery
MessageBox.Show($"Error discovering devices: {ex.Message}", "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
private void EnterLoadingPhase(string AnimationLocation, Control control)
{
LoadingForm loadingForm = new LoadingForm();
loadingForm.TopLevel = false;
loadingForm.StartPosition = FormStartPosition.Manual;
if (control is ListBox || control is MetroListBox)
{
System.Windows.Forms.Panel panel = new System.Windows.Forms.Panel();
panel.Size = loadingForm.Size;
panel.MaximumSize = loadingForm.MaximumSize;
loadingForm.Show();
if (AnimationLocation == "middle")
{
int x = control.DisplayRectangle.Left + (control.DisplayRectangle.Width - panel.Width) / 2;
int y = control.DisplayRectangle.Top + (control.DisplayRectangle.Height - panel.Height) / 2;
panel.Location = new Point(x, y);
}
loadingForm.Dock = DockStyle.Fill; // Set the DockStyle to Fill
panel.Controls.Add(loadingForm);
control.Controls.Add(panel);
panel.Show();
}
else
{
if (AnimationLocation == "middle")
{
int x = control.Left + (control.Width - loadingForm.Width) / 2;
int y = control.Top + (control.Height - loadingForm.Height) / 2;
loadingForm.Location = new Point(x, y);
}
control.Controls.Add(loadingForm);
loadingForm.Show();
}
}
private void LeaveLoadingPhase(Control control)
{
foreach (Control control1 in control.Controls.OfType<LoadingForm>().ToList())
{
deviceListBox.Controls.Remove(control1);
}
foreach (Control control1 in control.Controls.OfType<System.Windows.Forms.Panel>().ToList())
{
deviceListBox.Controls.Remove(control1);
}
}
private void DiscoverButton_Click(object sender, EventArgs e)
{
EnterLoadingPhase("middle", deviceListBox);
// Call the asynchronous method and wait for it to complete
Task.Run(async () => await DiscoverDevicesAsync()).Wait();
LeaveLoadingPhase(deviceListBox);
// Save the devices
_SaveDevices();
//Load the devices
_LoadDevices();
}
private void btnSaveDevices_Click(object sender, EventArgs e)
{
_SaveDevices();
}
private void btnLoadDevices_Click(object sender, EventArgs e)
{
_LoadDevices();
}
public Device SelectedDevice;
public bool DeviceState;
private async void deviceListBox_SelectedIndexChanged(object sender, EventArgs e)
{
// Selected item's text
string selectedDevice = deviceListBox.GetItemText(deviceListBox.SelectedItem);
SelectedDevice = new Device(selectedDevice);
await SelectedDevice.Connect();
await UpdateState();
}
private async Task UpdateState()
{
await IsDeviceOn();
lblDeviceStatus.Text = DeviceState ? "ON" : "OFF";
}
private async Task IsDeviceOn()
{
// Get the power state as an object
object powerStateObject = await SelectedDevice.GetProp(PROPERTIES.power);
// Check if the power state is "1" (on)
bool isDeviceOn = powerStateObject?.ToString() == "on";
// Set the label text based on the device status
DeviceState = isDeviceOn ? true : false;
}
private async void btnOn_Click(object sender, EventArgs e)
{
SelectedDevice.TurnOn();
await UpdateState();
}
private async void btnOff_Click(object sender, EventArgs e)
{
SelectedDevice.TurnOff();
await UpdateState();
}
private void lblDeviceStatus_TextChanged(object sender, EventArgs e)
{
if (lblDeviceStatus.Text == "ON")
{
lblDeviceStatus.ForeColor = Color.FromArgb(0, 47, 56);
}
else if (lblDeviceStatus.Text == "OFF")
{
lblDeviceStatus.ForeColor = Color.FromArgb(58, 35, 0);
}
}
}
}