DeviceExtension.cs 14 KB

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