You need to sign in or sign up before continuing.
FlyData_WarningHistory.cs 4.23 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
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;
        }
    }
}