WorkStart.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. using System.Reflection;
  2. using Dapper;
  3. using Npgsql;
  4. using ServiceCenter;
  5. using SqlSugar;
  6. using System.Reflection.Metadata;
  7. using WCS.Core;
  8. using WCS.Entity;
  9. using WCS.Entity.Protocol.BCR;
  10. using WCS.Entity.Protocol.RGV;
  11. using WCS.Entity.Protocol.Robot;
  12. using WCS.Entity.Protocol.SRM;
  13. using WCS.Entity.Protocol.Station;
  14. using WCS.Entity.Protocol.Truss;
  15. using WCS.WorkEngineering.Extensions;
  16. using DeviceFlags = WCS.WorkEngineering.Extensions.DeviceFlags;
  17. namespace WCS.WorkEngineering;
  18. /// <summary>
  19. /// 业务工程配置信息
  20. /// </summary>
  21. public static class WorkStart
  22. {
  23. /// <summary>
  24. /// 初始化 设备信息
  25. /// </summary>
  26. public static void InitializeDeviceInfo()
  27. {
  28. #region 初始化RGV相关信息
  29. var RgvInfo = new List<RgvSegmentInfo>
  30. {
  31. new(1, "10.30.37.113"), //库一北
  32. new(2, "10.30.37.118"), //库一南
  33. new(3, "10.30.37.123"), //库二北
  34. new(4, "10.30.37.128"), //库二南
  35. new(5, "10.30.37.133"), //库三北
  36. new(6, "10.30.37.138") //库三南
  37. };
  38. foreach (var item in RgvInfo)
  39. {
  40. var conv = new Device($"RGV{item.Code}");
  41. conv.AddFlag(DeviceFlags.RGV);
  42. conv.AddProtocol<IRGV520>(0, 520, item.Ip);
  43. conv.AddProtocol<IRGV521>(0, 521, item.Ip);
  44. conv.AddProtocol<IRGV523>(0, 523, item.Ip);
  45. }
  46. #endregion 初始化RGV相关信息
  47. #region 初始化输送机相关信息
  48. #region 基本信息
  49. #region 托盘线输送线
  50. var tuples = new List<Tuple<string, List<Tuple<int, int>>>>
  51. {
  52. //分拣库一
  53. new("10.30.37.89", new List<Tuple<int, int>>
  54. {
  55. new(1601, 1620),
  56. new(2501, 2540),
  57. new(2701, 2740),
  58. new(1661, 1690)
  59. }),
  60. //分拣库二
  61. new("10.30.37.97", new List<Tuple<int, int>>
  62. {
  63. new(1621, 1640),
  64. new(2901, 2940),
  65. new(3101, 3140),
  66. new(1691, 1720)
  67. }),
  68. //分拣库三
  69. new("10.30.37.105", new List<Tuple<int, int>>
  70. {
  71. new(1641, 1660),
  72. new(3301, 3340),
  73. new(3501, 3540),
  74. new(1721, 1750)
  75. })
  76. };
  77. foreach (var item in tuples)
  78. {
  79. var db520 = 0;
  80. var db521 = 0;
  81. var db523 = 0;
  82. foreach (var item1 in item.Item2)
  83. for (var i = item1.Item1; i <= item1.Item2; i++)
  84. {
  85. var conv = new Device(i.ToString());
  86. conv.AddFlag(DeviceFlags.输送机);
  87. conv.AddProtocol<IStation520>(db520, 520, item.Item1);
  88. conv.AddProtocol<IStation521>(db521, 521, item.Item1);
  89. conv.AddProtocol<IStation523>(db523, 523, item.Item1);
  90. db520 += 14;
  91. db521 += 16;
  92. db523 += 12;
  93. }
  94. }
  95. #endregion 托盘线输送线
  96. #region 满轮输送线
  97. #region DB523,所有线体都会有DB523
  98. var mLtuples523 = new List<Tuple<string, List<Tuple<int, int>>>>
  99. {
  100. new("10.30.37.166", new List<Tuple<int, int>>() //北侧满轮主线
  101. {
  102. new(1, 100),
  103. new(9001, 9010), //暂用,无意义
  104. new(401, 599),
  105. new(801, 999),
  106. new(1201, 1399)
  107. }),
  108. new("10.30.37.198", new List<Tuple<int, int>>() //南侧满轮主线
  109. {
  110. new(101, 210),
  111. new(601, 799),
  112. new(1001, 1199),
  113. new(1401, 1599),
  114. new(341, 379)
  115. })
  116. };
  117. foreach (var item in mLtuples523)
  118. {
  119. var db523 = 0;
  120. var db524 = 0;
  121. foreach (var item1 in item.Item2)
  122. for (var i = item1.Item1; i <= item1.Item2; i++)
  123. {
  124. var conv = new Device(i.ToString());
  125. conv.AddFlag(DeviceFlags.输送机);
  126. conv.AddProtocol<IStation523>(db523, 523, item.Item1);
  127. conv.AddProtocol<IStation524>(db524, 524, item.Item1);
  128. db523 += 12;
  129. db524 += 16;
  130. }
  131. }
  132. #endregion DB523,所有线体都会有DB523
  133. #region 520、521 交互线体会有520、521
  134. //Item2表示线体号集合,Item1表示IP
  135. var mLTuples520 = new List<Tuple<string, List<int>>>
  136. {
  137. //北侧
  138. new("10.30.37.166",
  139. new List<int>
  140. {
  141. 1, 22, 41, 61, 418, 426, 435, 444, 455, 466, 480, 494, 508, 522, 536, 550, 564, 578, 591, 818, 826,
  142. 835, 844, 855, 866, 880, 894, 908, 922, 936, 950, 964, 978, 991, 1218, 1226, 1235, 1244, 1255, 1266,
  143. 1280, 1294, 1308, 1322, 1336, 1350, 1364, 1378, 1391
  144. }),
  145. //南侧
  146. new("10.30.37.198",
  147. new List<int>
  148. {
  149. 101, 122, 141, 161, 618, 626, 635, 644, 655, 666, 680, 694, 708, 722, 736, 750, 764, 778, 791, 1018,
  150. 1026, 1035, 1044, 1055, 1066, 1080, 1094, 1108, 1122, 1136, 1150, 1164, 1178, 1191, 1418, 1426,
  151. 1435, 1444, 1455, 1466, 1480, 1494, 1508, 1522, 1536, 1550, 1564, 1578, 1591
  152. })
  153. };
  154. foreach (var item in mLTuples520)
  155. {
  156. var db520 = 0;
  157. var db521 = 0;
  158. foreach (var device in item.Item2.Select(
  159. item1 => Device.All.FirstOrDefault(v => v.Code == item1.ToString())))
  160. {
  161. if (device != null)
  162. {
  163. device.AddProtocol<IStation520>(db520, 520, item.Item1);
  164. device.AddProtocol<IStation521>(db521, 521, item.Item1);
  165. }
  166. db520 += 14;
  167. db521 += 16;
  168. }
  169. }
  170. var conv9 = Device.All.FirstOrDefault(x => x.Code == "1");
  171. conv9.AddProtocol<IStation5>(32, 5, "10.30.37.166");
  172. var conv10 = Device.All.FirstOrDefault(x => x.Code == "101");
  173. conv10.AddProtocol<IStation5>(32, 5, "10.30.37.198");
  174. #endregion 520、521 交互线体会有520、521
  175. #region 满轮扫码器
  176. //Item2表示线体号集合,Item1表示IP
  177. var mLTuples83 = new List<Tuple<string, List<int>>>
  178. {
  179. new("10.30.37.166", new List<int> { 3, 14, 18, 22, 38, 323, 41, 58, 61 }),
  180. new("10.30.37.198", new List<int> { 101, 114, 118, 122, 138, 363, 141, 158, 161 })
  181. };
  182. foreach (var item in mLTuples83)
  183. {
  184. var db83 = 0;
  185. foreach (var device in item.Item2.Select(
  186. item1 => Device.All.FirstOrDefault(v => v.Code == item1.ToString())))
  187. {
  188. device?.AddProtocol<IBCR83>(db83, 83, item.Item1);
  189. db83 += 604;
  190. }
  191. }
  192. #endregion 满轮扫码器
  193. #region 满轮线告诉分拣预分配
  194. //Item2表示线体号集合,Item1表示IP
  195. var mLTuples525 = new List<Tuple<string, List<int>>>
  196. {
  197. new("10.30.37.166", new List<int> { 18, 38, 58 }),
  198. new("10.30.37.198", new List<int> { 118, 138, 158 })
  199. };
  200. foreach (var item in mLTuples525)
  201. {
  202. var db525 = 0;
  203. foreach (var device in item.Item2.Select(
  204. item1 => Device.All.FirstOrDefault(v => v.Code == item1.ToString())))
  205. {
  206. device?.AddProtocol<IStation525>(db525, 525, item.Item1);
  207. db525 += 3266;
  208. }
  209. }
  210. #endregion 满轮线告诉分拣预分配
  211. #region 外检信息
  212. //Item2表示线体号集合,Item1表示IP
  213. var mLTuples91 = new List<Tuple<string, List<int>>>
  214. {
  215. new("10.30.37.166", new List<int> { 418, 818, 1218 }),
  216. new("10.30.37.198", new List<int> { 618, 1018, 1418 })
  217. };
  218. foreach (var item in mLTuples91)
  219. {
  220. var db91 = 0;
  221. foreach (var device in item.Item2.Select(
  222. item2 => Device.All.FirstOrDefault(v => v.Code == item2.ToString())))
  223. {
  224. device?.AddProtocol<IStation91>(db91, 91, item.Item1);
  225. db91 += 14;
  226. }
  227. }
  228. #endregion 外检信息
  229. #endregion 满轮输送线
  230. #endregion 基本信息
  231. #region 托盘线扫码器
  232. var bcrInfo = new List<BcrInfo>
  233. {
  234. new(new[] { "2532", "2732" }, "10.30.37.89"),
  235. new(new[] { "2932", "3132" }, "10.30.37.97"),
  236. new(new[] { "3332", "3532" }, "10.30.37.105"),
  237. new(new[] { "RGV1" }, "10.30.37.113"),
  238. new(new[] { "RGV2" }, "10.30.37.118"),
  239. new(new[] { "RGV3" }, "10.30.37.123"),
  240. new(new[] { "RGV4" }, "10.30.37.128"),
  241. new(new[] { "RGV5" }, "10.30.37.133"),
  242. new(new[] { "RGV6" }, "10.30.37.138")
  243. };
  244. foreach (var item in bcrInfo)
  245. for (var i = 0; i < item.DeviceNo.Length; i++)
  246. {
  247. var device = Device.All.FirstOrDefault(v => v.Code == item.DeviceNo[i]);
  248. device.AddFlag(DeviceFlags.扫码);
  249. var pos = i * 20;
  250. device.AddProtocol<IBCR81>(pos, 81, item.Ip);
  251. }
  252. #endregion 托盘线扫码器
  253. #region 外检信息
  254. var conv1 = Device.All.FirstOrDefault(x => x.Code == "2532");
  255. conv1.AddFlag(DeviceFlags.外检);
  256. conv1.AddProtocol<IStation91>(714, 91, "10.30.37.89");
  257. var conv2 = Device.All.FirstOrDefault(x => x.Code == "2732");
  258. conv2.AddFlag(DeviceFlags.外检);
  259. conv2.AddProtocol<IStation91>(1274, 91, "10.30.37.89");
  260. conv1 = Device.All.FirstOrDefault(x => x.Code == "2932");
  261. conv1.AddFlag(DeviceFlags.外检);
  262. conv1.AddProtocol<IStation91>(714, 91, "10.30.37.97");
  263. conv2 = Device.All.FirstOrDefault(x => x.Code == "3132");
  264. conv2.AddFlag(DeviceFlags.外检);
  265. conv2.AddProtocol<IStation91>(1274, 91, "10.30.37.97");
  266. conv1 = Device.All.FirstOrDefault(x => x.Code == "3332");
  267. conv1.AddFlag(DeviceFlags.外检);
  268. conv1.AddProtocol<IStation91>(714, 91, "10.30.37.105");
  269. conv2 = Device.All.FirstOrDefault(x => x.Code == "3532");
  270. conv2.AddFlag(DeviceFlags.外检);
  271. conv2.AddProtocol<IStation91>(1274, 91, "10.30.37.105");
  272. #endregion 外检信息
  273. #endregion 初始化输送机相关信息
  274. #region 初始化桁架相关信息
  275. var TrussInfo = new List<TrussSegmentInfo>
  276. {
  277. new(1, "10.30.37.211"),
  278. new(2, "10.30.37.217"),
  279. new(3, "10.30.37.223")
  280. };
  281. foreach (var item in TrussInfo)
  282. {
  283. var conv = new Device($"Truss{item.Code}");
  284. conv.AddFlag(DeviceFlags.桁架);
  285. conv.AddProtocol<ITruss520>(0, 520, item.Ip);
  286. conv.AddProtocol<ITruss521>(0, 521, item.Ip);
  287. conv.AddProtocol<ITruss523>(0, 522, item.Ip);
  288. }
  289. var tuples1 = new List<Tuple<string, List<int>>>
  290. {
  291. //桁架
  292. new("10.30.37.211",
  293. new List<int>
  294. {
  295. 1685, 1686, 1687, 1688, 1689, 1690, 1675, 1674, 1673, 1672, 1671, 1670, 1677, 1678, 1679, 1680,
  296. 1665, 1664, 1663, 1662
  297. }), //分拣库一
  298. new("10.30.37.217",
  299. new List<int>
  300. {
  301. 1715, 1716, 1717, 1718, 1719, 1720, 1705, 1704, 1703, 1702, 1701, 1700, 1707, 1708, 1709, 1710,
  302. 1695, 1694, 1693, 1692
  303. }), //分拣库二
  304. new("10.30.37.223",
  305. new List<int>
  306. {
  307. 1745, 1746, 1747, 1748, 1749, 1750, 1735, 1734, 1733, 1732, 1731, 1730, 1737, 1738, 1739, 1740,
  308. 1725, 1724, 1723, 1722
  309. }) //分拣库三
  310. };
  311. foreach (var item in tuples1)
  312. {
  313. var db530 = 0;
  314. var db531 = 0;
  315. foreach (var conv in item.Item2.Select(item1 => Device.All.FirstOrDefault(x => x.Code == item1.ToString())))
  316. {
  317. conv!.AddProtocol<ITruss530>(db530, 530, item.Item1);
  318. conv!.AddProtocol<ITruss531>(db531, 531, item.Item1);
  319. db530 += 18;
  320. db531 += 250;
  321. if (conv.Code == "1662")
  322. {
  323. var a = 0;
  324. }
  325. }
  326. }
  327. var tuples21 = new List<Tuple<string, List<int>>>
  328. {
  329. //机械臂
  330. new("10.30.37.230", new List<int> { 1666, 1661 }), //库一北
  331. new("10.30.37.232", new List<int> { 1681, 1676 }), //库一南
  332. new("10.30.37.234", new List<int> { 1696, 1691 }), //库二北
  333. new("10.30.37.236", new List<int> { 1711, 1706 }), //库二南
  334. new("10.30.37.238", new List<int> { 1726, 1721 }), //库三北
  335. new("10.30.37.240", new List<int> { 1741, 1736 }) //库三南
  336. };
  337. foreach (var item in tuples21)
  338. {
  339. var db530 = 0;
  340. var db531 = 0;
  341. foreach (var conv in item.Item2.Select(item1 => Device.All.FirstOrDefault(x => x.Code == item1.ToString())))
  342. {
  343. conv!.AddProtocol<IRobot530>(db530, 530, item.Item1);
  344. conv!.AddProtocol<IRobot531>(db531, 531, item.Item1);
  345. db530 += 8;
  346. db531 += 130;
  347. }
  348. }
  349. #endregion 初始化桁架相关信息
  350. #region 初始化机械臂相关信息
  351. var TrussInfo1 = new List<TrussSegmentInfo>
  352. {
  353. new(1, "10.30.37.230"),
  354. new(2, "10.30.37.232"),
  355. new(3, "10.30.37.234"),
  356. new(4, "10.30.37.236"),
  357. new(5, "10.30.37.238"),
  358. new(6, "10.30.37.240")
  359. };
  360. foreach (var item in TrussInfo1)
  361. {
  362. var conv = new Device($"Robot{item.Code}");
  363. conv.AddFlag(DeviceFlags.Robot);
  364. conv.AddProtocol<IRobot520>(0, 520, item.Ip);
  365. conv.AddProtocol<IRobot521>(0, 521, item.Ip);
  366. conv.AddProtocol<IRobot522>(0, 522, item.Ip);
  367. }
  368. #endregion 初始化机械臂相关信息
  369. #region 初始化堆垛机相关信息
  370. var ip = 41;
  371. for (var i = 0; i <= 5; i++)
  372. {
  373. var srm = new Device($"SRM{i + 1}");
  374. srm.AddFlag(DeviceFlags.堆垛机);
  375. ip = i == 0 ? ip : ip + 8;
  376. //三台堆垛机IP主机位分别是 41、49、57、65、73、81
  377. srm.AddProtocol<ISRM520>(0, 520, $"10.30.37.{ip}");
  378. srm.AddProtocol<ISRM521>(0, 521, $"10.30.37.{ip}");
  379. srm.AddProtocol<ISRM523>(0, 523, $"10.30.37.{ip}");
  380. //增加巷道
  381. var tunnel = new Device($"TY{i + 1}");
  382. tunnel.AddFlag(DeviceFlags.巷道);
  383. }
  384. #endregion 初始化堆垛机相关信息
  385. }
  386. private static string GetDbTypeString(Type type)
  387. {
  388. if (type.IsEnum) return GetDbTypeString(type.GetEnumUnderlyingType());
  389. if (type == typeof(int)) return "int";
  390. if (type == typeof(short))
  391. return "short";
  392. if (type == typeof(long))
  393. return "long";
  394. if (type == typeof(float))
  395. return "float";
  396. if (type == typeof(DateTime))
  397. return "date";
  398. if (type == typeof(string))
  399. return "string";
  400. if (type == typeof(byte[]))
  401. return "binary";
  402. throw new Exception($"类型{type.Name}不支持");
  403. }
  404. /// <summary>
  405. /// 初始化数据库连接
  406. /// </summary>
  407. /// <param name="datas"></param>
  408. public static void InitDB(this List<DataBaseConnectionString> datas)
  409. {
  410. //RobotCmdType a = (RobotCmdType)(1000000000000000);
  411. var types = AppDomain.CurrentDomain.GetAssemblies()
  412. .Select(v => v.GetTypes()).SelectMany(v => v)
  413. .Where(x => x.IsClass)
  414. .Where(x => x.IsDefined(typeof(SugarTable), false))
  415. .Where(x => typeof(IProtocol).IsAssignableFrom(x))
  416. .Select(v => new
  417. {
  418. type = v,
  419. itype = v.GetInterface($"I{v.Name.Replace("WCS_", "")}")
  420. })
  421. .ToList();
  422. using (var conn = new NpgsqlConnection(Configs.QdbConnString))
  423. {
  424. conn.Execute("Drop table if exists Frames");
  425. var sSql = "CREATE TABLE IF NOT EXISTS Frames(Frame TIMESTAMP) timestamp (Frame) PARTITION BY DAY BYPASS WAL";
  426. var rc = conn.Execute(sSql);
  427. foreach (var type in types)
  428. {
  429. var props = type.itype.GetProperties();
  430. if (props.Length==0)continue;
  431. var tableName = ((SugarTable)type.type.GetCustomAttribute(typeof(SugarTable))).TableName;
  432. conn.Execute($"Drop table if exists {tableName}");
  433. sSql = $"CREATE TABLE IF NOT EXISTS {tableName}(Frame TIMESTAMP,Code SYMBOL,";
  434. sSql += string.Join(',',
  435. props.Select(v => $"{v.Name} {GetDbTypeString(v.PropertyType)}"));
  436. sSql += ") timestamp (Frame) PARTITION BY DAY BYPASS WAL"; //BYPASS WAL
  437. conn.Execute(sSql);
  438. }
  439. }
  440. }
  441. }
  442. public class DevDbConfig<T>
  443. {
  444. public DevDbConfig()
  445. {
  446. }
  447. public DevDbConfig(string ip, T code)
  448. {
  449. IP = ip;
  450. Code = code;
  451. }
  452. public DevDbConfig(string ip, List<DevInterval<T>> devIntervalList)
  453. {
  454. IP = ip;
  455. DevIntervalList = devIntervalList;
  456. }
  457. public DevDbConfig(string ip, List<T> devCodeList)
  458. {
  459. IP = ip;
  460. DevCodeList = devCodeList;
  461. }
  462. public string IP { get; set; }
  463. public T Code { get; set; }
  464. public T StartCode { get; set; }
  465. public T EndCode { get; set; }
  466. public List<T> DevCodeList { get; set; }
  467. public List<DevInterval<T>> DevIntervalList { get; set; }
  468. }
  469. public class DevInterval<T>
  470. {
  471. public DevInterval(T s, T e)
  472. {
  473. StartCode = s;
  474. EndCode = e;
  475. }
  476. public T StartCode { get; set; }
  477. public T EndCode { get; set; }
  478. }