-
Notifications
You must be signed in to change notification settings - Fork 0
/
LEDServer.cs
172 lines (144 loc) · 5.18 KB
/
LEDServer.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
//+--------------------------------------------------------------------------
//
// NightDriver.Net - (c) 2019 Dave Plummer. All Rights Reserved.
//
// File: LERServer.cs
//
// Description:
//
// A server object that can load, start, and stop the LightStrips
//
// History: Dec-23-2023 Davepl Created
//
//---------------------------------------------------------------------------
using Newtonsoft.Json;
namespace NightDriver
{
internal class LEDServer
{
public bool IsRunning
{
get;
private set;
} = false;
public Statistics Stats = new Statistics();
// Main
//
// Application main loop - starts worker threads
private List<Site> SampleLocations = new List<Site>
{
new Banner() { FramesPerSecond = 60 },
new Tree() { FramesPerSecond = 28 },
new CeilingStrip { FramesPerSecond = 30 },
new Cabana() { FramesPerSecond = 20 },
new TV() { FramesPerSecond = 20 },
new ShopCupboards() { FramesPerSecond = 20 },
new ShopSouthWindows1() { FramesPerSecond = 2 },
new ShopSouthWindows2() { FramesPerSecond = 2 },
new ShopSouthWindows3() { FramesPerSecond = 2 },
};
internal class _Installation
{
public List<LightStrip> _AllStrips { get; set; }
public Dictionary<string, Site> _AllSites { get; set; }
};
public List<LightStrip> AllStrips = new List<LightStrip>();
public Dictionary<string, Site> AllSites = new Dictionary<string, Site>();
internal bool SaveStrips()
{
try
{
var settings = new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
};
string jsonString = JsonConvert.SerializeObject(AllSites, settings);
File.WriteAllText("AllStrips.json", jsonString); // Write to a file
return true;
}
catch (Exception ex)
{
Console.WriteLine($"Error during serialization: {ex.Message}");
return false;
}
}
public bool LoadStrips()
{
try
{
string jsonString = File.ReadAllText("AllStrips.json");
var settings = new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto,
PreserveReferencesHandling = PreserveReferencesHandling.Objects,
ObjectCreationHandling = ObjectCreationHandling.Replace
};
AllSites = JsonConvert.DeserializeObject<Dictionary<string, Site>>(jsonString, settings);
}
catch (Exception ex)
{
Console.WriteLine($"Error during deserialization: {ex.Message}");
return false;
}
// Go through each of the Site objects and see if we have a matching Site already
// in the AllSites dictionary. If not, add it, and if so, add the strips to the
// existing site.
foreach (var site in AllSites.Values)
{
foreach (var strip in site.LightStrips)
{
strip.StripSite = site;
AllStrips.Add(strip);
}
}
return true;
}
internal void LoadStripsFromTable()
{
AllSites.Clear();
AllStrips.Clear();
foreach (var location in SampleLocations)
{
AllSites[location.Name] = location;
foreach (var strip in location.LightStrips)
{
strip.StripSite = location;
AllStrips.Add(strip);
}
}
}
private void StartCommunications()
{
foreach (var location in AllSites.Values)
foreach (var strip in location.LightStrips)
strip.Start();
}
private void StopCommunications()
{
foreach (var location in AllSites.Values)
foreach (var strip in location.LightStrips)
strip.Stop();
}
internal void Start(CancellationToken token)
{
IsRunning = true;
foreach (var location in AllSites.Values)
location.StartWorkerThread(token);
StartCommunications();
}
internal void Stop(CancellationTokenSource cancelSource)
{
cancelSource.Cancel();
StopCommunications();
IsRunning = false;
}
public void RemoveStrip(LightStrip strip)
{
strip.Stop();
var siteContainingStrip = AllSites.Values.FirstOrDefault(site => site.LightStrips.Contains(strip));
siteContainingStrip.LightStrips.Remove(strip);
AllStrips.Remove(strip);
}
}
}