using FObjBase;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
///
/// 公共类型
///
namespace FLY.Thick.BulkDataModule
{
///
/// 一幅数据是否有效
///
public enum FR_DATA_TYPE
{
///
/// 无效
///
INVALID = 0,
///
/// 有效
///
VALID = 1
}
///
///
///
public class FR_DATA : IPack2
{
public FR_DATA_TYPE type;
public int bookmark;// bookmark ID
public DateTime timemark;
public int[] m_data;
public override string ToString()
{
return bookmark.ToString();
}
public FR_DATA(int bm, int[] data, FR_DATA_TYPE type)
{
bookmark = bm;
timemark = DateTime.Now;
m_data = (int[])data.Clone();
this.type = type;
}
public FR_DATA()
{
}
public bool TryParse(byte[] value, int index, out int cnt)
{
cnt = 4 + 8 + 4;
if (index + cnt > value.Length)
return false;
int idx = index;
bookmark = BitConverter.ToInt32(value, idx);
idx += 4;
try
{
timemark = new DateTime(BitConverter.ToInt64(value, idx));
}
catch
{
timemark = DateTime.MinValue;
}
idx += 8;
type = (FR_DATA_TYPE)BitConverter.ToInt32(value, idx);
idx += 4;
//data
cnt += 4;
if (index + cnt > value.Length)
return false;
int len = BitConverter.ToInt32(value, idx);
idx += 4;
cnt += len * 4;
if (index + cnt > value.Length)
return false;
if (len > 0)
{
m_data = new int[len];
for (int i = 0; i < len; i++)
{
m_data[i] = BitConverter.ToInt32(value, idx);
idx += 4;
}
}
return true;
}
public byte[] ToBytes()
{
List buf = new List();
buf.AddRange(BitConverter.GetBytes(bookmark));
buf.AddRange(BitConverter.GetBytes(timemark.Ticks));
buf.AddRange(BitConverter.GetBytes((int)type));
if (m_data == null)
{
buf.AddRange(BitConverter.GetBytes(0));
}
else
{
buf.AddRange(BitConverter.GetBytes(m_data.Length));
for (int i = 0; i < m_data.Length; i++)
{
buf.AddRange(BitConverter.GetBytes(m_data[i]));
}
}
return buf.ToArray();
}
public bool TryParse(byte[] value)
{
int cnt;
return TryParse(value, 0, out cnt);
}
}
///
/// 它是 struct, 值类型,所以不用 Clone!!!!!
///
public struct TrendDataCell
{
///
/// 时间
///
public DateTime Time;
///
/// 平均值
///
public int Avg;
///
/// sigma
///
public int Sigma;
///
/// 类型
///
public FR_DATA_TYPE Type;
///
///
///
///
public override string ToString()
{
return Time.ToShortTimeString() + " " + Avg.ToString();
}
#region IPack 成员
///
/// 输出20个字节
///
///
public byte[] ToBytes()
{
List buf = new List();
buf.AddRange(BitConverter.GetBytes(Time.Ticks));
buf.AddRange(BitConverter.GetBytes(Avg));
buf.AddRange(BitConverter.GetBytes(Sigma));
buf.AddRange(BitConverter.GetBytes((int)Type));
return buf.ToArray();
}
///
/// 使用20个字节转换
///
///
///
public bool TryParse(byte[] value, int startIndex )
{
int count = 8 + 4 * 3;
if (value.Length < count)
return false;
int idx = startIndex;
Time = new DateTime(BitConverter.ToInt64(value, idx));
idx += 8;
Avg = BitConverter.ToInt32(value, idx);
idx += 4;
Sigma = BitConverter.ToInt32(value, idx);
idx += 4;
Type = (FR_DATA_TYPE)BitConverter.ToInt32(value, idx);
idx += 4;
return true;
}
public TrendDataCell Clone()
{
return new TrendDataCell()
{
Time = Time,
Avg = Avg,
Sigma = Sigma,
Type = Type
};
}
#endregion
}
///
/// 获取模式
///
public enum PushMode
{
///
/// 获取并且推送
///
GetAndPush,
///
/// 只获取
///
Get
}
///
/// 它是 struct, 值类型,所以不用 Clone!!!!!
///
public struct FrameDataCell
{
///
/// 平均值
///
public int Avg;
///
/// sigma
///
public int Sigma;
///
///
///
///
public override string ToString()
{
return Avg.ToString();
}
#region IPack 成员
///
/// 输出20个字节
///
///
public byte[] ToBytes()
{
List buf = new List();
buf.AddRange(BitConverter.GetBytes(Avg));
buf.AddRange(BitConverter.GetBytes(Sigma));
return buf.ToArray();
}
///
/// 使用8个字节转换
///
///
///
///
public bool TryParse(byte[] value, int startIndex)
{
int count = 4 * 2;
if (value.Length < count)
return false;
int idx = startIndex;
Avg = BitConverter.ToInt32(value, idx);
idx += 4;
Sigma = BitConverter.ToInt32(value, idx);
idx += 4;
return true;
}
///
/// 复制
///
///
public FrameDataCell Clone()
{
return new FrameDataCell()
{
Avg = Avg,
Sigma = Sigma,
};
}
#endregion
}
}