FrmStandAloneMetering.cs 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  1. using com.hnshituo.core.webapp.vo;
  2. using Common;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Configuration;
  6. using System.Data;
  7. using System.Drawing;
  8. using System.IO;
  9. using System.IO.Ports;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Windows.Forms;
  13. namespace FrmStandAloneMetering
  14. {
  15. /// <summary>
  16. /// 仪表状态
  17. /// </summary>
  18. public enum MeterStatus
  19. {
  20. /// <summary>
  21. /// Stable 稳定
  22. /// </summary>
  23. Stable,
  24. /// <summary>
  25. /// 不稳定
  26. /// </summary>
  27. UnStable,
  28. /// <summary>
  29. /// 空磅
  30. /// </summary>
  31. Null,
  32. ///// <summary>
  33. ///// 未连接
  34. ///// </summary>
  35. UnConnect,
  36. ///// <summary>
  37. ///// 已连接
  38. ///// </summary>
  39. Connect
  40. }
  41. public partial class FrmStandAloneMetering : Form
  42. {
  43. private string strComPara; //仪表参数
  44. private int messageLength; //报文长度
  45. private string strSeparate;//分割符
  46. private int startPosition; //开始取值位置
  47. private int dataLength; //数据长度
  48. private int sleepTime; //采样频率
  49. private int stableTime; //稳定时间
  50. private int stableCount; //稳定次数
  51. private int stableDiff; //稳定次数
  52. private double tmpWeight; //临时重量,用于判断是否稳定状态
  53. private int weightStabCount; //波动次数
  54. private BaseDbCls bd = new BaseDbCls();
  55. private SerialPort serialPort1; //数据采集串口
  56. private bool blThreadFlag;//数据采集线程开关
  57. private StringBuilder weightLog = new StringBuilder(); //重量日志的文件
  58. private MeterWorkCarActualFirstService actualFirstService = new MeterWorkCarActualFirstService();
  59. public FrmStandAloneMetering()
  60. {
  61. InitializeComponent();
  62. }
  63. private void FrmStandAloneMetering_Load(object sender, EventArgs e)
  64. {
  65. strComPara = ConfigurationManager.AppSettings["ComPara"].ToString().Trim(); //仪表参数COM口
  66. messageLength = Convert.ToInt32(ConfigurationManager.AppSettings["MessageLength"].ToString().Trim()); //报文长度
  67. strSeparate = ConfigurationManager.AppSettings["Separate"].ToString().Trim();//分割符
  68. startPosition = Convert.ToInt32(ConfigurationManager.AppSettings["StartPosition"].ToString().Trim());//开始取值位置
  69. dataLength = Convert.ToInt32(ConfigurationManager.AppSettings["DataLength"].ToString().Trim());// 数据长度
  70. sleepTime = Convert.ToInt32(ConfigurationManager.AppSettings["SleepTime"].ToString().Trim());//采样频率
  71. stableTime = Convert.ToInt32(ConfigurationManager.AppSettings["StableTime"].ToString().Trim());//稳定时间
  72. stableDiff = Convert.ToInt32(ConfigurationManager.AppSettings["StableDiff"].ToString().Trim());//稳定重量
  73. stableCount = stableTime * 1000 / sleepTime;//稳定次数
  74. //界面COM赋值
  75. if (!String.IsNullOrEmpty(strComPara) && strComPara.Contains(","))
  76. {
  77. string[] strParams = strComPara.Split(new char[] { ',' });
  78. cbChooseCom.Text = strParams[0];
  79. }
  80. //物料下拉框赋值
  81. DataTable dtMatterInfo = selectMatterInfo();
  82. this.cbMatterName.DataSource = dtMatterInfo;
  83. this.cbMatterName.DisplayMember = "name";
  84. this.cbMatterName.ValueMember = "value";
  85. //发货单位下拉框赋值
  86. DataTable dtForwardingUnit = selectCustomerInfo();
  87. this.cbForwardingUnitName.DataSource = dtForwardingUnit;
  88. this.cbForwardingUnitName.DisplayMember = "name";
  89. this.cbForwardingUnitName.ValueMember = "value";
  90. //收货单位下拉框赋值
  91. DataTable dtReceivingUint = selectCustomerInfo();
  92. this.cbReceivingUintName.DataSource = dtReceivingUint;
  93. this.cbReceivingUintName.DisplayMember = "name";
  94. this.cbReceivingUintName.ValueMember = "value";
  95. //计量点下拉框赋值
  96. DataTable dtSpotInfo = selectSpotInfo();
  97. this.cbBaseSpot.DataSource = dtSpotInfo;
  98. this.cbBaseSpot.DisplayMember = "name";
  99. this.cbBaseSpot.ValueMember = "value";
  100. //界面稳定时间赋值
  101. cbStableSchedule.Text = stableTime.ToString();
  102. tmpWeight = 0;//临时重量
  103. weightStabCount = 0; //波动次数
  104. }
  105. #region 用户控件的事件
  106. /// <summary>
  107. /// 开启数据采集
  108. /// </summary>
  109. /// <param name="sender"></param>
  110. /// <param name="e"></param>
  111. private void btnCollection_Click(object sender, EventArgs e)
  112. {
  113. if (blThreadFlag)
  114. {
  115. MessageBox.Show("采集程序已经运行,禁止重新进行采集!");
  116. return;
  117. }
  118. blThreadFlag = true;
  119. System.Threading.Thread DataCollectThread = new System.Threading.Thread(new System.Threading.ThreadStart(DataCollect));
  120. DataCollectThread.Start();
  121. }
  122. /// <summary>
  123. /// 上传本地的计量数据
  124. /// </summary>
  125. /// <param name="sender"></param>
  126. /// <param name="e"></param>
  127. private void btnUploadData_Click(object sender, EventArgs e)
  128. {
  129. uploadData();
  130. }
  131. /// <summary>
  132. /// 下载服务器的基础数据
  133. /// </summary>
  134. /// <param name="sender"></param>
  135. /// <param name="e"></param>
  136. private void btnDownloadData_Click(object sender, EventArgs e)
  137. {
  138. // 删除CSV文件
  139. Utils.FileUtil.DeleteFile(AppDomain.CurrentDomain.BaseDirectory + "baseData\\MeterBaseMatterInfo.csv");
  140. // 获取物料信息
  141. MeterBaseMatterInfoService meterBaseMatterInfo = new MeterBaseMatterInfoService();//物料服务
  142. MeterBaseMatterInfo MatterInfo1 = new MeterBaseMatterInfo();
  143. MatterInfo1.validFlag = "1";
  144. MatterInfo1.pageNum = 1;
  145. MatterInfo1.pageSize = 9999;
  146. RESTfulResult<List<MeterBaseMatterInfo>> rmx = meterBaseMatterInfo.doQueryListLike(MatterInfo1);
  147. // 重新保存CSV文件
  148. saveMatterInfo(rmx);
  149. // 删除CSV文件
  150. Utils.FileUtil.DeleteFile(AppDomain.CurrentDomain.BaseDirectory + "baseData\\MeterBaseCustomerSupplier.csv");
  151. // 获取客商信息
  152. MeterBaseCustomerSupplierService service2 = new MeterBaseCustomerSupplierService();//物料服务
  153. MeterBaseCustomerSupplier customerInfo1 = new MeterBaseCustomerSupplier();
  154. customerInfo1.validFlag = "1";
  155. RESTfulResult<List<MeterBaseCustomerSupplier>> rmx2 = service2.doQuery(customerInfo1);
  156. // 重新保存CSV文件
  157. saveCustomerInfo(rmx2);
  158. // 删除CSV文件
  159. Utils.FileUtil.DeleteFile(AppDomain.CurrentDomain.BaseDirectory + "baseData\\MeterBaseSpotInfo.csv");
  160. // 获取客商信息
  161. MeterBaseSpotInfoService service3 = new MeterBaseSpotInfoService();//物料服务
  162. MeterBaseSpotInfo customerInfo2 = new MeterBaseSpotInfo();
  163. customerInfo2.validFlag = "1";
  164. RESTfulResult<List<MeterBaseSpotInfo>> rmx3 = service3.doQueryWf(customerInfo2);
  165. // 重新保存CSV文件
  166. saveSpotInfo(rmx3);
  167. //物料下拉框赋值
  168. DataTable dtMatterInfo = selectMatterInfo();
  169. this.cbMatterName.DataSource = dtMatterInfo;
  170. this.cbMatterName.DisplayMember = "name";
  171. this.cbMatterName.ValueMember = "value";
  172. //发货单位下拉框赋值
  173. DataTable dtForwardingUnit = selectCustomerInfo();
  174. this.cbForwardingUnitName.DataSource = dtForwardingUnit;
  175. this.cbForwardingUnitName.DisplayMember = "name";
  176. this.cbForwardingUnitName.ValueMember = "value";
  177. //收货单位下拉框赋值
  178. DataTable dtReceivingUint = selectCustomerInfo();
  179. this.cbReceivingUintName.DataSource = dtReceivingUint;
  180. this.cbReceivingUintName.DisplayMember = "name";
  181. this.cbReceivingUintName.ValueMember = "value";
  182. //计量点下拉框赋值
  183. DataTable dtSpotInfo = selectSpotInfo();
  184. this.cbBaseSpot.DataSource = dtSpotInfo;
  185. this.cbBaseSpot.DisplayMember = "name";
  186. this.cbBaseSpot.ValueMember = "value";
  187. }
  188. /// <summary>
  189. /// 数据查询
  190. /// </summary>
  191. /// <param name="sender"></param>
  192. /// <param name="e"></param>
  193. private void btQuery_Click(object sender, EventArgs e)
  194. {
  195. doQuery();
  196. }
  197. /// <summary>
  198. /// 毛重保存
  199. /// </summary>
  200. /// <param name="sender"></param>
  201. /// <param name="e"></param>
  202. private void btSaveGross_Click(object sender, EventArgs e)
  203. {
  204. saveData("0"); //毛重保存(0=毛重;1=常规皮重)
  205. doQuery();
  206. }
  207. /// <summary>
  208. /// 皮重保存
  209. /// </summary>
  210. /// <param name="sender"></param>
  211. /// <param name="e"></param>
  212. private void btSaveTare_Click(object sender, EventArgs e)
  213. {
  214. saveData("1"); //毛重保存(0=毛重;1=常规皮重)
  215. doQuery();
  216. }
  217. /// <summary>
  218. /// 界面关闭
  219. /// </summary>
  220. /// <param name="sender"></param>
  221. /// <param name="e"></param>
  222. private void FrmStandAloneMetering_FormClosing(object sender, FormClosingEventArgs e)
  223. {
  224. blThreadFlag = false;
  225. System.Threading.Thread.Sleep(sleepTime * 2);
  226. if (serialPort1 != null)
  227. {
  228. serialPort1.Close();
  229. }
  230. Application.Exit();
  231. }
  232. /// <summary>
  233. /// 稳定时间选择事件
  234. /// </summary>
  235. /// <param name="sender"></param>
  236. /// <param name="e"></param>
  237. private void cbStableSchedule_TextChanged(object sender, EventArgs e)
  238. {
  239. stableTime = Convert.ToInt32(cbStableSchedule.Text.Trim());//稳定时间
  240. stableCount = stableTime * 1000 / sleepTime;//稳定次数
  241. }
  242. /// <summary>
  243. /// COM口下拉事件
  244. /// </summary>
  245. /// <param name="sender"></param>
  246. /// <param name="e"></param>
  247. private void cbChooseCom_TextChanged(object sender, EventArgs e)
  248. {
  249. if (blThreadFlag)
  250. {
  251. MessageBox.Show("采集程序已经运行,禁止重新调整COM口!");
  252. return;
  253. }
  254. }
  255. #endregion 用户控件的事件
  256. #region 数据采集
  257. /// <summary>
  258. /// 数据采集线程
  259. /// </summary>
  260. private void DataCollect()
  261. {
  262. if (!blThreadFlag) return;
  263. string[] strParams = strComPara.Split(new char[] { ',' });
  264. Parity parity = Parity.None;
  265. if (strParams[2].ToUpper() == "ODD")
  266. {
  267. parity = Parity.Odd;
  268. }
  269. if (strParams[2].ToUpper() == "EVEN")
  270. {
  271. parity = Parity.Even;
  272. }
  273. StopBits stopBits = StopBits.None;
  274. if (strParams[4] == "1")
  275. {
  276. stopBits = StopBits.One;
  277. }
  278. if (strParams[4] == "1.5")
  279. {
  280. stopBits = StopBits.OnePointFive;
  281. }
  282. if (strParams[4] == "2")
  283. {
  284. stopBits = StopBits.Two;
  285. }
  286. serialPort1 = new SerialPort(strParams[0], Int32.Parse(strParams[1]),
  287. parity, Int32.Parse(strParams[3]), stopBits);
  288. while (!serialPort1.IsOpen)
  289. {
  290. try
  291. {
  292. serialPort1.Open();
  293. }
  294. catch (Exception err)
  295. {
  296. WriteCatchLog(err.ToString());
  297. }
  298. System.Threading.Thread.Sleep(500);
  299. }
  300. StringBuilder stringBuilder = new StringBuilder();
  301. string strtmp = "";
  302. while (blThreadFlag)
  303. {
  304. try
  305. {
  306. System.Threading.Thread.Sleep(sleepTime);
  307. if (serialPort1.BytesToRead > 0)
  308. {
  309. strtmp = serialPort1.ReadExisting();
  310. stringBuilder.Append(strtmp);
  311. //"US,GS,+0073.34 t\r\nUS,GS,+0073.96 t\r\nUS,GS,+0071.36 t\r\nUS,GS,+0070.40 t\r\nUS,GS,+0073.06 t\r\nUS,GS,+0073.16 t\r\nUS,GS,+0071.72 t\r\nUS,GS,+0073.30 t\r\nUS,GS,+0074.16 t\r\n"
  312. //0252.50 kg
  313. //20 20 0D 0A 7F 30 32 35 32 2E 35 30 20 6B 67
  314. //03 04 0D 0A FF 30 30 30 30 31 35 20 6B 67
  315. //char strSeparate = (char)0x6B;
  316. if (stringBuilder.ToString().LastIndexOf(strSeparate) >= 0)
  317. {
  318. if (stringBuilder.ToString().LastIndexOf(strSeparate) < messageLength)//不完整报文,抛掉
  319. {
  320. stringBuilder.Remove(0, stringBuilder.ToString().LastIndexOf(strSeparate) + strSeparate.Length);
  321. //stringBuilder.Remove(0, stringBuilder.ToString().LastIndexOf(strSeparate) + strSeparate.Length);
  322. }
  323. int index = stringBuilder.ToString().LastIndexOf(strSeparate);
  324. if ((index - messageLength) < 0)
  325. continue;
  326. string strData = stringBuilder.ToString().Substring(index - messageLength, messageLength + strSeparate.Length);
  327. // WriteCatchLog("strData " + strData);
  328. string weight = strData.Substring(startPosition, dataLength);
  329. if ("T".Equals(strSeparate.ToUpper()))
  330. {
  331. //单位为T是做特殊处理,界面显示的是T
  332. weight = (Convert.ToDouble(weight) * 1000) + "";
  333. }
  334. //WriteCatchLog("weight " + weight);
  335. stringBuilder.Remove(0, stringBuilder.Length);
  336. ucStorageWeightT1.setWgt(Math.Round(Convert.ToDouble(weight), 3));
  337. DoShowWeightStatus(Convert.ToDouble(weight));
  338. }
  339. }
  340. }
  341. catch (Exception err)
  342. {
  343. MessageBox.Show("串口打开异常!异常原因" + err.ToString());
  344. WriteCatchLog("DataCollect1" + err.ToString());
  345. }
  346. }
  347. }
  348. /// <summary>
  349. /// 重量稳定判断
  350. /// </summary>
  351. /// <param name="_weight"></param>
  352. public void DoShowWeightStatus(double _weight)
  353. {
  354. if (Math.Abs(_weight) < 50)
  355. {
  356. //不稳定状态;红灯
  357. ucStorageWeightT1.setStable(false);
  358. return;
  359. }
  360. //波动在范围内时,稳定次数+1,临时重量不变;波动范围超出时,稳定次数清零,更新临时重量,
  361. int inDiff = Math.Abs(Convert.ToInt32(tmpWeight - _weight));
  362. if (inDiff < stableDiff)
  363. {
  364. weightStabCount += 1;
  365. }
  366. else
  367. {
  368. weightStabCount = 0;
  369. }
  370. //WriteCatchLog("_tmpWeight: " + _tmpWeight + " _weight: " + _weight + " inDiff: "+inDiff+ " StabCount : " + weightStabCount);
  371. tmpWeight = Convert.ToSingle(_weight);
  372. //重量30kg内波动超过10次认为重量稳定
  373. if (weightStabCount > stableCount)
  374. {
  375. //稳定状态;绿灯
  376. ucStorageWeightT1.setStable(true);
  377. weightStabCount = stableCount;
  378. }
  379. else
  380. {
  381. ucStorageWeightT1.setStable(false);
  382. }
  383. }
  384. #endregion 数据采集
  385. /// <summary>
  386. /// 重量保存:0=毛重;1=常规皮重
  387. /// </summary>
  388. /// <param name="strWeightType"></param>
  389. private void saveData(string strWeightType)
  390. {
  391. try
  392. {
  393. MeterWorkCarActualFirst actualFirst = new MeterWorkCarActualFirst(); //一次计量实体
  394. actualFirst.actualFirstNo = "CAR" + DateTime.Now.ToString("yyyyMMddHHmmssfff"); //主键
  395. actualFirst.carNo = tbCarNo.Text.Trim();//车号
  396. actualFirst.baseSpotNo = cbBaseSpot.SelectedValue.ToString().Trim();//计量点
  397. actualFirst.baseSpotName = cbBaseSpot.Text.ToString().Trim();//计量点
  398. //actualFirst.meterTypeName = cbMeterTypeName.Text.Trim();//业务类型
  399. //actualFirst.contractNo = cbContractNo.Text.Trim();//合同号
  400. //actualFirst.batchNo = tbBatchNo.Text.Trim();//批次号
  401. //actualFirst.heatNo = tbHeatNo.Text.Trim();//炉号
  402. //actualFirst.shipmentNum = Convert.ToInt32(string.IsNullOrEmpty(tbShipmentNum.Text.Trim()) ? "0" : tbShipmentNum.Text.Trim());//包数/数量
  403. actualFirst.matterNo = cbMatterName.SelectedValue.ToString().Trim();//物资名称
  404. actualFirst.matterName = cbMatterName.Text.ToString().Trim();//物资名称
  405. actualFirst.receivingUintNo = cbReceivingUintName.SelectedValue.ToString().Trim();//收货单位
  406. actualFirst.receivingUintName = cbReceivingUintName.Text.ToString().Trim();//收货单位
  407. actualFirst.forwardingUnitNo = cbForwardingUnitName.SelectedValue.ToString().Trim();//发货单位
  408. actualFirst.forwardingUnitName = cbForwardingUnitName.Text.ToString().Trim();//发货单位
  409. //actualFirst.customerSupplierName = cbCustomerSupplierName.Text.Trim();//供应商
  410. //actualFirst.loadPointName = cbLoadPointName.Text.Trim();//卸货地点
  411. actualFirst.memo = tbMemo.Text.Trim();//备注
  412. actualFirst.weightType = strWeightType;//重量类型
  413. actualFirst.meterWeight = ucStorageWeightT1.getWgt() * 1000;
  414. //DataTable csvDataTable = OpenCSV(AppDomain.CurrentDomain.BaseDirectory + "alonData\\" + DateTime.Now.ToString("yyyy-MM-dd") + "\\actualFirst.csv");
  415. if (System.IO.Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "alonData\\" + DateTime.Now.ToString("yyyy-MM-dd")) == false
  416. || System.IO.File.Exists(AppDomain.CurrentDomain.BaseDirectory + "alonData\\" + DateTime.Now.ToString("yyyy-MM-dd") + "\\actualFirst.csv") == false)
  417. {
  418. StringBuilder actualFirstLogHead = new StringBuilder(); //一次计量数据保存文件
  419. actualFirstLogHead.Append("createTime,");//若第一行默认为当前时间则第一行多个逗号
  420. actualFirstLogHead.Append("actualFirstNo,");
  421. actualFirstLogHead.Append("carNo,");
  422. actualFirstLogHead.Append("baseSpotNo,");
  423. actualFirstLogHead.Append("baseSpotName,");
  424. //actualFirstLogHead.Append("meterTypeName,");
  425. //actualFirstLogHead.Append("contractNo,");
  426. //actualFirstLogHead.Append("batchNo,");
  427. //actualFirstLogHead.Append("heatNo,");
  428. //actualFirstLogHead.Append("shipmentNum,");
  429. actualFirstLogHead.Append("matterNo,");
  430. actualFirstLogHead.Append("matterName,");
  431. actualFirstLogHead.Append("receivingUintNo,");
  432. actualFirstLogHead.Append("receivingUintName,");
  433. actualFirstLogHead.Append("forwardingUnitNo,");
  434. actualFirstLogHead.Append("forwardingUnitName,");
  435. //actualFirstLogHead.Append("customerSupplierName,");
  436. //actualFirstLogHead.Append("loadPointName,");
  437. actualFirstLogHead.Append("memo,");
  438. actualFirstLogHead.Append("weightType,"); //最后一行不要逗号
  439. actualFirstLogHead.Append("meterWeight");
  440. logCsv.WriteDataLog("actualFirst", actualFirstLogHead.ToString());
  441. }
  442. StringBuilder actualFirstLog = new StringBuilder(); //一次计量数据保存文件
  443. actualFirstLog.Append(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ",");
  444. actualFirstLog.Append(actualFirst.actualFirstNo.ToString() + ",");
  445. actualFirstLog.Append(actualFirst.carNo.ToString() + ",");
  446. actualFirstLog.Append(actualFirst.baseSpotNo.ToString() + ",");
  447. actualFirstLog.Append(actualFirst.baseSpotName.ToString() + ",");
  448. // actualFirstLog.Append(actualFirst.meterTypeName.ToString() + ",");
  449. //actualFirstLog.Append(actualFirst.contractNo.ToString() + ",");
  450. //actualFirstLog.Append(actualFirst.batchNo.ToString() + ",");
  451. //actualFirstLog.Append(actualFirst.heatNo.ToString() + ",");
  452. //actualFirstLog.Append(actualFirst.shipmentNum.ToString() + ",");
  453. actualFirstLog.Append(actualFirst.matterNo.ToString() + ",");
  454. actualFirstLog.Append(actualFirst.matterName.ToString() + ",");
  455. actualFirstLog.Append(actualFirst.receivingUintNo.ToString() + ",");
  456. actualFirstLog.Append(actualFirst.receivingUintName.ToString() + ",");
  457. actualFirstLog.Append(actualFirst.forwardingUnitNo.ToString() + ",");
  458. actualFirstLog.Append(actualFirst.forwardingUnitName.ToString() + ",");
  459. //actualFirstLog.Append(actualFirst.customerSupplierName.ToString() + ",");
  460. //actualFirstLog.Append(actualFirst.loadPointName.ToString() + ",");
  461. actualFirstLog.Append(actualFirst.memo.ToString() + ",");
  462. actualFirstLog.Append(actualFirst.weightType.ToString() + ",");
  463. actualFirstLog.Append(actualFirst.meterWeight.ToString());
  464. logCsv.WriteDataLog("actualFirst", actualFirstLog.ToString());
  465. //截图初始化
  466. bd.setBaseDb();
  467. bd.getBaseDb();
  468. //图片截图保存
  469. shotImage(actualFirst.actualFirstNo.ToString());
  470. MessageBox.Show("保存成功");
  471. }
  472. catch (Exception e)
  473. {
  474. MessageBox.Show("保存失败:" + e.Message);
  475. }
  476. }
  477. /// <summary>
  478. /// 上传数据
  479. /// </summary>
  480. private void uploadData()
  481. {
  482. openFileDialogUpData.Filter = "csv文件|*.csv";//只允许csv文件;*.csv;*.xlsx
  483. openFileDialogUpData.Title = "选择要导入的计量数据文件"; //弹出框头部显示
  484. openFileDialogUpData.AddExtension = true; //自动增加后缀
  485. openFileDialogUpData.AutoUpgradeEnabled = true; //是否随系统自动升级弹出窗口样式
  486. openFileDialogUpData.InitialDirectory = Application.StartupPath + "\\alonData\\" + DateTime.Now.ToString("yyyy-MM-dd");//默认打开当前目录
  487. //openFileDialog1.Multiselect = true;//该值确定是否可以选择多个文件
  488. if (openFileDialogUpData.ShowDialog() == DialogResult.OK)
  489. {
  490. DataTable csvDataTable = OpenCSV(openFileDialogUpData.FileName);
  491. int rowCount = csvDataTable.Rows.Count;
  492. if (csvDataTable != null && csvDataTable.Rows.Count > 0)
  493. {
  494. List<MeterWorkCarActualFirst> lp = csvDataTable.TableToDataList<MeterWorkCarActualFirst>();
  495. DialogResult dr = MessageBox.Show("共 " + lp.Count + " 条计量数据,是否继续上传?", "提示", MessageBoxButtons.OKCancel);
  496. if (dr != DialogResult.OK)
  497. {
  498. return;
  499. }
  500. //操作日志
  501. RESTfulResult<string> rES = actualFirstService.addEmergency(lp);
  502. if (rES.Succeed)
  503. {
  504. MessageBox.Show("数据上传成功!");
  505. }
  506. else
  507. {
  508. MessageBox.Show("数据上传失败:" + rES.Message);
  509. }
  510. }
  511. };
  512. }
  513. public static DataTable OpenCSV(string filePath)
  514. {
  515. Encoding encoding = Encoding.UTF8; //Common.GetType(filePath); //Encoding.ASCII;//
  516. DataTable dt = new DataTable();
  517. FileStream fs = new FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
  518. //StreamReader sr = new StreamReader(fs, Encoding.UTF8);
  519. StreamReader sr = new StreamReader(fs, encoding);
  520. //string fileContent = sr.ReadToEnd();
  521. //encoding = sr.CurrentEncoding;
  522. //记录每次读取的一行记录
  523. string strLine = "";
  524. //记录每行记录中的各字段内容
  525. string[] aryLine = null;
  526. string[] tableHead = null;
  527. //标示列数
  528. int columnCount = 0;
  529. //标示是否是读取的第一行
  530. bool IsFirst = true;
  531. //逐行读取CSV中的数据
  532. while ((strLine = sr.ReadLine()) != null)
  533. {
  534. //strLine = Common.ConvertStringUTF8(strLine, encoding);
  535. //strLine = Common.ConvertStringUTF8(strLine);
  536. if (IsFirst == true)
  537. {
  538. tableHead = strLine.Split(',');
  539. IsFirst = false;
  540. columnCount = tableHead.Length;
  541. //创建列
  542. for (int i = 0; i < columnCount; i++)
  543. {
  544. DataColumn dc = new DataColumn(tableHead[i]);
  545. //DataColumn dc = new DataColumn(i.ToString());
  546. dt.Columns.Add(dc);
  547. switch (i) { }
  548. }
  549. }
  550. else
  551. {
  552. aryLine = strLine.Split(',');
  553. DataRow dr = dt.NewRow();
  554. for (int j = 0; j < columnCount; j++)
  555. {
  556. dr[j] = aryLine[j];
  557. }
  558. dt.Rows.Add(dr);
  559. }
  560. }
  561. //if (aryLine != null && aryLine.Length > 0)
  562. //{
  563. // dt.DefaultView.Sort = tableHead[0] + " " + "asc";
  564. //}
  565. sr.Close();
  566. fs.Close();
  567. return dt;
  568. }
  569. /// <summary>
  570. /// 数据查询
  571. /// </summary>
  572. private void doQuery()
  573. {
  574. if (System.IO.Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "alonData\\" + DateTime.Now.ToString("yyyy-MM-dd")) == true
  575. && System.IO.File.Exists(AppDomain.CurrentDomain.BaseDirectory + "alonData\\" + DateTime.Now.ToString("yyyy-MM-dd") + "\\actualFirst.csv") == true)
  576. {
  577. DataTable csvDataTable = OpenCSV(AppDomain.CurrentDomain.BaseDirectory + "alonData\\" + DateTime.Now.ToString("yyyy-MM-dd") + "\\actualFirst.csv");
  578. int rowCount = csvDataTable.Rows.Count;
  579. if (csvDataTable != null && csvDataTable.Rows.Count > 0)
  580. {
  581. List<MeterWorkCarActFirAlon> lp = csvDataTable.TableToDataList<MeterWorkCarActFirAlon>();
  582. rowCount = lp.Count;
  583. //毛重数据查询
  584. DataTable dtGross = csvDataTable.Clone();
  585. List<MeterWorkCarActFirAlon> lpGross = lp.Where(s => s.weightType == "0").ToList();
  586. if (lpGross != null && lpGross.Count > 0)
  587. {
  588. dtGross = lpGross.ListToDataTable<MeterWorkCarActFirAlon>();
  589. }
  590. ClsControlPack.CopyDataToDatatable(ref dtGross, ref this.dtCarActFirstGross, true);
  591. foreach (var item in this.ultraGridGross.Rows)
  592. {
  593. item.Cells["weightType"].Value = GetWeightType(item.Cells["weightType"].Value.ToString());
  594. }
  595. ClsControlPack.RefreshAndAutoSize(ultraGridGross);
  596. //皮重数据查询
  597. DataTable dtTare = csvDataTable.Clone();
  598. List<MeterWorkCarActFirAlon> lpTare = lp.Where(s => s.weightType == "1").ToList();
  599. if (lpTare != null && lpTare.Count > 0)
  600. {
  601. dtTare = lpTare.ListToDataTable<MeterWorkCarActFirAlon>();
  602. }
  603. ClsControlPack.CopyDataToDatatable(ref dtTare, ref this.dtCarActFirstTare, true);
  604. foreach (var item in this.ultraGridTare.Rows)
  605. {
  606. item.Cells["weightType"].Value = GetWeightType(item.Cells["weightType"].Value.ToString());
  607. }
  608. ClsControlPack.RefreshAndAutoSize(ultraGridTare);
  609. }
  610. }
  611. else
  612. {
  613. MessageBox.Show("未查询到当天的数据文件!");
  614. }
  615. }
  616. /// <summary>
  617. /// 重量类型
  618. /// </summary>
  619. /// <param name="strCode"></param>
  620. /// <returns></returns>
  621. public static string GetWeightType(string strCode)
  622. {
  623. string weightType = ""; //重量类型(0:毛重;1:皮重)
  624. switch (strCode)
  625. {
  626. case "0": weightType = "毛重"; break;
  627. case "1": weightType = "皮重"; break;
  628. default:
  629. break;
  630. }
  631. return weightType;
  632. }
  633. /// <summary>
  634. /// 记录错误日志
  635. /// </summary>
  636. /// <param name="str"></param>
  637. private void WriteCatchLog(string str)
  638. {
  639. try
  640. {
  641. string m_szRunPath = System.Environment.CurrentDirectory.ToString();
  642. if (!(System.IO.Directory.Exists(m_szRunPath + "\\log")))
  643. {
  644. System.IO.Directory.CreateDirectory(m_szRunPath + "\\log");
  645. }
  646. string strDate = System.DateTime.Now.ToString("yyyyMMddhh");
  647. System.IO.TextWriter tw = new System.IO.StreamWriter(m_szRunPath + "\\log\\catch.log", true);
  648. tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\t" + str);
  649. tw.Close();
  650. }
  651. catch
  652. {
  653. }
  654. }
  655. /// <summary>
  656. /// 保存物料信息到csv文件
  657. /// </summary>
  658. /// <param name="result"></param>
  659. private void saveMatterInfo(RESTfulResult<List<MeterBaseMatterInfo>> result)
  660. {
  661. StringBuilder materInfoHead = new StringBuilder(); // 物料头
  662. materInfoHead.Append("matterNo,");//若第一行默认为当前时间则第一行多个逗号
  663. materInfoHead.Append("matterName");
  664. logCsv.WriteBaseData("MeterBaseMatterInfo", materInfoHead.ToString());
  665. if (result.Succeed)
  666. {
  667. StringBuilder materInfo = new StringBuilder(); // 物料体
  668. List<MeterBaseMatterInfo> list = result.Data;
  669. foreach (MeterBaseMatterInfo info in list)
  670. {
  671. materInfo.Clear();
  672. materInfo.Append(info.matterNo + ",");
  673. materInfo.Append(info.matterName + ",");
  674. logCsv.WriteBaseData("MeterBaseMatterInfo", materInfo.ToString());
  675. }
  676. }
  677. }
  678. /// <summary>
  679. /// 保存收发货单位信息到csv文件
  680. /// </summary>
  681. /// <param name="result"></param>
  682. private void saveCustomerInfo(RESTfulResult<List<MeterBaseCustomerSupplier>> result)
  683. {
  684. StringBuilder customerInfoHead = new StringBuilder(); // 客商头
  685. customerInfoHead.Append("customerSupplierNo,");//若第一行默认为当前时间则第一行多个逗号
  686. customerInfoHead.Append("customerSupplierName");
  687. logCsv.WriteBaseData("MeterBaseCustomerSupplier", customerInfoHead.ToString());
  688. if (result.Succeed)
  689. {
  690. StringBuilder customerInfo = new StringBuilder(); // 物料体
  691. List<MeterBaseCustomerSupplier> list = result.Data;
  692. if(result.Data != null)
  693. {
  694. foreach (MeterBaseCustomerSupplier info in list)
  695. {
  696. customerInfo.Clear();
  697. customerInfo.Append(info.customerSupplierNo + ",");
  698. customerInfo.Append(info.customerSupplierName + ",");
  699. logCsv.WriteBaseData("MeterBaseCustomerSupplier", customerInfo.ToString());
  700. }
  701. }
  702. }
  703. }
  704. /// <summary>
  705. /// 保存计量点信息到csv文件
  706. /// </summary>
  707. /// <param name="result"></param>
  708. private void saveSpotInfo(RESTfulResult<List<MeterBaseSpotInfo>> result)
  709. {
  710. StringBuilder spotInfoHead = new StringBuilder(); // 客商头
  711. spotInfoHead.Append("spotNo,");//若第一行默认为当前时间则第一行多个逗号
  712. spotInfoHead.Append("spotName");
  713. logCsv.WriteBaseData("MeterBaseSpotInfo", spotInfoHead.ToString());
  714. if (result.Succeed)
  715. {
  716. StringBuilder spotInfo = new StringBuilder(); // 物料体
  717. List<MeterBaseSpotInfo> list = result.Data;
  718. if (result.Data != null)
  719. {
  720. foreach (MeterBaseSpotInfo info in list)
  721. {
  722. spotInfo.Clear();
  723. spotInfo.Append(info.baseSpotNo + ",");
  724. spotInfo.Append(info.baseSpotName + ",");
  725. logCsv.WriteBaseData("MeterBaseSpotInfo", spotInfo.ToString());
  726. }
  727. }
  728. }
  729. }
  730. /// <summary>
  731. /// 从csv文件读取物料信息
  732. /// </summary>
  733. /// <returns></returns>
  734. private DataTable selectMatterInfo()
  735. {
  736. if (System.IO.Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "baseData") == true
  737. && System.IO.File.Exists(AppDomain.CurrentDomain.BaseDirectory + "baseData\\MeterBaseMatterInfo.csv") == true)
  738. {
  739. string path = AppDomain.CurrentDomain.BaseDirectory + "baseData\\MeterBaseMatterInfo.csv";
  740. DataTable csvDataTable = OpenCSV(path);
  741. int rowCount = csvDataTable.Rows.Count;
  742. if (csvDataTable != null && csvDataTable.Rows.Count > 0)
  743. {
  744. DataTable newCsvDataTable = new DataTable();
  745. newCsvDataTable.Columns.Add("name");
  746. newCsvDataTable.Columns.Add("value");
  747. for (int i = 1; i < csvDataTable.Rows.Count; i++)
  748. {
  749. DataRow dr = newCsvDataTable.NewRow();
  750. dr[0] = csvDataTable.Rows[i]["matterName"].ToString();
  751. dr[1] = csvDataTable.Rows[i]["matterNo"].ToString();
  752. newCsvDataTable.Rows.Add(dr);
  753. }
  754. return newCsvDataTable;
  755. }
  756. else
  757. {
  758. return null;
  759. }
  760. }
  761. else
  762. {
  763. return null;
  764. }
  765. }
  766. /// <summary>
  767. /// 从csv文件读取收发货单位信息
  768. /// </summary>
  769. /// <returns></returns>
  770. private DataTable selectCustomerInfo()
  771. {
  772. if (System.IO.Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "baseData") == true
  773. && System.IO.File.Exists(AppDomain.CurrentDomain.BaseDirectory + "baseData\\MeterBaseCustomerSupplier.csv") == true)
  774. {
  775. string path = AppDomain.CurrentDomain.BaseDirectory + "baseData\\MeterBaseCustomerSupplier.csv";
  776. DataTable csvDataTable = OpenCSV(path);
  777. int rowCount = csvDataTable.Rows.Count;
  778. if (csvDataTable != null && csvDataTable.Rows.Count > 0)
  779. {
  780. DataTable newCsvDataTable = new DataTable();
  781. newCsvDataTable.Columns.Add("name");
  782. newCsvDataTable.Columns.Add("value");
  783. for (int i = 1; i < csvDataTable.Rows.Count; i++)
  784. {
  785. DataRow dr = newCsvDataTable.NewRow();
  786. dr[0] = csvDataTable.Rows[i]["customerSupplierName"].ToString();
  787. dr[1] = csvDataTable.Rows[i]["customerSupplierNo"].ToString();
  788. newCsvDataTable.Rows.Add(dr);
  789. }
  790. return newCsvDataTable;
  791. }
  792. else
  793. {
  794. return null;
  795. }
  796. }
  797. else
  798. {
  799. return null;
  800. }
  801. }
  802. /// <summary>
  803. /// 从csv文件读取计量点信息
  804. /// </summary>
  805. /// <returns></returns>
  806. private DataTable selectSpotInfo()
  807. {
  808. if (System.IO.Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "baseData") == true
  809. && System.IO.File.Exists(AppDomain.CurrentDomain.BaseDirectory + "baseData\\MeterBaseSpotInfo.csv") == true)
  810. {
  811. string path = AppDomain.CurrentDomain.BaseDirectory + "baseData\\MeterBaseSpotInfo.csv";
  812. DataTable csvDataTable = OpenCSV(path);
  813. int rowCount = csvDataTable.Rows.Count;
  814. if (csvDataTable != null && csvDataTable.Rows.Count > 0)
  815. {
  816. DataTable newCsvDataTable = new DataTable();
  817. newCsvDataTable.Columns.Add("name");
  818. newCsvDataTable.Columns.Add("value");
  819. for (int i = 1; i < csvDataTable.Rows.Count; i++)
  820. {
  821. DataRow dr = newCsvDataTable.NewRow();
  822. dr[0] = csvDataTable.Rows[i]["spotName"].ToString();
  823. dr[1] = csvDataTable.Rows[i]["spotNo"].ToString();
  824. newCsvDataTable.Rows.Add(dr);
  825. }
  826. return newCsvDataTable;
  827. }
  828. else
  829. {
  830. return null;
  831. }
  832. }
  833. else
  834. {
  835. return null;
  836. }
  837. }
  838. /// <summary>
  839. /// 终端截图
  840. /// </summary>
  841. public void shotImage(string actualFirstNo)
  842. {
  843. try
  844. {
  845. string spotNo = ConfigurationManager.AppSettings["spotNo"].ToString().Trim();
  846. #region 截取图片信息
  847. //截取屏幕信息
  848. Point screenPoint = this.PointToScreen(new Point());
  849. Rectangle rect = new Rectangle(screenPoint, this.Size);
  850. Image img = new Bitmap(rect.Width, rect.Height);
  851. Graphics g = Graphics.FromImage(img);
  852. g.CopyFromScreen(rect.X - 1, rect.Y - 1, 0, 0, rect.Size);//"D://file/1.jpg"
  853. img.Save(string.Format("{0}baseData\\imgShort\\formalImg\\{1}_{2}_{3}.jpg",
  854. AppDomain.CurrentDomain.SetupInformation.ApplicationBase,
  855. spotNo, actualFirstNo, 7), System.Drawing.Imaging.ImageFormat.Jpeg);
  856. //(PbCache.videoChild.Count + 1)), System.Drawing.Imaging.ImageFormat.Jpeg);
  857. //*
  858. //最后进行截图操作
  859. CameraShotCls cameraShot = new CameraShotCls();
  860. cameraShot.CapMethod(actualFirstNo);
  861. //*/
  862. #endregion 截取图片信息
  863. }
  864. catch (Exception ex)
  865. {
  866. }
  867. }
  868. }
  869. }