DataCollectionSysyem.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using Newtonsoft.Json;
  2. using ServiceCenter;
  3. using ServiceCenter.Helpers;
  4. using ServiceCenter.SqlSugars;
  5. using System.ComponentModel;
  6. using WCS.Core;
  7. using WCS.Entity;
  8. using WCS.Entity.Protocol.DataStructure;
  9. using WCS.Entity.Protocol.Station;
  10. using WCS.Service.Worlds;
  11. using WCS.WorkEngineering.Extensions;
  12. namespace WCS.Service.Systems
  13. {
  14. /// <summary>
  15. /// 数据采集系统
  16. /// </summary>
  17. [BelongTo(typeof(DataCollectionWorld))]
  18. [Description("数据采集系统")]
  19. public class DataCollectionSysyem : DeviceSystem<SRM>
  20. {
  21. public List<Station> ConvList;
  22. public DataCollectionSysyem()
  23. {
  24. ConvList = Device.All.Where(v => v.HasProtocol(typeof(IStation523))).Select(v => new Station(v, this.World)).ToList();
  25. }
  26. protected override bool ParallelDo => true;
  27. protected override bool SaveLogsToFile => true;
  28. public override bool Select(Device dev)
  29. {
  30. return dev.Code == "SRM1";
  31. }
  32. public override void Do(SRM obj)
  33. {
  34. SqlSugarHelper.Do(db =>
  35. {
  36. DeviceDataPack pack = new DeviceDataPack();
  37. pack.StationDatas = new DeviceDataCollection<StationData>();
  38. pack.StationDatas.Frame = DateTime.Now;
  39. foreach (var item in ConvList)
  40. {
  41. var dev = new StationData()
  42. {
  43. Code = item.Entity.Code,
  44. Frame = DateTime.Now,
  45. D520 = item.Data as WCS_Station520,
  46. D521 = item.Data2 as WCS_Station521,
  47. D523 = item.Data3 as WCS_Station523,
  48. };
  49. pack.StationDatas.Datas.Append(dev);
  50. }
  51. pack.Frame = DateTime.Now;
  52. //pack.
  53. //byte[] bytes = System.Text.Encoding.Default.GetBytes(JsonConvert.SerializeObject(ConvList, null, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }));
  54. var plcData = new WCS_PlcData()
  55. {
  56. AddWho = "WCS",
  57. WAREHOUSE = ServiceHub.WarehouseName,
  58. CONTENT = JsonConvert.SerializeObject(ConvList, null, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }),
  59. };
  60. var a = TypeConversionHelper.SerializeRedisValue(ConvList);
  61. //对bytes进行数据压缩
  62. //plcData.CONTENT = bytes.Compress();
  63. db.Default.Insertable(plcData).ExecuteCommand();
  64. });
  65. }
  66. }
  67. }