HisRunTag.cs 7.0 KB
Newer Older
cdy816's avatar
cdy816 已提交
1 2 3 4 5 6 7 8 9
//==============================================================
//  Copyright (C) 2019  Inc. All rights reserved.
//
//==============================================================
//  Create by 种道洋 at 2019/12/27 18:45:02.
//  Version 1.0
//  种道洋
//==============================================================
using System;
cdy816's avatar
cdy816 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22
using System.Collections.Generic;
using System.Text;

namespace Cdy.Tag
{
    /// <summary>
    /// 
    /// </summary>
    public  abstract class HisRunTag:HisTag
    {

        #region ... Variables  ...

cdy816's avatar
cdy816 已提交
23 24
        private byte[] headBytes;

cdy816's avatar
cdy816 已提交
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
        #endregion ...Variables...

        #region ... Events     ...

        #endregion ...Events...

        #region ... Constructor...

        #endregion ...Constructor...

        #region ... Properties ...

        /// <summary>
        /// 实时值地址
        /// </summary>
        public byte[] RealMemoryAddr { get; set; }

        /// <summary>
        /// 实时值的内存地址
        /// </summary>
        public int RealValueAddr { get; set; }

        /// <summary>
        /// 历史缓存数据偏移地址
        /// </summary>
cdy816's avatar
cdy816 已提交
50 51 52 53 54 55
        public static MemoryBlock HisAddr { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public static DateTime StartTime { get; set; }
cdy816's avatar
cdy816 已提交
56 57 58 59 60 61

        /// <summary>
        /// 头部信息起始地址
        /// </summary>
        public int BlockHeadStartAddr { get; set; }

cdy816's avatar
cdy816 已提交
62 63 64 65 66
        /// <summary>
        /// 时间戳存访地址
        /// </summary>
        public int TimerValueStartAddr { get; set; }

cdy816's avatar
cdy816 已提交
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
        /// <summary>
        /// 历史数据缓存内存起始地址
        /// </summary>
        public int HisValueStartAddr { get; set; }

        /// <summary>
        /// 质量戳起始地址
        /// </summary>
        public int HisQulityStartAddr { get; set; }

        /// <summary>
        /// 数据内存大小
        /// </summary>
        public int DataSize { get; set; }

        /// <summary>
        /// 已经记录的个数
        /// </summary>
        public int Count { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public int MaxCount { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public virtual byte SizeOfValue { get; }
cdy816's avatar
cdy816 已提交
96 97


cdy816's avatar
cdy816 已提交
98 99 100 101
        #endregion ...Properties...

        #region ... Methods    ...

cdy816's avatar
cdy816 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114
        public void Init()
        {
            var hbyts = new List<byte>(19);
            hbyts.AddRange(BitConverter.GetBytes(this.HisQulityStartAddr - this.TimerValueStartAddr));
            hbyts.Add((byte)this.Type);
            hbyts.Add((byte)this.TagType);
            hbyts.Add((byte)this.CompressType);
            hbyts.AddRange(BitConverter.GetBytes(CompressParameter1));
            hbyts.AddRange(BitConverter.GetBytes(CompressParameter2));
            hbyts.AddRange(BitConverter.GetBytes(CompressParameter3));
            headBytes = hbyts.ToArray();
        }

cdy816's avatar
cdy816 已提交
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
        /// <summary>
        /// 
        /// </summary>
        public void Reset()
        {
            Count = 0;
        }

        /// <summary>
        /// 内部计数更新
        /// 不做实际的数据更新
        /// </summary>
        public void UpdateNone()
        {
            Count = ++Count > MaxCount ? MaxCount : Count;
            HisAddr[HisQulityStartAddr + Count] = (byte)QulityConst.Tick;
        }

        /// <summary>
        /// 写入头部信息
        /// </summary>
        public void UpdateHeader()
        {
            //数据块头部结构
            //数据区大小(int)+记录类型(byte)+变量类型(byte)+压缩类型(byte)+压缩参数1(float)+压缩参数2(float)+压缩参数3(float)
cdy816's avatar
cdy816 已提交
140
            
cdy816's avatar
cdy816 已提交
141 142 143
            //var bids = BitConverter.GetBytes(this.HisQulityStartAddr - this.HisValueStartAddr);
            //Buffer.BlockCopy(HisAddr, BlockHeadStartAddr, bids, 0, bids.Length);

cdy816's avatar
cdy816 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156 157
            HisAddr.WriteBytes(BlockHeadStartAddr, headBytes);

            //HisAddr.WriteInt(BlockHeadStartAddr, this.HisQulityStartAddr - this.TimerValueStartAddr);

            ////写入记录类型
            //HisAddr.WriteByte(BlockHeadStartAddr+4, (byte)this.Type);
            ////写入变量类型
            //HisAddr.WriteByte(BlockHeadStartAddr+5, (byte)this.TagType);
            ////写入压缩类型
            //HisAddr.WriteByte(BlockHeadStartAddr+6, (byte)this.CompressType);
            ////写入压缩附属参数
            //HisAddr.WriteFloat(BlockHeadStartAddr + 7, CompressParameter1);
            //HisAddr.WriteFloat(BlockHeadStartAddr + 11, CompressParameter2);
            //HisAddr.WriteFloat(BlockHeadStartAddr + 15, CompressParameter3);
cdy816's avatar
cdy816 已提交
158 159 160 161 162
        }

        /// <summary>
        /// 在每个内存块的开始和结束部分插入值
        /// </summary>
cdy816's avatar
cdy816 已提交
163
        public void AppendValue(DateTime time)
cdy816's avatar
cdy816 已提交
164
        {
cdy816's avatar
cdy816 已提交
165
            //数据内容:时间戳(time1+time2+...) + 数值区(value1+value2+...)+质量戳区(q1+q2+....)
cdy816's avatar
cdy816 已提交
166
            //更新数值
cdy816's avatar
cdy816 已提交
167 168 169 170 171 172 173 174 175 176

            //写入时间戳
            int tim = (int)((time - StartTime).TotalMilliseconds / HisEnginer.MemoryTimeTick);

            HisAddr.WriteInt(TimerValueStartAddr + Count * 2, tim);

            //写入数值
            HisAddr.WriteBytes(HisValueStartAddr + Count * SizeOfValue, RealMemoryAddr, RealValueAddr, SizeOfValue);

            //Buffer.BlockCopy(RealMemoryAddr, RealValueAddr, HisAddr, HisValueStartAddr + Count * SizeOfValue, SizeOfValue);
cdy816's avatar
cdy816 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
            //更新质量戳
            //实时数据内存结构为:实时值+时间戳+质量戳

            //在现有质量戳基础上+100表示,该数值是强制添加到内存中的
            byte bval = (byte)(RealMemoryAddr[RealValueAddr + SizeOfValue + 8] + 100);

            HisAddr[HisQulityStartAddr + Count] = bval;

            Count = ++Count > MaxCount ? MaxCount : Count;
        }

        /// <summary>
        /// 更新历史数据到缓存中
        /// </summary>
        public void UpdateValue(DateTime time)
        {
cdy816's avatar
cdy816 已提交
193
            //数据内容: 时间戳(time1+time2+...) +数值区(value1+value2+...)+质量戳区(q1+q2+....)
cdy816's avatar
cdy816 已提交
194
            //更新数值
cdy816's avatar
cdy816 已提交
195 196 197 198 199 200 201 202 203 204
            //Buffer.BlockCopy(RealMemoryAddr, RealValueAddr, HisAddr, HisValueStartAddr + Count * SizeOfValue, SizeOfValue);

            //写入时间戳
            int tim = (int)((time - StartTime).TotalMilliseconds / HisEnginer.MemoryTimeTick);

            HisAddr.WriteInt(TimerValueStartAddr + Count * 2, tim);

            //写入数值
            HisAddr.WriteBytes(HisValueStartAddr + Count * SizeOfValue, RealMemoryAddr, RealValueAddr, SizeOfValue);

cdy816's avatar
cdy816 已提交
205 206
            //更新质量戳
            //实时数据内存结构为:实时值+时间戳+质量戳
cdy816's avatar
cdy816 已提交
207 208 209 210

            HisAddr[HisQulityStartAddr + Count] = RealMemoryAddr[RealValueAddr + SizeOfValue + 8];

           // Buffer.BlockCopy(RealMemoryAddr, RealValueAddr + SizeOfValue + 8, HisAddr, HisQulityStartAddr + Count, 1);
cdy816's avatar
cdy816 已提交
211 212 213 214 215 216 217 218 219 220 221

            Count = ++Count > MaxCount ? MaxCount : Count;
        }

        #endregion ...Methods...

        #region ... Interfaces ...

        #endregion ...Interfaces...
    }
}