90 lines
3.1 KiB
C#
90 lines
3.1 KiB
C#
using System;
|
|
|
|
using BCG.Utilities.General;
|
|
using BCG;
|
|
using bts;
|
|
|
|
namespace bts.BlueOrb {
|
|
[DataStreamUserRespresentation("MillisecondStream")]
|
|
public class MillisecondStream : ViewStream, IWorker {
|
|
private DateTime _last_fire_time = DateTime.MinValue;
|
|
private readonly TimeSpan _period;
|
|
private readonly int _size;
|
|
private readonly ViewStreamDatabase _vsd;
|
|
private IBlueTableSchema _schema;
|
|
private bts.CircularBufferTable _table;
|
|
|
|
public MillisecondStream(int interval_msecs, int size)
|
|
: this(ViewStreamDatabase.instance, interval_msecs, size) {
|
|
}
|
|
|
|
public MillisecondStream(ViewStreamDatabase vsd, int interval_msecs, int size) {
|
|
_vsd = vsd;
|
|
_period = TimeSpan.FromMilliseconds(interval_msecs);
|
|
_size = size;
|
|
_schema = BlueDataUtility.form_schema("Milliseconds",
|
|
Tuple.Create("id", TypeCode.Int32),
|
|
Tuple.Create("time", TypeCode.DateTime),
|
|
Tuple.Create("display_time", TypeCode.String));
|
|
_table = new CircularBufferTable(_schema, _size);
|
|
}
|
|
|
|
protected override void myOnFirstSubscriber() {
|
|
_vsd.addWorker(this);
|
|
}
|
|
|
|
protected override void myOnLastSubscriber() {
|
|
_vsd.removeWorker(this);
|
|
}
|
|
|
|
private int _id = 0;
|
|
public void doWork() {
|
|
if (DateTime.Now - _last_fire_time >= _period) {
|
|
_last_fire_time = DateTime.Now;
|
|
IBlueRow nr = new BlueRow(_schema, _id++, _last_fire_time,
|
|
_last_fire_time.ToString("yyyy-MM-dd HH:mm:ss.fff"));
|
|
_table.add_row(nr);
|
|
var dp = BlueDataUtility.build_dp_inc(_table, myLastValue as DataPacket);
|
|
myFireEvent(dp);
|
|
}
|
|
}
|
|
}
|
|
public class MSecondStream : TimeUnitStream {
|
|
public MSecondStream(int intervalInms, int size)
|
|
: base(DateTime.Now, new TimeSpan(0, 0, 0, 0, intervalInms), size, ViewStreamDatabase.instance) { }
|
|
|
|
protected override DateTime myRoundTime(DateTime dt) { return dt; }
|
|
}
|
|
|
|
public class DisplayableMillisecondStream : ViewStream, IStreamMapper {
|
|
private IBlueTableSchema mySchema = new BlueTableSchema("ms",
|
|
new ColumnInfo("id", 0, typeof(int)),
|
|
new ColumnInfo("time", 1, typeof(string)));
|
|
public DisplayableMillisecondStream(int intervalInMs, int sz) {
|
|
mySetRealSource(new MapStream(new MSecondStream(intervalInMs, sz), this));
|
|
}
|
|
#region IStreamMapper Members
|
|
|
|
public IBlueTableSchema getOutputSchema(IBlueTableSchema inputSchema) {
|
|
return mySchema;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region IDynamicStreamMapper Members
|
|
|
|
public IBlueRow map(IBlueRow row) {
|
|
DateTime time = row.get_datetime("time");
|
|
var time_txt = time.ToString("yyyy-MM-dd HH:mm:ss.fff");
|
|
return new BlueRow(mySchema, row["id"], time_txt);
|
|
}
|
|
|
|
#endregion
|
|
|
|
private string myToString(DateTime dt) {
|
|
string sep = ":";
|
|
return dt.Hour + sep + dt.Minute + sep + dt.Second + sep + dt.Millisecond;
|
|
}
|
|
}
|
|
}
|