DeviceExtension.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. using PlcSiemens.Core.Extension;
  2. using ServiceCenter.Extensions;
  3. using ServiceCenter.Logs;
  4. using ServiceCenter.Redis;
  5. using WCS.Core;
  6. using WCS.WorkEngineering.Protocol.BCR;
  7. using WCS.WorkEngineering.Protocol.Robot;
  8. using WCS.WorkEngineering.Protocol.Station;
  9. using WCS.WorkEngineering.Protocol.Truss;
  10. namespace WCS.WorkEngineering.Extensions
  11. {
  12. /// <summary>
  13. /// 设备扩展
  14. /// </summary>
  15. public static class DeviceExtension
  16. {
  17. #region 设备类型
  18. public static List<ProtocolInfo> ProtocolInfos = new List<ProtocolInfo>();
  19. /// <summary>
  20. /// 是否是巷道
  21. /// </summary>
  22. /// <param name="source">设备信息</param>
  23. /// <returns></returns>
  24. public static bool IsTunnel(this Device source)
  25. {
  26. return source.HasFlag(DeviceFlags.巷道);
  27. }
  28. #endregion 设备类型
  29. #region 标签判断
  30. public static bool HasFlag(this Device device, params DeviceFlags[] flag)
  31. {
  32. return flag.Any(device.HasFlag);
  33. }
  34. #endregion 标签判断
  35. #region 协议操作扩展
  36. public static void AddProtocol<T>(this Device device, int position, ushort db, string ip)
  37. {
  38. var info = new ProtocolInfo
  39. {
  40. Position = position,
  41. DBInfo = new DBInfo
  42. {
  43. No = db,
  44. PLCInfo = new PLCInfo
  45. {
  46. IP = ip,
  47. Port = 102,
  48. Rack = 0,
  49. Slot = 1,
  50. Type = PLCType.Siemens
  51. }
  52. }
  53. };
  54. ProtocolInfos.Add(info);
  55. device.AddProtocol<T>(info);
  56. }
  57. #endregion 协议操作扩展
  58. #region 设备扩展方法
  59. /// <summary>
  60. /// 入库站点是否被禁止
  61. /// </summary>
  62. /// <returns></returns>
  63. public static void 入库站点是否被禁止(this Device<IStation520, IStation521, IStation523> device)
  64. {
  65. var config = RedisHub.Default.Check("ForbidTubuEnter") ?? throw new Exception("请在Redis中配置入库口禁用");
  66. var configs = config.Split(",");
  67. if (configs.Contains(device.Entity.Code)) throw new KnownException("当前入库口已被禁用,请联系运维人员了解具体情况", LogLevelEnum.High);
  68. }
  69. /// <summary>
  70. /// 入库站点是否被禁止
  71. /// </summary>
  72. /// <returns></returns>
  73. public static void 入库站点是否被禁止(this Device<IStation520, IStation521, IStation523, IBCR81> device)
  74. {
  75. var config = RedisHub.Default.Check("ForbidTubuEnter") ?? throw new Exception("请在Redis中配置入库口禁用");
  76. var configs = config.Split(",");
  77. if (configs.Contains(device.Entity.Code)) throw new KnownException("当前入库口已被禁用,请联系运维人员了解具体情况", LogLevelEnum.High);
  78. }
  79. /// <summary>
  80. /// 入库站点是否被禁止
  81. /// </summary>
  82. /// <returns></returns>
  83. public static void 入库站点是否被禁止(this Device<IStation520, IStation521, IStation523, IStation91> device)
  84. {
  85. var config = RedisHub.Default.Check("ForbidTubuEnter") ?? throw new Exception("请在Redis中配置入库口禁用");
  86. var configs = config.Split(",");
  87. if (configs.Contains(device.Entity.Code)) throw new KnownException("当前入库口已被禁用,请联系运维人员了解具体情况", LogLevelEnum.High);
  88. }
  89. /// <summary>
  90. /// 入库站点是否被禁止
  91. /// </summary>
  92. /// <returns></returns>
  93. public static void 入库站点是否被禁止(this Device<IStation520, IStation521, IStation523, IStation91, IBCR81> device)
  94. {
  95. var config = RedisHub.Default.Check("ForbidTubuEnter") ?? throw new Exception("请在Redis中配置入库口禁用");
  96. var configs = config.Split(",");
  97. if (configs.Contains(device.Entity.Code)) throw new KnownException("当前入库口已被禁用,请联系运维人员了解具体情况", LogLevelEnum.High);
  98. }
  99. /// <summary>
  100. /// 入库站点是否满足执行条件
  101. /// </summary>
  102. /// <returns></returns>
  103. public static void 入库站点是否满足执行条件(this Device<IStation520, IStation521, IStation523> device)
  104. {
  105. if (device.Data.VoucherNo != device.Data2.VoucherNo) throw new KnownException($"凭证号不一致,DB520:{device.Data.VoucherNo}-DB521:{device.Data2.VoucherNo}", LogLevelEnum.High);
  106. if (device.Data3.Status.HasFlag(StationStatus.Run)) throw new KnownException("设备运行中", LogLevelEnum.Low);
  107. if (device.Data3.Status.HasFlag(StationStatus.PH_Status) && device.Data2.Request == 0) throw new KnownException("有光电无请求", LogLevelEnum.Mid);
  108. if (!device.Data3.Status.HasFlag(StationStatus.PH_Status) && device.Data2.Request == 1) throw new KnownException("无光电有请求", LogLevelEnum.Mid);
  109. if (!device.Data3.Status.HasFlag(StationStatus.OT_Status)) throw new KnownException("站台货物信息与实际占用不一致", LogLevelEnum.Low);
  110. if (!device.Data3.Status.HasFlag(StationStatus.PH_Status)) throw new KnownException("无光电", LogLevelEnum.Mid);
  111. if (device.Data2.Request != 1) throw new KnownException("无请求", LogLevelEnum.Mid);
  112. }
  113. /// <summary>
  114. /// 入库站点是否满足执行条件
  115. /// </summary>
  116. /// <returns></returns>
  117. public static void 入库站点是否满足执行条件(this Device<IStation520, IStation521, IStation523, IBCR81> device)
  118. {
  119. if (device.Data.VoucherNo != device.Data2.VoucherNo) throw new KnownException($"凭证号不一致,DB520:{device.Data.VoucherNo}-DB521:{device.Data2.VoucherNo}", LogLevelEnum.High);
  120. if (device.Data3.Status.HasFlag(StationStatus.Run)) throw new KnownException("设备运行中", LogLevelEnum.Low);
  121. if (device.Data3.Status.HasFlag(StationStatus.PH_Status) && device.Data2.Request == 0) throw new KnownException("有光电无请求", LogLevelEnum.Mid);
  122. if (!device.Data3.Status.HasFlag(StationStatus.PH_Status) && device.Data2.Request == 1) throw new KnownException("无光电有请求", LogLevelEnum.Mid);
  123. if (!device.Data3.Status.HasFlag(StationStatus.OT_Status)) throw new KnownException("站台货物信息与实际占用不一致", LogLevelEnum.Low);
  124. if (!device.Data3.Status.HasFlag(StationStatus.PH_Status)) throw new KnownException("无光电", LogLevelEnum.Mid);
  125. if (device.Data2.Request != 1) throw new KnownException("无请求", LogLevelEnum.Mid);
  126. }
  127. /// <summary>
  128. /// 入库站点是否满足执行条件
  129. /// </summary>
  130. /// <returns></returns>
  131. public static void 入库站点是否满足执行条件(this Device<IStation520, IStation521, IStation523, IStation91> device)
  132. {
  133. if (device.Data.VoucherNo != device.Data2.VoucherNo) throw new KnownException($"凭证号不一致,DB520:{device.Data.VoucherNo}-DB521:{device.Data2.VoucherNo}", LogLevelEnum.High);
  134. if (device.Data3.Status.HasFlag(StationStatus.Run)) throw new KnownException("设备运行中", LogLevelEnum.Low);
  135. if (device.Data3.Status.HasFlag(StationStatus.PH_Status) && device.Data2.Request == 0) throw new KnownException("有光电无请求", LogLevelEnum.Mid);
  136. if (!device.Data3.Status.HasFlag(StationStatus.PH_Status) && device.Data2.Request == 1) throw new KnownException("无光电有请求", LogLevelEnum.Mid);
  137. if (!device.Data3.Status.HasFlag(StationStatus.OT_Status)) throw new KnownException("站台货物信息与实际占用不一致", LogLevelEnum.Low);
  138. if (!device.Data3.Status.HasFlag(StationStatus.PH_Status)) throw new KnownException("无光电", LogLevelEnum.Mid);
  139. if (device.Data2.Request != 1) throw new KnownException("无请求", LogLevelEnum.Mid);
  140. }
  141. /// <summary>
  142. /// 入库站点是否满足执行条件
  143. /// </summary>
  144. /// <returns></returns>
  145. public static void 入库站点是否满足执行条件(this Device<IStation520, IStation521, IStation523, IStation91, IBCR81> device)
  146. {
  147. if (device.Data.VoucherNo != device.Data2.VoucherNo) throw new KnownException($"凭证号不一致,DB520:{device.Data.VoucherNo}-DB521:{device.Data2.VoucherNo}", LogLevelEnum.High);
  148. if (device.Data3.Status.HasFlag(StationStatus.Run)) throw new KnownException("设备运行中", LogLevelEnum.Low);
  149. if (device.Data3.Status.HasFlag(StationStatus.PH_Status) && device.Data2.Request == 0) throw new KnownException("有光电无请求", LogLevelEnum.Mid);
  150. if (!device.Data3.Status.HasFlag(StationStatus.PH_Status) && device.Data2.Request == 1) throw new KnownException("无光电有请求", LogLevelEnum.Mid);
  151. if (!device.Data3.Status.HasFlag(StationStatus.OT_Status)) throw new KnownException("站台货物信息与实际占用不一致", LogLevelEnum.Low);
  152. if (device.Data2.Request != 1.ToShort()) throw new KnownException("无请求", LogLevelEnum.Mid);
  153. if (!device.Data3.Status.HasFlag(StationStatus.PH_Status)) throw new KnownException("无光电", LogLevelEnum.Mid);
  154. if (device.Data2.Request != 1) throw new KnownException("无请求", LogLevelEnum.Mid);
  155. }
  156. /// <summary>
  157. /// 获取BCR码
  158. /// </summary>
  159. /// <returns></returns>
  160. public static string GetBCRCode(this IBCR81 bCR)
  161. {
  162. var barcode = bCR.Content.Trim('\0');
  163. if (barcode.IsNullOrWhiteSpace()) throw new KnownException($"扫码失败,内容为空", LogLevelEnum.High);
  164. return barcode;
  165. }
  166. /// <summary>
  167. /// 获取历史扫码记录
  168. /// </summary>
  169. /// <returns></returns>
  170. public static List<string> GetBcrCodeList(this IBCR83 bCR)
  171. {
  172. return new List<string>() {
  173. bCR.BcrCode1.Trim('\0').Trim(),
  174. bCR.BcrCode2.Trim('\0').Trim(),
  175. bCR.BcrCode3.Trim('\0').Trim(),
  176. bCR.BcrCode4.Trim('\0').Trim(),
  177. bCR.BcrCode5.Trim('\0').Trim(),
  178. bCR.BcrCode6.Trim('\0').Trim(),
  179. bCR.BcrCode7.Trim('\0').Trim(),
  180. bCR.BcrCode8.Trim('\0').Trim(),
  181. bCR.BcrCode9.Trim('\0').Trim(),
  182. bCR.BcrCode10.Trim('\0').Trim(),
  183. bCR.BcrCode11.Trim('\0').Trim() };
  184. }
  185. /// <summary>
  186. /// 获取缓存列表
  187. /// </summary>
  188. /// <returns></returns>
  189. public static string[] GetBcrCodeList(this IStation525 station525)
  190. {
  191. var properties = station525.GetType().GetProperties().ToArray();
  192. var res = new List<string>();
  193. var index = 1;
  194. for (var i = 1; i <= 50; i++)
  195. {
  196. var value = properties[index].GetValue(station525).ToString();
  197. res.Add(value.Trim('\0').Trim());
  198. index += 7;
  199. }
  200. return res.ToArray();
  201. }
  202. /// <summary>
  203. /// 获取缓存列表
  204. /// </summary>
  205. /// <returns></returns>
  206. public static int[] GetTaskNoList(this ITruss531 station525)
  207. {
  208. var properties = station525.GetType().GetProperties().ToArray();
  209. var res = new List<int>();
  210. var index = 0;
  211. for (var i = 0; i <= 59; i++)
  212. {
  213. var value = properties[index].GetValue(station525)!.ToInt();
  214. res.Add(value);
  215. index += 1;
  216. }
  217. return res.ToArray();
  218. }
  219. /// <summary>
  220. /// 获取缓存列表
  221. /// </summary>
  222. /// <returns></returns>
  223. public static int[] GetTaskNoList(this IRobot531 station525)
  224. {
  225. var properties = station525.GetType().GetProperties().ToArray();
  226. var res = new List<int>();
  227. var index = 0;
  228. for (var i = 0; i <= 29; i++)
  229. {
  230. var value = properties[index].GetValue(station525)!.ToInt();
  231. res.Add(value);
  232. index += 1;
  233. }
  234. return res.ToArray();
  235. }
  236. /// <summary>
  237. /// 通过仓库编码获取对应堆垛机信息
  238. /// </summary>
  239. /// <param name="warehouseCode">仓库编码</param>
  240. /// <returns></returns>
  241. public static string WarehouseToSrm(this string warehouseCode)
  242. {
  243. return warehouseCode switch
  244. {
  245. "1N" => "SRM1",
  246. "1S" => "SRM2",
  247. "2N" => "SRM3",
  248. "2S" => "SRM4",
  249. "3N" => "SRM5",
  250. "3S" => "SRM6",
  251. _ => "Error"
  252. };
  253. }
  254. #endregion 设备扩展方法
  255. }
  256. /// <summary>
  257. /// 设备标签
  258. /// </summary>
  259. [Flags]
  260. public enum DeviceFlags : ulong
  261. {
  262. 扫码 = 1L << 0,
  263. 称重 = 1L << 1,
  264. 外检 = 1L << 2,
  265. 顶升 = 1L << 3,
  266. 移栽 = 1L << 4,
  267. 旋转 = 1L << 5,
  268. 入库 = 1L << 6,
  269. 出库 = 1L << 7,
  270. 巷道口 = 1L << 8,
  271. RGV口 = 1L << 9,
  272. AGV取货站台口 = 1L << 10,
  273. 直轨 = 1L << 11,
  274. 弯轨 = 1L << 12,
  275. 环轨 = 1L << 13,
  276. 巷道 = 1L << 14,
  277. 堆垛机 = 1L << 15,
  278. 输送机 = 1L << 16,
  279. #region 一轨双车堆垛机
  280. 一列堆垛机 = 1L << 23,
  281. 二列堆垛机 = 1L << 24,
  282. #endregion 一轨双车堆垛机
  283. RGV = 1L << 25,
  284. 桁架 = 1L << 26,
  285. 一楼扫码 = 1L << 27,
  286. 满轮主线第一次扫码 = 1L << 28,
  287. 主线分流点 = 1L << 29,
  288. 一楼叠盘机 = 1L << 30,
  289. 环形库分流点 = 1L << 31,
  290. 桁架分流点 = 1L << 32,
  291. 桁架缓存放行点 = 1L << 33,
  292. 桁架取货点 = 1L << 34,
  293. 拆盘机09 = 1L << 35,
  294. 拆盘机非09 = 1L << 36,
  295. 码垛位09 = 1L << 37,
  296. 环形库码垛工位 = 1L << 38,
  297. Robot = 1L << 39
  298. }
  299. /// <summary>
  300. /// 输送机段信息
  301. /// </summary>
  302. public class StationSegmentInfo
  303. {
  304. /// <summary>
  305. /// 构造函数
  306. /// </summary>
  307. /// <param name="start">起始设备号</param>
  308. /// <param name="end">结束设备号</param>
  309. /// <param name="ip">ip</param>
  310. public StationSegmentInfo(int start, int end, string ip)
  311. {
  312. Start = start;
  313. End = end;
  314. Ip = ip;
  315. }
  316. /// <summary>
  317. /// 起始设备编号
  318. /// </summary>
  319. public int Start { get; set; }
  320. /// <summary>
  321. ///结束设备编号
  322. /// </summary>
  323. public int End { get; set; }
  324. /// <summary>
  325. /// 输送机段所属IP
  326. /// </summary>
  327. public string Ip { get; set; }
  328. }
  329. /// <summary>
  330. /// RGV信息
  331. /// </summary>
  332. public class RgvSegmentInfo
  333. {
  334. /// <summary>
  335. /// 构造函数
  336. /// </summary>
  337. /// <param name="code"></param>
  338. /// <param name="ip">ip</param>
  339. public RgvSegmentInfo(int code, string ip)
  340. {
  341. Code = code;
  342. Ip = ip;
  343. }
  344. /// <summary>
  345. /// 设备编号
  346. /// </summary>
  347. public int Code { get; set; }
  348. /// <summary>
  349. /// 输送机段所属IP
  350. /// </summary>
  351. public string Ip { get; set; }
  352. }
  353. /// <summary>
  354. /// 桁架信息
  355. /// </summary>
  356. public class TrussSegmentInfo
  357. {
  358. /// <summary>
  359. /// 构造函数
  360. /// </summary>
  361. /// <param name="code"></param>
  362. /// <param name="ip">ip</param>
  363. public TrussSegmentInfo(int code, string ip)
  364. {
  365. Code = code;
  366. Ip = ip;
  367. }
  368. /// <summary>
  369. /// 设备编号
  370. /// </summary>
  371. public int Code { get; set; }
  372. /// <summary>
  373. /// 输送机段所属IP
  374. /// </summary>
  375. public string Ip { get; set; }
  376. }
  377. /// <summary>
  378. /// 扫码器信息
  379. /// </summary>
  380. public class BcrInfo
  381. {
  382. /// <summary>
  383. /// 构造函数
  384. /// </summary>
  385. /// <param name="deviceNo">设备编号</param>
  386. /// <param name="ip">ip</param>
  387. public BcrInfo(string[] deviceNo, string ip)
  388. {
  389. DeviceNo = deviceNo;
  390. Ip = ip;
  391. }
  392. /// <summary>
  393. /// 设备编号
  394. /// </summary>
  395. public string[] DeviceNo { get; set; }
  396. /// <summary>
  397. /// ip
  398. /// </summary>
  399. public string Ip { get; set; }
  400. }
  401. /// <summary>
  402. /// 外形信息
  403. /// </summary>
  404. public class ShapeInfo
  405. {
  406. /// <summary>
  407. /// 构造函数
  408. /// </summary>
  409. /// <param name="deviceNo">设备编号</param>
  410. /// <param name="ip">ip</param>
  411. public ShapeInfo(int[] deviceNo, string ip)
  412. {
  413. DeviceNo = deviceNo;
  414. Ip = ip;
  415. }
  416. /// <summary>
  417. /// 设备编号
  418. /// </summary>
  419. public int[] DeviceNo { get; set; }
  420. /// <summary>
  421. /// ip
  422. /// </summary>
  423. public string Ip { get; set; }
  424. }
  425. /// <summary>
  426. /// 路径信息
  427. /// </summary>
  428. public class RouteInfo
  429. {
  430. /// <summary>
  431. /// 构造函数
  432. /// </summary>
  433. /// <param name="deviceCode">起始点设备号</param>
  434. /// <param name="nextList">下一个设备集合</param>
  435. public RouteInfo(string deviceCode, string[] nextList)
  436. {
  437. DeviceCode = deviceCode;
  438. NextList = nextList;
  439. }
  440. /// <summary>
  441. /// 设备号
  442. /// </summary>
  443. public string DeviceCode { get; set; }
  444. /// <summary>
  445. /// 下一个设备
  446. /// </summary>
  447. public string[] NextList { get; set; }
  448. }
  449. /// <summary>
  450. /// 缓存分流信息
  451. /// </summary>
  452. public class CacheBcr
  453. {
  454. public CacheBcr(string bcrCode, short nextAdd)
  455. {
  456. BcrCode = bcrCode;
  457. NextAdd = nextAdd;
  458. }
  459. /// <summary>
  460. /// 条码
  461. /// </summary>
  462. public string BcrCode { get; set; }
  463. /// <summary>
  464. /// 下一个地址
  465. /// </summary>
  466. public short NextAdd { get; set; }
  467. }
  468. }