/vvvv45/addonpack/src/nodes/plugins/Devices/TodoMap/Lib/Modules/Osc/TodoOscDevice.cs

https://github.com/woeishi/vvvv-sdk · C# · 282 lines · 238 code · 44 blank · 0 comment · 38 complexity · 59e4a0b3b166f8043cfe13c28b4d644c MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Threading;
  5. using VVVV.Utils.OSC;
  6. namespace VVVV.TodoMap.Lib.Modules.Osc
  7. {
  8. public enum eTodoOscStatus { Idle,Started,Error }
  9. public delegate void OscStatusChangedDelegate(eTodoOscStatus status);
  10. public delegate void OscOutputStatusChangedDelegate(bool enabled);
  11. public delegate void OscReceivedDelegate(OSCMessage msg);
  12. public class TodoOscDevice : AbstractTodoDevice<TodoOscInput>
  13. {
  14. private int localport = 6666;
  15. private int remoteport = 7777;
  16. private string remoteip = "255.255.255.255";
  17. private Thread thr;
  18. private eTodoOscStatus inputStatus = eTodoOscStatus.Idle;
  19. private bool enableOutput = false;
  20. private OSCReceiver receiver;
  21. public bool AutoStartInput { get; set; }
  22. public bool AutoStartOutput { get; set; }
  23. public event OscStatusChangedDelegate OscInputStatusChanged;
  24. public event OscOutputStatusChangedDelegate OscOutputStatusChanged;
  25. public event OscReceivedDelegate OscDataReceived;
  26. private void ChangeInputStatus(eTodoOscStatus status)
  27. {
  28. if (this.OscInputStatusChanged != null)
  29. {
  30. this.OscInputStatusChanged(status);
  31. }
  32. }
  33. public TodoOscDevice(TodoEngine engine) : base(engine)
  34. {
  35. }
  36. public int LocalPort
  37. {
  38. get { return this.localport; }
  39. set
  40. {
  41. if (this.inputStatus != eTodoOscStatus.Started)
  42. {
  43. this.localport = value;
  44. }
  45. }
  46. }
  47. public int RemotePort
  48. {
  49. get { return this.remoteport; }
  50. set
  51. {
  52. if (!this.enableOutput)
  53. {
  54. this.remoteport = value;
  55. }
  56. }
  57. }
  58. public eTodoOscStatus LocalStatus
  59. {
  60. get { return this.inputStatus; }
  61. }
  62. public bool RemoteEnabled
  63. {
  64. get { return this.enableOutput; }
  65. }
  66. public List<string> IgnoreList
  67. {
  68. get;
  69. set;
  70. }
  71. #region Enable/Disable
  72. public void SetEnabled(bool enabled)
  73. {
  74. if (enabled)
  75. {
  76. if (this.inputStatus == eTodoOscStatus.Idle || this.inputStatus == eTodoOscStatus.Error && enabled)
  77. {
  78. try
  79. {
  80. this.receiver = new OSCReceiver(this.localport);
  81. this.Start();
  82. this.inputStatus = eTodoOscStatus.Started;
  83. this.ChangeInputStatus(this.inputStatus);
  84. }
  85. catch
  86. {
  87. this.inputStatus = eTodoOscStatus.Error;
  88. this.ChangeInputStatus(this.inputStatus);
  89. }
  90. }
  91. }
  92. else
  93. {
  94. if (this.inputStatus == eTodoOscStatus.Started)
  95. {
  96. this.Stop();
  97. this.inputStatus = eTodoOscStatus.Idle;
  98. this.ChangeInputStatus(this.inputStatus);
  99. }
  100. }
  101. }
  102. public void SetOutputEnabled(bool enabled)
  103. {
  104. if (enabled != this.enableOutput)
  105. {
  106. this.enableOutput = enabled;
  107. if (this.OscOutputStatusChanged != null)
  108. {
  109. this.OscOutputStatusChanged(enabled);
  110. }
  111. }
  112. }
  113. #endregion
  114. private void Start()
  115. {
  116. this.thr = new Thread(new ThreadStart(this.Run));
  117. this.thr.Start();
  118. }
  119. private void Run()
  120. {
  121. while (true)
  122. {
  123. OSCPacket bundle = this.receiver.Receive();
  124. this.ProcessPacket(bundle);
  125. }
  126. }
  127. private void ProcessPacket(OSCPacket packet)
  128. {
  129. if (packet.IsBundle())
  130. {
  131. OSCBundle bundle = packet as OSCBundle;
  132. foreach (object o in bundle.Values)
  133. {
  134. this.ProcessPacket(o as OSCPacket);
  135. }
  136. }
  137. else
  138. {
  139. this.ProcessMessage(packet as OSCMessage);
  140. }
  141. }
  142. private void Stop()
  143. {
  144. try
  145. {
  146. if (thr != null) { thr.Abort(); }
  147. }
  148. catch
  149. {
  150. }
  151. try
  152. {
  153. this.receiver.Close();
  154. }
  155. catch
  156. {
  157. }
  158. }
  159. public void Dispose()
  160. {
  161. this.Stop();
  162. }
  163. private void ProcessMessage(OSCMessage msg)
  164. {
  165. if (!this.IgnoreList.Contains(msg.Address))
  166. {
  167. if (this.engine.LearnMode && this.engine.SelectedVariable != null)
  168. {
  169. TodoOscInput input = null;
  170. bool isnew = false;
  171. bool found = false;
  172. foreach (AbstractTodoInput ainput in this.engine.SelectedVariable.Inputs)
  173. {
  174. if (ainput is TodoOscInput)
  175. {
  176. TodoOscInput osc = ainput as TodoOscInput;
  177. if (osc.Message == msg.Address)
  178. {
  179. input = osc;
  180. found = true;
  181. }
  182. }
  183. }
  184. if (!found)
  185. {
  186. if (this.engine.SelectedInput == null)
  187. {
  188. input = new TodoOscInput(this.engine.SelectedVariable);
  189. this.inputvars.Add(input);
  190. isnew = true;
  191. }
  192. else
  193. {
  194. if (this.engine.SelectedInput is TodoOscInput)
  195. {
  196. input = (TodoOscInput)this.engine.SelectedInput;
  197. }
  198. else
  199. {
  200. input = new TodoOscInput(this.engine.SelectedVariable);
  201. this.inputvars.Add(input);
  202. isnew = true;
  203. }
  204. }
  205. }
  206. this.engine.SelectInput(input);
  207. input.Message = msg.Address;
  208. this.engine.VarriableMappingAdded(input, isnew);
  209. }
  210. if (!this.engine.LearnMode)
  211. {
  212. foreach (TodoOscInput toi in this.inputvars)
  213. {
  214. if (toi.Message == msg.Address)
  215. {
  216. double dblval = Convert.ToDouble(msg.Values[0]);
  217. toi.UpdateValue(dblval);
  218. }
  219. }
  220. }
  221. }
  222. if (this.OscDataReceived != null)
  223. {
  224. this.OscDataReceived(msg);
  225. }
  226. }
  227. protected override void DoFeedBack(TodoVariable var, TodoOscInput source)
  228. {
  229. if (this.enableOutput)
  230. {
  231. OSCMessage msg = new OSCMessage(source.Message);
  232. msg.Append(Convert.ToSingle(var.Value));
  233. OSCTransmitter tr = new OSCTransmitter(this.remoteip, this.remoteport);
  234. tr.Send(msg);
  235. }
  236. }
  237. }
  238. }