2024-11-13 17:09:15 +08:00
|
|
|
|
using detect.device.Utils;
|
2024-11-19 16:13:59 +08:00
|
|
|
|
using Newtonsoft.Json;
|
2024-11-13 17:09:15 +08:00
|
|
|
|
|
|
|
|
|
namespace detect.device;
|
|
|
|
|
|
|
|
|
|
public class DeviceEvent
|
|
|
|
|
{
|
2024-11-15 10:05:49 +08:00
|
|
|
|
public const string EventAnalyzerData = "event_analyzer_data";
|
|
|
|
|
public const string EventDeviceConnected = "event_device_connected";
|
|
|
|
|
public const string EventDeviceDisConnected = "event_device_disconnected";
|
|
|
|
|
|
2024-11-13 17:09:15 +08:00
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
public IDictionary<string, object> Result { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class DeviceClientRequestBuilder
|
|
|
|
|
{
|
|
|
|
|
private readonly Dictionary<string, object> _params = new();
|
|
|
|
|
public readonly string RequestId = Guid.NewGuid().ToString();
|
|
|
|
|
private string? _type;
|
|
|
|
|
private string? _component;
|
|
|
|
|
private string? _method;
|
|
|
|
|
|
|
|
|
|
public static DeviceClientRequestBuilder Create()
|
|
|
|
|
{
|
|
|
|
|
return new DeviceClientRequestBuilder();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DeviceClientRequestBuilder WithType(string type)
|
|
|
|
|
{
|
|
|
|
|
this._type = type;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DeviceClientRequestBuilder WithComponent(string component)
|
|
|
|
|
{
|
|
|
|
|
this._component = component;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DeviceClientRequestBuilder WithMethod(string method)
|
|
|
|
|
{
|
|
|
|
|
this._method = method;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-19 16:13:59 +08:00
|
|
|
|
public DeviceClientRequestBuilder WithParam(string name, object value)
|
2024-11-13 17:09:15 +08:00
|
|
|
|
{
|
|
|
|
|
this._params.TryAdd(name, value);
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-19 16:13:59 +08:00
|
|
|
|
public DeviceClientRequestBuilder WithParams(Dictionary<string, object> dictionary)
|
|
|
|
|
{
|
|
|
|
|
foreach (var (key, value) in dictionary)
|
|
|
|
|
{
|
|
|
|
|
this._params.TryAdd(key, value);
|
|
|
|
|
}
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-13 17:09:15 +08:00
|
|
|
|
public string Build()
|
|
|
|
|
{
|
|
|
|
|
Dictionary<string, object?> request = new()
|
|
|
|
|
{
|
|
|
|
|
["requestId"] = RequestId,
|
|
|
|
|
["type"] = _type,
|
|
|
|
|
["component"] = _component,
|
|
|
|
|
["method"] = _method,
|
|
|
|
|
["params"] = _params
|
|
|
|
|
};
|
|
|
|
|
return JsonUtil.SerializeObject(request);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class DeviceClientResponse<TResult>
|
|
|
|
|
{
|
|
|
|
|
public DeviceClientResponse(int code, string message)
|
|
|
|
|
{
|
|
|
|
|
Code = code;
|
|
|
|
|
Message = message;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string? RequestId { get; set; }
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 回复类型,service,task,event
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string Type { get; set; } = "service";
|
|
|
|
|
public int Code { get; set; }
|
|
|
|
|
public string Message { get; set; }
|
|
|
|
|
public double? Duration { get; set; }
|
|
|
|
|
public TResult? Result { get; set; }
|
|
|
|
|
public bool IsSuccess => this.Code == 0;
|
|
|
|
|
public bool IsFailed => this.Code != 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public interface IDeviceClient
|
|
|
|
|
{
|
|
|
|
|
public event EventHandler<DeviceEvent> DeviceEvent;
|
|
|
|
|
|
|
|
|
|
public bool Connected();
|
|
|
|
|
|
|
|
|
|
public bool ConnectAsync();
|
|
|
|
|
|
|
|
|
|
public bool DisConnectAsync();
|
|
|
|
|
|
|
|
|
|
public void PublishEvent(DeviceEvent deviceEvent);
|
|
|
|
|
|
|
|
|
|
public Task<DeviceClientResponse<T>> RequestAction<T>(DeviceClientRequestBuilder builder);
|
|
|
|
|
|
|
|
|
|
public DeviceClientResponse<object> RequestActionSync(DeviceClientRequestBuilder builder);
|
|
|
|
|
}
|