FUSIONOptBridge.cs 20 KB

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