MacOrderDalForEverlight.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. using Cksoft.Data;
  2. using Cksoft.Unity;
  3. using DllEapEntity;
  4. using DllHsms;
  5. using DllHsmsWeb;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading;
  11. namespace DllEapDal
  12. {
  13. public class MacOrderDalForEverlight
  14. {
  15. private IDatabase CurrDb = null;
  16. public MacOrderDalForEverlight(IDatabase db)
  17. {
  18. CurrDb = db;
  19. }
  20. public Machine ReadMachine(string maccode, ref string errorinfo)
  21. {
  22. //读取机台信息
  23. string condition = $" and a.fcode='{maccode}'";
  24. List<Machine> macs = CurrDb.FindListForCondition<Machine>(condition, ref errorinfo).ToList();
  25. if (macs == null)
  26. return null;
  27. if (macs.Count <= 0)
  28. {
  29. errorinfo = $"未找到机台编号【{maccode}】的机台信息。";
  30. return null;
  31. }
  32. return macs[0];
  33. }
  34. public OrderDetail ReadMachineOrderDetail(int macid, int sval, int fval, ref string errorinfo)
  35. {
  36. //读取机台信息
  37. string condition = $" and a.macid={macid}";
  38. List<MacOrder> macs = CurrDb.FindListForCondition<MacOrder>(condition, ref errorinfo).ToList();
  39. if (macs == null)
  40. return null;
  41. if (macs.Count <= 0)
  42. {
  43. errorinfo = $"未找到机台ID【{macid}】的机台指令信息。";
  44. return null;
  45. }
  46. condition = $" and a.preid={macs[0].PreID} and a.sval={sval} and a.fval={fval}";
  47. List<OrderDetail> details = CurrDb.FindListForCondition<OrderDetail>(condition, ref errorinfo).ToList();
  48. if (details == null)
  49. return null;
  50. if (details.Count <= 0)
  51. {
  52. errorinfo = "未找到您要的指令。";
  53. return null;
  54. }
  55. return details[0];
  56. }
  57. public OrderDetail ReadMachineOrderDetail(int macid, int sval, int fval, string fname, ref string errorinfo)
  58. {
  59. //读取机台信息
  60. string condition = $" and a.macid={macid}";
  61. List<MacOrder> macs = CurrDb.FindListForCondition<MacOrder>(condition, ref errorinfo).ToList();
  62. if (macs == null)
  63. return null;
  64. if (macs.Count <= 0)
  65. {
  66. errorinfo = $"未找到机台ID【{macid}】的机台指令信息。";
  67. return null;
  68. }
  69. condition = $" and a.preid={macs[0].PreID} and a.sval={sval} and a.fval={fval} and lower(a.FName)='{fname.ToLower()}'";
  70. List<OrderDetail> details = CurrDb.FindListForCondition<OrderDetail>(condition, ref errorinfo).ToList();
  71. if (details == null)
  72. return null;
  73. if (details.Count <= 0)
  74. {
  75. errorinfo = "未找到您要的指令。";
  76. return null;
  77. }
  78. return details[0];
  79. }
  80. public int SendStopMac(string maccode, ref string errorinfo)
  81. {
  82. try
  83. {
  84. Machine mac = ReadMachine(maccode, ref errorinfo);
  85. if (mac == null)
  86. return -1;
  87. OrderDetail order = ReadMachineOrderDetail(mac.ID, 2, 41, "s2f41stop", ref errorinfo);
  88. if (order == null)
  89. return -1;
  90. //从机型参数中读取程序参数信息
  91. string condition = $" and a.preid={order.ID}";
  92. List<OrderData> datas = CurrDb.FindListForCondition<OrderData>(condition, ref errorinfo).ToList();
  93. if (datas == null)
  94. return -1;
  95. HsmsWeb accessmac = new HsmsWeb();
  96. OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  97. if (rec == null)
  98. return -1;
  99. //休眠5秒,等待机台状态恢复
  100. //Thread.Sleep(5000);
  101. //order = ReadMachineOrderDetail(mac.ID, 2, 41, "s2f41resume", ref errorinfo);
  102. //if (order == null)
  103. // return -1;
  104. ////从机型参数中读取程序参数信息
  105. //condition = $" and a.preid={order.ID}";
  106. //datas = CurrDb.FindListForCondition<OrderData>(condition, ref errorinfo).ToList();
  107. //if (datas == null)
  108. // return -1;
  109. //rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  110. //if (rec == null)
  111. // return -1;
  112. return 1;
  113. }
  114. catch (Exception ex)
  115. {
  116. errorinfo = ex.Message.ToString();
  117. return -1;
  118. }
  119. }
  120. public int SendS10F3(string maccode,string info, ref string errorinfo)
  121. {
  122. try
  123. {
  124. Machine mac = ReadMachine(maccode, ref errorinfo);
  125. if (mac == null)
  126. return -1;
  127. OrderDetail order = ReadMachineOrderDetail(mac.ID, 10, 3, "s10f3", ref errorinfo);
  128. if (order == null)
  129. return -1;
  130. //从机型参数中读取程序参数信息
  131. string condition = $" and a.preid={order.ID}";
  132. List<OrderData> datas = CurrDb.FindListForCondition<OrderData>(condition, ref errorinfo).OrderBy(t => t.FNum).ToList();
  133. if (datas == null)
  134. return -1;
  135. List<OrderData> tempdatas = datas.Where(t => t.ParentID == 0).ToList();
  136. tempdatas = datas.Where(t => t.ParentID == tempdatas[0].ID).OrderBy(t => t.FNum).ToList();
  137. tempdatas[1].FContent = info;
  138. tempdatas[1].FLen = info.Length;
  139. HsmsWeb accessmac = new HsmsWeb();
  140. OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  141. if (rec == null)
  142. return -1;
  143. if (rec.Datalists[0].FContent != "00")
  144. {
  145. errorinfo = rec.Datalists[0].FContent;
  146. return -1;
  147. }
  148. return 1;
  149. }
  150. catch (Exception ex)
  151. {
  152. errorinfo = ex.Message.ToString();
  153. return -1;
  154. }
  155. }
  156. public string GetProgramName(Machine mac, ref string errorinfo)
  157. {
  158. try
  159. {
  160. OrderDetail order = ReadMachineOrderDetail(mac.ID, 1, 3, "programname", ref errorinfo);
  161. if (order == null)
  162. return "";
  163. string condition = $" and a.preid={order.ID}";
  164. List<OrderData> datas = CurrDb.FindListForCondition<OrderData>(condition, ref errorinfo).ToList();
  165. if (datas == null)
  166. return "";
  167. HsmsWeb accessmac = new HsmsWeb();
  168. OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  169. if (rec == null)
  170. return "";
  171. string org = rec.Datalists[1].FContent;
  172. //condition = $" and a.id={org}";
  173. //List<MacProgram> programs = CurrDb.FindListForCondition<MacProgram>(condition, ref errorinfo).ToList();
  174. //if (programs.Count <= 0)
  175. //{
  176. // errorinfo = $"未找到ID【{org}】的程序。";
  177. // return "";
  178. //}
  179. //return programs[0].ProgramName;
  180. return org;
  181. }
  182. catch (Exception ex)
  183. {
  184. errorinfo = ex.Message.ToString();
  185. return "";
  186. }
  187. }
  188. public byte[] GetProgram(Machine mac,string programname, ref string errorinfo)
  189. {
  190. try
  191. {
  192. OrderDetail order = ReadMachineOrderDetail(mac.ID, 7, 5, "requestprogram", ref errorinfo);
  193. if (order == null)
  194. return null;
  195. string condition = $" and a.preid={order.ID}";
  196. List<OrderData> datas = CurrDb.FindListForCondition<OrderData>(condition, ref errorinfo).ToList();
  197. if (datas == null)
  198. return null;
  199. datas[0].FContent = programname;
  200. datas[0].FLen = programname.Length;
  201. HsmsWeb accessmac = new HsmsWeb();
  202. OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  203. if (rec == null)
  204. return null;
  205. if(rec.Datalists[2].OrgDatas==null)
  206. {
  207. errorinfo = rec.ToJson();
  208. return null;
  209. }
  210. return rec.Datalists[2].OrgDatas;
  211. //string org = rec.Datalists[1].FContent;
  212. //condition = $" and a.id={org}";
  213. //List<MacProgram> programs = CurrDb.FindListForCondition<MacProgram>(condition, ref errorinfo).ToList();
  214. //if (programs.Count <= 0)
  215. //{
  216. // errorinfo = $"未找到ID【{org}】的程序。";
  217. // return "";
  218. //}
  219. //return programs[0].ProgramName;
  220. }
  221. catch (Exception ex)
  222. {
  223. errorinfo = ex.Message.ToString();
  224. return null;
  225. }
  226. }
  227. private List<OrderData> GetS7F1Data(string programname, int len, int preid, ref string errorinfo)
  228. {
  229. List<OrderData> ldata = new List<OrderData>();
  230. OrderData lentity = new OrderData();
  231. lentity.ID = 1;
  232. lentity.ParentID = 0;
  233. lentity.PreID = preid;
  234. lentity.FCode = "L";
  235. lentity.FLen = 2;
  236. ldata.Add(lentity);
  237. OrderData entity = new OrderData();
  238. entity.ID = 2;
  239. entity.ParentID = 1;
  240. entity.FNum = 10;
  241. entity.PreID = preid;
  242. entity.FCode = "A";
  243. entity.FContent = programname;
  244. entity.FLen = programname.Length;
  245. ldata.Add(entity);
  246. entity = new OrderData();
  247. entity.ID = 2;
  248. entity.ParentID = 1;
  249. entity.FNum = 20;
  250. entity.PreID = preid;
  251. entity.FCode = "U4";
  252. entity.FLen = 1;
  253. entity.FContent = len.ToString();
  254. //entity.OrgDatas = filedatas;
  255. //entity.FLen = programname.Length;
  256. ldata.Add(entity);
  257. return ldata;
  258. }
  259. private void SendInfo(string maccode, string info)
  260. {
  261. try
  262. {
  263. string temperrorinfo = "";
  264. SendS10F3(maccode, info, ref temperrorinfo);
  265. }
  266. catch
  267. {
  268. return;
  269. }
  270. }
  271. private List<OrderData> GetS7F3Data(string programname, int filelen, string filepath, int preid, ref string errorinfo)
  272. {
  273. List<OrderData> ldata = new List<OrderData>();
  274. OrderData lentity = new OrderData();
  275. lentity.ID = 1;
  276. lentity.ParentID = 0;
  277. lentity.PreID = preid;
  278. lentity.FCode = "L";
  279. lentity.FLen = 2;
  280. ldata.Add(lentity);
  281. OrderData entity = new OrderData();
  282. entity.ID = 2;
  283. entity.ParentID = 1;
  284. entity.FNum = 10;
  285. entity.PreID = preid;
  286. entity.FCode = "A";
  287. entity.FContent = programname;
  288. entity.FLen = programname.Length;
  289. entity.Remark = "";
  290. ldata.Add(entity);
  291. entity = new OrderData();
  292. entity.ID = 2;
  293. entity.ParentID = 1;
  294. entity.FNum = 20;
  295. entity.PreID = preid;
  296. entity.FCode = "B";
  297. entity.FContent = filepath;
  298. entity.OrgDatas = null;
  299. entity.FLen = filelen;
  300. //entity.FLen = programname.Length;
  301. ldata.Add(entity);
  302. return ldata;
  303. }
  304. public int DownloadProgram(Machine mac, string programname, int filelen, string filepath, ref string errorinfo)
  305. {
  306. try
  307. {
  308. //要修改程序名称
  309. OrderDetail order = ReadMachineOrderDetail(mac.ID, 7, 1, ref errorinfo);
  310. if (order == null)
  311. return -1;
  312. string condition = $" and a.preid={order.ID}";
  313. List<OrderData> datas = GetS7F1Data(programname, filelen, order.ID, ref errorinfo);//
  314. if (datas == null)
  315. return -1;
  316. HsmsWeb accessmac = new HsmsWeb();
  317. OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  318. if (rec == null)
  319. return -1;
  320. int result = int.Parse(rec.Datalists[0].FContent);
  321. if (result > 1)
  322. {
  323. errorinfo = $"机台不接受程序。{rec.Datalists[0].FContent}";
  324. SendInfo(mac.FCode, $" Recipe download fail [{rec.Datalists[0].FContent}]");
  325. return -1;
  326. }
  327. if (result == 0)
  328. {
  329. //产生文件
  330. ShareFileDal filedal = new ShareFileDal(CurrDb);
  331. ShareFile fileentity = filedal.MadeShareFile(filepath, ref errorinfo);
  332. if (fileentity == null)
  333. return -1;
  334. order = ReadMachineOrderDetail(mac.ID, 7, 3, ref errorinfo);
  335. datas = GetS7F3Data(programname, filelen, fileentity.FilePath, order.PreID, ref errorinfo);
  336. rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  337. if (rec == null)
  338. return -1;
  339. if (int.Parse(rec.Datalists[0].FContent) != 0)
  340. {
  341. errorinfo = "机台没有正确接受程序。";
  342. SendInfo(mac.FCode, $" Recipe download fail [{rec.Datalists[0].FContent}]");
  343. return -1;
  344. }
  345. }
  346. return 1;
  347. }
  348. catch (Exception ex)
  349. {
  350. errorinfo = ex.Message.ToString();
  351. return -1;
  352. }
  353. }
  354. }
  355. }