autoPacking6151.ashx 35 KB

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