WorkStart.cs 19 KB

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