FUSIONOptBridge.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. 
  2. using System;
  3. using System.Collections.ObjectModel;
  4. using System.Collections.Generic;
  5. //using System.Diagnostics;
  6. using Wayne.Lib;
  7. using Wayne.ForecourtControl.Fusion;
  8. //using DWItaly.Sinp.Utility;
  9. namespace Wayne.ForecourtControl.OptBridge.Fusion
  10. {
  11. public enum OPTManagedBy
  12. {
  13. SINP = 1,
  14. Fusion = 2
  15. }
  16. internal class FUSIONOptBridge : IOptBridge, IConnectable, IIdentifiableEntity, IDisposable
  17. {
  18. // Fields
  19. private int clientId;
  20. private string clientName;
  21. private DeviceConnectionState connectionState;
  22. private int id;
  23. public FUSIONManager manager;
  24. private List<IOpt> optList = new List<IOpt>();
  25. private IIdentifiableEntity parentEntity;
  26. // Events
  27. public event EventHandler OnConfigurationChanged;
  28. public event EventHandler<ConnectionChangedEventArgs> OnConnectionStateChange;
  29. // Methods
  30. public FUSIONOptBridge(int id, IIdentifiableEntity parentEntity)
  31. {
  32. this.id = id;
  33. this.parentEntity = parentEntity;
  34. try
  35. {
  36. Trace.Add(this);
  37. if (!FUSIONFactory.fusionForecourtControlList.ContainsKey(id))
  38. {
  39. Trace.WriteLine(string.Format("fusionForecourtControl id={0} NOT exist - creating", id));
  40. Wayne.ForecourtControl.Fusion.FUSIONFactory.CreateForecourtControl(id);
  41. }
  42. if (FUSIONFactory.fusionForecourtControlList.ContainsKey(id))
  43. {
  44. Trace.WriteLine(string.Format("fusionForecourtControl id={0} adding event OnConnectionStateChange", id));
  45. manager = ((FUSIONForecourtControl)(FUSIONFactory.fusionForecourtControlList[id])).manager;
  46. this.manager.ifsfManager.clientSocket.OnConnectionStateChange += new EventHandler<ConnectionChangedEventArgs>(clientSocket_OnConnectionStateChange); //new EventHandler<ConnectionChangedEventArgs>(this, (IntPtr)this.clientSocket_OnConnectionStateChange);
  47. }
  48. else
  49. Trace.WriteLine(string.Format("fusionForecourtControl id={0} NOT exist - error!!!", id));
  50. }
  51. catch (Exception ex)
  52. {
  53. Trace.WriteLine(string.Format("Exception creating FUSIONOptBridge ! '{0}'", ex.Message + " - " + ex.StackTrace));
  54. }
  55. }
  56. public void AddOptAsync(int optId, EventHandler<AsyncCompletedEventArgs> requestCompleted, object userToken)
  57. {
  58. //this.SetTerminalModeAsync(optId, NfsTerminalMode.Configured, requestCompleted, userToken);
  59. string sValue = "";
  60. IOpt opt = null;
  61. try
  62. {
  63. opt = this.GetOptFromId(optId);
  64. if (opt == null)
  65. {
  66. opt = new FUSIONOpt(optId, this);
  67. this.WritableOptList.Add(opt);
  68. }
  69. }
  70. catch (Exception ex)
  71. {
  72. Trace.WriteLine(string.Format("Exception adding opt ! '{0}'", ex.Message + " - " + ex.StackTrace));
  73. }
  74. try
  75. {
  76. sValue = IniFile.IniReadValue(ConfigurationParams.inifile, "OPT" + optId.ToString(), "Port");
  77. if (sValue.Length > 0)
  78. {
  79. //sValue = sValue.ToUpper().Replace("COM", "");
  80. ((FUSIONOpt)opt).COMPort = sValue;
  81. Trace.WriteLine(string.Format("port={0}", ((FUSIONOpt)opt).COMPort));
  82. sValue = IniFile.IniReadValue(ConfigurationParams.inifile, "OPT" + optId.ToString(), "BaudRate");
  83. if (sValue.Length > 0)
  84. ((FUSIONOpt)opt).baudRate = Convert.ToInt16(sValue);
  85. Trace.WriteLine(string.Format("baudRate={0}", ((FUSIONOpt)opt).baudRate));
  86. sValue = IniFile.IniReadValue(ConfigurationParams.inifile, "OPT" + optId.ToString(), "DataBit");
  87. if (sValue.Length > 0)
  88. ((FUSIONOpt)opt).dataBit = Convert.ToInt16(sValue);
  89. Trace.WriteLine(string.Format("dataBit={0}", ((FUSIONOpt)opt).dataBit));
  90. sValue = IniFile.IniReadValue(ConfigurationParams.inifile, "OPT" + optId.ToString(), "StopBit");
  91. if (sValue.Length > 0)
  92. ((FUSIONOpt)opt).stopBit = Convert.ToInt16(sValue);
  93. Trace.WriteLine(string.Format("stopBit={0}", ((FUSIONOpt)opt).stopBit));
  94. sValue = IniFile.IniReadValue(ConfigurationParams.inifile, "OPT" + optId.ToString(), "Parity");
  95. if (sValue.Length > 0)
  96. ((FUSIONOpt)opt).parity = Convert.ToInt16(sValue);
  97. Trace.WriteLine(string.Format("parity={0}", ((FUSIONOpt)opt).parity));
  98. ((FUSIONOpt)opt).managedBy = OPTManagedBy.Fusion;
  99. if (this.manager == null || this.manager.ifsfManager == null)
  100. Trace.WriteLine((this.manager == null) ? "this.manager == null" : "this.manager.ifsfManager == null");
  101. else
  102. {
  103. Trace.WriteLine(string.Format("OptAddSerialPort calling"));
  104. this.manager.ifsfManager.OptAddSerialPort(optId, ((FUSIONOpt)opt).COMPort, ((FUSIONOpt)opt).baudRate, ((FUSIONOpt)opt).dataBit, ((FUSIONOpt)opt).stopBit, ((FUSIONOpt)opt).parity, requestCompleted, userToken, this);
  105. }
  106. Trace.WriteLine(string.Format("OptAddSerialPort called"));
  107. }
  108. else
  109. {
  110. sValue = IniFile.IniReadValue(ConfigurationParams.inifile, "OPT" + optId.ToString(), "IPAddress");
  111. Trace.WriteLine(string.Format("IPAddress={0}", sValue));
  112. if (sValue.Length > 0)
  113. {
  114. ((FUSIONOpt)opt).address = sValue;
  115. sValue = IniFile.IniReadValue(ConfigurationParams.inifile, "OPT" + optId.ToString(), "IPPort");
  116. if (sValue.Length > 0)
  117. ((FUSIONOpt)opt).IPPort = Convert.ToInt16(sValue);
  118. Trace.WriteLine(string.Format("port={0}", ((FUSIONOpt)opt).IPPort));
  119. sValue = IniFile.IniReadValue(ConfigurationParams.inifile, "OPT" + optId.ToString(), "ManagedBy");
  120. if (sValue.Length == 0 || sValue.ToUpper() == "FUSION")
  121. ((FUSIONOpt)opt).managedBy = OPTManagedBy.Fusion;
  122. else
  123. ((FUSIONOpt)opt).managedBy = OPTManagedBy.SINP;
  124. Trace.WriteLine(string.Format("managedBy={0}", sValue));
  125. if (((FUSIONOpt)opt).managedBy == OPTManagedBy.Fusion && manager != null && manager.ifsfManager != null)
  126. this.manager.ifsfManager.OptAddTCP(optId, ((FUSIONOpt)opt).address, ((FUSIONOpt)opt).IPPort, requestCompleted, userToken, this);
  127. //OPTTCP
  128. else
  129. {
  130. // TODO sistemare
  131. if (((FUSIONOpt)opt).optTCP == null)
  132. {
  133. ((FUSIONOpt)opt).optTCP = new OptTCP(optId, ((FUSIONOpt)opt).address, ((FUSIONOpt)opt).IPPort, ((FUSIONOpt)opt));
  134. }
  135. // send request completed ...
  136. requestCompleted.Invoke(this, new AsyncCompletedEventArgs(true, userToken));
  137. ((FUSIONOpt)opt).optTCP.ConnectionState = DeviceConnectionState.Disconnected;
  138. ((FUSIONOpt)opt).optTCP.Connect();
  139. }
  140. }
  141. }
  142. if (((FUSIONOpt)opt).managedBy == OPTManagedBy.Fusion && manager != null && manager.ifsfManager != null)
  143. manager.ifsfManager.GetDeviceState(Wayne.FDCPOSLibrary.DeviceType.DT_OutdoorPaymentTerminal, optId, null, null, null);
  144. Trace.WriteLine(string.Format("GetDeviceState called"));
  145. }
  146. catch (Exception ex)
  147. {
  148. Trace.WriteLine(string.Format("Exception ! '{0}'", ex.Message + " - " + ex.StackTrace));
  149. }
  150. }
  151. public void AddOptAsync(int optId, string port, int baudRate, int dataBit, int stopBit, int parity, EventHandler<AsyncCompletedEventArgs> requestCompleted, object userToken)
  152. {
  153. //this.SetTerminalModeAsync(optId, NfsTerminalMode.Configured, requestCompleted, userToken);
  154. IOpt opt = this.GetOptFromId(optId);
  155. if (opt == null)
  156. {
  157. opt = new FUSIONOpt(optId, this);
  158. this.WritableOptList.Add(opt);
  159. }
  160. this.manager.ifsfManager.OptAddSerialPort(optId, port, baudRate, dataBit, stopBit, parity, requestCompleted, userToken, this);
  161. manager.ifsfManager.GetDeviceState(Wayne.FDCPOSLibrary.DeviceType.DT_OutdoorPaymentTerminal, optId, null, null, null);
  162. }
  163. public void AddOptAsync(int optId, string address, int port, EventHandler<AsyncCompletedEventArgs> requestCompleted, object userToken)
  164. {
  165. //this.SetTerminalModeAsync(optId, NfsTerminalMode.Configured, requestCompleted, userToken);
  166. IOpt opt = this.GetOptFromId(optId);
  167. if (opt == null)
  168. {
  169. opt = new FUSIONOpt(optId, this);
  170. this.WritableOptList.Add(opt);
  171. }
  172. if (((FUSIONOpt)opt).managedBy == OPTManagedBy.Fusion && manager != null && manager.ifsfManager != null)
  173. {
  174. this.manager.ifsfManager.OptAddTCP(optId, address, port, requestCompleted, userToken, this);
  175. manager.ifsfManager.GetDeviceState(Wayne.FDCPOSLibrary.DeviceType.DT_OutdoorPaymentTerminal, optId, null, null, null);
  176. }
  177. //OPTTCP
  178. else
  179. {
  180. // TODO sistemare
  181. ((FUSIONOpt)opt).optTCP = new OptTCP(optId, address, port, ((FUSIONOpt)opt));
  182. // send request completed ...
  183. requestCompleted.Invoke(this, new AsyncCompletedEventArgs(true, userToken));
  184. ((FUSIONOpt)opt).optTCP.ConnectionState = DeviceConnectionState.Disconnected;
  185. ((FUSIONOpt)opt).optTCP.Connect();
  186. }
  187. }
  188. public void Connect(string connectionString)
  189. {
  190. //if (this.manager.ifsfManager.clientSocket.mainConnectionState == DeviceConnectionState.Connected)
  191. WritableConnectionState = DeviceConnectionState.Connected;
  192. //else
  193. // this.manager.Connect(connectionString);
  194. }
  195. public void Disconnect()
  196. {
  197. this.manager.Disconnect();
  198. }
  199. public void Dispose()
  200. {
  201. this.Dispose(true);
  202. GC.SuppressFinalize(this);
  203. }
  204. private void Dispose(bool disposing)
  205. {
  206. if (disposing && (this.manager != null))
  207. {
  208. this.manager.Dispose();
  209. }
  210. this.manager = null;
  211. }
  212. ~FUSIONOptBridge()
  213. {
  214. this.Dispose(false);
  215. }
  216. internal void FireOnConfigurationChanged()
  217. {
  218. if (this.OnConfigurationChanged != null)
  219. {
  220. this.OnConfigurationChanged(this, EventArgs.Empty);
  221. }
  222. }
  223. internal void FireOnDataRead(int optId, byte[] data)
  224. {
  225. FUSIONOpt opt = this.GetOptFromId(optId);
  226. if (opt != null)
  227. {
  228. opt.FireOnDataRead(data);
  229. }
  230. }
  231. public void RemoveOptAsync(int optId, EventHandler<AsyncCompletedEventArgs> requestCompleted, object userToken)
  232. {
  233. //this.SetTerminalModeAsync(optId, NfsTerminalMode.NotConfigured, requestCompleted, userToken);
  234. this.manager.ifsfManager.OptRemove(optId, requestCompleted, userToken, this);
  235. }
  236. //private void ReserveCompleted(object sender, AsyncCompletedEventArgs e)
  237. //{
  238. // SetTerminalModeParams userToken = e.UserToken as SetTerminalModeParams;
  239. // if (userToken != null)
  240. // {
  241. // if (e.Success)
  242. // {
  243. // AsyncOperation<ushort> operation = this.manager.AsyncManager.RegisterOperation<AsyncCompletedEventArgs>(this, new EventHandler<AsyncCompletedEventArgs>(this.SetTerminalModeCompleted), userToken);
  244. // Function function = new Function(0x7d3, new Parameter[] { new PrimitiveParameter(0x66, PrimitiveType.UInt16, operation.Id), new PrimitiveParameter(5, PrimitiveType.Byte, userToken.OptId), new PrimitiveParameter(0x25b, PrimitiveType.Byte, NfsTerminalMode.Configured) });
  245. // this.manager.Send(function);
  246. // }
  247. // else if (userToken.RequestCompleted != null)
  248. // {
  249. // userToken.RequestCompleted(this, new AsyncCompletedEventArgs(false, userToken.UserToken));
  250. // }
  251. // }
  252. //}
  253. //private void SetTerminalModeAsync(int optId, NfsTerminalMode terminalMode, EventHandler<AsyncCompletedEventArgs> requestCompleted, object userToken)
  254. //{
  255. // SetTerminalModeParams params = new SetTerminalModeParams(optId, terminalMode, requestCompleted, userToken);
  256. // AsyncOperation<ushort> operation = this.manager.AsyncManager.RegisterOperation<AsyncCompletedEventArgs>(this, new EventHandler<AsyncCompletedEventArgs>(this.ReserveCompleted), params);
  257. // Function function = new Function(0x7d1, new Parameter[] { new PrimitiveParameter(0x66, PrimitiveType.UInt16, operation.Id), new PrimitiveParameter(5, PrimitiveType.Byte, optId) });
  258. // this.manager.Send(function);
  259. //}
  260. //private void SetTerminalModeCompleted(object sender, AsyncCompletedEventArgs e)
  261. //{
  262. // SetTerminalModeParams userToken = e.UserToken as SetTerminalModeParams;
  263. // if (userToken != null)
  264. // {
  265. // AsyncOperation<ushort> operation = this.manager.AsyncManager.RegisterOperation<AsyncCompletedEventArgs>(this, new EventHandler<AsyncCompletedEventArgs>(this.UnreserveCompleted), userToken);
  266. // userToken.Success = e.Success;
  267. // Function function = new Function(0x7d2, new Parameter[] { new PrimitiveParameter(0x66, PrimitiveType.UInt16, operation.Id), new PrimitiveParameter(5, PrimitiveType.Byte, userToken.OptId) });
  268. // this.manager.Send(function);
  269. // }
  270. //}
  271. //private void UnreserveCompleted(object sender, AsyncCompletedEventArgs e)
  272. //{
  273. // SetTerminalModeParams userToken = e.UserToken as SetTerminalModeParams;
  274. // if ((userToken != null) && (userToken.RequestCompleted != null))
  275. // {
  276. // userToken.RequestCompleted(this, new AsyncCompletedEventArgs(userToken.Success, userToken.UserToken));
  277. // }
  278. //}
  279. // Properties
  280. public int ClientId
  281. {
  282. get
  283. {
  284. return this.clientId;
  285. }
  286. }
  287. public string ClientName
  288. {
  289. get
  290. {
  291. return this.clientName;
  292. }
  293. }
  294. public DeviceConnectionState ConnectionState
  295. {
  296. get
  297. {
  298. return this.connectionState;
  299. }
  300. }
  301. public string EntitySubType
  302. {
  303. get
  304. {
  305. return "Nfs";
  306. }
  307. }
  308. public string EntityType
  309. {
  310. get
  311. {
  312. return "OptBridge";
  313. }
  314. }
  315. /// <summary>
  316. /// This is used by the logger and should never be set by implementing classes
  317. /// </summary>
  318. public string FullEntityName { get; set; }
  319. public int Id
  320. {
  321. get
  322. {
  323. return this.id;
  324. }
  325. }
  326. public ReadOnlyCollection<IOpt> Opts
  327. {
  328. get
  329. {
  330. return this.optList.AsReadOnly();
  331. }
  332. }
  333. public IIdentifiableEntity ParentEntity
  334. {
  335. get
  336. {
  337. return this.parentEntity;
  338. }
  339. }
  340. internal int WritableClientId
  341. {
  342. get
  343. {
  344. return this.clientId;
  345. }
  346. set
  347. {
  348. this.clientId = value;
  349. }
  350. }
  351. internal string WritableClientName
  352. {
  353. get
  354. {
  355. return this.clientName;
  356. }
  357. set
  358. {
  359. this.clientName = value;
  360. }
  361. }
  362. internal DeviceConnectionState WritableConnectionState
  363. {
  364. get
  365. {
  366. return this.connectionState;
  367. }
  368. set
  369. {
  370. if (this.connectionState != value)
  371. {
  372. this.connectionState = value;
  373. if (this.OnConnectionStateChange != null)
  374. {
  375. this.OnConnectionStateChange.BeginInvoke(this, new ConnectionChangedEventArgs(this.connectionState), null, null);
  376. }
  377. }
  378. }
  379. }
  380. private void clientSocket_OnConnectionStateChange(object sender, ConnectionChangedEventArgs e)
  381. {
  382. Trace.WriteLine("bridge clientSocket_OnConnectionStateChange ConnectionState=" + e.ConnectionState);
  383. this.WritableConnectionState = e.ConnectionState;
  384. if (e.ConnectionState == DeviceConnectionState.Disconnected)
  385. {
  386. foreach (IOpt opt in Opts)
  387. {
  388. if (((FUSIONOpt)opt).managedBy == OPTManagedBy.Fusion)
  389. ((FUSIONOpt)opt).WritableConnectionState = DeviceConnectionState.Disconnected;
  390. }
  391. }
  392. }
  393. internal List<IOpt> WritableOptList
  394. {
  395. get
  396. {
  397. return this.optList;
  398. }
  399. }
  400. public FUSIONOpt GetOptFromId(int deviceId)
  401. {
  402. foreach (FUSIONOpt opt in this.Opts)
  403. {
  404. if (opt.Id == deviceId)
  405. {
  406. return opt;
  407. }
  408. }
  409. return null;
  410. }
  411. // Nested Types
  412. //private class SetTerminalModeParams
  413. //{
  414. // // Fields
  415. // private int optId;
  416. // private EventHandler<AsyncCompletedEventArgs> requestCompleted;
  417. // private bool success;
  418. // private NfsTerminalMode terminalMode;
  419. // private object userToken;
  420. // // Methods
  421. // public SetTerminalModeParams(int optId, NfsTerminalMode terminalMode, EventHandler<AsyncCompletedEventArgs> requestCompleted, object userToken)
  422. // {
  423. // this.optId = optId;
  424. // this.requestCompleted = requestCompleted;
  425. // this.userToken = userToken;
  426. // this.terminalMode = terminalMode;
  427. // }
  428. // // Properties
  429. // public int OptId
  430. // {
  431. // get
  432. // {
  433. // return this.optId;
  434. // }
  435. // }
  436. // public EventHandler<AsyncCompletedEventArgs> RequestCompleted
  437. // {
  438. // get
  439. // {
  440. // return this.requestCompleted;
  441. // }
  442. // }
  443. // public bool Success
  444. // {
  445. // get
  446. // {
  447. // return this.success;
  448. // }
  449. // set
  450. // {
  451. // this.success = value;
  452. // }
  453. // }
  454. // public NfsTerminalMode TerminalMode
  455. // {
  456. // get
  457. // {
  458. // return this.terminalMode;
  459. // }
  460. // }
  461. // public object UserToken
  462. // {
  463. // get
  464. // {
  465. // return this.userToken;
  466. // }
  467. // }
  468. //}
  469. }
  470. }