BaseLedManager.cs
9.6 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace OnlineStore.DeviceLibrary
{
public class LEDManager
{
public static Dictionary<string, LEDBaseModule> deviceMap = new Dictionary<string, LEDBaseModule>();
public static byte DefaultLight = 100;
public static int DeviceLedType = ConfigAppSettings.GetIntValue(Setting_Init.DeviceLedType);
public static string ColorRule = ConfigAppSettings.GetValue(Setting_Init.ColorRuleConfig).ToUpper();
public static string DefaultIP = ConfigAppSettings.GetValue(Setting_Init.DefaultDeviceIP);
public static LEDBaseModule GetLedModule(string ip)
{
LEDBaseModule led = null;
if (deviceMap.ContainsKey(ip))
{
led = deviceMap[ip];
}
else
{
led = LEDBaseModule.GetModule(ip);
deviceMap.Add(ip, led);
}
return led;
}
public static void Init()
{
ColorRule = ConfigAppSettings.GetValue(Setting_Init.ColorRuleConfig).ToUpper();
if (ColorRule.Length == 3 && ColorRule.Contains("R") && ColorRule.Contains("G") && ColorRule.Contains("B"))
{
}
else
{
LEDManager.ColorRule = "RGB";
ConfigAppSettings.SaveValue(Setting_Init.ColorRuleConfig, LEDManager.ColorRule);
}
LoadStatusDMX();
}
public static void LoadStatusDMX()
{
StatusLedDmx = new List<int>();
string str = ConfigAppSettings.GetValue(Setting_Init.StatusLedDmx);
if (String.IsNullOrEmpty(str))
{
str = "0";
ConfigAppSettings.SaveValue(Setting_Init.StatusLedDmx, str);
}
string[] array = str.Split(';');
foreach (string s in array)
{
try
{
int a = Convert.ToInt32(s);
StatusLedDmx.Add(a);
}
catch (Exception ex)
{
}
}
}
/// <summary>
/// 1=绿灯,2=黄灯
/// </summary>
public static int CurrLedStatus = -1;
private static List<int> StatusLedDmx = new List<int>();
/// <summary>
/// 打开状态灯
/// </summary>
public static void OpenStatusLights(string color = "green")
{
CloseStatusLights("");
foreach (LEDBaseModule module in deviceMap.Values)
{
List<Light> sLed = new List<Light>();
foreach (int dmx in StatusLedDmx)
{
for (int index = 0; index < module.Max_Light; index++)
{
if ("green".Equals(color))
{
CurrLedStatus = 1;
sLed.Add(Light.GreenLight(dmx, index));
}
else if ("yellow".Equals(color))
{
CurrLedStatus = 2;
sLed.Add(Light.BlueLight(dmx, index));
}
}
}
module.LightOn(sLed.ToArray());
}
}
/// <summary>
/// 关闭状态灯
/// </summary>
/// <param name="color"></param>
public static void CloseStatusLights(string color = "")
{
CurrLedStatus = 0;
foreach (LEDBaseModule module in deviceMap.Values)
{
foreach (int dmx in StatusLedDmx)
{
module.AllLightOff(dmx);
}
}
}
}
public abstract class LEDBaseModule
{
protected ushort Max_DMX = 16;
public int Max_Light = 170;
public string ModuleIP = "";
public static LEDBaseModule GetModule(string ip)
{
//if (LEDManager.DeviceLedType.Equals(1))
//{
// LEDColorModule module = new LEDColorModule(ip);
// return module;
//}
//else if (LEDManager.DeviceLedType.Equals(2))
//{
LEDColorArtNet module = new LEDColorArtNet(ip);
return module;
//}
//else
//{
// LEDSingleModule module = new LEDSingleModule(ip);
// return module;
//}
}
internal LEDBaseModule(string ip)
{
ModuleIP = ip;
}
public abstract void AllLightOff(int dmx = -1);
public abstract void AllLightOn(int dmx = -1);
public abstract void AllLightOn(Light light);
public abstract void OnlyLightOn(params Light[] lights);
public abstract void LightOn(params Light[] lights);
public abstract void LightOff(int dmx, params int[] lightIndexs);
public abstract void LightOff(params Light[] lights);
/// <summary>
/// 打包方法,可以将十六制字符串转成byte[] ,字符串没有空格
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
protected byte[] StringToByte(string s)
{
string temps = s.Replace(" ", "").Replace(":", "");
if (temps.Length % 2 != 0)
{
temps = "0" + temps;
}
byte[] tempb = new byte[temps.Length / 2];
int j = 0;
for (int i = 0; i < temps.Length; i = i + 2, j++)
{
tempb[j] = Convert.ToByte(temps.Substring(i, 2), 16);
}
byte[] send = new byte[j];
Array.Copy(tempb, send, j);
return send;
}
}
public class Light
{
public static string defaultColor = "green";
public static byte defaultR = 0;
public static byte defaultG = 50;
public static byte defaultB = 0;
public static Light DefaultLight(int dmxId, int index)
{
return new Light(dmxId, index, defaultR, defaultG, defaultB, 200);
}
public static Light GetLight(int dmxId, int index, string color = "green")
{
return GetLight(dmxId, index, color, 50);
}
public static Light[] GetLights(int dmxId, string color, params int[] indexes)
{
List<Light> lights = new List<Light>();
foreach (int index in indexes)
{
lights.Add(GetLight(dmxId, index, color));
}
return lights.ToArray();
}
public static Light GetLight(int dmxId, int index, string color, byte bright)
{
color = color.ToLower();
switch (color)
{
case "green":
return GreenLight(dmxId, index, bright);
case "red":
return RedLight(dmxId, index, bright);
case "yellow":
return YellowLight(dmxId, index, bright);
case "blue":
return BlueLight(dmxId, index, bright);
}
return DefaultLight(dmxId, index);
}
public static Light RedLight(int dmxId, int index, byte bright = 50)
{
return new Light(dmxId, index, bright, 0, 0);
}
public static Light YellowLight(int dmxId, int index, byte bright = 50)
{
return new Light(dmxId, index, bright, bright, 0);
}
public static Light BlueLight(int dmxId, int index, byte bright = 50)
{
return new Light(dmxId, index, 0, 0, bright);
}
public static Light GreenLight(int dmxId, int index, byte bright = 50)
{
return new Light(dmxId, index, 0, bright, 0);
}
public static Light CyanLight(int dmxId, int index)
{
return new Light(dmxId, index, 0, 255, 255);
}
public static Light ChocolateLight(int dmxId, int index)
{
return new Light(dmxId, index, 210, 105, 30);
}
public Light(int dmxId, int index, byte Red, byte Green, byte Blue, byte lightValue = 200)
{
this.dmx = dmxId;
this.index = index;
this.Red = Red;
this.Green = Green;
this.Blue = Blue;
this.lightValue = lightValue;
}
public int index { get; set; }
public byte Red { get; set; }
public byte Green { get; set; }
public byte Blue { get; set; }
/// <summary>
/// 单色灯
/// </summary>
public static Light[] GetLights(int dmx, List<int> leds, byte light = 200)
{
Light[] lights = new Light[leds.Count];
int i = 0;
foreach (int led in leds)
{
lights[i] = new Light(dmx, led, light);
i++;
}
return lights;
}
/// <summary>
/// 单色灯
/// </summary>
/// <param name="dmx">区域ID</param>
/// <param name="index">索引号</param>
/// <param name="lightValue">亮度</param>
public Light(int dmx, int index, byte lightValue)
{
this.index = index;
this.dmx = dmx;
this.lightValue = lightValue;
}
public int dmx = -1;
public byte lightValue = LEDManager.DefaultLight;
}
}