using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.ComponentModel;
using FObjBase;
using System.Collections.Specialized;
namespace FLY.Thick.BulkDataModule
{
public class BulkDataService : IBulkDataService, IBulkDataServiceAdd
{
#region 成员变量
int m_size;//最大纵向幅数
int m_nbolts;//分区数
int m_boltno1st;//第1分区号
int pushmix_max = -1;
///
/// 缓冲数据区
///
private List m_fr_data;
public List Datas
{
get { return m_fr_data; }
}
public event NotifyDatasChangedEventHandler DatasChanged;
private int count = 0;
///
/// 数量
///
public int Count {
get { return count; }
set {
if (count != value) {
count = value;
NotifyPropertyChanged("Count");
}
}
}
private int currentbookmark = -1;
///
/// 最新的bookmark
///
public int CurrentBookmark
{
get { return currentbookmark; }
set
{
if (currentbookmark != value)
{
currentbookmark = value;
NotifyPropertyChanged("CurrentBookmark");
}
}
}
#endregion
private string param_path = "bulk.csv";
public BulkDataService(int nbolts, int boltno1st, int size) : this(nbolts, boltno1st, size, "bulk.csv")
{
}
public BulkDataService(int nbolts, int boltno1st, int size, string param_path)
{
m_nbolts = nbolts;
m_size = size;
m_boltno1st = boltno1st;
m_fr_data = new List();
if (!string.IsNullOrEmpty(param_path))
this.param_path = param_path;
Load();
}
#region ITDParam 成员
public bool Load()
{
if (!File.Exists(param_path))
return false;
StreamReader sr = new StreamReader(param_path);
string s = sr.ReadLine();
if (s == null)
return false;
//第1行,标题
//bookmark,time,type,0,1,2,3...........
string[] ss = s.Split(new char[] { ',' });
int nbs = ss.Length - 3;
if (nbs != NBolts)
{
sr.Close();
return false; // boltmap does not match!
}
int boltno1st;
if (!int.TryParse(ss[3], out boltno1st))
return false; // boltmap does not match!
BoltNo1st = boltno1st;
m_fr_data.Clear();
int i = 0;
while (!sr.EndOfStream)
{
if (i >= m_size)
{
sr.Close(); return true;
}
s = sr.ReadLine();
ss = s.Split(new char[] { ',' });
FR_DATA frdata = new FR_DATA();
if (!int.TryParse(ss[0], out frdata.bookmark))
{
//失败,跳过
continue;
}
if (!DateTime.TryParse(ss[1], out frdata.timemark))
{
//失败,跳过
continue;
}
int t;
if (!int.TryParse(ss[2], out t))
{
//失败,跳过
continue;
}
frdata.type = (FR_DATA_TYPE)t;
frdata.m_data = new int[m_nbolts];
for (int j = 0; j < ss.Length - 3; j++)
{
if (j >= m_nbolts)
break;
string data_str = ss[j + 3];
if (string.IsNullOrWhiteSpace(data_str))
frdata.m_data[j] = Misc.MyBase.NULL_VALUE;
else
{
if (!int.TryParse(ss[j + 3], out frdata.m_data[j]))
frdata.m_data[j] = Misc.MyBase.NULL_VALUE;
}
}
m_fr_data.Add(frdata);
i++;
}
sr.Close();
return true;
}
public void Save()
{
if (string.IsNullOrEmpty(param_path))
return;
StreamWriter sw = new StreamWriter(param_path, false);
//第1行,标题
//bookmark, time, type, 0,1,2,3......
sw.Write("bookmark,time,type");
for (int i = 0; i < NBolts; i++)
{
sw.Write("," + (BoltNo1st+i).ToString());
}
sw.WriteLine();
for (int i = 0; i < m_fr_data.Count; i++)
{
string s = m_fr_data[i].bookmark.ToString() + "," +
m_fr_data[i].timemark.ToString("yyyy/MM/dd HH:mm:ss") + "," +
((int)m_fr_data[i].type).ToString() + ",";
for (int j = 0; j < m_nbolts; j++)
{
if (Misc.MyBase.ISVALIDATA(m_fr_data[i].m_data[j]))
s += m_fr_data[i].m_data[j].ToString();
if (j != m_nbolts - 1)
s += ",";
}
sw.WriteLine(s);
}
sw.Flush();
sw.Close();
return;
}
#endregion
#region IBulkDataService
///
/// 注册新数据推送
///
///
///
///
public void PushNewFrameData(
BULKDATA_OBJ_INTERFACE.Pack_CallPushNewFrameDataParam param, AsyncCBHandler AsyncDelegate, object AsyncState)
{
if (!dataRequestList.Exists(dr => dr.AsyncState.Equals(AsyncState) && IsSameParam(dr.Param, param)))
{
dataRequestList.Add(
new DataRequest()
{
Param = param,
AsyncState = AsyncState,
AsyncDelegate = AsyncDelegate
});
}
}
void GetNewFrameData(BULKDATA_OBJ_INTERFACE.Pack_CallPushNewFrameDataParam param, AsyncCBHandler AsyncDelegate, object AsyncState)
{
int[] result;
if (pushmix_max < 1)
return;
int mix = (param.mix < pushmix_max) ? param.mix : pushmix_max;
GetFrame(out result, -1, param.mix, 0, NBolts);
BULKDATA_OBJ_INTERFACE.Pack_CallPushNewFrameDataReturn reponse = new BULKDATA_OBJ_INTERFACE.Pack_CallPushNewFrameDataReturn()
{
mix = param.mix
};
FR_DATA frdata = GetBulkFrame(-1);
if (frdata != null)
{
reponse.bookmark = frdata.bookmark;//结果对应的 bookmark
reponse.dt = frdata.timemark;//结果对应的 时间
reponse.type = frdata.type;//结果.数据是否有效
reponse.result = result;//结果, 当needPush=true, 以后推送时,只发送变化了的数据
}
AsyncDelegate(AsyncState, reponse);
}
///
/// 获取一幅数据, 默认带推送
///
///
///
///
public void GetFrameData(BULKDATA_OBJ_INTERFACE.Pack_CallGetFrameDataParam param, AsyncCBHandler AsyncDelegate, object AsyncState)
{
if (param.NeedPush)//推送
{
if (!dataRequestList.Exists(dr => dr.AsyncState.Equals(AsyncState) && IsSameParam(dr.Param, param)))
{
dataRequestList.Add(
new DataRequest()
{
Param = param,
AsyncState = AsyncState,
AsyncDelegate = AsyncDelegate
});
}
}
int[] result;
GetFrame(out result, param.bm, param.mix, 0, NBolts);
BULKDATA_OBJ_INTERFACE.Pack_CallGetFrameDataReturn reponse = new BULKDATA_OBJ_INTERFACE.Pack_CallGetFrameDataReturn()
{
NeedPush = param.NeedPush,
bm = param.bm,
mix = param.mix
};
FR_DATA frdata = GetBulkFrame(param.bm);
if (frdata != null)
{
reponse.bookmark = frdata.bookmark;//结果对应的 bookmark
reponse.dt = frdata.timemark;//结果对应的 时间
reponse.fromBoltIndex = 0;//开始分区index
reponse.toBoltIndex = result.Length - 1;//结束分区index
reponse.type = frdata.type;//结果.数据是否有效
reponse.result = result;//结果, 当needPush=true, 以后推送时,只发送变化了的数据
}
AsyncDelegate(AsyncState, reponse);
}
void GetFrameData(BULKDATA_OBJ_INTERFACE.Pack_CallGetFrameDataParam param, AsyncCBHandler AsyncDelegate, object AsyncState, int boltIndex, int len)
{
int[] result;
GetFrame(out result, param.bm, param.mix, boltIndex, len);
BULKDATA_OBJ_INTERFACE.Pack_CallGetFrameDataReturn reponse = new BULKDATA_OBJ_INTERFACE.Pack_CallGetFrameDataReturn()
{
NeedPush = param.NeedPush,
bm = param.bm,
mix = param.mix
};
FR_DATA frdata = GetBulkFrame(param.bm);
if (frdata != null)
{
reponse.bookmark = frdata.bookmark;//结果对应的 bookmark
reponse.dt = frdata.timemark;//结果对应的 时间
reponse.fromBoltIndex = boltIndex;//开始分区index
reponse.toBoltIndex = boltIndex + len - 1;//结束分区index
reponse.type = frdata.type;//结果.数据是否有效
reponse.result = result;//结果, 当needPush=true, 以后推送时,只发送变化了的数据
}
AsyncDelegate(AsyncState, reponse);
}
bool IsSameParam_PushNewFrameData(object src, object dest)
{
if ((src is BULKDATA_OBJ_INTERFACE.Pack_CallPushNewFrameDataParam)
&& (dest is BULKDATA_OBJ_INTERFACE.Pack_CallPushNewFrameDataParam))
{
BULKDATA_OBJ_INTERFACE.Pack_CallPushNewFrameDataParam fdparam
= src as BULKDATA_OBJ_INTERFACE.Pack_CallPushNewFrameDataParam;
BULKDATA_OBJ_INTERFACE.Pack_CallPushNewFrameDataParam cfdparam
= dest as BULKDATA_OBJ_INTERFACE.Pack_CallPushNewFrameDataParam;
if (fdparam.mix == cfdparam.mix)
{
return true;
}
else
{
return false;
}
}
return false;
}
bool IsSameParam_GetFrameData(object src, object dest)
{
if ((src is BULKDATA_OBJ_INTERFACE.Pack_CallClosePushFrameData) &&
(dest is BULKDATA_OBJ_INTERFACE.Pack_CallClosePushFrameData))
{
BULKDATA_OBJ_INTERFACE.Pack_CallClosePushFrameData fdparam =
src as BULKDATA_OBJ_INTERFACE.Pack_CallClosePushFrameData;
BULKDATA_OBJ_INTERFACE.Pack_CallClosePushFrameData cfdparam =
dest as BULKDATA_OBJ_INTERFACE.Pack_CallClosePushFrameData;
if ((fdparam.bm == cfdparam.bm) && (fdparam.mix == cfdparam.mix))
{
return true;
}
else
{
return false;
}
}
return false;
}
bool IsSameParam_GetTrendData(object src, object dest)
{
if ((src is BULKDATA_OBJ_INTERFACE.Pack_CallClosePushTrendData) &&
(dest is BULKDATA_OBJ_INTERFACE.Pack_CallClosePushTrendData))
{
BULKDATA_OBJ_INTERFACE.Pack_CallClosePushTrendData tdparam =
src as BULKDATA_OBJ_INTERFACE.Pack_CallClosePushTrendData;
BULKDATA_OBJ_INTERFACE.Pack_CallClosePushTrendData ctdparam =
dest as BULKDATA_OBJ_INTERFACE.Pack_CallClosePushTrendData;
if ((tdparam.mix == ctdparam.mix) &&
(tdparam.fromBoltNo == ctdparam.fromBoltNo) &&
(tdparam.toBoltNo == ctdparam.toBoltNo))
{
return true;
}
else
{
return false;
}
}
return false;
}
bool IsSameParam_GetFrameDataMix(object src, object dest)
{
if ((src is Pack_CallClosePushFrameDataMixRequest) &&
(dest is Pack_CallClosePushFrameDataMixRequest))
{
return ((Pack_CallClosePushFrameDataMixRequest)src).IsSame(
(Pack_CallClosePushFrameDataMixRequest)dest);
}
return false;
}
bool IsSameParam(object src, object dest)
{
if (IsSameParam_GetFrameData(src, dest) == true)
return true;
if (IsSameParam_GetTrendData(src, dest) == true)
return true;
if (IsSameParam_PushNewFrameData(src, dest) == true)
return true;
if (IsSameParam_GetFrameDataMix(src, dest) == true)
return true;
return false;
}
///
/// 获取纵向趋势图,默认带推送
///
///
///
///
public void GetTrendData(BULKDATA_OBJ_INTERFACE.Pack_CallGetTrendDataParam param, AsyncCBHandler AsyncDelegate, object AsyncState)
{
if (param.NeedPush)
{
if (!dataRequestList.Exists(dr => dr.AsyncState.Equals(AsyncState) && IsSameParam(dr.Param, param)))
{
dataRequestList.Add(
new DataRequest()
{
Param = param,
AsyncState = AsyncState,
AsyncDelegate = AsyncDelegate
});
}
}
TrendDataCell[] result;
GetTrend(out result, param.len, param.mix, param.fromBoltNo, param.toBoltNo);
BULKDATA_OBJ_INTERFACE.Pack_CallGetTrendDataReturn reponse = new BULKDATA_OBJ_INTERFACE.Pack_CallGetTrendDataReturn();
reponse.NeedPush = param.NeedPush;//当数据变化时,是否需要推送
reponse.len = param.len;//获取的长度
reponse.mix = param.mix;//混合数
reponse.fromBoltNo = param.fromBoltNo;//开始分区index
reponse.toBoltNo = param.toBoltNo;//结束分区index
//TODO
//输出
//FR_DATA frdata = GetBulkFrame(-1);
//if (frdata != null)//有数据,不是空的!!!!!
//{
if(result != null)
{
reponse.edit_position = 0;
reponse.result = result;//结果
}
AsyncDelegate(AsyncState, reponse);
}
///
/// 关闭 扫描图 推送
///
///
///
public void ClosePushFrameData(BULKDATA_OBJ_INTERFACE.Pack_CallClosePushFrameData param, object AsyncState)
{
dataRequestList.RemoveAll(
dr => dr.AsyncState.Equals(AsyncState) && IsSameParam(dr.Param, param));
}
///
/// 关闭 新数据推送
///
///
///
public void ClosePushNewFrameData(BULKDATA_OBJ_INTERFACE.Pack_CallClosePushNewFrameData param, object AsyncState)
{
dataRequestList.RemoveAll(
dr => dr.AsyncState.Equals(AsyncState) && IsSameParam(dr.Param, param));
}
///
/// 关闭 纵向趋势图 推送
///
///
///
public void ClosePushTrendData(BULKDATA_OBJ_INTERFACE.Pack_CallClosePushTrendData param, object AsyncState)
{
dataRequestList.RemoveAll(
dr => dr.AsyncState.Equals(AsyncState) && IsSameParam(dr.Param, param));
}
///
/// 获取一幅数据, 默认带推送
/// 参数 Pack_CallGetFrameDataMixRequest
/// 回复 Pack_CallGetFrameDataMixReponse
///
///
///
///
public void GetFrameDataMix(
Pack_CallGetFrameDataMixRequest param, AsyncCBHandler AsyncDelegate, object AsyncState)
{
if (param.NeedPush)//推送
{
if (!dataRequestList.Exists(dr => dr.AsyncState.Equals(AsyncState) && IsSameParam(dr.Param, param)))
{
dataRequestList.Add(
new DataRequest()
{
Param = param,
AsyncState = AsyncState,
AsyncDelegate = AsyncDelegate
});
}
}
GetFrameDataMix(param, AsyncDelegate, AsyncState, 0, NBolts);
}
void GetFrameDataMix(Pack_CallGetFrameDataMixRequest param, AsyncCBHandler AsyncDelegate, object AsyncState, int boltIndex, int len)
{
FrameDataCell[] result;
int start_bookmark;
GetFrameMix(out result,out start_bookmark, param.bm, param.mix, boltIndex, len, param.start_dt);
Pack_CallGetFrameDataMixReponse reponse = new Pack_CallGetFrameDataMixReponse()
{
NeedPush = param.NeedPush,
bm = param.bm,
mix = param.mix,
start_dt = param.start_dt
};
FR_DATA frdata = GetBulkFrame(param.bm);
if (frdata != null)
{
reponse.bookmark = frdata.bookmark;//结果对应的 bookmark
reponse.dt = frdata.timemark;//结果对应的 时间
reponse.fromBoltIndex = boltIndex;//开始分区index
reponse.toBoltIndex = boltIndex + len - 1;//结束分区index
reponse.type = frdata.type;//结果.数据是否有效
reponse.result = result;//结果, 当needPush=true, 以后推送时,只发送变化了的数据
FR_DATA start_frdata = GetBulkFrame(start_bookmark);
reponse.act_startDt = start_frdata.timemark;//结果对应的 实际开始时间
}
AsyncDelegate(AsyncState, reponse);
}
///
/// 关闭 扫描图 推送
///
///
///
public void ClosePushFrameDataMix(
Pack_CallClosePushFrameDataMixRequest param, object AsyncState)
{
dataRequestList.RemoveAll(
dr => dr.AsyncState.Equals(AsyncState) && IsSameParam(dr.Param, param));
}
///
/// 只要关于AsyncState 的都要删除
///
///
public void ClosePush(Predicate