frmLocalTruckScaleActuals.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 frmLocalTruckScaleActuals : FrmBase
  42. {
  43. public frmLocalTruckScaleActuals(OpeBase _ob)
  44. {
  45. InitializeComponent();
  46. new CodeFile.FormMove(this, panel1);
  47. ob = _ob;
  48. }
  49. bool start = true;
  50. private static object sync = new object();
  51. private static frmLocalTruckScaleActuals instance;
  52. public static frmLocalTruckScaleActuals GetSingleton(OpeBase _ob)
  53. {
  54. lock (sync)
  55. {
  56. ;
  57. if (instance == null || instance.IsDisposed)
  58. instance = new frmLocalTruckScaleActuals(_ob);
  59. }
  60. return instance;
  61. }
  62. private CommunicationEntity communicationnow;
  63. private frmMessage _frmMessage;
  64. private CmmPoundBaseEntity cmmPoundBase;
  65. private CmmActualWeightEntity cmmactual = new CmmActualWeightEntity();
  66. private CmmActualWeightEntity cmmactualbefore = new CmmActualWeightEntity();
  67. private List<CmmPoundBaseEntity> poundInfos;
  68. private List<CmmBaseSpotInfoEntity> CmmBaseSpotInfo;
  69. private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
  70. {
  71. if (cboPoundList.Text.Trim() == "")
  72. return;
  73. if ((cboPoundList.Text.Trim() != null) && (communicationnow != null))
  74. {
  75. communicationnow.Communication.Show = false;
  76. communicationnow.Communication.Stop();
  77. communicationnow.Communication.ReceiveData -= defultClenttEntity_ReceiveData;
  78. }
  79. communicationnow = null;
  80. communicationnow = new CommunicationEntity();
  81. var combase = CmmBaseSpotInfo.First(p => p.BaseSpotNo == cboPoundList.Text);
  82. communicationnow.Communication = new SocketClient
  83. {
  84. Ip = combase.WeightIp,
  85. Port = combase.WeightPort,
  86. Show = true
  87. };
  88. communicationnow.PoundNo = cboPoundList.Text;
  89. communicationnow.Communication.Show = true;
  90. WeightLoad(communicationnow.Communication, communicationnow.PoundNo);
  91. }
  92. /// <summary>
  93. /// 启动采集
  94. /// </summary>
  95. /// <param name="communication"></param>
  96. private void WeightLoad(iCommunication communication, string PNO)
  97. {
  98. communication.ReceiveData += defultClenttEntity_ReceiveData;
  99. CmmBaseProtocolEntity cmmBaseProtocol = null;
  100. cmmPoundBase = poundInfos.First(p => p.PoundNo == PNO);
  101. if (cmmPoundBase != null)
  102. {
  103. List<CmmBaseProtocolCEntity> protocols = EntityHelper.GetData<CmmBaseProtocolCEntity>(
  104. "com.steering.Mcms.ProcotolServer.getProcotolId",
  105. new object[] { cmmPoundBase.PoundProtocolId },
  106. ob);
  107. if ((protocols != null) && protocols.Any())
  108. cmmBaseProtocol = new CmmBaseProtocolEntity
  109. {
  110. ProtocolId = cmmPoundBase.PoundProtocolId,
  111. ListC = protocols
  112. };
  113. // cboPoundList.Text = cmmPoundBase.PoundNo;
  114. }
  115. communication.ChangeProtocol(new TruckScaleProtocol(cmmBaseProtocol));
  116. utbConnect_Click(null, null);
  117. }
  118. private void defultClenttEntity_ReceiveData(object sender, object data, string message, DataType datatype,
  119. byte[] bdata)
  120. {
  121. SocketClient client = sender as SocketClient;
  122. if (client.Show == true)
  123. {
  124. ShowMessage(data, message, datatype, sender);
  125. }
  126. }
  127. private void ShowMessage(object data, string message, DataType dataType, object sender)
  128. {
  129. SocketClient client = sender as SocketClient;
  130. if (dataType == DataType.ReceiveData)
  131. {
  132. double wt;
  133. if (double.TryParse(data.ToString2(), out wt))
  134. txtWeight.SafeRefreshControl(() => { txtWeight.Text = (wt / 100d).ToString("0.00") + "t"; });
  135. }
  136. else if (dataType == DataType.Open)
  137. {
  138. utbConnect.SafeRefreshControl(() => utbConnect.Appearance.ForeColor = Color.Red);
  139. txtMessage.SafeRefreshControl(
  140. () =>
  141. txtMessage.Text =
  142. client.Status
  143. ? "已打开"
  144. : "已关闭");
  145. // cboNo.SafeRefreshControl(() => cboNo.Enabled = false);
  146. }
  147. else if (dataType == DataType.Close)
  148. {
  149. utbConnect.SafeRefreshControl(() => utbConnect.Appearance.ForeColor = Color.Lime);
  150. txtMessage.SafeRefreshControl(
  151. () =>
  152. txtMessage.Text =
  153. client.Status
  154. ? "已打开"
  155. : "已关闭");
  156. }
  157. else if (dataType == DataType.Error)
  158. {
  159. MessageBox.Show(message);
  160. }
  161. else if (dataType == DataType.ClientReconnect)
  162. {
  163. txtMessage.SafeRefreshControl(() => txtMessage.Text = "网络异常,正在重连服务!");
  164. }
  165. // txtMessage.SafeRefreshControl(() => txtMessage.Text = client.ToString());
  166. if ((_frmMessage != null) && !_frmMessage.IsDisposed)
  167. _frmMessage.SafeShowMsg(message, dataType);
  168. else
  169. _frmMessage = null;
  170. }
  171. private void frmLocalTruckScaleActual_Load(object sender, EventArgs e)
  172. {
  173. txtMessage.Text = "";
  174. labRealWgt.Text = "";
  175. txtTaer_wt.Text = "";
  176. txtNet_wet.Text = "";
  177. txtCross_wt.Text = "";
  178. poundInfos = EntityHelper.GetData<CmmPoundBaseEntity>(
  179. "com.steering.Mcms.PoundBaseServer.getPoudInfo",
  180. new object[] { ValidDataPurviewIds },
  181. ob);
  182. CmmBaseSpotInfo = EntityHelper.GetData<CmmBaseSpotInfoEntity>(
  183. "com.steering.Mcms.CmmBaseSpotInfoServer.doQuery",
  184. new object[] { },
  185. ob);
  186. this.WindowState = FormWindowState.Normal;
  187. }
  188. private void utbConnect_Click(object sender, EventArgs e)
  189. {
  190. if (communicationnow.Communication == null) return;
  191. if ((communicationnow.Communication != null) && communicationnow.Communication.Status)
  192. {
  193. communicationnow.Communication.Stop();
  194. }
  195. else
  196. {
  197. communicationnow.Communication.Start();
  198. }
  199. }
  200. private bool boolbefor = false;
  201. private void timer1_Tick(object sender, EventArgs e)
  202. {
  203. try
  204. {
  205. double wt;
  206. if (double.TryParse(txtWeight.Text.Replace("t", ""), out wt))
  207. {
  208. if ((wt == 0 || wt < 0)&&txtMessage.Text != " ")
  209. {
  210. start = false;
  211. txtMessage.SafeRefreshControl(() => txtMessage.Text = " ");
  212. }
  213. else
  214. {
  215. start = true;
  216. }
  217. if (communicationnow == null)
  218. return;
  219. if (wt < 0.5 && boolbefor == false)
  220. {
  221. textCar_no.SafeRefreshControl(() => textCar_no.Text = "");
  222. labRealWgt.SafeRefreshControl(() => labRealWgt.Text = "");
  223. txtTaer_wt.SafeRefreshControl(() => txtTaer_wt.Text = "");
  224. txtCross_wt.SafeRefreshControl(() => txtCross_wt.Text = "");
  225. txtNet_wet.SafeRefreshControl(() => txtNet_wet.Text = "");
  226. boolbefor = true;
  227. var CmmA = EntityHelper.GetData<CmmActualWeightEntity>(
  228. "com.steering.Mcms.CmmActualWeightServer.doQuery", new object[] { }, ob);
  229. cmmactualbefore = CmmA.First(p => p.BaseSpotNo == communicationnow.PoundNo);
  230. }
  231. if ((cmmactualbefore != null) && (cmmactualbefore.BaseSpotNo != cboPoundList.Text.Trim()))
  232. {
  233. boolbefor = false;
  234. }
  235. if (wt >= 0.5)
  236. {
  237. txtMessage.SafeRefreshControl(() => txtMessage.Text = "开始计量");
  238. var CmmA = EntityHelper.GetData<CmmActualWeightEntity>(
  239. "com.steering.Mcms.CmmActualWeightServer.doQuery", new object[] { }, ob);
  240. cmmactual = CmmA.First(p => p.BaseSpotNo == cboPoundList.Text);
  241. }
  242. if ((cmmactualbefore == null && cmmactual != null) || ((cmmactualbefore.RecordNo != cmmactual.RecordNo) && (!(string.IsNullOrEmpty(cmmactual.RecordNo)))))
  243. {
  244. boolbefor = false;
  245. var dic = new Dictionary<string, object>();
  246. dic.Add("recordNo", cmmactual.RecordNo);
  247. // dic.Add("validflag", new List<string> { "2" });
  248. CmmWeightRecordEntity cmmrecord = EntityHelper.GetData<CmmWeightRecordEntity>(
  249. "com.steering.Mcms.RecordServer.doQueryRecord",
  250. new object[] { dic },
  251. ob).First();
  252. textCar_no.SafeRefreshControl(() => textCar_no.Text = cmmrecord.CarNo);
  253. labRealWgt.SafeRefreshControl(() => labRealWgt.Text = cmmrecord.RecordWeight.ToString() + "t");
  254. if (cmmrecord.Validflag == "2")
  255. {
  256. var dic1 = new Dictionary<string, object>();
  257. string strtime = DateTime.Now.ToString("yyyy-MM-dd 00:00:00");
  258. string strtime1 = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd 07:00:00");
  259. dic1.Add("time", strtime);
  260. dic1.Add("time1", strtime1);
  261. dic1.Add("carNo", cmmrecord.CarNo);
  262. // dic.Add("shippersName", cmmrecord.ShippersName);
  263. // dic.Add("receiveName", cmmrecord.ReceiveName);
  264. dic1.Add("validflag", new List<string> { "1" });
  265. List<CmmWeightResultEntity> list = EntityHelper.GetData<CmmWeightResultEntity>(
  266. "com.steering.Mcms.ResultServer.doQuery",
  267. new object[] { dic1 },
  268. ob);
  269. var p = list.First();
  270. if (p != null)
  271. {
  272. txtTaer_wt.SafeRefreshControl(() => txtTaer_wt.Text = p.TareWt + "t");
  273. txtCross_wt.SafeRefreshControl(() => txtCross_wt.Text = p.GrossWt + "t");
  274. txtNet_wet.SafeRefreshControl(() => txtNet_wet.Text = p.NetWt + "t");
  275. }
  276. }
  277. }
  278. }
  279. }
  280. catch
  281. { }
  282. }
  283. private void btnClose_Click(object sender, EventArgs e)
  284. {
  285. this.Close();
  286. }
  287. private void pictureBox1_Click(object sender, EventArgs e)
  288. {
  289. this.WindowState = FormWindowState.Maximized;
  290. }
  291. private void timer2_Tick(object sender, EventArgs e)
  292. {
  293. labDate.Text = DateTime.Now.ToString();
  294. }
  295. }
  296. }