using Edge.Core.Processor.Dispatcher;
using Edge.Core.Processor.Dispatcher.Attributes;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Text;
using System.Threading.Tasks;
using System.Linq;
using System.Reflection;
namespace Edge.Core.Processor
{
public interface IProcessor
{
///
/// Gets the meta config name of this Processor instance created from.
/// this is unique guaranteed for instances.
/// Framework will set this value at runtime.
///
string MetaConfigName { get; set; }
//string SerialNumber { get; set; }
///
/// Framework does not guarantee the sequence of this call between all processors(include AppProcessor and DeviceProcessor).
/// For AppProcessor, the framework guarantees Init(...) for all AppProcessors have called at this stage, but the DeviceProcessor may or may not be started.
///
///
Task Start() { return Task.FromResult(true); }
Task Stop() { return Task.FromResult(true); }
//bool Destroy(string reason) { return true; }
///
/// The method will be called from user for a generic test purpose, should implemented this method
/// to cover most real life time logic to give user a meaningful and confidence result for how this processor would work
/// when run for real.
///
///
/// throw exception to indicates the test failed, otherwise, return a Completed task.
Task Test(params object[] parameters) { throw new NotImplementedException("暂不支持测试"); }
}
public class ProcessorEqualityComparer : IEqualityComparer
{
public bool Equals([AllowNull] IProcessor x, [AllowNull] IProcessor y)
{
if (x == null || y == null) return false;
if (x.MetaConfigName != y.MetaConfigName) return false;
//if (x.SerialNumber != y.SerialNumber) return false;
if (x is IAppProcessor && !(y is IAppProcessor)) return false;
if (y is IAppProcessor && !(x is IAppProcessor)) return false;
return true;
}
public int GetHashCode([DisallowNull] IProcessor obj)
{
return obj.GetHashCode();
}
}
///
/// the result for operating a processor's instance.
///
public class ProcessorInstanceOperatingResult
{
public IProcessor ProcessorInstance { get; set; }
public ProcessorMetaConfig MetaConfig { get; set; }
//public string Description { get; set; }
public bool Succeed { get; set; }
///
/// the version of current assembly, in most case, it's the AppProcessor or DeviceProcessor's assembly version.
///
public string HostVersion { get; set; }
///
/// the version of the Edge.Core assembly.
///
public string CoreVersion { get; set; }
public string FailedReason { get; set; }
}
public class ProcessorLifeCycleOperationResult
{
public static ProcessorLifeCycleOperationResult From(ProcessorInstanceOperatingResult input)
{
var output = new ProcessorLifeCycleOperationResult();
var metaPartsDescriptor = input.ProcessorInstance?.ProcessorDescriptor()?.DeviceHandlerOrApp?.GetType()
?.GetCustomAttributes()?.FirstOrDefault();
output.Description = metaPartsDescriptor?.Description;
output.IsSystemInternalComponent = metaPartsDescriptor?.IsSystemInternalComponent ?? false;
output.ProcessorInstanceEntryName = input.MetaConfig?.Name;
output.Tags = metaPartsDescriptor?.Tags;
output.ProcessorMetaConfigName = input.MetaConfig?.Name;
output.HostVersion = input.HostVersion;
output.CoreVersion = input.CoreVersion;
output.Succeed = input.Succeed;
output.FailedReason = input.FailedReason;
return output;
}
public string Description { get; set; }
public string ProcessorMetaConfigName { get; set; }
public string ProcessorInstanceEntryName { get; set; }
///
/// the version of current assembly, in most case, it's the AppProcessor or DeviceProcessor's assembly version.
///
public string HostVersion { get; set; }
///
/// the version of the Edge.Core assembly.
///
public string CoreVersion { get; set; }
public bool IsSystemInternalComponent { get; set; }
public bool Succeed { get; set; }
public string FailedReason { get; set; }
public string[] Tags { get; set; }
}
}