autoPacking6151.ashx 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  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 = "192.168.0.250";
  19. public static int plcPort = 102;
  20. public static string dbBarcode = "1.2"; //1.0 => 1.2
  21. public static string dbGoodsId = "1.14";
  22. public static string dbGoodsNum = "1.16";
  23. public static string dbResult = "1.18";
  24. public static string dbLogoId = "1.22";//替换产品等级
  25. public static string dbMaterialcode = "1.30";
  26. public static string dbRead = "1.26";
  27. public static string[] dbBarcodeBlock = { "2.2", "2.32", "2.62", "2.92", "2.122", "2.152", "2.182", "2.212", "2.242", "2.272", "2.302", "2.332" };
  28. public static string dbBanMa = "2.362";//2.360 => 2.362
  29. public static string dbUsercode = "2.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 = "绑板完成:ERR";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. if (json["d"]["Status"].ToString() == "0" && jsonArray[0]["out_errMsg"].ToString()== "")
  596. {
  597. result.Add(new JObject(
  598. new JProperty("API调用(包装码垛:OK)", "操作成功!"),
  599. new JProperty("接口", "/DKService/PDAModuleService/AddWorkPiece"),
  600. new JProperty("参数(procedureID)", API.procedureID),
  601. new JProperty("参数(productionDataEntitys)", productionDataEntitys)
  602. //,new JProperty("结果", jsonStr)
  603. )
  604. );
  605. return 1;
  606. }
  607. else
  608. {
  609. resultFlag = false;
  610. result.Add(new JObject(
  611. new JProperty("API调用(包装码垛:ERR)", json["d"]["Message"].ToString()),
  612. new JProperty("接口", "/DKService/PDAModuleService/AddWorkPiece"),
  613. new JProperty("参数(procedureID)", API.procedureID),
  614. new JProperty("参数(productionDataEntitys)", productionDataEntitys)
  615. ,new JProperty("结果", jsonStr)
  616. )
  617. );
  618. return -1;
  619. }
  620. }
  621. catch (Exception e)
  622. {
  623. resultFlag = false;
  624. result.Add(new JObject(
  625. new JProperty("API调用(包装码垛:ERROR)", "系统异常!"),
  626. new JProperty("接口", "/DKService/PDAModuleService/AddWorkPiece"),
  627. new JProperty("参数(procedureID)", API.procedureID),
  628. new JProperty("参数(productionDataEntitys)", productionDataEntitys),
  629. new JProperty("异常", e.Message)
  630. )
  631. );
  632. return -1;
  633. }
  634. }
  635. public DataRow GetGoods(string outCode)
  636. {
  637. try
  638. {
  639. using (IDataAccess conn = DataAccess.Create())
  640. {
  641. DataTable dt = conn.ExecuteDatatable(@"
  642. SELECT
  643. gd.OUTLABELCODE,gd.BARCODE,gd.GOODSID,gd.LOGOID,gd.MATERIALCODE
  644. FROM
  645. TP_PM_GROUTINGDAILYDETAIL gd
  646. WHERE
  647. gd.VALUEFLAG = '1'
  648. AND gd.OUTLABELCODE = @OUTLABELCODE@
  649. ",
  650. new CDAParameter("OUTLABELCODE", outCode)
  651. );
  652. if(dt.Rows.Count >0 )
  653. {
  654. JObject obj = new JObject();
  655. obj.Add(new JProperty("OUTLABELCODE", dt.Rows[0]["OUTLABELCODE"].ToString()));
  656. obj.Add(new JProperty("BARCODE", dt.Rows[0]["BARCODE"].ToString()));
  657. obj.Add(new JProperty("MATERIALCODE", dt.Rows[0]["MATERIALCODE"].ToString()));
  658. obj.Add(new JProperty("GOODSID", dt.Rows[0]["GOODSID"].ToString()));
  659. obj.Add(new JProperty("LOGOID", dt.Rows[0]["LOGOID"].ToString()));
  660. result.Add(new JObject(new JProperty("MES读取(产品信息:OK)", obj)));
  661. return dt.Rows[0];
  662. }
  663. else
  664. {
  665. resultFlag = false;
  666. result.Add(new JObject(new JProperty("MES读取(产品信息:ERR)","产品未找到!")));
  667. return null;
  668. }
  669. }
  670. }
  671. catch (Exception e)
  672. {
  673. resultFlag = false;
  674. result.Add(new JObject(new JProperty("系统异常", e.Message)));
  675. return null;
  676. }
  677. }
  678. //读取用户信息
  679. public DataRow GetUserinfo(string userCode)
  680. {
  681. try
  682. {
  683. using (IDataAccess conn = DataAccess.Create())
  684. {
  685. DataTable dt = conn.ExecuteDatatable(@"
  686. SELECT
  687. u.ACCOUNTCODE,
  688. u.USERID,
  689. u.USERCODE,
  690. u.USERNAME,
  691. u.PASSWORD,
  692. l.SESSIONKEY
  693. FROM
  694. TP_MST_USER u
  695. LEFT JOIN TP_MST_USERLOGIN l ON u.USERID = l.USERID
  696. WHERE
  697. u.USERCODE = @USERCODE@
  698. ",
  699. new CDAParameter("USERCODE", userCode)
  700. );
  701. if(dt.Rows.Count >0 )
  702. {
  703. API.accountCode = dt.Rows[0]["ACCOUNTCODE"].ToString();
  704. API.userId = dt.Rows[0]["USERID"].ToString();
  705. API.userCode = dt.Rows[0]["USERCODE"].ToString();
  706. API.userName = dt.Rows[0]["USERNAME"].ToString();
  707. API.userPassword = dt.Rows[0]["PASSWORD"].ToString();
  708. API.sessionKey = dt.Rows[0]["SESSIONKEY"].ToString();
  709. JObject obj = new JObject();
  710. obj.Add(new JProperty("accountCode", dt.Rows[0]["ACCOUNTCODE"].ToString()));
  711. obj.Add(new JProperty("userId", dt.Rows[0]["USERID"].ToString()));
  712. obj.Add(new JProperty("userCode", dt.Rows[0]["USERCODE"].ToString()));
  713. obj.Add(new JProperty("userName", dt.Rows[0]["USERNAME"].ToString()));
  714. obj.Add(new JProperty("userPassword", dt.Rows[0]["PASSWORD"].ToString()));
  715. obj.Add(new JProperty("sessionKey", dt.Rows[0]["SESSIONKEY"].ToString()));
  716. result.Add(new JObject(new JProperty("MES读取(用户信息:OK)", obj)));
  717. return dt.Rows[0];
  718. }
  719. else
  720. {
  721. resultFlag = false;
  722. result.Add(new JObject(new JProperty("MES读取(用户信息:ERR)","用户未找到!")));
  723. return null;
  724. }
  725. }
  726. }
  727. catch (Exception e)
  728. {
  729. resultFlag = false;
  730. result.Add(new JObject(new JProperty("系统异常:ERROR", e.Message)));
  731. return null;
  732. }
  733. }
  734. //主程序入口
  735. public void ProcessRequest(HttpContext context)
  736. {
  737. context.Response.ContentType = "text/plain";
  738. if (context.Request["barcode"] is object)
  739. {
  740. BarcodeRead(context);
  741. }
  742. else
  743. {
  744. BarcodeDo(context);
  745. }
  746. }
  747. // 读取条码,对接扫描头使用
  748. public void BarcodeRead(HttpContext context)
  749. {
  750. if (PlcOpen() == 1)
  751. {
  752. int readFlag = PlcReadReadFlag();
  753. string barCode = "";
  754. string materialCode = "";
  755. Int16 goodsId = 0;
  756. Int16 logoId = 0;
  757. Int16 goodsNum = 0;
  758. //条码处理
  759. if(readFlag == 1 || readFlag == 0 || readFlag == 10)
  760. {
  761. DataRow dr = GetGoods(context.Request["barcode"].ToString());
  762. if (resultFlag)
  763. {
  764. barCode = dr["BARCODE"].ToString();
  765. materialCode = dr["MATERIALCODE"].ToString();
  766. goodsId = Convert.ToInt16(dr["GOODSID"]);
  767. logoId = Convert.ToInt16(dr["LOGOID"]);
  768. //读取工号
  769. string userCode = PlcReadUsercode();
  770. if (userCode != "")
  771. {
  772. GetUserinfo(userCode);
  773. //接口验证条码状态(非必须取消这个接口)
  774. //int status = ApiGetStatusBarcode(barCode);
  775. //接口验证可到达
  776. int check = ApiCheckBarcode(barCode);
  777. //写验证结果
  778. if (check == -1) PlcWriteResult(3);
  779. }
  780. else
  781. {
  782. //工号错误
  783. PlcWriteResult(7);
  784. }
  785. }
  786. }
  787. //首个条码处理
  788. if ((readFlag == 1 || readFlag == 10) && resultFlag)
  789. {
  790. //写PLC
  791. PlcWriteBarcode(barCode);
  792. PlcWriteMaterialcode(materialCode);
  793. //PlcWriteGoodsid(goodsId);
  794. //PlcWriteLogoid(logoId);
  795. //获取装板数量
  796. if(readFlag == 1) goodsNum = Convert.ToInt16(ApiFinishedLoadingCar(barCode, logoId, goodsId,1));
  797. if(readFlag == 10) goodsNum = Convert.ToInt16(ApiFinishedLoadingCar(barCode, logoId, goodsId,0));
  798. PlcWriteGoodsnum(goodsNum);
  799. //写结果
  800. if (resultFlag) PlcWriteResult(1); else PlcWriteResult(4);
  801. }
  802. //过程条码处理
  803. if (readFlag == 0 && resultFlag)
  804. {
  805. //验证条码是否符合装板
  806. int plcGoodsnum = PlcReadGoodsnum();
  807. string plcMaterialcode = PlcReadMaterialcode();
  808. if (resultFlag)
  809. {
  810. if(materialCode == plcMaterialcode)
  811. {
  812. //写PLC
  813. PlcWriteBarcode(barCode);
  814. PlcWriteMaterialcode(materialCode);
  815. //PlcWriteGoodsid(goodsId);
  816. //PlcWriteLogoid(logoId);
  817. //PlcWriteGoodsnum(goodsNum);
  818. //通过 或 异常
  819. if (resultFlag) PlcWriteResult(1); else PlcWriteResult(4);
  820. }
  821. else
  822. {
  823. //条码不一致
  824. PlcWriteResult(5);
  825. }
  826. }
  827. else
  828. {
  829. //异常
  830. PlcWriteResult(4);
  831. }
  832. }
  833. //PlcClose();
  834. }
  835. context.Response.Write(new JsonResult(resultFlag ? JsonStatus.success : JsonStatus.error) { rows = result }.ToJson());
  836. }
  837. // 绑板处理,轮询到标识,绑板处理
  838. public void BarcodeDo(HttpContext context)
  839. {
  840. if (PlcOpen() == 1)
  841. {
  842. int readFlag = PlcReadReadFlag();
  843. int goodsNum = PlcReadGoodsnum();
  844. int resultNum = PlcReadResultNum();
  845. if(resultNum != 6)
  846. {
  847. //绑板处理
  848. if(readFlag == 2 || readFlag == 3)
  849. {
  850. //读取工号
  851. string userCode = PlcReadUsercode();
  852. if (userCode != "")
  853. {
  854. //读取工号信息
  855. GetUserinfo(userCode);
  856. //读取条码
  857. String[] barCodes = PlcReadBarcodes(goodsNum);
  858. string banMa = "";
  859. if (readFlag == 3) banMa = PlcReadBanma();
  860. //绑板处理
  861. ApiAddWorkPiece(barCodes, banMa, goodsNum);
  862. //绑板完成
  863. PlcWriteResult(6);
  864. }
  865. else
  866. {
  867. //工号错误
  868. PlcWriteResult(7);
  869. }
  870. }
  871. //强制绑板完成
  872. if(readFlag == 4)
  873. {
  874. PlcWriteResult(6);
  875. }
  876. }
  877. //PlcClose();
  878. }
  879. context.Response.Write(new JsonResult(resultFlag ? JsonStatus.success : JsonStatus.error) { rows = result }.ToJson());
  880. }
  881. public bool IsReusable
  882. {
  883. get
  884. {
  885. return false;
  886. }
  887. }
  888. }