autoPacking6151.ashx 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  1. <%@ WebHandler Language="C#" Class="autoPacking6151" %>
  2. using System;
  3. using System.Web;
  4. using System.Data;
  5. using DK.XuWei.WebMes;
  6. using Curtain.DataAccess;
  7. using Newtonsoft.Json.Linq;
  8. /// <summary>
  9. /// PLC对接使用
  10. /// 带参数barcode,扫描头处理逻辑;
  11. /// 不带参数barcode,轮询处理逻辑;
  12. /// xuwei add 2023-12-31
  13. /// </summary>
  14. public class autoPacking6151 : IHttpHandler
  15. {
  16. public static class PLC
  17. {
  18. public static string plcIp = "172.18.36.95";
  19. public static int plcPort = 102;
  20. public static string dbBarcode = "151.2"; //151.0 => 151.2
  21. public static string dbGoodsId = "151.14";
  22. public static string dbGoodsNum = "151.16";
  23. public static string dbResult = "151.18";
  24. public static string dbLogoId = "151.22";//替换产品等级
  25. public static string dbMaterialcode = "151.30";
  26. public static string dbRead = "151.26";
  27. public static string[] dbBarcodeBlock = { "150.2", "150.32", "150.62", "150.92", "150.122", "150.152", "150.182", "150.212", "150.242", "150.272", "150.302", "150.332" };
  28. public static string dbBanMa = "150.362";//150.360 => 150.362
  29. public static string dbUsercode = "150.376"; //150.374 => 150.376
  30. public static string dbGoodsCodes = "151.46"; //151.44 => 151.46
  31. }
  32. public static class API
  33. {
  34. public static string accountCode = "dongke";
  35. public static string userId = "1529";
  36. public static string userCode = "DONGKEXW";
  37. public static string userName = "徐伟";
  38. public static string userPassword = "D63AC55256F8048265387A2B0388F1D2";
  39. public static string sessionKey = "C2A5AE55-8E9B-45F9-A0AF-6A5A1BF3457C";
  40. public static int procedureID = 107; //3#包装
  41. public static bool debug = false;
  42. }
  43. public bool resultFlag = false;
  44. public JArray result = new JArray();
  45. public S7 s7 = new S7();
  46. //打开PLC
  47. public int PlcOpen()
  48. {
  49. try
  50. {
  51. s7.Open(PLC.plcIp, PLC.plcPort);
  52. resultFlag = true;
  53. result.Add(new JObject(new JProperty("PLC连接", PLC.plcIp + ":" + PLC.plcPort + " 连接成功!")));
  54. }
  55. catch (Exception e)
  56. {
  57. resultFlag = false;
  58. result.Add(new JObject(new JProperty("PLC连接", PLC.plcIp + ":" + PLC.plcPort + " 连接失败!")));
  59. result.Add(new JObject(new JProperty("PLC错误信息", e.Message)));
  60. }
  61. return 1;
  62. }
  63. //关闭PLC
  64. public int PlcClose()
  65. {
  66. s7.Close();
  67. result.Add(new JObject(new JProperty("PLC关闭", PLC.plcIp + ":" + PLC.plcPort + " 关闭成功!")));
  68. return 1;
  69. }
  70. //读取PLC标识
  71. public int PlcReadReadFlag()
  72. {
  73. if (!resultFlag) return -1;
  74. try
  75. {
  76. int read = Convert.ToInt16(s7.Read<short>(PLC.dbRead));
  77. result.Add(new JObject(new JProperty("PLC读取(读取标识:OK)", PLC.dbRead + " = " + read.ToString())));
  78. return read;
  79. }
  80. catch (Exception e)
  81. {
  82. result.Add(new JObject(new JProperty("PLC错误信息:ERR", e.Message)));
  83. resultFlag = false;
  84. return -1;
  85. }
  86. }
  87. //读取PLC产品ID
  88. public int PlcReadGoodsid()
  89. {
  90. if (!resultFlag) return -1;
  91. try
  92. {
  93. int read = Convert.ToInt16(s7.Read<short>(PLC.dbGoodsId));
  94. result.Add(new JObject(new JProperty("PLC读取(产品ID:OK)", PLC.dbGoodsId + " = " + read.ToString())));
  95. return read;
  96. }
  97. catch (Exception e)
  98. {
  99. result.Add(new JObject(new JProperty("PLC错误信息:ERR", e.Message)));
  100. resultFlag = false;
  101. return -1;
  102. }
  103. }
  104. //读取装板数量
  105. public int PlcReadGoodsnum()
  106. {
  107. if (!resultFlag) return -1;
  108. try
  109. {
  110. int read = Convert.ToInt16(s7.Read<short>(PLC.dbGoodsNum));
  111. result.Add(new JObject(new JProperty("PLC读取(装板数量:OK)", PLC.dbGoodsNum + " = " + read.ToString())));
  112. return read;
  113. }
  114. catch (Exception e)
  115. {
  116. result.Add(new JObject(new JProperty("PLC错误信息:ERR", e.Message)));
  117. resultFlag = false;
  118. return -1;
  119. }
  120. }
  121. //读取PLC商标ID
  122. public int PlcReadLogoid()
  123. {
  124. if (!resultFlag) return -1;
  125. try
  126. {
  127. int read = Convert.ToInt16(s7.Read<short>(PLC.dbLogoId));
  128. result.Add(new JObject(new JProperty("PLC读取(产品ID:OK)", PLC.dbLogoId + " = " + read.ToString())));
  129. return read;
  130. }
  131. catch (Exception e)
  132. {
  133. result.Add(new JObject(new JProperty("PLC错误信息:ERR", e.Message)));
  134. resultFlag = false;
  135. return -1;
  136. }
  137. }
  138. public string[] PlcReadBarcodes(int goodsNum)
  139. {
  140. if (!resultFlag) return null;
  141. try
  142. {
  143. string[] barCodes = new string[12];
  144. JObject obj = new JObject();
  145. for (int i = 0; i < goodsNum; i++)
  146. {
  147. barCodes[i] = s7.Read<string>(PLC.dbBarcodeBlock[i], 11).ToString();
  148. obj.Add(new JProperty("条码" + (i + 1).ToString().PadLeft(2, '0') + "(" + PLC.dbBarcodeBlock[i] + ")", barCodes[i]));
  149. }
  150. result.Add(new JObject(new JProperty("PLC读取(产品条码:OK)", obj)));
  151. return barCodes;
  152. }
  153. catch (Exception e)
  154. {
  155. result.Add(new JObject(new JProperty("PLC错误信息:ERR", e.Message)));
  156. resultFlag = false;
  157. return null;
  158. }
  159. }
  160. //读取板码
  161. public string PlcReadBanma()
  162. {
  163. if (!resultFlag) return "";
  164. try
  165. {
  166. string read = s7.Read<string>(PLC.dbBanMa, 8).ToString();
  167. result.Add(new JObject(new JProperty("PLC读取(板码:OK)", PLC.dbBanMa + " = " + read.ToString())));
  168. return read;
  169. }
  170. catch (Exception e)
  171. {
  172. result.Add(new JObject(new JProperty("PLC错误信息:ERR", e.Message)));
  173. resultFlag = false;
  174. return "";
  175. }
  176. }
  177. //读取物料号
  178. public string PlcReadMaterialcode()
  179. {
  180. if (!resultFlag) return "";
  181. try
  182. {
  183. string read = s7.Read<string>(PLC.dbMaterialcode, 14).ToString();
  184. result.Add(new JObject(new JProperty("PLC读取(物料号:OK)", PLC.dbMaterialcode + " = " + read.ToString())));
  185. return read;
  186. }
  187. catch (Exception e)
  188. {
  189. result.Add(new JObject(new JProperty("PLC错误信息:ERR", e.Message)));
  190. resultFlag = false;
  191. return "";
  192. }
  193. }
  194. //读取结果标识
  195. public int PlcReadResultNum()
  196. {
  197. if (!resultFlag) return -1;
  198. try
  199. {
  200. int read = Convert.ToInt32(s7.Read<short>(PLC.dbResult));
  201. result.Add(new JObject(new JProperty("PLC读取(结果:OK)", PLC.dbResult + " = " + read.ToString())));
  202. return read;
  203. }
  204. catch (Exception e)
  205. {
  206. result.Add(new JObject(new JProperty("PLC错误信息:ERR", e.Message)));
  207. resultFlag = false;
  208. return -1;
  209. }
  210. }
  211. //读取USERCODE
  212. public string PlcReadUsercode()
  213. {
  214. if (!resultFlag) return "";
  215. try
  216. {
  217. string read = s7.Read<string>(PLC.dbUsercode,6).ToString();
  218. result.Add(new JObject(new JProperty("PLC读取(USECODE:OK)", PLC.dbUsercode + " = " + read.ToString())));
  219. return read;
  220. }
  221. catch (Exception e)
  222. {
  223. result.Add(new JObject(new JProperty("PLC错误信息:ERR", e.Message)));
  224. resultFlag = false;
  225. return "";
  226. }
  227. }
  228. //写入条码
  229. public int PlcWriteBarcode(string barCode)
  230. {
  231. if (!resultFlag) return -1;
  232. try
  233. {
  234. s7.Write<string>(PLC.dbBarcode, barCode);
  235. result.Add(new JObject(new JProperty("PLC写入(条码:OK)", PLC.dbBarcode + " = " + barCode)));
  236. resultFlag = true;
  237. return 1;
  238. }
  239. catch (Exception e)
  240. {
  241. result.Add(new JObject(new JProperty("PLC错误信息:ERR", e.Message)));
  242. resultFlag = false;
  243. return -1;
  244. }
  245. }
  246. //写入物料码
  247. public int PlcWriteMaterialcode(string materialCode)
  248. {
  249. if (!resultFlag) return -1;
  250. try
  251. {
  252. s7.Write<string>(PLC.dbMaterialcode, materialCode);
  253. result.Add(new JObject(new JProperty("PLC写入(物料码:OK)", PLC.dbMaterialcode + " = " + materialCode)));
  254. resultFlag = true;
  255. return 1;
  256. }
  257. catch (Exception e)
  258. {
  259. result.Add(new JObject(new JProperty("PLC错误信息:ERR", e.Message)));
  260. resultFlag = false;
  261. return -1;
  262. }
  263. }
  264. //写入产品型号
  265. public int PlcWriteGoodsCodes(string goodsCodes)
  266. {
  267. if (!resultFlag) return -1;
  268. try
  269. {
  270. s7.Write<string>(PLC.dbGoodsCodes, goodsCodes);
  271. result.Add(new JObject(new JProperty("PLC写入(产品型号:OK)", PLC.dbGoodsCodes + " = " + goodsCodes)));
  272. resultFlag = true;
  273. return 1;
  274. }
  275. catch (Exception e)
  276. {
  277. result.Add(new JObject(new JProperty("PLC错误信息:ERR", e.Message)));
  278. resultFlag = false;
  279. return -1;
  280. }
  281. }
  282. //写入产品ID
  283. public int PlcWriteGoodsid(Int16 goodsId)
  284. {
  285. if (!resultFlag) return -1;
  286. try
  287. {
  288. s7.Write<short>(PLC.dbGoodsId, goodsId);
  289. result.Add(new JObject(new JProperty("PLC写入(产品ID:OK)", PLC.dbGoodsId + " = " + goodsId)));
  290. resultFlag = true;
  291. return 1;
  292. }
  293. catch (Exception e)
  294. {
  295. result.Add(new JObject(new JProperty("PLC错误信息:ERR", e.Message)));
  296. resultFlag = false;
  297. return -1;
  298. }
  299. }
  300. //写入商标ID
  301. public int PlcWriteLogoid(Int16 logoId)
  302. {
  303. if (!resultFlag) return -1;
  304. try
  305. {
  306. s7.Write<short>(PLC.dbLogoId, logoId);
  307. result.Add(new JObject(new JProperty("PLC写入(商标ID:OK)", PLC.dbLogoId + " = " + logoId)));
  308. resultFlag = true;
  309. return 1;
  310. }
  311. catch (Exception e)
  312. {
  313. result.Add(new JObject(new JProperty("PLC错误信息:ERR", e.Message)));
  314. resultFlag = false;
  315. return -1;
  316. }
  317. }
  318. //写入装板数量
  319. public int PlcWriteGoodsnum(Int16 goodsNum)
  320. {
  321. if (!resultFlag) return -1;
  322. try
  323. {
  324. s7.Write<short>(PLC.dbGoodsNum, goodsNum);
  325. result.Add(new JObject(new JProperty("PLC写入(装板数量:OK)", PLC.dbGoodsNum + " = " + goodsNum)));
  326. resultFlag = true;
  327. return 1;
  328. }
  329. catch (Exception e)
  330. {
  331. result.Add(new JObject(new JProperty("PLC错误信息:ERR", e.Message)));
  332. resultFlag = false;
  333. return -1;
  334. }
  335. }
  336. //写入执行结果
  337. // 1 通过 2 条码格式错误 3 产品不可到达 4 MES异常 5 条码不一致 6 绑板完成 7 工号读取错误
  338. public int PlcWriteResult(Int16 flag)
  339. {
  340. try
  341. {
  342. string message = "";
  343. switch(flag)
  344. {
  345. case 1:message = "扫码通过:OK";break;
  346. case 2:message = "条码格式错误:ERR";break;
  347. case 3:message = "产品不可到达:ERR";break;
  348. case 4:message = "MES异常:ERR";break;
  349. case 5:message = "条码不一致:ERR";break;
  350. case 6:message = "绑板完成:OK";break;
  351. case 7:message = "工号读取错误:ERR";break;
  352. default:message = "未定义:ERR";break;
  353. }
  354. s7.Write<short>(PLC.dbResult, flag);
  355. result.Add(new JObject(new JProperty("PLC写入("+message+")", PLC.dbResult + " = " + flag.ToString())));
  356. return 1;
  357. }
  358. catch (Exception e)
  359. {
  360. result.Add(new JObject(new JProperty("PLC错误信息:ERROR", e.Message)));
  361. return -1;
  362. }
  363. }
  364. //加载默认参数
  365. public static JObject ApiLoadDefaultPara()
  366. {
  367. JObject userPara = new JObject();
  368. userPara.Add(new JProperty("accountCode", API.accountCode));
  369. userPara.Add(new JProperty("userCode", API.userCode));
  370. userPara.Add(new JProperty("userPassword", API.userPassword));
  371. userPara.Add(new JProperty("sessionKey", API.sessionKey));
  372. return userPara;
  373. }
  374. //PDA接口验证条码
  375. public int ApiGetStatusBarcode(string barCode)
  376. {
  377. if (!resultFlag) return -1;
  378. try
  379. {
  380. WCF wcf = new WCF();
  381. wcf.Para = ApiLoadDefaultPara();
  382. wcf.Para.Add(new JProperty("procedureID", API.procedureID));
  383. wcf.Para.Add(new JProperty("barCode", barCode));
  384. //测试用
  385. string jsonStr = @"
  386. {
  387. 'd': {
  388. '__type': 'ActionResult:#Dongke.IBOSS.PRD.WCF.DataModels',
  389. 'Message': '读取成功!',
  390. 'Result': '{\'data\':[{\'STATUS\':0.0}]}',
  391. 'Status': 0
  392. }
  393. }
  394. ".Replace("'","\"");
  395. //正式调用
  396. if (!API.debug) jsonStr = wcf.Post("/DKService/PDAModuleService/GetStatusByBarcode");
  397. JObject json = JObject.Parse(jsonStr);
  398. if (json["d"]["Status"].ToString() == "0")
  399. {
  400. result.Add(new JObject(
  401. new JProperty("API调用(条码状态:OK)", "操作成功!"),
  402. new JProperty("接口", "/DKService/PDAModuleService/GetStatusByBarcode"),
  403. new JProperty("参数(procedureID)", API.procedureID),
  404. new JProperty("参数(barCode)", barCode)
  405. //,new JProperty("结果", jsonStr)
  406. )
  407. );
  408. return 1;
  409. }
  410. else
  411. {
  412. resultFlag = false;
  413. result.Add(new JObject(
  414. new JProperty("API调用(条码状态:ERR)", json["d"]["Message"].ToString()),
  415. new JProperty("接口", "/DKService/PDAModuleService/GetStatusByBarcode"),
  416. new JProperty("参数(procedureID)", API.procedureID),
  417. new JProperty("参数(barCode)", barCode)
  418. //,new JProperty("结果", jsonStr)
  419. )
  420. );
  421. return -1;
  422. }
  423. }
  424. catch (Exception e)
  425. {
  426. resultFlag = false;
  427. result.Add(new JObject(
  428. new JProperty("API调用(条码状态:ERROR)", "系统异常!"),
  429. new JProperty("接口", "/DKService/PDAModuleService/GetStatusByBarcode"),
  430. new JProperty("参数(procedureID)", API.procedureID),
  431. new JProperty("参数(barCode)", barCode),
  432. new JProperty("异常", e.Message)
  433. )
  434. );
  435. return -1;
  436. }
  437. }
  438. public int ApiCheckBarcode(string barCode)
  439. {
  440. if (!resultFlag) return -1;
  441. try
  442. {
  443. WCF wcf = new WCF();
  444. wcf.Para = ApiLoadDefaultPara();
  445. wcf.Para.Add(new JProperty("procedureID", API.procedureID));
  446. wcf.Para.Add(new JProperty("barcode", barCode));
  447. //测试用
  448. string jsonStr = @"
  449. {
  450. 'd': {
  451. '__type': 'ActionResult:#Dongke.IBOSS.PRD.WCF.DataModels',
  452. 'Message': '操作成功',
  453. 'Result': '[{\'out_errMsg\':\'\',\'out_goodsID\':\'179\',\'out_goodsCode\':\'H0175M\',\'out_goodsName\':\'HC0175PT-305\',\'out_groutingUserCode\':\'SFC035\',\'out_groutingUserName\':null,\'out_groutingUserID\':null,\'out_groutingNum\':\'73\',\'out_mouldCode\':\'C06B11-001\',\'out_ispublicbody\':\'0\',\'out_ispublicbodyTrach\':\'\',\'out_groutingdate\':\'2023/12/30 0:00:00\',\'out_specialRepairFlag\':\'0\',\'out_isReFire\':\'0\',\'out_isLengBu\':\'0\',\'out_missFlag\':\'0\',\'out_logoID\':\'15\',\'out_logoCode\':\'020\',\'out_logoName\':\'HEGII(新)\',\'out_MaterialCode\':\'CT175PD2210B01\',\'out_glazeName\':\'智洁釉\',\'out_deliverLimitCycle\':null,\'out_barcode\':\'00000000001\',\'out_WaterLabelCode\':null,\'out_CodeCheckFlag\':null,\'out_LeakFlag1\':\'\',\'out_LeakFlag2\':\'\',\'out_LeakFlag3\':\'\',\'out_specialRepairFlagName\':\'否\',\'out_isReFireName\':\'否\',\'out_LeakFlag1Name\':\'未检测\',\'out_LeakFlag2Name\':\'未检测\',\'out_LeakFlag3Name\':\'未检测\',\'out_lengBuName\':\'否\',\'out_LeakFlag4\':\'\',\'out_LeakFlag5\':\'\',\'out_LeakFlag4Name\':\'未检测\',\'out_LeakFlag5Name\':\'未检测\',\'GOODSMODELforCheck\':\'CT175PD2210B01#0\',\'PlateLimitNum\':12,\'DefectFlagID\':null,\'pdid\':null,\'InspectionLevel\':\'\',\'PackingDefect\':\'\',\'InspectionGoodsLevel\':\'\',\'offlineFlag\':\'0\',\'recyclingFlag\':\'0\',\'waterLabelCode\':\'http://wl.bbqk.com/2bwta/0.html\',\'PLCWeight\':0.0}]',
  454. 'Status': 0
  455. }
  456. }
  457. ".Replace("'","\"");
  458. //正式用
  459. if (!API.debug) jsonStr = wcf.Post("/DKService/PDAModuleService/CheckBarcode");
  460. JObject json = JObject.Parse(jsonStr);
  461. JArray jsonArray = JArray.Parse(json["d"]["Result"].ToString());
  462. if (json["d"]["Status"].ToString() == "0" && jsonArray[0]["out_errMsg"].ToString()== "")
  463. {
  464. result.Add(new JObject(
  465. new JProperty("API调用(条码验证:OK)", "操作成功!"),
  466. new JProperty("接口", "/DKService/PDAModuleService/CheckBarcode"),
  467. new JProperty("参数(procedureID)", API.procedureID),
  468. new JProperty("参数(barcode)", barCode)
  469. //,new JProperty("结果", jsonStr)
  470. )
  471. );
  472. return 1;
  473. }
  474. else
  475. {
  476. resultFlag = false;
  477. result.Add(new JObject(
  478. new JProperty("API调用(条码验证:ERR)", json["d"]["Message"].ToString()),
  479. new JProperty("接口", "/DKService/PDAModuleService/CheckBarcode"),
  480. new JProperty("参数(procedureID)", API.procedureID),
  481. new JProperty("参数(barcode)", barCode)
  482. ,new JProperty("结果", jsonStr)
  483. )
  484. );
  485. return -1;
  486. }
  487. }
  488. catch (Exception e)
  489. {
  490. resultFlag = false;
  491. result.Add(new JObject(
  492. new JProperty("API调用(条码验证:ERROR)", "系统异常!"),
  493. new JProperty("接口", "/DKService/PDAModuleService/CheckBarcode"),
  494. new JProperty("参数(procedureID)", API.procedureID),
  495. new JProperty("参数(barcode)", barCode),
  496. new JProperty("异常", e.Message)
  497. )
  498. );
  499. return -1;
  500. }
  501. }
  502. public int ApiFinishedLoadingCar(string barCode,int logoId,int goodsId,int banMa = 0)
  503. {
  504. if (!resultFlag) return -1;
  505. string jsonData = "{\"logoID\":\"" + logoId.ToString() + "\",\"GoodsID\":\"" + goodsId.ToString() + "\"}";
  506. try
  507. {
  508. WCF wcf = new WCF();
  509. wcf.Para = ApiLoadDefaultPara();
  510. wcf.Para.Add(new JProperty("procedureID", API.procedureID));
  511. wcf.Para.Add(new JProperty("barcode", barCode));
  512. wcf.Para.Add(new JProperty("module", "FinishedLoadingCar"));
  513. wcf.Para.Add(new JProperty("action", "GetSetting"));
  514. wcf.Para.Add(new JProperty("jsonData", jsonData));
  515. //测试用
  516. string jsonStr = @"
  517. {
  518. 'd': {
  519. '__type': 'ActionResult:#Dongke.IBOSS.PRD.WCF.DataModels',
  520. 'Message': '操作成功',
  521. 'Result': '{\'GoodsID\':\'179\',\'logoID\':\'15\',\'S_PM_011\':\'1\',\'S_PM_012\':\'1\',\'S_PM_013\':\'1\',\'PlatelitNum\':12,\'PlatelitNumNew\':2}',
  522. 'Status': 0
  523. }
  524. }
  525. ".Replace("'","\"");
  526. //正式用
  527. if (!API.debug) jsonStr = wcf.Post("/DKService/PDAModuleService/DoAction");
  528. JObject json = JObject.Parse(jsonStr);
  529. if (json["d"]["Status"].ToString() == "0")
  530. {
  531. JObject goods = JObject.Parse(json["d"]["Result"].ToString());
  532. int goodsNum = 0;
  533. //新板
  534. if(banMa == 1) goodsNum = Convert.ToInt32(goods["PlatelitNumNew"]);
  535. //旧板
  536. if(banMa == 0) goodsNum = Convert.ToInt32(goods["PlatelitNum"]);
  537. result.Add(new JObject(
  538. new JProperty("API调用(装板数量:OK)", goodsNum.ToString() + "操作成功!"),
  539. new JProperty("接口", "/DKService/PDAModuleService/DoAction"),
  540. new JProperty("参数(ACTION)", "GetSetting"),
  541. new JProperty("参数(MODULE)", "FinishedLoadingCar"),
  542. new JProperty("参数(jsonData)",jsonData)
  543. //,new JProperty("结果", jsonStr)
  544. )
  545. );
  546. return goodsNum;
  547. }
  548. else
  549. {
  550. resultFlag = false;
  551. result.Add(new JObject(
  552. new JProperty("API调用(装板数量:ERR)", json["d"]["Message"].ToString()),
  553. new JProperty("接口", "/DKService/PDAModuleService/DoAction"),
  554. new JProperty("参数(ACTION)", "GetSetting"),
  555. new JProperty("参数(MODULE)", "FinishedLoadingCar"),
  556. new JProperty("参数(jsonData)",jsonData)
  557. //,new JProperty("结果", jsonStr)
  558. )
  559. );
  560. return -1;
  561. }
  562. }
  563. catch (Exception e)
  564. {
  565. resultFlag = false;
  566. result.Add(new JObject(
  567. new JProperty("API调用(装板数量:ERROR)", "系统异常!"),
  568. new JProperty("接口", "/DKService/PDAModuleService/DoAction"),
  569. new JProperty("参数(ACTION)", "GetSetting"),
  570. new JProperty("参数(MODULE)", "FinishedLoadingCar"),
  571. new JProperty("参数(jsonData)",jsonData),
  572. new JProperty("异常", e.Message)
  573. )
  574. );
  575. return -1;
  576. }
  577. }
  578. public int ApiAddWorkPiece(string[] barCodes, string banMa, int goodsNum)
  579. {
  580. if (!resultFlag) return -1;
  581. JArray productionDataEntitys = new JArray();
  582. for (int i = 0; i < goodsNum; i++)
  583. {
  584. JObject obj = new JObject(
  585. new JProperty("UserID", API.userId),
  586. new JProperty("UserCode", API.userCode),
  587. new JProperty("UserName", API.userName),
  588. new JProperty("Barcode", barCodes[i]),
  589. new JProperty("BanMa", banMa)
  590. );
  591. productionDataEntitys.Add(obj);
  592. }
  593. try
  594. {
  595. WCF wcf = new WCF();
  596. wcf.Para = ApiLoadDefaultPara();
  597. wcf.Para.Add(new JProperty("procedureID", API.procedureID));
  598. wcf.Para.Add(new JProperty("productionDataEntitys", productionDataEntitys));
  599. //测试用
  600. string jsonStr = @"
  601. {
  602. 'd': {
  603. '__type': 'ActionResult:#Dongke.IBOSS.PRD.WCF.DataModels',
  604. 'Message': '操作成功',
  605. 'Result': '[{\'out_errMsg\':\'\',\'out_goodsID\':\'179\',\'out_goodsCode\':\'H0175M\',\'out_goodsName\':\'HC0175PT-305\',\'out_groutingUserCode\':\'SFC035\',\'out_groutingUserName\':null,\'out_groutingUserID\':null,\'out_groutingNum\':null,\'out_mouldCode\':null,\'out_ispublicbody\':null,\'out_ispublicbodyTrach\':null,\'out_groutingdate\':\'\',\'out_specialRepairFlag\':null,\'out_isReFire\':null,\'out_isLengBu\':null,\'out_missFlag\':null,\'out_logoID\':null,\'out_logoCode\':\'\',\'out_logoName\':\'\',\'out_MaterialCode\':null,\'out_glazeName\':null,\'out_deliverLimitCycle\':null,\'out_barcode\':null,\'out_WaterLabelCode\':null,\'out_CodeCheckFlag\':null,\'out_LeakFlag1\':null,\'out_LeakFlag2\':null,\'out_LeakFlag3\':null,\'out_specialRepairFlagName\':null,\'out_isReFireName\':null,\'out_LeakFlag1Name\':null,\'out_LeakFlag2Name\':null,\'out_LeakFlag3Name\':null,\'out_lengBuName\':null,\'out_LeakFlag4\':null,\'out_LeakFlag5\':null,\'out_LeakFlag4Name\':null,\'out_LeakFlag5Name\':null,\'GOODSMODELforCheck\':null,\'PlateLimitNum\':null,\'DefectFlagID\':null,\'pdid\':null,\'InspectionLevel\':null,\'PackingDefect\':null,\'InspectionGoodsLevel\':null,\'offlineFlag\':null,\'recyclingFlag\':null,\'waterLabelCode\':null}]',
  606. 'Status': 0
  607. }
  608. }
  609. ".Replace("'","\"");
  610. //正式用
  611. if (!API.debug) jsonStr = wcf.Post("/DKService/PDAModuleService/AddWorkPiece");
  612. JObject json = JObject.Parse(jsonStr);
  613. JArray jsonArray = JArray.Parse(json["d"]["Result"].ToString());
  614. //此处容错处理,如果已经绑定完成,重复绑码则视为完成
  615. if (json["d"]["Status"].ToString() == "0" && (jsonArray[0]["out_errMsg"].ToString()== "" || jsonArray[0]["out_errMsg"].ToString().IndexOf("已经完成生产") >= 0))
  616. {
  617. result.Add(new JObject(
  618. new JProperty("API调用(包装码垛:OK)", "操作成功!"),
  619. new JProperty("接口", "/DKService/PDAModuleService/AddWorkPiece"),
  620. new JProperty("参数(procedureID)", API.procedureID),
  621. new JProperty("参数(productionDataEntitys)", productionDataEntitys)
  622. //,new JProperty("结果", jsonStr)
  623. )
  624. );
  625. return 1;
  626. }
  627. else
  628. {
  629. resultFlag = false;
  630. result.Add(new JObject(
  631. new JProperty("API调用(包装码垛:ERR)", json["d"]["Message"].ToString()),
  632. new JProperty("接口", "/DKService/PDAModuleService/AddWorkPiece"),
  633. new JProperty("参数(procedureID)", API.procedureID),
  634. new JProperty("参数(productionDataEntitys)", productionDataEntitys)
  635. ,new JProperty("结果", jsonStr)
  636. )
  637. );
  638. return -1;
  639. }
  640. }
  641. catch (Exception e)
  642. {
  643. resultFlag = false;
  644. result.Add(new JObject(
  645. new JProperty("API调用(包装码垛:ERROR)", "系统异常!"),
  646. new JProperty("接口", "/DKService/PDAModuleService/AddWorkPiece"),
  647. new JProperty("参数(procedureID)", API.procedureID),
  648. new JProperty("参数(productionDataEntitys)", productionDataEntitys),
  649. new JProperty("异常", e.Message)
  650. )
  651. );
  652. return -1;
  653. }
  654. }
  655. public DataRow GetGoods(string outCode)
  656. {
  657. try
  658. {
  659. using (IDataAccess conn = DataAccess.Create())
  660. {
  661. DataTable dt = conn.ExecuteDatatable(@"
  662. SELECT
  663. gd.OUTLABELCODE,gd.BARCODE,gd.GOODSID,gd.LOGOID,gd.MATERIALCODE,gd.GOODSCODE AS GOODSCODES
  664. FROM
  665. TP_PM_GROUTINGDAILYDETAIL gd
  666. WHERE
  667. gd.VALUEFLAG = '1'
  668. AND gd.OUTLABELCODE = @OUTLABELCODE@
  669. ",
  670. new CDAParameter("OUTLABELCODE", outCode)
  671. );
  672. if(dt.Rows.Count >0 )
  673. {
  674. JObject obj = new JObject();
  675. obj.Add(new JProperty("OUTLABELCODE", dt.Rows[0]["OUTLABELCODE"].ToString()));
  676. obj.Add(new JProperty("BARCODE", dt.Rows[0]["BARCODE"].ToString()));
  677. obj.Add(new JProperty("MATERIALCODE", dt.Rows[0]["MATERIALCODE"].ToString()));
  678. obj.Add(new JProperty("GOODSID", dt.Rows[0]["GOODSID"].ToString()));
  679. obj.Add(new JProperty("LOGOID", dt.Rows[0]["LOGOID"].ToString()));
  680. obj.Add(new JProperty("GOODSCODES", dt.Rows[0]["GOODSCODES"].ToString()));
  681. result.Add(new JObject(new JProperty("MES读取(产品信息:OK)", obj)));
  682. return dt.Rows[0];
  683. }
  684. else
  685. {
  686. resultFlag = false;
  687. result.Add(new JObject(new JProperty("MES读取(产品信息:ERR)","产品未找到!")));
  688. return null;
  689. }
  690. }
  691. }
  692. catch (Exception e)
  693. {
  694. resultFlag = false;
  695. result.Add(new JObject(new JProperty("系统异常", e.Message)));
  696. return null;
  697. }
  698. }
  699. //读取用户信息
  700. public DataRow GetUserinfo(string userCode)
  701. {
  702. try
  703. {
  704. using (IDataAccess conn = DataAccess.Create())
  705. {
  706. DataTable dt = conn.ExecuteDatatable(@"
  707. SELECT
  708. u.ACCOUNTCODE,
  709. u.USERID,
  710. u.USERCODE,
  711. u.USERNAME,
  712. u.PASSWORD,
  713. l.SESSIONKEY
  714. FROM
  715. TP_MST_USER u
  716. LEFT JOIN TP_MST_USERLOGIN l ON u.USERID = l.USERID
  717. WHERE
  718. u.USERCODE = @USERCODE@
  719. ",
  720. new CDAParameter("USERCODE", userCode)
  721. );
  722. if(dt.Rows.Count >0 )
  723. {
  724. API.accountCode = dt.Rows[0]["ACCOUNTCODE"].ToString();
  725. API.userId = dt.Rows[0]["USERID"].ToString();
  726. API.userCode = dt.Rows[0]["USERCODE"].ToString();
  727. API.userName = dt.Rows[0]["USERNAME"].ToString();
  728. API.userPassword = dt.Rows[0]["PASSWORD"].ToString();
  729. API.sessionKey = dt.Rows[0]["SESSIONKEY"].ToString();
  730. JObject obj = new JObject();
  731. obj.Add(new JProperty("accountCode", dt.Rows[0]["ACCOUNTCODE"].ToString()));
  732. obj.Add(new JProperty("userId", dt.Rows[0]["USERID"].ToString()));
  733. obj.Add(new JProperty("userCode", dt.Rows[0]["USERCODE"].ToString()));
  734. obj.Add(new JProperty("userName", dt.Rows[0]["USERNAME"].ToString()));
  735. obj.Add(new JProperty("userPassword", dt.Rows[0]["PASSWORD"].ToString()));
  736. obj.Add(new JProperty("sessionKey", dt.Rows[0]["SESSIONKEY"].ToString()));
  737. result.Add(new JObject(new JProperty("MES读取(用户信息:OK)", obj)));
  738. return dt.Rows[0];
  739. }
  740. else
  741. {
  742. resultFlag = false;
  743. result.Add(new JObject(new JProperty("MES读取(用户信息:ERR)","用户未找到!")));
  744. return null;
  745. }
  746. }
  747. }
  748. catch (Exception e)
  749. {
  750. resultFlag = false;
  751. result.Add(new JObject(new JProperty("系统异常:ERROR", e.Message)));
  752. return null;
  753. }
  754. }
  755. //主程序入口
  756. public void ProcessRequest(HttpContext context)
  757. {
  758. context.Response.ContentType = "text/plain";
  759. if (context.Request["barcode"] is object)
  760. {
  761. BarcodeRead(context);
  762. }
  763. else
  764. {
  765. BarcodeDo(context);
  766. }
  767. }
  768. // 读取条码,对接扫描头使用
  769. public void BarcodeRead(HttpContext context)
  770. {
  771. if (PlcOpen() == 1)
  772. {
  773. int readFlag = PlcReadReadFlag();
  774. string barCode = "";
  775. string materialCode = "";
  776. string goodsCodes = "";
  777. Int16 goodsId = 0;
  778. Int16 logoId = 0;
  779. Int16 goodsNum = 0;
  780. //条码处理
  781. if(readFlag == 1 || readFlag == 0 || readFlag == 10)
  782. {
  783. DataRow dr = GetGoods(context.Request["barcode"].ToString());
  784. if (resultFlag)
  785. {
  786. barCode = dr["BARCODE"].ToString();
  787. materialCode = dr["MATERIALCODE"].ToString();
  788. goodsCodes = dr["GOODSCODES"].ToString();
  789. goodsId = Convert.ToInt16(dr["GOODSID"]);
  790. logoId = Convert.ToInt16(dr["LOGOID"]);
  791. //读取工号
  792. string userCode = PlcReadUsercode();
  793. if (userCode != "")
  794. {
  795. GetUserinfo(userCode);
  796. //接口验证条码状态(非必须取消这个接口)
  797. //int status = ApiGetStatusBarcode(barCode);
  798. //接口验证可到达
  799. int check = ApiCheckBarcode(barCode);
  800. //写验证结果
  801. if (check == -1) PlcWriteResult(3);
  802. }
  803. else
  804. {
  805. //工号错误
  806. PlcWriteResult(7);
  807. }
  808. }
  809. }
  810. //首个条码处理
  811. if ((readFlag == 1 || readFlag == 10) && resultFlag)
  812. {
  813. //写PLC
  814. PlcWriteBarcode(barCode);
  815. PlcWriteMaterialcode(materialCode);
  816. PlcWriteGoodsCodes(goodsCodes);
  817. //PlcWriteGoodsid(goodsId);
  818. //PlcWriteLogoid(logoId);
  819. //获取装板数量
  820. if(readFlag == 1) goodsNum = Convert.ToInt16(ApiFinishedLoadingCar(barCode, logoId, goodsId,1));
  821. if(readFlag == 10) goodsNum = Convert.ToInt16(ApiFinishedLoadingCar(barCode, logoId, goodsId,0));
  822. PlcWriteGoodsnum(goodsNum);
  823. //写结果
  824. if (resultFlag) PlcWriteResult(1); else PlcWriteResult(4);
  825. }
  826. //过程条码处理
  827. if (readFlag == 0 && resultFlag)
  828. {
  829. //验证条码是否符合装板
  830. int plcGoodsnum = PlcReadGoodsnum();
  831. string plcMaterialcode = PlcReadMaterialcode();
  832. if (resultFlag)
  833. {
  834. if(materialCode == plcMaterialcode)
  835. {
  836. //写PLC
  837. PlcWriteBarcode(barCode);
  838. PlcWriteMaterialcode(materialCode);
  839. PlcWriteGoodsCodes(goodsCodes);
  840. //PlcWriteGoodsid(goodsId);
  841. //PlcWriteLogoid(logoId);
  842. //PlcWriteGoodsnum(goodsNum);
  843. //通过 或 异常
  844. if (resultFlag) PlcWriteResult(1); else PlcWriteResult(4);
  845. }
  846. else
  847. {
  848. //条码不一致
  849. PlcWriteResult(5);
  850. }
  851. }
  852. else
  853. {
  854. //异常
  855. PlcWriteResult(4);
  856. }
  857. }
  858. PlcClose();
  859. }
  860. context.Response.Write(new JsonResult(resultFlag ? JsonStatus.success : JsonStatus.error) { rows = result }.ToJson());
  861. }
  862. // 绑板处理,轮询到标识,绑板处理
  863. public void BarcodeDo(HttpContext context)
  864. {
  865. if (PlcOpen() == 1)
  866. {
  867. int readFlag = PlcReadReadFlag();
  868. int goodsNum = PlcReadGoodsnum();
  869. int resultNum = PlcReadResultNum();
  870. if(resultNum != 6)
  871. {
  872. //绑板处理
  873. if(readFlag == 2 || readFlag == 3)
  874. {
  875. //读取工号
  876. string userCode = PlcReadUsercode();
  877. if (userCode != "")
  878. {
  879. //读取工号信息
  880. GetUserinfo(userCode);
  881. //读取条码
  882. String[] barCodes = PlcReadBarcodes(goodsNum);
  883. string banMa = "";
  884. if (readFlag == 3) banMa = PlcReadBanma();
  885. //绑板处理
  886. int addWorkPiece = ApiAddWorkPiece(barCodes, banMa, goodsNum);
  887. if(addWorkPiece == 1)
  888. {
  889. //绑板完成
  890. PlcWriteResult(6);
  891. }
  892. else
  893. {
  894. //MES异常 绑定异常
  895. PlcWriteResult(4);
  896. }
  897. }
  898. else
  899. {
  900. //工号错误
  901. PlcWriteResult(7);
  902. }
  903. }
  904. //强制绑板完成
  905. if(readFlag == 4)
  906. {
  907. PlcWriteResult(6);
  908. }
  909. }
  910. PlcClose();
  911. }
  912. context.Response.Write(new JsonResult(resultFlag ? JsonStatus.success : JsonStatus.error) { rows = result }.ToJson());
  913. }
  914. public bool IsReusable
  915. {
  916. get
  917. {
  918. return false;
  919. }
  920. }
  921. }