DeviceExtension.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. using PlcSiemens.Core.Extension;
  2. using ServiceCenter.Logs;
  3. using ServiceCenter.Redis;
  4. using WCS.Core;
  5. using WCS.WorkEngineering.Protocol.BCR;
  6. using WCS.WorkEngineering.Protocol.Station;
  7. namespace WCS.WorkEngineering.Extensions
  8. {
  9. /// <summary>
  10. /// 设备扩展
  11. /// </summary>
  12. public static class DeviceExtension
  13. {
  14. #region 设备类型
  15. public static List<ProtocolInfo> ProtocolInfos = new List<ProtocolInfo>();
  16. /// <summary>
  17. /// 是否是巷道
  18. /// </summary>
  19. /// <param name="source">设备信息</param>
  20. /// <returns></returns>
  21. public static bool IsTunnel(this Device source)
  22. {
  23. return source.HasFlag(DeviceFlags.巷道);
  24. }
  25. #endregion 设备类型
  26. #region 标签判断
  27. public static bool HasFlag(this Device device, params DeviceFlags[] flag)
  28. {
  29. foreach (var item in flag)
  30. {
  31. if (device.HasFlag(item)) continue;
  32. return false;
  33. }
  34. return true;
  35. }
  36. #endregion 标签判断
  37. #region 协议操作扩展
  38. public static void AddProtocol<T>(this Device device, int position, ushort db, string ip)
  39. {
  40. var info = new ProtocolInfo
  41. {
  42. Position = position,
  43. DBInfo = new DBInfo
  44. {
  45. No = db,
  46. PLCInfo = new PLCInfo
  47. {
  48. IP = ip,
  49. Port = 102,
  50. Rack = 0,
  51. Slot = 1,
  52. Type = PLCType.Siemens
  53. }
  54. }
  55. };
  56. ProtocolInfos.Add(info);
  57. device.AddProtocol<T>(info);
  58. }
  59. #endregion 协议操作扩展
  60. #region 设备扩展方法
  61. /// <summary>
  62. /// 入库站点是否被禁止
  63. /// </summary>
  64. /// <returns></returns>
  65. public static void 入库站点是否被禁止(this Device<IStation520, IStation521, IStation523, IStation91> device)
  66. {
  67. var config = RedisHub.Default.Check("ForbidTubuEnter") ?? throw new Exception("请在Redis中配置入库口禁用");
  68. var configs = config.Split(",");
  69. if (configs.Contains(device.Entity.Code)) throw new KnownException("当前入库口已被禁用,请联系运维人员了解具体情况", LogLevelEnum.High);
  70. }
  71. /// <summary>
  72. /// 入库站点是否被禁止
  73. /// </summary>
  74. /// <returns></returns>
  75. public static void 入库站点是否被禁止(this Device<IStation520, IStation521, IStation523, IBCR81> device)
  76. {
  77. var config = RedisHub.Default.Check("ForbidTubuEnter") ?? throw new Exception("请在Redis中配置入库口禁用");
  78. var configs = config.Split(",");
  79. if (configs.Contains(device.Entity.Code)) throw new KnownException("当前入库口已被禁用,请联系运维人员了解具体情况", LogLevelEnum.High);
  80. }
  81. /// <summary>
  82. /// 入库站点是否被禁止
  83. /// </summary>
  84. /// <returns></returns>
  85. public static void 入库站点是否被禁止(this Device<IStation520, IStation521, IStation523, IStation91, IBCR81> device)
  86. {
  87. var config = RedisHub.Default.Check("ForbidTubuEnter") ?? throw new Exception("请在Redis中配置入库口禁用");
  88. var configs = config.Split(",");
  89. if (configs.Contains(device.Entity.Code)) throw new KnownException("当前入库口已被禁用,请联系运维人员了解具体情况", LogLevelEnum.High);
  90. }
  91. /// <summary>
  92. /// 入库站点是否满足执行条件
  93. /// </summary>
  94. /// <returns></returns>
  95. public static void 入库站点是否满足执行条件(this Device<IStation520, IStation521, IStation523, IStation91> device)
  96. {
  97. if (device.Data.VoucherNo != device.Data2.VoucherNo) throw new KnownException($"凭证号不一致,DB520:{device.Data.VoucherNo}-DB521:{device.Data2.VoucherNo}", LogLevelEnum.High);
  98. if (device.Data3.Status.HasFlag(StationStatus.Run)) throw new KnownException("设备运行中", LogLevelEnum.Low);
  99. if (device.Data3.Status.HasFlag(StationStatus.PH_Status) && device.Data2.Request == 0) throw new KnownException("有光电无请求", LogLevelEnum.Mid);
  100. if (!device.Data3.Status.HasFlag(StationStatus.PH_Status) && device.Data2.Request == 1) throw new KnownException("无光电有请求", LogLevelEnum.Mid);
  101. if (!device.Data3.Status.HasFlag(StationStatus.OT_Status)) throw new KnownException("站台货物信息与实际占用不一致", LogLevelEnum.Low);
  102. }
  103. /// <summary>
  104. /// 入库站点是否满足执行条件
  105. /// </summary>
  106. /// <returns></returns>
  107. public static void 入库站点是否满足执行条件(this Device<IStation520, IStation521, IStation523, IBCR81> device)
  108. {
  109. if (device.Data.VoucherNo != device.Data2.VoucherNo) throw new KnownException($"凭证号不一致,DB520:{device.Data.VoucherNo}-DB521:{device.Data2.VoucherNo}", LogLevelEnum.High);
  110. if (device.Data3.Status.HasFlag(StationStatus.Run)) throw new KnownException("设备运行中", LogLevelEnum.Low);
  111. if (device.Data3.Status.HasFlag(StationStatus.PH_Status) && device.Data2.Request == 0) throw new KnownException("有光电无请求", LogLevelEnum.Mid);
  112. if (!device.Data3.Status.HasFlag(StationStatus.PH_Status) && device.Data2.Request == 1) throw new KnownException("无光电有请求", LogLevelEnum.Mid);
  113. if (!device.Data3.Status.HasFlag(StationStatus.OT_Status)) throw new KnownException("站台货物信息与实际占用不一致", LogLevelEnum.Low);
  114. }
  115. /// <summary>
  116. /// 入库站点是否满足执行条件
  117. /// </summary>
  118. /// <returns></returns>
  119. public static void 入库站点是否满足执行条件(this Device<IStation520, IStation521, IStation523, IStation91, IBCR81> device)
  120. {
  121. if (device.Data.VoucherNo != device.Data2.VoucherNo) throw new KnownException($"凭证号不一致,DB520:{device.Data.VoucherNo}-DB521:{device.Data2.VoucherNo}", LogLevelEnum.High);
  122. if (device.Data3.Status.HasFlag(StationStatus.Run)) throw new KnownException("设备运行中", LogLevelEnum.Low);
  123. if (device.Data3.Status.HasFlag(StationStatus.PH_Status) && device.Data2.Request == 0) throw new KnownException("有光电无请求", LogLevelEnum.Mid);
  124. if (!device.Data3.Status.HasFlag(StationStatus.PH_Status) && device.Data2.Request == 1) throw new KnownException("无光电有请求", LogLevelEnum.Mid);
  125. if (!device.Data3.Status.HasFlag(StationStatus.OT_Status)) throw new KnownException("站台货物信息与实际占用不一致", LogLevelEnum.Low);
  126. }
  127. /// <summary>
  128. /// 获取BCR码
  129. /// </summary>
  130. /// <returns></returns>
  131. public static string GetBCRCode(this IBCR81 bCR)
  132. {
  133. var barcode = bCR.Content.Trim('\0');
  134. if (barcode.IsNullOrWhiteSpace()) throw new KnownException($"扫码失败,内容为空", LogLevelEnum.High);
  135. return barcode;
  136. }
  137. #endregion 设备扩展方法
  138. }
  139. /// <summary>
  140. /// 设备标签
  141. /// </summary>
  142. [Flags]
  143. public enum DeviceFlags
  144. {
  145. 扫码 = 1 << 0,
  146. 称重 = 1 << 1,
  147. 外检 = 1 << 2,
  148. 顶升 = 1 << 3,
  149. 移栽 = 1 << 4,
  150. 旋转 = 1 << 5,
  151. 入库 = 1 << 6,
  152. 出库 = 1 << 7,
  153. 巷道口 = 1 << 8,
  154. RGV口 = 1 << 9,
  155. AGV口 = 1 << 10,
  156. 直轨 = 1 << 11,
  157. 弯轨 = 1 << 12,
  158. 环轨 = 1 << 13,
  159. 巷道 = 1 << 14,
  160. 堆垛机 = 1 << 15,
  161. 输送机 = 1 << 16,
  162. #region 一轨双车堆垛机
  163. 一列堆垛机 = 1 << 23,
  164. 二列堆垛机 = 1 << 24,
  165. #endregion 一轨双车堆垛机
  166. RGV = 1 << 25,
  167. 桁架 = 1 << 26,
  168. 一楼扫码 = 1 << 27,
  169. 满轮主线第一次扫码 = 1 << 28,
  170. 主线分流点 = 1 << 29,
  171. 一楼叠盘机 = 1 << 30,
  172. }
  173. /// <summary>
  174. /// 输送机段信息
  175. /// </summary>
  176. public class StationSegmentInfo
  177. {
  178. /// <summary>
  179. /// 构造函数
  180. /// </summary>
  181. /// <param name="start">起始设备号</param>
  182. /// <param name="end">结束设备号</param>
  183. /// <param name="ip">ip</param>
  184. public StationSegmentInfo(int start, int end, string ip)
  185. {
  186. Start = start;
  187. End = end;
  188. IP = ip;
  189. }
  190. /// <summary>
  191. /// 起始设备编号
  192. /// </summary>
  193. public int Start { get; set; }
  194. /// <summary>
  195. ///结束设备编号
  196. /// </summary>
  197. public int End { get; set; }
  198. /// <summary>
  199. /// 输送机段所属IP
  200. /// </summary>
  201. public string IP { get; set; }
  202. }
  203. /// <summary>
  204. /// RGV信息
  205. /// </summary>
  206. public class RGVSegmentInfo
  207. {
  208. /// <summary>
  209. /// 构造函数
  210. /// </summary>
  211. /// <param name="start">起始设备号</param>
  212. /// <param name="end">结束设备号</param>
  213. /// <param name="ip">ip</param>
  214. public RGVSegmentInfo(int code, string ip)
  215. {
  216. Code = code;
  217. IP = ip;
  218. }
  219. /// <summary>
  220. /// 设备编号
  221. /// </summary>
  222. public int Code { get; set; }
  223. /// <summary>
  224. /// 输送机段所属IP
  225. /// </summary>
  226. public string IP { get; set; }
  227. }
  228. /// <summary>
  229. /// 桁架信息
  230. /// </summary>
  231. public class TrussSegmentInfo
  232. {
  233. /// <summary>
  234. /// 构造函数
  235. /// </summary>
  236. /// <param name="code"></param>
  237. /// <param name="ip">ip</param>
  238. public TrussSegmentInfo(int code, string ip)
  239. {
  240. Code = code;
  241. IP = ip;
  242. }
  243. /// <summary>
  244. /// 设备编号
  245. /// </summary>
  246. public int Code { get; set; }
  247. /// <summary>
  248. /// 输送机段所属IP
  249. /// </summary>
  250. public string IP { get; set; }
  251. }
  252. /// <summary>
  253. /// 扫码器信息
  254. /// </summary>
  255. public class BcrInfo
  256. {
  257. /// <summary>
  258. /// 构造函数
  259. /// </summary>
  260. /// <param name="deviceNo">设备编号</param>
  261. /// <param name="ip">ip</param>
  262. public BcrInfo(int[] deviceNo, string ip)
  263. {
  264. DeviceNo = deviceNo;
  265. IP = ip;
  266. }
  267. /// <summary>
  268. /// 设备编号
  269. /// </summary>
  270. public int[] DeviceNo { get; set; }
  271. /// <summary>
  272. /// ip
  273. /// </summary>
  274. public string IP { get; set; }
  275. }
  276. /// <summary>
  277. /// 外形信息
  278. /// </summary>
  279. public class ShapeInfo
  280. {
  281. /// <summary>
  282. /// 构造函数
  283. /// </summary>
  284. /// <param name="deviceNo">设备编号</param>
  285. /// <param name="ip">ip</param>
  286. public ShapeInfo(int[] deviceNo, string ip)
  287. {
  288. DeviceNo = deviceNo;
  289. IP = ip;
  290. }
  291. /// <summary>
  292. /// 设备编号
  293. /// </summary>
  294. public int[] DeviceNo { get; set; }
  295. /// <summary>
  296. /// ip
  297. /// </summary>
  298. public string IP { get; set; }
  299. }
  300. /// <summary>
  301. /// 路径信息
  302. /// </summary>
  303. public class RouteInfo
  304. {
  305. /// <summary>
  306. /// 构造函数
  307. /// </summary>
  308. /// <param name="deviceCode">起始点设备号</param>
  309. /// <param name="nextList">下一个设备集合</param>
  310. public RouteInfo(string deviceCode, string[] nextList)
  311. {
  312. DeviceCode = deviceCode;
  313. NextList = nextList;
  314. }
  315. /// <summary>
  316. /// 设备号
  317. /// </summary>
  318. public string DeviceCode { get; set; }
  319. /// <summary>
  320. /// 下一个设备
  321. /// </summary>
  322. public string[] NextList { get; set; }
  323. }
  324. }