frmLocalTruckScaleActual.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. using Infragistics.Win.UltraWinListView;
  2. using Infragistics.Win.UltraWinTabControl;
  3. using Newtonsoft.Json;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.Diagnostics;
  9. using System.Drawing;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Timers;
  13. using System.Windows.Forms;
  14. using System.Xml.Linq;
  15. using Core.Mes.Client.Comm.Control;
  16. using Core.Mes.Client.Comm.Server;
  17. using Core.Mes.Client.Comm.Tool;
  18. using Core.StlMes.Client.LgResMgt.Mcms.entity;
  19. using CoreFS.CA06;
  20. using Infragistics.Win;
  21. using Infragistics.Win.Misc;
  22. using Infragistics.Win.UltraWinEditors;
  23. using Infragistics.Win.UltraWinGrid;
  24. using Infragistics.Win.UltraWinListView;
  25. using Infragistics.Win.UltraWinTabControl;
  26. using Newtonsoft.Json;
  27. using Newtonsoft.Json.Serialization;
  28. using NPOI.HSSF.UserModel;
  29. using NPOI.SS.UserModel;
  30. using NPOI.SS.Util;
  31. using BorderStyle = NPOI.SS.UserModel.BorderStyle;
  32. using HorizontalAlignment = NPOI.SS.UserModel.HorizontalAlignment;
  33. using Timer = System.Timers.Timer;
  34. using System.Net.Sockets;
  35. using System.Threading;
  36. using System.Text;
  37. using System.Collections;
  38. using Core.Mes.Client.Comm.Format;
  39. namespace Core.StlMes.Client.LgResMgt.Mcms
  40. {
  41. public partial class frmLocalTruckScaleActual : FrmBase
  42. {
  43. public frmLocalTruckScaleActual()
  44. {
  45. InitializeComponent();
  46. }
  47. private CommunicationEntity communicationnow;
  48. private frmMessage _frmMessage;
  49. private CmmPoundBaseEntity cmmPoundBase;
  50. private CmmActualWeightEntity cmmactual=new CmmActualWeightEntity();
  51. private CmmActualWeightEntity cmmactualbefore =new CmmActualWeightEntity();
  52. private List<CmmPoundBaseEntity> poundInfos;
  53. private List<CmmBaseSpotInfoEntity> CmmBaseSpotInfo;
  54. private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
  55. {
  56. if (cboPoundList.Text.Trim() == "")
  57. return;
  58. if ((cboPoundList.Text.Trim() != null)&&(communicationnow!=null))
  59. {
  60. communicationnow.Communication.Show = false;
  61. communicationnow.Communication.Stop();
  62. communicationnow.Communication.ReceiveData -= defultClenttEntity_ReceiveData;
  63. }
  64. communicationnow = null;
  65. communicationnow = new CommunicationEntity();
  66. var combase= CmmBaseSpotInfo.First(p => p.BaseSpotNo == cboPoundList.Text);
  67. communicationnow.Communication = new SocketClient
  68. {
  69. Ip = combase.WeightIp,
  70. Port = combase.WeightPort,
  71. Show = true
  72. };
  73. communicationnow.PoundNo = cboPoundList.Text;
  74. communicationnow.Communication.Show = true;
  75. WeightLoad(communicationnow.Communication, communicationnow.PoundNo);
  76. }
  77. /// <summary>
  78. /// 启动采集
  79. /// </summary>
  80. /// <param name="communication"></param>
  81. private void WeightLoad(iCommunication communication, string PNO)
  82. {
  83. communication.ReceiveData += defultClenttEntity_ReceiveData;
  84. CmmBaseProtocolEntity cmmBaseProtocol = null;
  85. cmmPoundBase = poundInfos.First(p => p.PoundNo == PNO);
  86. if (cmmPoundBase != null)
  87. {
  88. List<CmmBaseProtocolCEntity> protocols = EntityHelper.GetData<CmmBaseProtocolCEntity>(
  89. "com.steering.Mcms.ProcotolServer.getProcotolId",
  90. new object[] { cmmPoundBase.PoundProtocolId },
  91. ob);
  92. if ((protocols != null) && protocols.Any())
  93. cmmBaseProtocol = new CmmBaseProtocolEntity
  94. {
  95. ProtocolId = cmmPoundBase.PoundProtocolId,
  96. ListC = protocols
  97. };
  98. // cboPoundList.Text = cmmPoundBase.PoundNo;
  99. }
  100. communication.ChangeProtocol(new TruckScaleProtocol(cmmBaseProtocol));
  101. utbConnect_Click(null, null);
  102. }
  103. private void defultClenttEntity_ReceiveData(object sender, object data, string message, DataType datatype,
  104. byte[] bdata)
  105. {
  106. SocketClient client = sender as SocketClient;
  107. if (client.Show == true)
  108. {
  109. ShowMessage(data, message, datatype, sender);
  110. }
  111. }
  112. private void ShowMessage(object data, string message, DataType dataType, object sender)
  113. {
  114. SocketClient client = sender as SocketClient;
  115. if (dataType == DataType.ReceiveData)
  116. {
  117. double wt;
  118. if (double.TryParse(data.ToString2(), out wt))
  119. txtWeight.SafeRefreshControl(() => { txtWeight.Text = (wt / 100d).ToString("0.00") + "t"; });
  120. }
  121. else if (dataType == DataType.Open)
  122. {
  123. utbConnect.SafeRefreshControl(() => utbConnect.Appearance.ForeColor = Color.Red);
  124. txtMessage.SafeRefreshControl(
  125. () =>
  126. txtMessage.Text =
  127. client.Status
  128. ? "已打开"
  129. : "已关闭");
  130. // cboNo.SafeRefreshControl(() => cboNo.Enabled = false);
  131. }
  132. else if (dataType == DataType.Close)
  133. {
  134. utbConnect.SafeRefreshControl(() => utbConnect.Appearance.ForeColor = Color.Lime);
  135. txtMessage.SafeRefreshControl(
  136. () =>
  137. txtMessage.Text =
  138. client.Status
  139. ? "已打开"
  140. : "已关闭");
  141. }
  142. else if (dataType == DataType.Error)
  143. {
  144. MessageBox.Show(message);
  145. }
  146. else if (dataType == DataType.ClientReconnect)
  147. {
  148. txtMessage.SafeRefreshControl(() => txtMessage.Text = "网络异常,正在重连服务!");
  149. }
  150. txtMessage.SafeRefreshControl(() => txtMessage.Text = client.ToString());
  151. if ((_frmMessage != null) && !_frmMessage.IsDisposed)
  152. _frmMessage.SafeShowMsg(message, dataType);
  153. else
  154. _frmMessage = null;
  155. }
  156. private void frmLocalTruckScaleActual_Load(object sender, EventArgs e)
  157. {
  158. poundInfos = EntityHelper.GetData<CmmPoundBaseEntity>(
  159. "com.steering.Mcms.PoundBaseServer.getPoudInfo",
  160. new object[] { ValidDataPurviewIds },
  161. ob);
  162. CmmBaseSpotInfo = EntityHelper.GetData<CmmBaseSpotInfoEntity>(
  163. "com.steering.Mcms.CmmBaseSpotInfoServer.doQuery",
  164. new object[] { },
  165. ob);
  166. }
  167. private void utbConnect_Click(object sender, EventArgs e)
  168. {
  169. if (communicationnow.Communication == null) return;
  170. if ((communicationnow.Communication != null) && communicationnow.Communication.Status)
  171. {
  172. communicationnow.Communication.Stop();
  173. }
  174. else
  175. {
  176. communicationnow.Communication.Start();
  177. }
  178. }
  179. private bool boolbefor = false;
  180. private void timer1_Tick(object sender, EventArgs e)
  181. {
  182. double wt;
  183. try
  184. {
  185. if (double.TryParse(txtWeight.Text.Replace("t", ""), out wt))
  186. {
  187. if (communicationnow == null)
  188. return;
  189. if (wt < 0.5 && boolbefor == false)
  190. {
  191. boolbefor = true;
  192. var CmmA = EntityHelper.GetData<CmmActualWeightEntity>(
  193. "com.steering.Mcms.CmmActualWeightServer.doQuery", new object[] { }, ob);
  194. cmmactualbefore = CmmA.First(p => p.BaseSpotNo == communicationnow.PoundNo);
  195. }
  196. if ((cmmactualbefore != null) && (cmmactualbefore.BaseSpotNo != cboPoundList.Text.Trim()))
  197. {
  198. boolbefor = false;
  199. }
  200. if (wt >= 0.5)
  201. {
  202. txtMessage.SafeRefreshControl(() => txtMessage.Text = "开始计量");
  203. var CmmA = EntityHelper.GetData<CmmActualWeightEntity>(
  204. "com.steering.Mcms.CmmActualWeightServer.doQuery", new object[] { }, ob);
  205. cmmactual = CmmA.First(p => p.BaseSpotNo == cboPoundList.Text);
  206. }
  207. if ((cmmactualbefore == null && cmmactual != null) || ((cmmactualbefore.RecordNo != cmmactual.RecordNo) && (!(string.IsNullOrEmpty(cmmactual.RecordNo)))))
  208. {
  209. boolbefor = false;
  210. var dic = new Dictionary<string, object>();
  211. dic.Add("recordNo", cmmactual.RecordNo);
  212. // dic.Add("validflag", new List<string> { "2" });
  213. CmmWeightRecordEntity cmmrecord = EntityHelper.GetData<CmmWeightRecordEntity>(
  214. "com.steering.Mcms.RecordServer.doQueryRecord",
  215. new object[] { dic },
  216. ob).First();
  217. textCar_no.SafeRefreshControl(() => textCar_no.Text = cmmrecord.CarNo);
  218. // txtMessage.SafeRefreshControl(() => txtMessage.Text = client.ToString());
  219. txtShipper.SafeRefreshControl(() => txtShipper.Text = cmmrecord.ShippersName);
  220. txtRecive.SafeRefreshControl(() => txtRecive.Text = cmmrecord.ReceiveName);
  221. txtMat_name.SafeRefreshControl(() => txtMat_name.Text = cmmrecord.MatName);
  222. txtMat_type.SafeRefreshControl(() => txtMat_type.Text = cmmrecord.MatType);
  223. if (cmmrecord.Validflag == "2")
  224. {
  225. var dic1 = new Dictionary<string, object>();
  226. string strtime = DateTime.Now.ToString("yyyy-MM-dd 07:00:00");
  227. string strtime1 = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd 07:00:00");
  228. dic1.Add("time", strtime);
  229. dic1.Add("time1", strtime1);
  230. dic1.Add("carNo", cmmrecord.CarNo);
  231. dic1.Add("shippersName", cmmrecord.ShippersName);
  232. dic1.Add("receiveName", cmmrecord.ReceiveName);
  233. dic1.Add("validflag", new List<string> { "1" });
  234. List<CmmWeightResultEntity> list = EntityHelper.GetData<CmmWeightResultEntity>(
  235. "com.steering.Mcms.ResultServer.doQuery",
  236. new object[] { dic1 },
  237. ob);
  238. var p = list.First();
  239. if (p != null)
  240. {
  241. txtTaer_wt.SafeRefreshControl(() => txtTaer_wt.Text = p.TareWt + "t");
  242. txtCross_wt.SafeRefreshControl(() => txtCross_wt.Text = p.GrossWt + "t");
  243. txtNet_wet.SafeRefreshControl(() => txtNet_wet.Text = p.NetWt + "t");
  244. }
  245. }
  246. }
  247. }
  248. }
  249. catch { }
  250. }
  251. private void btnMaxShow_Click(object sender, EventArgs e)
  252. {
  253. Form form = frmLocalTruckScaleActuals.GetSingleton(ob); // 有参数
  254. form.Show();
  255. form.Activate();
  256. }
  257. }
  258. }