FlyData_WarningHistory.cs 4.23 KB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FLY.Thick.RemoteHistory;

namespace FLY.Thick.Base.Common
{
    /// <summary>
    /// 报警状态
    /// </summary>
    public enum ERR_STATE
    {
        /// <summary>
        /// 报警中
        /// </summary>
        ON,
        /// <summary>
        /// 关闭
        /// </summary>
        OFF,
        /// <summary>
        /// 只发生了一次
        /// </summary>
        ONCE
    }
    public class FlyData_WarningHistory : IFlyData
    {
        /// <summary>
        /// 时间
        /// </summary>
        public DateTime Time { get; set; }
        /// <summary>
        /// 分类
        /// </summary>
        public byte ErrType { get; set; }
        /// <summary>
        /// 出错码
        /// </summary>
        public byte ErrCode { get; set; }
        /// <summary>
        /// 出错状态
        /// </summary>
        public ERR_STATE State { get; set; }
        /// <summary>
        /// 描述
        /// </summary>
        public string Description { get; set; }

        public FlyData_WarningHistory Clone() 
        {
            return new FlyData_WarningHistory()
            {
                Time = Time,
                ErrType = ErrType,
                ErrCode = ErrCode,
                State = State,
                Description = Description
            };
        }
        public string GetHeader()
        {
            string header = "时间,分类,出错码,状态,描述";
            return header;
        }

        public bool TryParse(string header, string str)
        {
            string[] items = str.Split(new char[] { ',' });
            if (items.Length != 5)
                return false;
            DateTime dt;
            if (DateTime.TryParse(items[0], out dt))
                Time = dt;
            else
                return false;
            byte a,b;
            if (byte.TryParse(items[1], out a))
                ErrType = a;
            else 
                return false;
            if (byte.TryParse(items[2], out b))
                ErrCode= b;
            else 
                return false;
            ERR_STATE state;
            if (Enum.TryParse<ERR_STATE>(items[3], out state))
                State = state;
            else
                return false;
            Description = items[4];
            return true;
        }

        public override string ToString()
        {
            string str;
            str = Time.ToString();
            str += "," + ErrType.ToString();
            str += "," + ErrCode.ToString();
            str += "," + State.ToString();
            str += "," + Description.ToString();
            return str;
        }

        public byte[] ToBytes() 
        {
            List<byte> buf = new List<byte>();

            buf.AddRange(BitConverter.GetBytes(Time.Ticks));
            buf.Add(ErrType);
            buf.Add(ErrCode);
            buf.AddRange(BitConverter.GetBytes((int)State));
            byte[] bs = Misc.Converter.StringToBytes(Description);
            int len;
            if (bs == null)
                len = 0;
            else
                len = bs.Length;

            buf.AddRange(BitConverter.GetBytes(len));
            if(len>0)
                buf.AddRange(bs);
            
            return buf.ToArray();
        }
        public bool TryParse(byte[] value, int index, out int cnt)//  ref int idx) 
        {
            cnt = 8 + 1 + 1 + 4+4;
            if (value.Length-index < cnt)
                return false;
            int idx = index;
            Time = new DateTime(BitConverter.ToInt64(value, idx));
            idx += 8;
            ErrType = value[idx];
            idx++;
            ErrCode = value[idx];
            idx++;
            State = (ERR_STATE)BitConverter.ToInt32(value, idx);
            idx += 4;

            int len = BitConverter.ToInt32(value, idx);
            idx += 4;
            cnt += len;
            if (value.Length-index < cnt)
                return false;
            if (len == 0)
                Description = "";
            else
            {
                Description = Misc.Converter.BytesToString(value, idx, len);
            }
            idx += len;
            
            return true;
        }
    }
}