PageRenderTime 25ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/MedSpeech/MedicalDictation/DictationManager/DictationManager.cs

http://4chan.codeplex.com
C# | 304 lines | 273 code | 30 blank | 1 comment | 16 complexity | ba6313059fa2685e6e4036bd70744715 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using AudioCtrl;
  6. using USBDeviceCtrl;
  7. using System.IO;
  8. using System.Configuration;
  9. namespace DictationManager
  10. {
  11. public class CoreManager
  12. {
  13. AudioLib _audio;
  14. PhilipsManager _device;
  15. bool _dictationMode;
  16. bool success;
  17. StreamWriter _logsFile;
  18. String _logPath;
  19. public delegate void LoadingHandler(object sender, LoadArgs args);
  20. public event LoadingHandler dictationLoaded;
  21. public delegate void ConnectionHandler(object sender, LoadArgs args);
  22. public event LoadingHandler deviceConnection;
  23. public delegate void EOLHandler(object sender, LoadArgs args);
  24. public event EOLHandler EOLButtonPressed;
  25. public delegate void NoFileFoundHandler(object sender, FileArgs args);
  26. public event NoFileFoundHandler fileNotFound;
  27. public CoreManager()
  28. {
  29. InstanceLogs();
  30. WriteInLogs("Initializing Program");
  31. _dictationMode = true;
  32. try
  33. {
  34. WriteInLogs("Initializing Controllers");
  35. _audio = new AudioLib();
  36. _device = new PhilipsManager();
  37. _device.eventButton += new PhilipsManager.ButtonHandler(_device_eventButton);
  38. _device.deviceConnected += new PhilipsManager.ConnectionHandler(_device_deviceConnected);
  39. if (_audio.recordMode.Equals("Insert"))
  40. _device.RecordModeLight(0);
  41. else
  42. _device.RecordModeLight(1);
  43. }
  44. catch (Exception ex)
  45. {
  46. WriteInLogs("Exception loading Controllers \n" + ex.Message);
  47. throw (ex);
  48. }
  49. }
  50. void _device_deviceConnected(object sender, buttonArgs args)
  51. {
  52. WriteInLogs("A device found");
  53. deviceConnection(this, new LoadArgs(0));
  54. }
  55. void _device_eventButton(object sender, buttonArgs args)
  56. {
  57. WriteInLogs("Button " + args.button + " pressed since device");
  58. switch (args.button)
  59. {
  60. case ("Play"):
  61. EventButton("Play", "Pressed");
  62. break;
  63. case ("PlayStopToggle"):
  64. EventButton("Play", "Pressed");
  65. break;
  66. case ("Record"):
  67. EventButton("Record", "Pressed");
  68. break;
  69. case ("Stop"):
  70. EventButton("Stop", "Pressed");
  71. break;
  72. case ("Insert"):
  73. EventButton("Insert", "Pressed");
  74. break;
  75. case ("EOL"):
  76. EventButton("EOL", "Pressed");
  77. break;
  78. case ("FForwardPressed"):
  79. EventButton("FForward", "Pressed");
  80. break;
  81. case ("FRewindPressed"):
  82. EventButton("FRewind", "Pressed");
  83. break;
  84. case ("FForwardReleased"):
  85. EventButton("FForward", "Released");
  86. break;
  87. case ("FRewindReleased"):
  88. EventButton("FRewind", "Released");
  89. break;
  90. }
  91. }
  92. public void LoadDictation(String[] parameters, bool createNew)
  93. {
  94. WriteInLogs("Loading dictation : \nFilePath: " + parameters[0] + "\nDictation Mode: " + parameters[1] + "\nPID: " + parameters[2] + "\nPatient Name: " + parameters[3] + "\nAccession Num: " + parameters[4] + "\nStudy: " + parameters[5] + "\nRadiologist: " + parameters[6] + "\nDateTime: " + parameters[7]);
  95. success = true;
  96. _dictationMode = bool.Equals(parameters[1], "0");
  97. if (createNew)
  98. {
  99. WriteInLogs("Creating new dictation");
  100. _audio.CreateFile(parameters, false);
  101. }
  102. else
  103. {
  104. WriteInLogs("Loading already exist dictation");
  105. success = _audio.LoadFile(parameters[0], false);
  106. }
  107. if (success)
  108. {
  109. WriteInLogs("Successfull process");
  110. dictationLoaded(this, new LoadArgs(_audio.length));
  111. }
  112. else
  113. {
  114. WriteInLogs("Trouble was detected loading dictation (file not found)");
  115. fileNotFound(this, new FileArgs(parameters[0]));
  116. }
  117. }
  118. public bool SaveDictation()
  119. {
  120. WriteInLogs("Saving dictation");
  121. bool result;
  122. _audio.Stop();
  123. result = _audio.SaveNewFile();
  124. WriteInLogs("Saving success : " + result);
  125. return result;
  126. }
  127. public void EventButton(String button, String buttonEvent)
  128. {
  129. WriteInLogs("Executing " + button + " " + buttonEvent);
  130. if (success)
  131. {
  132. if (buttonEvent == "Pressed")
  133. {
  134. _device.LightsOff();
  135. switch (button)
  136. {
  137. case "Play":
  138. _audio.Play();
  139. _device.GreenLight(_audio.IsPlaying);
  140. break;
  141. case "Stop":
  142. _audio.Stop();
  143. break;
  144. case "Record":
  145. if (_dictationMode)
  146. {
  147. _audio.Record();
  148. _device.RedLight(_audio.IsRecording);
  149. }
  150. break;
  151. case "FForward":
  152. _audio.FForward();
  153. break;
  154. case "FRewind":
  155. _audio.FRewind();
  156. break;
  157. case "EOL":
  158. EOLButtonPressed(this, new LoadArgs(0));
  159. break;
  160. case "Insert":
  161. {
  162. _audio.setRecordMode();
  163. if (_audio.recordMode.Equals("Insert"))
  164. _device.RecordModeLight(0);
  165. else
  166. _device.RecordModeLight(1);
  167. }
  168. break;
  169. }
  170. }
  171. if (buttonEvent == "Released")
  172. {
  173. switch (button)
  174. {
  175. case "FForward":
  176. _audio.Stop();
  177. break;
  178. case "FRewind":
  179. _audio.Stop();
  180. break;
  181. }
  182. }
  183. }
  184. }
  185. private void InstanceLogs()
  186. {
  187. //ActivateLogs
  188. if (ConfigurationManager.AppSettings["ActivateLogs"].ToString().ToUpper().Equals("TRUE"))
  189. {
  190. _logPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\MedSpeech\MedSpeech " + DateTime.Today.ToString("yyyy MM dd") + ".log";
  191. string onlyPath = _logPath.Substring(0, _logPath.LastIndexOf('\\'));
  192. if (!Directory.Exists(onlyPath))
  193. Directory.CreateDirectory(onlyPath);
  194. _logsFile = new StreamWriter(_logPath, true);
  195. }
  196. }
  197. private void WriteInLogs(string message)
  198. {
  199. if (ConfigurationManager.AppSettings["ActivateLogs"].ToString().ToUpper().Equals("TRUE"))
  200. _logsFile.WriteLine(DateTime.Now.ToString("HH:mm:ss") + " " + message);
  201. }
  202. public int currentPosition
  203. {
  204. get { return _audio.position; }
  205. }
  206. public int fileLength
  207. {
  208. get { return _audio.length; }
  209. }
  210. public int outputVolume
  211. {
  212. get { return _audio.SpeakerVolume; }
  213. set { _audio.SpeakerVolume = value; }
  214. }
  215. public int inputVolume
  216. {
  217. get { return _audio.MicVolume; }
  218. set { _audio.MicVolume = value; }
  219. }
  220. public bool ExistPreviousDictation
  221. {
  222. get { return _audio.fileLoaded; }
  223. }
  224. public string FileTargetPath
  225. {
  226. get { return _audio.targetFilePath; }
  227. }
  228. public void DiscardDictation()
  229. {
  230. WriteInLogs("Discarting previous dictation");
  231. _audio.CloseFile();
  232. }
  233. public void FreeComponents()
  234. {
  235. WriteInLogs("Free resources");
  236. _device.OffLights();
  237. _device.FreeManager();
  238. WriteInLogs("Closing logs");
  239. _logsFile.Close();
  240. }
  241. public void GoToStart()
  242. {
  243. _audio.GoToStart();
  244. }
  245. public bool IsDeviceRecordConnected()
  246. {
  247. return _device.IsRecordConnected;
  248. }
  249. }
  250. public class LoadArgs
  251. {
  252. int _length;
  253. public LoadArgs(int length)
  254. {
  255. _length = length;
  256. }
  257. public int length
  258. {
  259. get { return _length; }
  260. }
  261. }
  262. public class FileArgs
  263. {
  264. string audioFileName;
  265. public FileArgs(string _fileName)
  266. {
  267. audioFileName = _fileName;
  268. }
  269. public string fileName
  270. {
  271. get { return audioFileName; }
  272. }
  273. }
  274. }