123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606 |
- using System.Reflection;
- using Dapper;
- using Npgsql;
- using ServiceCenter;
- using SqlSugar;
- using System.Reflection.Metadata;
- using WCS.Core;
- using WCS.Entity;
- using WCS.Entity.Protocol.BCR;
- using WCS.Entity.Protocol.RGV;
- using WCS.Entity.Protocol.Robot;
- using WCS.Entity.Protocol.SRM;
- using WCS.Entity.Protocol.Station;
- using WCS.Entity.Protocol.Truss;
- using WCS.WorkEngineering.Extensions;
- using DeviceFlags = WCS.WorkEngineering.Extensions.DeviceFlags;
- namespace WCS.WorkEngineering;
- /// <summary>
- /// 业务工程配置信息
- /// </summary>
- public static class WorkStart
- {
- /// <summary>
- /// 初始化 设备信息
- /// </summary>
- public static void InitializeDeviceInfo()
- {
- #region 初始化RGV相关信息
- var RgvInfo = new List<RgvSegmentInfo>
- {
- new(1, "10.30.37.113"), //库一北
- new(2, "10.30.37.118"), //库一南
- new(3, "10.30.37.123"), //库二北
- new(4, "10.30.37.128"), //库二南
- new(5, "10.30.37.133"), //库三北
- new(6, "10.30.37.138") //库三南
- };
- foreach (var item in RgvInfo)
- {
- var conv = new Device($"RGV{item.Code}");
- conv.AddFlag(DeviceFlags.RGV);
- conv.AddProtocol<IRGV520>(0, 520, item.Ip);
- conv.AddProtocol<IRGV521>(0, 521, item.Ip);
- conv.AddProtocol<IRGV523>(0, 523, item.Ip);
- }
- #endregion 初始化RGV相关信息
- #region 初始化输送机相关信息
- #region 基本信息
- #region 托盘线输送线
- var tuples = new List<Tuple<string, List<Tuple<int, int>>>>
- {
- //分拣库一
- new("10.30.37.89", new List<Tuple<int, int>>
- {
- new(1601, 1620),
- new(2501, 2540),
- new(2701, 2740),
- new(1661, 1690)
- }),
- //分拣库二
- new("10.30.37.97", new List<Tuple<int, int>>
- {
- new(1621, 1640),
- new(2901, 2940),
- new(3101, 3140),
- new(1691, 1720)
- }),
- //分拣库三
- new("10.30.37.105", new List<Tuple<int, int>>
- {
- new(1641, 1660),
- new(3301, 3340),
- new(3501, 3540),
- new(1721, 1750)
- })
- };
- foreach (var item in tuples)
- {
- var db520 = 0;
- var db521 = 0;
- var db523 = 0;
- foreach (var item1 in item.Item2)
- for (var i = item1.Item1; i <= item1.Item2; i++)
- {
- var conv = new Device(i.ToString());
- conv.AddFlag(DeviceFlags.输送机);
- conv.AddProtocol<IStation520>(db520, 520, item.Item1);
- conv.AddProtocol<IStation521>(db521, 521, item.Item1);
- conv.AddProtocol<IStation523>(db523, 523, item.Item1);
- db520 += 14;
- db521 += 16;
- db523 += 12;
- }
- }
- #endregion 托盘线输送线
- #region 满轮输送线
- #region DB523,所有线体都会有DB523
- var mLtuples523 = new List<Tuple<string, List<Tuple<int, int>>>>
- {
- new("10.30.37.166", new List<Tuple<int, int>>() //北侧满轮主线
- {
- new(1, 100),
- new(9001, 9010), //暂用,无意义
- new(401, 599),
- new(801, 999),
- new(1201, 1399)
- })
- };
- foreach (var item in mLtuples523)
- {
- var db523 = 0;
- var db524 = 0;
- var db222 = 0;
- foreach (var item1 in item.Item2)
- {
- for (var i = item1.Item1; i <= item1.Item2; i++)
- {
- var conv = new Device(i.ToString());
- conv.AddFlag(DeviceFlags.输送机);
-
- conv.AddProtocol<IStation523>(db523, 523, item.Item1);
- conv.AddProtocol<IStation524>(db524, 524, item.Item1);
- conv.AddProtocol<IStation222>(db222, 7, item.Item1);
- db523 += 12;
- db524 += 16;
- db222 += 4;
- }
- }
- }
- var mLtuples5231 = new List<Tuple<string, List<Tuple<int, int>>>>
- {
- new("10.30.37.198", new List<Tuple<int, int>>() //南侧满轮主线
- {
- new(101, 210),
- new(601, 799),
- new(1001, 1199),
- new(1401, 1599),
- new(341, 379)
- })
- };
- foreach (var item in mLtuples5231)
- {
- var db523 = 0;
- var db524 = 0;
- var db222 = 0;
- foreach (var item1 in item.Item2)
- {
- for (var i = item1.Item1; i <= item1.Item2; i++)
- {
- var conv = new Device(i.ToString());
- conv.AddFlag(DeviceFlags.输送机);
-
- conv.AddProtocol<IStation523>(db523, 523, item.Item1);
- conv.AddProtocol<IStation524>(db524, 524, item.Item1);
- conv.AddProtocol<IStation222>(db222, 7, item.Item1);
- db523 += 12;
- db524 += 16;
- db222 += 4;
- }
- }
- }
- #endregion DB523,所有线体都会有DB523
- #region 520、521 交互线体会有520、521
- //Item2表示线体号集合,Item1表示IP
- var mLTuples520 = new List<Tuple<string, List<int>>>
- {
- //北侧
- new("10.30.37.166",
- new List<int>
- {
- 1, 22, 41, 61, 418, 426, 435, 444, 455, 466, 480, 494, 508, 522, 536, 550, 564, 578, 591, 818, 826,
- 835, 844, 855, 866, 880, 894, 908, 922, 936, 950, 964, 978, 991, 1218, 1226, 1235, 1244, 1255, 1266,
- 1280, 1294, 1308, 1322, 1336, 1350, 1364, 1378, 1391
- }),
- //南侧
- new("10.30.37.198",
- new List<int>
- {
- 101, 122, 141, 161, 618, 626, 635, 644, 655, 666, 680, 694, 708, 722, 736, 750, 764, 778, 791, 1018,
- 1026, 1035, 1044, 1055, 1066, 1080, 1094, 1108, 1122, 1136, 1150, 1164, 1178, 1191, 1418, 1426,
- 1435, 1444, 1455, 1466, 1480, 1494, 1508, 1522, 1536, 1550, 1564, 1578, 1591
- })
- };
- foreach (var item in mLTuples520)
- {
- var db520 = 0;
- var db521 = 0;
- foreach (var device in item.Item2.Select(
- item1 => Device.All.FirstOrDefault(v => v.Code == item1.ToString())))
- {
- if (device != null)
- {
- device.AddProtocol<IStation520>(db520, 520, item.Item1);
- device.AddProtocol<IStation521>(db521, 521, item.Item1);
- }
- db520 += 14;
- db521 += 16;
- }
- }
- var conv9 = Device.All.FirstOrDefault(x => x.Code == "1");
- conv9.AddProtocol<IStation5>(32, 5, "10.30.37.166");
- var conv10 = Device.All.FirstOrDefault(x => x.Code == "101");
- conv10.AddProtocol<IStation5>(32, 5, "10.30.37.198");
- #endregion 520、521 交互线体会有520、521
- #region 满轮扫码器
- //Item2表示线体号集合,Item1表示IP
- var mLTuples83 = new List<Tuple<string, List<int>>>
- {
- new("10.30.37.166", new List<int> { 3, 14, 18, 22, 38, 323, 41, 58, 61 }),
- new("10.30.37.198", new List<int> { 101, 114, 118, 122, 138, 363, 141, 158, 161 })
- };
- foreach (var item in mLTuples83)
- {
- var db83 = 0;
- foreach (var device in item.Item2.Select(
- item1 => Device.All.FirstOrDefault(v => v.Code == item1.ToString())))
- {
- device?.AddProtocol<IBCR83>(db83, 83, item.Item1);
- db83 += 604;
- }
- }
- #endregion 满轮扫码器
- #region 满轮线告诉分拣预分配
- //Item2表示线体号集合,Item1表示IP
- var mLTuples525 = new List<Tuple<string, List<int>>>
- {
- new("10.30.37.166", new List<int> { 18, 38, 58 }),
- new("10.30.37.198", new List<int> { 118, 138, 158 })
- };
- foreach (var item in mLTuples525)
- {
- var db525 = 0;
- foreach (var device in item.Item2.Select(
- item1 => Device.All.FirstOrDefault(v => v.Code == item1.ToString())))
- {
- device?.AddProtocol<IStation525>(db525, 525, item.Item1);
- db525 += 3266;
- }
- }
- #endregion 满轮线告诉分拣预分配
- #region 外检信息
- //Item2表示线体号集合,Item1表示IP
- var mLTuples91 = new List<Tuple<string, List<int>>>
- {
- new("10.30.37.166", new List<int> { 418, 818, 1218 }),
- new("10.30.37.198", new List<int> { 618, 1018, 1418 })
- };
- foreach (var item in mLTuples91)
- {
- var db91 = 0;
- foreach (var device in item.Item2.Select(
- item2 => Device.All.FirstOrDefault(v => v.Code == item2.ToString())))
- {
- device?.AddProtocol<IStation91>(db91, 91, item.Item1);
- db91 += 14;
- }
- }
- #endregion 外检信息
- #endregion 满轮输送线
- #endregion 基本信息
- #region 托盘线扫码器
- var bcrInfo = new List<BcrInfo>
- {
- new(new[] { "2532", "2732" }, "10.30.37.89"),
- new(new[] { "2932", "3132" }, "10.30.37.97"),
- new(new[] { "3332", "3532" }, "10.30.37.105"),
- new(new[] { "RGV1" }, "10.30.37.113"),
- new(new[] { "RGV2" }, "10.30.37.118"),
- new(new[] { "RGV3" }, "10.30.37.123"),
- new(new[] { "RGV4" }, "10.30.37.128"),
- new(new[] { "RGV5" }, "10.30.37.133"),
- new(new[] { "RGV6" }, "10.30.37.138")
- };
- foreach (var item in bcrInfo)
- for (var i = 0; i < item.DeviceNo.Length; i++)
- {
- var device = Device.All.FirstOrDefault(v => v.Code == item.DeviceNo[i]);
- device.AddFlag(DeviceFlags.扫码);
- var pos = i * 20;
- device.AddProtocol<IBCR81>(pos, 81, item.Ip);
- }
- #endregion 托盘线扫码器
- #region 外检信息
- var conv1 = Device.All.FirstOrDefault(x => x.Code == "2532");
- conv1.AddFlag(DeviceFlags.外检);
- conv1.AddProtocol<IStation91>(714, 91, "10.30.37.89");
- var conv2 = Device.All.FirstOrDefault(x => x.Code == "2732");
- conv2.AddFlag(DeviceFlags.外检);
- conv2.AddProtocol<IStation91>(1274, 91, "10.30.37.89");
- conv1 = Device.All.FirstOrDefault(x => x.Code == "2932");
- conv1.AddFlag(DeviceFlags.外检);
- conv1.AddProtocol<IStation91>(714, 91, "10.30.37.97");
- conv2 = Device.All.FirstOrDefault(x => x.Code == "3132");
- conv2.AddFlag(DeviceFlags.外检);
- conv2.AddProtocol<IStation91>(1274, 91, "10.30.37.97");
- conv1 = Device.All.FirstOrDefault(x => x.Code == "3332");
- conv1.AddFlag(DeviceFlags.外检);
- conv1.AddProtocol<IStation91>(714, 91, "10.30.37.105");
- conv2 = Device.All.FirstOrDefault(x => x.Code == "3532");
- conv2.AddFlag(DeviceFlags.外检);
- conv2.AddProtocol<IStation91>(1274, 91, "10.30.37.105");
- #endregion 外检信息
- #endregion 初始化输送机相关信息
- #region 初始化桁架相关信息
- var TrussInfo = new List<TrussSegmentInfo>
- {
- new(1, "10.30.37.211"),
- new(2, "10.30.37.217"),
- new(3, "10.30.37.223")
- };
- foreach (var item in TrussInfo)
- {
- var conv = new Device($"Truss{item.Code}");
- conv.AddFlag(DeviceFlags.桁架);
- conv.AddProtocol<ITruss520>(0, 520, item.Ip);
- conv.AddProtocol<ITruss521>(0, 521, item.Ip);
- conv.AddProtocol<ITruss523>(0, 522, item.Ip);
- }
- var tuples1 = new List<Tuple<string, List<int>>>
- {
- //桁架
- new("10.30.37.211",
- new List<int>
- {
- 1685, 1686, 1687, 1688, 1689, 1690, 1675, 1674, 1673, 1672, 1671, 1670, 1677, 1678, 1679, 1680,
- 1665, 1664, 1663, 1662
- }), //分拣库一
- new("10.30.37.217",
- new List<int>
- {
- 1715, 1716, 1717, 1718, 1719, 1720, 1705, 1704, 1703, 1702, 1701, 1700, 1707, 1708, 1709, 1710,
- 1695, 1694, 1693, 1692
- }), //分拣库二
- new("10.30.37.223",
- new List<int>
- {
- 1745, 1746, 1747, 1748, 1749, 1750, 1735, 1734, 1733, 1732, 1731, 1730, 1737, 1738, 1739, 1740,
- 1725, 1724, 1723, 1722
- }) //分拣库三
- };
- foreach (var item in tuples1)
- {
- var db530 = 0;
- var db531 = 0;
- foreach (var conv in item.Item2.Select(item1 => Device.All.FirstOrDefault(x => x.Code == item1.ToString())))
- {
- conv!.AddProtocol<ITruss530>(db530, 530, item.Item1);
- conv!.AddProtocol<ITruss531>(db531, 531, item.Item1);
- db530 += 18;
- db531 += 250;
- if (conv.Code == "1662")
- {
- var a = 0;
- }
- }
- }
- var tuples21 = new List<Tuple<string, List<int>>>
- {
- //机械臂
- new("10.30.37.230", new List<int> { 1666, 1661 }), //库一北
- new("10.30.37.232", new List<int> { 1681, 1676 }), //库一南
- new("10.30.37.234", new List<int> { 1696, 1691 }), //库二北
- new("10.30.37.236", new List<int> { 1711, 1706 }), //库二南
- new("10.30.37.238", new List<int> { 1726, 1721 }), //库三北
- new("10.30.37.240", new List<int> { 1741, 1736 }) //库三南
- };
- foreach (var item in tuples21)
- {
- var db530 = 0;
- var db531 = 0;
- foreach (var conv in item.Item2.Select(item1 => Device.All.FirstOrDefault(x => x.Code == item1.ToString())))
- {
- conv!.AddProtocol<IRobot530>(db530, 530, item.Item1);
- conv!.AddProtocol<IRobot531>(db531, 531, item.Item1);
- db530 += 8;
- db531 += 130;
- }
- }
- #endregion 初始化桁架相关信息
- #region 初始化机械臂相关信息
- var TrussInfo1 = new List<TrussSegmentInfo>
- {
- new(1, "10.30.37.230"),
- new(2, "10.30.37.232"),
- new(3, "10.30.37.234"),
- new(4, "10.30.37.236"),
- new(5, "10.30.37.238"),
- new(6, "10.30.37.240")
- };
- foreach (var item in TrussInfo1)
- {
- var conv = new Device($"Robot{item.Code}");
- conv.AddFlag(DeviceFlags.Robot);
- conv.AddProtocol<IRobot520>(0, 520, item.Ip);
- conv.AddProtocol<IRobot521>(0, 521, item.Ip);
- conv.AddProtocol<IRobot522>(0, 522, item.Ip);
- }
- #endregion 初始化机械臂相关信息
- #region 初始化堆垛机相关信息
- var ip = 41;
- for (var i = 0; i <= 5; i++)
- {
- var srm = new Device($"SRM{i + 1}");
- srm.AddFlag(DeviceFlags.堆垛机);
- ip = i == 0 ? ip : ip + 8;
- //三台堆垛机IP主机位分别是 41、49、57、65、73、81
- srm.AddProtocol<ISRM520>(0, 520, $"10.30.37.{ip}");
- srm.AddProtocol<ISRM521>(0, 521, $"10.30.37.{ip}");
- srm.AddProtocol<ISRM523>(0, 523, $"10.30.37.{ip}");
- //增加巷道
- var tunnel = new Device($"TY{i + 1}");
- tunnel.AddFlag(DeviceFlags.巷道);
- }
- #endregion 初始化堆垛机相关信息
- }
- private static string GetDbTypeString(Type type)
- {
- if (type.IsEnum) return GetDbTypeString(type.GetEnumUnderlyingType());
- if (type == typeof(int)) return "int";
- if (type == typeof(short))
- return "short";
- if (type == typeof(long))
- return "long";
- if (type == typeof(float))
- return "float";
- if (type == typeof(DateTime))
- return "date";
- if (type == typeof(string))
- return "string";
- if (type == typeof(byte[]))
- return "binary";
- throw new Exception($"类型{type.Name}不支持");
- }
- /// <summary>
- /// 初始化数据库连接
- /// </summary>
- /// <param name="datas"></param>
- public static void InitDB(this List<DataBaseConnectionString> datas)
- {
- //RobotCmdType a = (RobotCmdType)(1000000000000000);
- var types = AppDomain.CurrentDomain.GetAssemblies()
- .Select(v => v.GetTypes()).SelectMany(v => v)
- .Where(x => x.IsClass)
- .Where(x => x.IsDefined(typeof(SugarTable), false))
- .Where(x => typeof(IProtocol).IsAssignableFrom(x))
- .Select(v => new
- {
- type = v,
- itype = v.GetInterface($"I{v.Name.Replace("WCS_", "")}")
- })
- .ToList();
- using (var conn = new NpgsqlConnection(Configs.QdbConnString))
- {
- conn.Execute("Drop table if exists Frames");
- var sSql = "CREATE TABLE IF NOT EXISTS Frames(Frame TIMESTAMP) timestamp (Frame) PARTITION BY DAY BYPASS WAL";
- var rc = conn.Execute(sSql);
- foreach (var type in types)
- {
- var props = type.itype.GetProperties();
- if (props.Length==0)continue;
- var tableName = ((SugarTable)type.type.GetCustomAttribute(typeof(SugarTable))).TableName;
- conn.Execute($"Drop table if exists {tableName}");
- sSql = $"CREATE TABLE IF NOT EXISTS {tableName}(Frame TIMESTAMP,Code SYMBOL,";
- sSql += string.Join(',',
- props.Select(v => $"{v.Name} {GetDbTypeString(v.PropertyType)}"));
- sSql += ") timestamp (Frame) PARTITION BY DAY BYPASS WAL"; //BYPASS WAL
- conn.Execute(sSql);
- }
- }
- }
- }
- public class DevDbConfig<T>
- {
- public DevDbConfig()
- {
- }
- public DevDbConfig(string ip, T code)
- {
- IP = ip;
- Code = code;
- }
- public DevDbConfig(string ip, List<DevInterval<T>> devIntervalList)
- {
- IP = ip;
- DevIntervalList = devIntervalList;
- }
- public DevDbConfig(string ip, List<T> devCodeList)
- {
- IP = ip;
- DevCodeList = devCodeList;
- }
- public string IP { get; set; }
- public T Code { get; set; }
- public T StartCode { get; set; }
- public T EndCode { get; set; }
- public List<T> DevCodeList { get; set; }
- public List<DevInterval<T>> DevIntervalList { get; set; }
- }
- public class DevInterval<T>
- {
- public DevInterval(T s, T e)
- {
- StartCode = s;
- EndCode = e;
- }
- public T StartCode { get; set; }
- public T EndCode { get; set; }
- }
|