using CryptoExchange.Net.SharedApis;
using System;
namespace CryptoExchange.Net.Objects.Sockets
{
///
/// An update received from a socket update subscription
///
/// The type of the data
public class DataEvent
{
///
/// The timestamp the data was received
///
public DateTime ReceiveTime { get; set; }
///
/// The timestamp of the data as specified by the server. Note that the server time and client time might not be 100% in sync so this value might not be fully comparable to local time.
///
public DateTime? DataTime { get; set; }
///
/// The stream producing the update
///
public string? StreamId { get; set; }
///
/// The symbol the update is for
///
public string? Symbol { get; set; }
///
/// The original data that was received, only available when OutputOriginalData is set to true in the client options
///
public string? OriginalData { get; set; }
///
/// Type of update
///
public SocketUpdateType? UpdateType { get; set; }
///
/// The received data deserialized into an object
///
public T Data { get; set; }
///
/// ctor
///
public DataEvent(T data, string? streamId, string? symbol, string? originalData, DateTime receiveTimestamp, SocketUpdateType? updateType)
{
Data = data;
StreamId = streamId;
Symbol = symbol;
OriginalData = originalData;
ReceiveTime = receiveTimestamp;
UpdateType = updateType;
}
///
/// Create a new DataEvent with data in the from of type K based on the current DataEvent. Topic, OriginalData and ReceivedTimestamp will be copied over
///
/// The type of the new data
/// The new data
///
public DataEvent As(K data)
{
return new DataEvent(data, StreamId, Symbol, OriginalData, ReceiveTime, UpdateType)
{
DataTime = DataTime
};
}
///
/// Create a new DataEvent with data in the from of type K based on the current DataEvent. OriginalData and ReceivedTimestamp will be copied over
///
/// The type of the new data
/// The new data
/// The new symbol
///
public DataEvent As(K data, string? symbol)
{
return new DataEvent(data, StreamId, symbol, OriginalData, ReceiveTime, UpdateType)
{
DataTime = DataTime
};
}
///
/// Create a new DataEvent with data in the from of type K based on the current DataEvent. OriginalData and ReceivedTimestamp will be copied over
///
/// The type of the new data
/// The new data
/// The new stream id
/// The new symbol
/// The type of update
///
public DataEvent As(K data, string streamId, string? symbol, SocketUpdateType updateType)
{
return new DataEvent(data, streamId, symbol, OriginalData, ReceiveTime, updateType)
{
DataTime = DataTime
};
}
///
/// Copy the WebCallResult to a new data type
///
/// The new type
/// The exchange the result is for
/// The data
///
public ExchangeEvent AsExchangeEvent(string exchange, K data)
{
return new ExchangeEvent(exchange, this.As(data))
{
DataTime = DataTime
};
}
///
/// Specify the symbol
///
///
///
public DataEvent WithSymbol(string symbol)
{
Symbol = symbol;
return this;
}
///
/// Specify the update type
///
///
///
public DataEvent WithUpdateType(SocketUpdateType type)
{
UpdateType = type;
return this;
}
///
/// Specify the stream id
///
///
///
public DataEvent WithStreamId(string streamId)
{
StreamId = streamId;
return this;
}
///
/// Specify the data timestamp
///
public DataEvent WithDataTimestamp(DateTime? timestamp)
{
DataTime = timestamp;
return this;
}
///
/// Create a CallResult from this DataEvent
///
///
public CallResult ToCallResult()
{
return new CallResult(Data, OriginalData, null);
}
///
/// Create a CallResult from this DataEvent
///
///
public CallResult ToCallResult(K data)
{
return new CallResult(data, OriginalData, null);
}
///
/// Create a CallResult from this DataEvent
///
///
public CallResult ToCallResult(Error error)
{
return new CallResult(default, OriginalData, error);
}
///
public override string ToString()
{
return $"{StreamId} - {(Symbol == null ? "" : (Symbol + " - "))}{(UpdateType == null ? "" : (UpdateType + " - "))}{Data}";
}
}
}