ProfileParam.cs 2.27 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
using System;
using System.Collections.Generic;
using System.Text;
using Misc;


namespace FLY.Thick.Base.Common
{
    public class ProfileParam 
    {
        public string pname;
        public int target;
        public int alarm;
        public double comp;
        public int shift;
        public int beginNo;
        public int endNo;
        public int dataBeginNo;
        public int dataEndNo;

        #region IPack 成员

        public byte[] ToBytes()
        {
            List<byte> buf = new List<byte>();
            byte[] bs = Misc.Converter.StringToBytes(pname);
            buf.AddRange(BitConverter.GetBytes(bs.Length));
            buf.AddRange(bs);

            buf.AddRange(BitConverter.GetBytes(target));
            buf.AddRange(BitConverter.GetBytes(alarm));

            buf.AddRange(BitConverter.GetBytes(comp));
            buf.AddRange(BitConverter.GetBytes(shift));
            buf.AddRange(BitConverter.GetBytes(beginNo));
            buf.AddRange(BitConverter.GetBytes(endNo));

            buf.AddRange(BitConverter.GetBytes(dataBeginNo));
            buf.AddRange(BitConverter.GetBytes(dataEndNo));
            return buf.ToArray();
        }

        public bool TryParse(byte[] value)
        {
            int cnt = 4 + 4 * 2 + 8 + 4 * 5;
            if (value.Length < cnt)
                return false;
            int idx = 0;
            int len = BitConverter.ToInt32(value, idx);
            idx += 4;
            cnt += len;
            if (value.Length < cnt)
                return false;

            pname = Misc.Converter.BytesToString(value, idx, len);
            idx += len;

            target = BitConverter.ToInt32(value, idx);
            idx += 4;

            alarm = BitConverter.ToInt32(value, idx);
            idx += 4;


            comp = BitConverter.ToDouble(value, idx);
            idx += 8;

            shift = BitConverter.ToInt32(value, idx);
            idx += 4;


            beginNo = BitConverter.ToInt32(value, idx);
            idx += 4;
            endNo = BitConverter.ToInt32(value, idx);
            idx += 4;

            dataBeginNo = BitConverter.ToInt32(value, idx);
            idx += 4;
            dataEndNo = BitConverter.ToInt32(value, idx);
            idx += 4;
            return true;
        }

        #endregion
    }
}