IProcessor.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using Edge.Core.Processor.Dispatcher;
  2. using Edge.Core.Processor.Dispatcher.Attributes;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics.CodeAnalysis;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Linq;
  9. using System.Reflection;
  10. namespace Edge.Core.Processor
  11. {
  12. public interface IProcessor
  13. {
  14. /// <summary>
  15. /// Gets the meta config name of this Processor instance created from.
  16. /// this is unique guaranteed for instances.
  17. /// Framework will set this value at runtime.
  18. /// </summary>
  19. string MetaConfigName { get; set; }
  20. //string SerialNumber { get; set; }
  21. /// <summary>
  22. /// Framework does not guarantee the sequence of this call between all processors(include AppProcessor and DeviceProcessor).
  23. /// For AppProcessor, the framework guarantees Init(...) for all AppProcessors have called at this stage, but the DeviceProcessor may or may not be started.
  24. /// </summary>
  25. /// <returns></returns>
  26. Task<bool> Start() { return Task.FromResult(true); }
  27. Task<bool> Stop() { return Task.FromResult(true); }
  28. //bool Destroy(string reason) { return true; }
  29. /// <summary>
  30. /// The method will be called from user for a generic test purpose, should implemented this method
  31. /// to cover most real life time logic to give user a meaningful and confidence result for how this processor would work
  32. /// when run for real.
  33. /// </summary>
  34. /// <param name="parameters"></param>
  35. /// <returns>throw exception to indicates the test failed, otherwise, return a Completed task.</returns>
  36. Task Test(params object[] parameters) { throw new NotImplementedException("暂不支持测试"); }
  37. }
  38. public class ProcessorEqualityComparer : IEqualityComparer<IProcessor>
  39. {
  40. public bool Equals([AllowNull] IProcessor x, [AllowNull] IProcessor y)
  41. {
  42. if (x == null || y == null) return false;
  43. if (x.MetaConfigName != y.MetaConfigName) return false;
  44. //if (x.SerialNumber != y.SerialNumber) return false;
  45. if (x is IAppProcessor && !(y is IAppProcessor)) return false;
  46. if (y is IAppProcessor && !(x is IAppProcessor)) return false;
  47. return true;
  48. }
  49. public int GetHashCode([DisallowNull] IProcessor obj)
  50. {
  51. return obj.GetHashCode();
  52. }
  53. }
  54. /// <summary>
  55. /// the result for operating a processor's instance.
  56. /// </summary>
  57. public class ProcessorInstanceOperatingResult
  58. {
  59. public IProcessor ProcessorInstance { get; set; }
  60. public ProcessorMetaConfig MetaConfig { get; set; }
  61. //public string Description { get; set; }
  62. public bool Succeed { get; set; }
  63. /// <summary>
  64. /// the version of current assembly, in most case, it's the AppProcessor or DeviceProcessor's assembly version.
  65. /// </summary>
  66. public string HostVersion { get; set; }
  67. /// <summary>
  68. /// the version of the Edge.Core assembly.
  69. /// </summary>
  70. public string CoreVersion { get; set; }
  71. public string FailedReason { get; set; }
  72. }
  73. public class ProcessorLifeCycleOperationResult
  74. {
  75. public static ProcessorLifeCycleOperationResult From(ProcessorInstanceOperatingResult input)
  76. {
  77. var output = new ProcessorLifeCycleOperationResult();
  78. var metaPartsDescriptor = input.ProcessorInstance?.ProcessorDescriptor()?.DeviceHandlerOrApp?.GetType()
  79. ?.GetCustomAttributes<MetaPartsDescriptor>()?.FirstOrDefault();
  80. output.Description = metaPartsDescriptor?.Description;
  81. output.IsSystemInternalComponent = metaPartsDescriptor?.IsSystemInternalComponent ?? false;
  82. output.ProcessorInstanceEntryName = input.MetaConfig?.Name;
  83. output.Tags = metaPartsDescriptor?.Tags;
  84. output.ProcessorMetaConfigName = input.MetaConfig?.Name;
  85. output.HostVersion = input.HostVersion;
  86. output.CoreVersion = input.CoreVersion;
  87. output.Succeed = input.Succeed;
  88. output.FailedReason = input.FailedReason;
  89. return output;
  90. }
  91. public string Description { get; set; }
  92. public string ProcessorMetaConfigName { get; set; }
  93. public string ProcessorInstanceEntryName { get; set; }
  94. /// <summary>
  95. /// the version of current assembly, in most case, it's the AppProcessor or DeviceProcessor's assembly version.
  96. /// </summary>
  97. public string HostVersion { get; set; }
  98. /// <summary>
  99. /// the version of the Edge.Core assembly.
  100. /// </summary>
  101. public string CoreVersion { get; set; }
  102. public bool IsSystemInternalComponent { get; set; }
  103. public bool Succeed { get; set; }
  104. public string FailedReason { get; set; }
  105. public string[] Tags { get; set; }
  106. }
  107. }