autoPacking6151.ashx 36 KB

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