using System; using BCG.Utilities.General; namespace BCG { public abstract class TimeUnitStream : ViewStream, IWorker { protected TimeSpan myOffset; protected DateTime myConstructionTime; protected int mySizeOfStream; protected DateTime myZeroTime; protected CircularRowCollection myCollection; protected ViewStreamDatabase myDb; protected int myId = 0; public TimeUnitStream(DateTime zeroTime, TimeSpan offSet, int sizeOfStream, ViewStreamDatabase db) { myConstructionTime = DateTime.Now; myZeroTime = zeroTime; myOffset = offSet; mySizeOfStream = sizeOfStream; myDb = db; myCollection = new CircularRowCollection( new BlueTableSchema("Time", new ColumnInfo("id", 0, typeof(Int32)), new ColumnInfo("time", 1, typeof(DateTime))), mySizeOfStream); } #region ViewStream overriden methods protected override void myOnFirstSubscriber() { myDb.addWorker(this); myInit(); } protected override void myOnLastSubscriber() { myDb.removeWorker(this); } #endregion protected virtual void myInit() { int periods = (int) (DateTime.Now - myConstructionTime).TotalMilliseconds / (int)myOffset.TotalMilliseconds; DateTime dt = myGetLatestRow() == null ? myRoundTime(myZeroTime) : myRoundTime(myZeroTime + new TimeSpan(myOffset.Ticks * periods)); if (myGetLatestRow() == null || !myGetLatestRow()["time"].Equals(dt)) { IBlueRow ar = new BlueRow(myCollection.schema, myId++, dt); IBlueRow dr = myCollection.addRowWithReturn(ar); if (n_updates <= 3000) myFireEvent(new DataPacket(myCollection, myConvertToCollection(ar), myConvertToCollection(dr))); } } #region IWorker methods public void doWork() { myInit(); } #endregion protected abstract DateTime myRoundTime(DateTime dt); #region Private Helper methods private IBlueRow myGetLatestRow() { return myCollection.Count == 0 ? null : BlueDataUtility.extractRow(myCollection, myCollection.Count - 1); } private IBlueRowCollection myConvertToCollection(IBlueRow r) { BlueTable retVal = new BlueTable(myCollection.schema); if (r != null) retVal.addRow(r); return retVal; } #endregion } }