123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472 |
- using RestSharp;
- using ServiceCenter.Redis;
- using ServiceCenter.SqlSugars;
- using ServiceCenter.WebApi;
- using SqlSugar;
- using System.ComponentModel;
- using System.Diagnostics;
- using System.Text;
- using WCS.Core;
- using WCS.Entity.Protocol.BCR;
- using WCS.Entity.Protocol.DataStructure;
- 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 WCS.WorkEngineering.Worlds;
- namespace WCS.WorkEngineering.Systems
- {
- /// <summary>
- /// 数据处理
- /// </summary>
- //[BelongTo(typeof(MainWorld))]
- [Description("数据处理")]
- public class MainSysyem : SystemBase
- {
- public override void Update(List<WorkTimes> list)
- {
- try
- {
- var channel = new Channel
- {
- World = World.Description,
- Stage = "DoLogics",
- System = Description,
- Item = this.Description
- };
- Ltc.SetChannel(channel);
- var sw = new Stopwatch();
- sw.Start();
- var number = new List<int>();
- number.AddRange(new[] { 1, 2, 3, 4, 5 });
- var sql1 = "";
- Parallel.ForEach(number, i =>
- {
- var db = new SqlSugarHelper().PLC;
- var pack = RedisHub.Monitor.LPop<DeviceDataPack>("DataCollectionpacks");
- ;
- if (pack == null) return;
- Parallel.ForEach(pack.GetType().GetProperties().OrderBy(x => x.Name), ps =>
- {
- var sw10 = new Stopwatch();
- sw10.Start();
- var typeName = "";
- try
- {
- //if (ps.PropertyType == typeof(ProtocolData<WCS_BCR80>[]))
- //{
- // if (pack.BCR80.Any())
- // {
- // var value = pack.BCR80.Select(x => x.Data).ToList();
- // var sql = db.Insertable(value).ToSqlString();
- // db.Ado.ExecuteCommand(sql);
- // typeName = typeof(WCS_BCR80).Name;
- // }
- //}
- //else if (ps.PropertyType == typeof(ProtocolData<WCS_BCR81>[]))
- //{
- // if (pack.BCR81.Any())
- // {
- // var value = pack.BCR81.Select(x => x.Data).ToList();
- // var sql = db.Insertable(value).ToSqlString();
- // db.Ado.ExecuteCommand(sql);
- // typeName = typeof(WCS_BCR81).Name;
- // }
- //}
- //else if (ps.PropertyType == typeof(ProtocolData<WCS_BCR83>[]))
- //{
- // if (pack.BCR83.Any())
- // {
- // var value = pack.BCR83.Select(x => x.Data).ToList();
- // var sql = db.Insertable(value).ToSqlString();
- // db.Ado.ExecuteCommand(sql);
- // typeName = typeof(WCS_BCR83).Name;
- // }
- //}
- //else if (ps.PropertyType == typeof(ProtocolData<WCS_RGV520>[]))
- //{
- // if (pack.RGV520.Any())
- // {
- // var value = pack.RGV520.Select(x => x.Data).ToList();
- // var sql = db.Insertable(value).ToSqlString();
- // db.Ado.ExecuteCommand(sql);
- // typeName = typeof(WCS_RGV520).Name;
- // }
- //}
- //else if (ps.PropertyType == typeof(ProtocolData<WCS_RGV521>[]))
- //{
- // if (pack.RGV521.Any())
- // {
- // var value = pack.RGV521.Select(x => x.Data).ToList();
- // var sql = db.Insertable(value).ToSqlString();
- // db.Ado.ExecuteCommand(sql);
- // typeName = typeof(WCS_RGV521).Name;
- // }
- //}
- //else if (ps.PropertyType == typeof(ProtocolData<WCS_RGV523>[]))
- //{
- // if (pack.RGV523.Any())
- // {
- // var value = pack.RGV523.Select(x => new QuestDb_RGV523()
- // {
- // Alarm = Convert.ToString(x.Data.Alarm),
- // Code = x.Data.Code,
- // Frame = x.Data.Frame.ToLocalTime()
- // }).ToList();
- // var sql = db.Insertable(value).ToSqlString();
- // db.Ado.ExecuteCommand(GetString(sql));
- // typeName = typeof(WCS_RGV523).Name;
- // }
- //}
- //else
- if (ps.PropertyType == typeof(ProtocolData<WCS_Robot520>[]))
- {
- if (pack.Robot520.Any())
- {
- var value = pack.Robot520.Select(x => x.Data).ToList();
- var sql = db.Insertable(value).ToSqlString();
- db.Ado.ExecuteCommand(sql);
- typeName = typeof(WCS_Robot520).Name;
- }
- }
- else if (ps.PropertyType == typeof(ProtocolData<WCS_Robot521>[]))
- {
- if (pack.Robot521.Any())
- {
- var value = pack.Robot521.Select(x => x.Data).ToList();
- var sql = db.Insertable(value).ToSqlString();
- db.Ado.ExecuteCommand(sql);
- typeName = typeof(WCS_Robot521).Name;
- }
- }
- else if (ps.PropertyType == typeof(ProtocolData<WCS_Station5>[]))
- {
- if (pack.Station5.Any())
- {
- var value = pack.Station5.Select(x => x.Data).ToList();
- var sql = db.Insertable(value).ToSqlString();
- db.Ado.ExecuteCommand(sql);
- typeName = typeof(WCS_Station5).Name;
- }
- }
- //else if (ps.PropertyType == typeof(ProtocolData<WCS_Robot522>[]))
- //{
- // if (pack.Robot522.Any())
- // {
- // var value = pack.Robot522.Select(x => new QuestDb_Robot522()
- // {
- // Alarm = Convert.ToString(x.Data.Alarm),
- // Code = x.Data.Code,
- // Frame = x.Data.Frame.ToLocalTime()
- // }).ToList();
- // var sql = db.Insertable(value).ToSqlString();
- // db.Ado.ExecuteCommand(sql);
- // typeName = typeof(WCS_Robot522).Name;
- // }
- //}
- //else if (ps.PropertyType == typeof(ProtocolData<WCS_Robot530>[]))
- //{
- // if (pack.Robot530.Any())
- // {
- // var value = pack.Robot530.Select(x => x.Data).ToList();
- // var sql = db.Insertable(value).ToSqlString();
- // db.Ado.ExecuteCommand(sql);
- // typeName = typeof(WCS_Robot530).Name;
- // }
- //}
- //else if (ps.PropertyType == typeof(ProtocolData<WCS_Robot531>[]))
- //{
- // if (pack.Robot531.Any())
- // {
- // var value = pack.Robot531.Select(x => x.Data).ToList();
- // var sql = db.Insertable(value).ToSqlString();
- // db.Ado.ExecuteCommand(sql);
- // typeName = typeof(WCS_Robot531).Name;
- // }
- //}
- else if (ps.PropertyType == typeof(ProtocolData<WCS_SRM520>[]))
- {
- if (pack.SRM520.Any())
- {
- var value = pack.SRM520.Select(x => x.Data).ToList();
- var sql = db.Insertable(value).ToSqlString();
- db.Ado.ExecuteCommand(sql);
- typeName = typeof(WCS_SRM520).Name;
- }
- }
- else if (ps.PropertyType == typeof(ProtocolData<WCS_SRM521>[]))
- {
- if (pack.SRM521.Any())
- {
- var value = pack.SRM521.Select(x => x.Data).ToList();
- var sql = db.Insertable(value).ToSqlString();
- db.Ado.ExecuteCommand(sql);
- typeName = typeof(WCS_SRM521).Name;
- }
- }
- //else if (ps.PropertyType == typeof(ProtocolData<WCS_SRM523>[]))
- //{
- // if (pack.SRM523.Any())
- // {
- // var value = pack.SRM523.Select(x => new QuestDb_SRM523()
- // {
- // Alarm = $"{Convert.ToString(x.Data.Alarm1)},{Convert.ToString(x.Data.Alarm1)}",
- // Code = x.Data.Code,
- // Frame = x.Data.Frame.ToLocalTime()
- // }).ToList();
- // var sql = db.Insertable(value).ToSqlString();
- // db.Ado.ExecuteCommand(GetString(sql));
- // typeName = typeof(WCS_SRM523).Name;
- // }
- //}
- //else if (ps.PropertyType == typeof(ProtocolData<WCS_Station520>[]))
- //{
- // if (pack.Station520.Any())
- // {
- // var value = pack.Station520.Select(x => x.Data).ToList();
- // var sql = db.Insertable(value).ToSqlString();
- // db.Ado.ExecuteCommand(sql);
- // typeName = typeof(WCS_Station520).Name;
- // }
- //}
- //else if (ps.PropertyType == typeof(ProtocolData<WCS_Station521>[]))
- //{
- // if (pack.Station521.Any())
- // {
- // var value = pack.Station521.Select(x => x.Data).ToList();
- // var sql = db.Insertable(value).ToSqlString();
- // db.Ado.ExecuteCommand(sql);
- // typeName = typeof(WCS_Station521).Name;
- // }
- //}
- //else if (ps.PropertyType == typeof(ProtocolData<WCS_Station523>[]))
- //{
- // if (pack.Station523.Any())
- // {
- // var value = pack.Station523.Select(x => x.Data).ToList();
- // var sql = db.Insertable(value).ToSqlString();
- // db.Ado.ExecuteCommand(sql);
- // typeName = typeof(WCS_Station523).Name;
- // }
- //}
- //else if (ps.PropertyType == typeof(ProtocolData<WCS_Station524>[]))
- //{
- // if (pack.Station524.Any())
- // {
- // var value = pack.Station524.Select(x => x.Data).ToList();
- // var sql = db.Insertable(value).ToSqlString();
- // db.Ado.ExecuteCommand(sql);
- // typeName = typeof(WCS_Station524).Name;
- // }
- //}
- //else if (ps.PropertyType == typeof(ProtocolData<WCS_Station525>[]))
- //{
- // if (pack.Station525.Any())
- // {
- // var value = pack.Station525.Select(x => x.Data).ToList();
- // var sql = db.Insertable(value).ToSqlString();
- // db.Ado.ExecuteCommand(sql);
- // typeName = typeof(WCS_Station525).Name;
- // }
- //}
- //else if (ps.PropertyType == typeof(ProtocolData<WCS_Station90>[]))
- //{
- // if (pack.Station90.Any())
- // {
- // var value = pack.Station90.Select(x => x.Data).ToList();
- // var sql = db.Insertable(value).ToSqlString();
- // db.Ado.ExecuteCommand(sql);
- // typeName = typeof(WCS_Station90).Name;
- // }
- //}
- //else if (ps.PropertyType == typeof(ProtocolData<WCS_Station91>[]))
- //{
- // if (pack.Station91.Any())
- // {
- // var value = pack.Station91.Select(x => x.Data).ToList();
- // var sql = db.Insertable(value).ToSqlString();
- // db.Ado.ExecuteCommand(sql);
- // typeName = typeof(WCS_Station91).Name;
- // }
- //}
- else if (ps.PropertyType == typeof(ProtocolData<WCS_Truss520>[]))
- {
- if (pack.Truss520.Any())
- {
- var value = pack.Truss520.Select(x => x.Data).ToList();
- var sql = db.Insertable(value).ToSqlString();
- db.Ado.ExecuteCommand(sql);
- typeName = typeof(WCS_Truss520).Name;
- }
- }
- else if (ps.PropertyType == typeof(ProtocolData<WCS_Truss521>[]))
- {
- if (pack.Truss521.Any())
- {
- var value = pack.Truss521.Select(x => x.Data).ToList();
- var sql = db.Insertable(value).ToSqlString();
- db.Ado.ExecuteCommand(sql);
- typeName = typeof(WCS_Truss521).Name;
- }
- }
- //else if (ps.PropertyType == typeof(ProtocolData<WCS_Truss523>[]))
- //{
- // if (pack.Truss523.Any())
- // {
- // var value = pack.Truss523.Select(x => new QuestDb_Truss523()
- // {
- // Alarm = Convert.ToString(x.Data.Alarm),
- // Code = x.Data.Code,
- // Frame = x.Data.Frame.ToLocalTime()
- // }).ToList();
- // var sql = db.Insertable(value).ToSqlString();
- // db.Ado.ExecuteCommand(sql);
- // typeName = typeof(WCS_Truss523).Name;
- // }
- //}
- //else if (ps.PropertyType == typeof(ProtocolData<WCS_Truss530>[]))
- //{
- // if (pack.Truss530.Any())
- // {
- // var value = pack.Truss530.Select(x => x.Data).ToList();
- // var sql = db.Insertable(value).ToSqlString();
- // db.Ado.ExecuteCommand(sql);
- // typeName = typeof(WCS_Truss530).Name;
- // }
- //}
- //else if (ps.PropertyType == typeof(ProtocolData<WCS_Truss531>[]))
- //{
- // if (pack.Truss531.Any())
- // {
- // var value = pack.Truss531.Select(x => x.Data).ToList();
- // var sql = db.Insertable(value).ToSqlString();
- // db.Ado.ExecuteCommand(sql);
- // typeName = typeof(WCS_Truss531).Name;
- // }
- //}
- }
- catch (Exception e)
- {
- World.Log($"错误内容:{e.Message}");
- }
- finally
- {
- sw10.Stop();
- if (sw10.ElapsedMilliseconds > 1000)
- {
- World.Log($"执行耗时:{typeName}:{sw10.ElapsedMilliseconds}");
- }
- }
- });
- });
- sw.Stop();
- if (sw.ElapsedMilliseconds > 1000)
- {
- World.Log($"数据处理耗时:{sw.ElapsedMilliseconds}");
- }
- }
- catch (Exception e)
- {
- World.Log($"错误内容:{e.Message}");
- }
- }
- private Type GetProtocolType(Type source)
- {
- var t = source.GetInterfaces().FirstOrDefault(v => v.GetInterfaces().Any(d => d.Name == "IProtocol"));
- var t1 = source.GetInterfaces().FirstOrDefault(v => v.GetInterfaces().Any(d => d.Name == "IProtocol"));
- return t;
- }
- private object AppendLock = new object();
- public StringBuilder Append(StringBuilder sql, string value)
- {
- lock (AppendLock)
- {
- return sql.Append(value);
- }
- }
- public string GetString(string value)
- {
- return value.Replace(",N'", ",'")
- .Replace("\0", "")
- .Replace("(N'", "('") + "\r";
- }
- public override List<object> GetObjects()
- {
- return new List<object>();
- }
- public bool QuestDbInsert(string query)
- {
- var client = new RestClient($"http://10.30.37.2:9000");
- var request = new RestRequest("exec")
- .AddParameter("limit", "0,1000")
- .AddParameter("explain", "true")
- .AddParameter("count", "true")
- .AddParameter("src", "con")
- .AddParameter("query", query)
- .AddParameter("timings", "true");
- var response = client.Get<QuestDbResponse>(request);
- Console.WriteLine(response.ddl);
- var res = APICaller.CallApi1<QuestDbResponse>("http://10.30.37.2:9000" + "/exec", $"?limit=0,1000&explain=true&count=true&src=con&query={query}&timings=true", "GET");
- return true;
- }
- }
- /// <summary>
- /// QuestDb接口返回结果
- /// </summary>
- public class QuestDbResponse
- {
- /// <summary>
- /// 执行INSERT语句成功后会返回OK
- /// </summary>
- public string ddl { get; set; } = "fail";
- }
- public class QuestDbRequest
- {
- /// <summary>
- /// 分页,limit=0,20相当于返回前20行
- /// </summary>
- public string limit { get; set; }
- /// <summary>
- /// 异常时,返回详细的信息
- /// </summary>
- public string explain { get; set; }
- /// <summary>
- /// 返回执行SQL获取数据的数量
- /// </summary>
- public string count { get; set; }
- /// <summary>
- /// 执行INSERT语句成功后会返回OK
- /// </summary>
- public string src { get; set; }
- /// <summary>
- /// 需要执行的SQL语句
- /// </summary>
- public string query { get; set; }
- /// <summary>
- /// 详细的执行耗时信息
- /// </summary>
- public string timings { get; set; }
- }
- }
|