2 using System.Threading;
3 using DirectOutput.Cab.Out.DMX.ArtnetEngine;
4 using System.Xml.Serialization;
7 namespace DirectOutput.Cab.Out.DMX
24 private byte[] DMXData =
new byte[512];
25 private int LastDMXChannel = 0;
26 private bool UpdateRequired =
true;
27 private object UpdateLocker =
new object();
29 private short _Universe = 0;
39 get {
return _Universe; }
40 set { _Universe = value; }
43 private string _BroadcastAddress=
"";
52 public string BroadcastAddress
54 get {
return _BroadcastAddress; }
55 set { _BroadcastAddress = value; }
63 private void AddOutputs()
65 for (
int i = 1; i <= 512; i++)
67 if (!Outputs.Any(x => ((
DMXOutput)x).DmxChannel == i))
69 Outputs.Add(
new DMXOutput() { Name =
"{0}.{1:000}".Build(Name, i), DmxChannel=i });
85 throw new Exception(
"The OutputValueChanged event handler for ArtNet node {0} (controlling Dmx universe {1}) has been called by a sender which is not a DmxOutput.".Build(Name, Universe));
88 DMXOutput O = (DMXOutput)Output;
92 Log.
Exception(
"ArtNet node {0} has received a update for a illegal dmx channel number ({1}).".Build(Name, O.
DmxChannel));
93 throw new ArgumentOutOfRangeException(
"ArtNet node {0} has received a update for a illegal dmx channel number ({1}).".Build(Name, O.
DmxChannel));
106 UpdateRequired =
true;
117 public override void Update()
121 UpdaterThreadSignal();
137 Log.
Write(
"ArtNet node {0} (controlling universe {1}) initialized and updater thread started.".Build(Name,Universe));
143 public override void Finish()
145 FinishUpdaterThread();
146 Log.
Write(
"ArtNet node {0} (controlling universe {1}) finished and updater thread stopped.".Build(Name, Universe));
152 #region UpdaterThread
157 private void InitUpdaterThread()
160 if (!UpdaterThreadIsActive)
162 KeepUpdaterThreadAlive =
true;
165 UpdaterThread =
new Thread(UpdaterThreadDoIt);
166 UpdaterThread.Name =
"ArtNet node {0} updater thread ".Build(Name);
167 UpdaterThread.Start();
171 Log.
Exception(
"Artnet node {0} updater thread could not start.".Build(Name), E);
172 throw new Exception(
"Artnet node {0} updater thread could not start.".Build(Name), E);
181 private void FinishUpdaterThread()
183 if (UpdaterThread != null)
187 KeepUpdaterThreadAlive =
false;
188 lock (UpdaterThreadLocker)
190 Monitor.Pulse(UpdaterThreadLocker);
192 if (!UpdaterThread.Join(1000))
194 UpdaterThread.Abort();
196 UpdaterThread = null;
200 Log.Exception(
"A error occured during termination of ArtNet updater thread.", E);
201 throw new Exception(
"A error occured during termination of ArtNet updater thread.", E);
210 public bool UpdaterThreadIsActive
214 if (UpdaterThread != null)
216 if (UpdaterThread.IsAlive)
228 private void UpdaterThreadSignal()
231 lock (UpdaterThreadLocker)
233 Monitor.Pulse(UpdaterThreadLocker);
238 private Thread UpdaterThread {
get;
set; }
239 private object UpdaterThreadLocker =
new object();
240 private bool KeepUpdaterThreadAlive =
true;
248 private void UpdaterThreadDoIt()
259 while (KeepUpdaterThreadAlive)
266 UpdateRequired =
false;
267 Engine.
SendDMX(BroadcastAddress, Universe, DMXData, ((LastDMXChannel | 1) + 1).Limit(2, 512));
272 if (KeepUpdaterThreadAlive)
274 lock (UpdaterThreadLocker)
276 while (UpdateRequired ==
false && KeepUpdaterThreadAlive)
278 Monitor.Wait(UpdaterThreadLocker, 50);