PLCModuleLogic.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*******************************************************************************
  2. * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:PLCModuleLogic.cs
  5. * 2.功能描述:发送信息到智能设备。
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 陈晓野 2018/10/18 1.00 新建
  9. *******************************************************************************/
  10. using System;
  11. using System.Data;
  12. using System.Net.Sockets;
  13. using System.Text;
  14. using Curtain.DataAccess;
  15. using Dongke.IBOSS.PRD.Basics.DataAccess;
  16. using Dongke.IBOSS.PRD.Service.DataModels;
  17. using Dongke.IBOSS.PRD.WCF.DataModels;
  18. using Oracle.ManagedDataAccess.Client;
  19. using PCLCommunication;
  20. using DataBaseType = Dongke.IBOSS.PRD.Basics.DataAccess.DataBaseType;
  21. namespace Dongke.IBOSS.PRD.Service.SmartDeviceService
  22. {
  23. /// <summary>
  24. /// PLC相关处理
  25. /// </summary>
  26. public class PLCModuleLogic
  27. {
  28. #region 坐便盖板
  29. /// <summary>
  30. /// 发送产品型号到PLC(坐便盖板)-恒洁三水
  31. /// </summary>
  32. /// <param name="barcode"></param>
  33. /// <param name="sUserInfo"></param>
  34. /// <returns></returns>
  35. public static int SendGoodsCodeToPLCBySeatCover(string barcode, SUserInfo sUserInfo)
  36. {
  37. //filePath = INIUtility.Instance(INIUtility.IniFile.Config).ReadIniData("PathSetting", filePath);
  38. IDBConnection con = ClsDbFactory.CreateDBConnection(DataBaseType.ORACLE, DataManager.ConnectionString);
  39. try
  40. {
  41. con.Open();
  42. string sqlString = "select u.plcid, p.ip, p.port,p.PLCDescription from tp_mst_user u " +
  43. "inner join tp_mst_plc p on p.plcid = u.plcid where u.userid = :userid and p.valueflag='1'";
  44. OracleParameter[] paras = new OracleParameter[]
  45. {
  46. new OracleParameter(":userid",OracleDbType.Int32, sUserInfo.UserID,ParameterDirection.Input),
  47. };
  48. DataTable plc = con.GetSqlResultToDt(sqlString, paras);
  49. if (plc == null || plc.Rows.Count == 0)
  50. {
  51. return 4;
  52. }
  53. string ip = plc.Rows[0]["ip"].ToString();
  54. int port = Convert.ToInt32(plc.Rows[0]["port"]);
  55. string dec = plc.Rows[0]["PLCDescription"].ToString();
  56. if (string.IsNullOrWhiteSpace(ip) || port == 0 || string.IsNullOrWhiteSpace(dec))
  57. {
  58. return 5;
  59. }
  60. sqlString = @"select FUN_CMN_GetBarCode(:barcode,null,:accountid) From DUAL";
  61. OracleParameter[] paras1 = new OracleParameter[]
  62. {
  63. new OracleParameter(":barcode",OracleDbType.Varchar2, barcode,ParameterDirection.Input),
  64. new OracleParameter(":accountid",OracleDbType.Int32, sUserInfo.AccountID,ParameterDirection.Input),
  65. };
  66. barcode = con.GetSqlResultToStr(sqlString, paras1);
  67. if (string.IsNullOrEmpty(barcode))
  68. {
  69. return 1;
  70. }
  71. sqlString = "SELECT g.goodscode, g.seatcovercode\n" +
  72. " FROM tp_pm_groutingdailydetail t\n" +
  73. " INNER JOIN tp_mst_goods g\n" +
  74. " ON g.goodsid = t.goodsid\n" +
  75. " WHERE t.barcode = :barcode";
  76. OracleParameter[] paras2 = new OracleParameter[]
  77. {
  78. new OracleParameter(":barcode",OracleDbType.Varchar2, barcode,ParameterDirection.Input),
  79. };
  80. DataTable code = con.GetSqlResultToDt(sqlString, paras2);
  81. if (code == null || code.Rows.Count == 0)
  82. {
  83. return 2;
  84. }
  85. object sccode = code.Rows[0]["seatcovercode"];
  86. if (sccode == null || sccode == DBNull.Value)
  87. {
  88. return 3;
  89. }
  90. //using (MelsecA1EAscii melsec = new MelsecA1EAscii(ip, port))
  91. using (MelsecA1EAsciiSocket melsec = new MelsecA1EAsciiSocket(ip, port))
  92. {
  93. int icode = Convert.ToInt32(sccode);
  94. string type = dec[0].ToString();
  95. int begin = Convert.ToInt32(dec.Substring(1));
  96. // 写入
  97. //bool result = melsec.SetData("D", 1000, 1, icode.ToString("X4"));
  98. bool result = melsec.SetData(type, begin, 1, icode.ToString("X4"));
  99. //// 写入 产品编码
  100. //byte[] byteCode = Encoding.ASCII.GetBytes(goodscode);
  101. //result = melsec.SetData("D", 1001, 10, byteCode);
  102. if (result)
  103. {
  104. return 0;
  105. }
  106. }
  107. return -1;
  108. }
  109. catch (Exception ex)
  110. {
  111. throw ex;
  112. }
  113. finally
  114. {
  115. if (con.ConnState == ConnectionState.Open)
  116. {
  117. con.Close();
  118. }
  119. }
  120. }
  121. /// <summary>
  122. /// 发送产品型号到PLC(坐便盖板)-恒洁潮州
  123. /// </summary>
  124. /// <param name="barcode"></param>
  125. /// <param name="sUserInfo"></param>
  126. /// <returns></returns>
  127. public static int SendBarcodeToPLCServer(string barcode, SUserInfo sUserInfo)
  128. {
  129. IDBConnection con = ClsDbFactory.CreateDBConnection(DataBaseType.ORACLE, DataManager.ConnectionString);
  130. try
  131. {
  132. con.Open();
  133. string sqlString = "select u.plcid, p.ip, p.port,p.PLCDescription from tp_mst_user u " +
  134. "inner join tp_mst_plc p on p.plcid = u.plcid where u.userid = :userid and p.valueflag='1'";
  135. OracleParameter[] paras = new OracleParameter[]
  136. {
  137. new OracleParameter(":userid",OracleDbType.Int32, sUserInfo.UserID,ParameterDirection.Input),
  138. };
  139. DataTable plc = con.GetSqlResultToDt(sqlString, paras);
  140. if (plc == null || plc.Rows.Count == 0)
  141. {
  142. return 4;
  143. }
  144. string ip = plc.Rows[0]["ip"].ToString();
  145. int port = Convert.ToInt32(plc.Rows[0]["port"]);
  146. //string dec = plc.Rows[0]["PLCDescription"].ToString();
  147. if (string.IsNullOrWhiteSpace(ip) || port == 0
  148. //|| string.IsNullOrWhiteSpace(dec)
  149. )
  150. {
  151. return 5;
  152. }
  153. if (string.IsNullOrEmpty(barcode) || barcode.Length != 11)
  154. {
  155. return 1;
  156. }
  157. sqlString = "SELECT t.goodscode\n" +
  158. " FROM tp_pm_groutingdailydetail t\n" +
  159. //" INNER JOIN tp_mst_goods g\n" +
  160. //" ON g.goodsid = t.goodsid\n" +
  161. " WHERE t.barcode = :barcode";
  162. OracleParameter[] paras2 = new OracleParameter[]
  163. {
  164. new OracleParameter(":barcode", OracleDbType.Varchar2, barcode,ParameterDirection.Input),
  165. };
  166. object code = con.GetSqlResultToObj(sqlString, paras2);
  167. if (code == null || code == DBNull.Value)
  168. {
  169. return 2;
  170. }
  171. try
  172. {
  173. using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
  174. {
  175. socket.Connect(ip, port);
  176. //socket.SendTimeout = 3000;
  177. socket.Send(Encoding.ASCII.GetBytes("0001000B" + barcode));
  178. }
  179. }
  180. catch
  181. {
  182. return 3;
  183. }
  184. return 0;
  185. }
  186. catch (Exception ex)
  187. {
  188. throw ex;
  189. }
  190. finally
  191. {
  192. if (con.ConnState == ConnectionState.Open)
  193. {
  194. con.Close();
  195. }
  196. }
  197. }
  198. #endregion
  199. }
  200. }