App.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using Edge.Core.Processor;
  2. using Edge.Core.Processor.Dispatcher.Attributes;
  3. using Edge.Core.UniversalApi;
  4. using Microsoft.Extensions.DependencyInjection;
  5. using Microsoft.Extensions.Logging;
  6. using Microsoft.Extensions.Logging.Abstractions;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Threading;
  10. namespace PetroChinaOnlineWatchPlugin
  11. {
  12. [UniversalApi(Name = OnUploadOnlineMonitorDataStateChangeEventName, EventDataType = typeof(UploadState),
  13. Description = "Notify the data uploading state change.")]
  14. [MetaPartsDescriptor(
  15. "lang-zh-cn:中国石油在线监测系统 Applang-en-us:Petro china online watch system",
  16. "lang-en-us:中国石油在线监测系统APP, 用于链接多个气液比收集板,以及多个压力表和多个气体浓度表。",
  17. new[] { "lang-zh-cn:在线监测lang-zh-tw:在線監測lang-en-us:OnlineWatch" })]
  18. //<webview src = "http://localhost:8384/WebConsoleHome" ></ webview >
  19. public class App : IAppProcessor
  20. {
  21. public const string OnUploadOnlineMonitorDataStateChangeEventName = "OnUploadOnlineMonitorDataStateChange";
  22. public string MetaConfigName { get; set; }
  23. private ILogger logger = NullLogger.Instance;
  24. private UniversalApiHub universalApiHub;
  25. //The specified number of seconds.
  26. private int heartBeatUdpPushingInterval = 0;
  27. private Timer heartBeatUdpPushingTimer;
  28. private AppConfigV1 appConfig;
  29. [ParamsJsonSchemas("PetroChinaOnlineWatchParamsJsonSchemas")]
  30. public App(AppConfigV1 appConfig, IServiceProvider services)
  31. {
  32. if (services != null)
  33. {
  34. this.appConfig = appConfig;
  35. var loggerFactory = services.GetRequiredService<ILoggerFactory>();
  36. logger = loggerFactory.CreateLogger("DynamicPrivate_PetroChinaOnlineWatch");
  37. universalApiHub = services.GetRequiredService<UniversalApiHub>();
  38. }
  39. }
  40. public void Init(IEnumerable<IProcessor> processors)
  41. {
  42. try
  43. {
  44. logger.LogInformation($"配置LNA地址为, RemoteSubnet {appConfig.RemoteSubnet}, RemoteNode {appConfig.RemoteNode}," +
  45. $" LocalSubnet {appConfig.LocalSubnet}, LocalNode {appConfig.LocalNode}");
  46. ConnectionController.Default.Start(appConfig, logger, this, universalApiHub);
  47. HeartBeatUdpProxy.Default.Start(appConfig, logger);
  48. heartBeatUdpPushingTimer = new Timer(FireTimerEvent, null, 600, 1000);
  49. }
  50. catch (Exception ex)
  51. {
  52. logger.LogError($"Init error, exception detail: {ex}");
  53. }
  54. }
  55. private void FireTimerEvent(object state)
  56. {
  57. try
  58. {
  59. if (heartBeatUdpPushingInterval < 0)
  60. {
  61. heartBeatUdpPushingInterval = 10;
  62. HeartBeatUdpProxy.Default.Udp_SendData();
  63. }
  64. else
  65. {
  66. heartBeatUdpPushingInterval -= 1;
  67. }
  68. ConnectionController.Default.SendUnsolicitedMessage();
  69. }
  70. catch (Exception ex)
  71. {
  72. logger.LogError($"Exception in FireTimerEvent(...):{Environment.NewLine}{ex}");
  73. }
  74. }
  75. }
  76. public enum DbAddressType
  77. {
  78. Configure,
  79. EnvironmentData,
  80. PumpData,
  81. FaultData
  82. }
  83. public class AppConfigV1
  84. {
  85. public int HeartBeatPortNumber { get; set; }
  86. public byte RemoteSubnet { get; set; }
  87. public byte RemoteNode { get; set; }
  88. public byte LocalSubnet { get; set; }
  89. public byte LocalNode { get; set; }
  90. public string NetworkConnections { get; set; }
  91. public string DatabaseConnStr { get; set; }
  92. public byte DispenserNumber { get; set; }
  93. public string MainAppDirectory { get; set; }
  94. }
  95. }