diff --git a/Common/Cdy.Tag.Common/Common/FixedMemoryBlock.cs b/Common/Cdy.Tag.Common/Common/FixedMemoryBlock.cs index 33754696085bb7f5e7261f4256565390d80af3a5..3c44d3009008d74c086a5b4258567a8652e50f94 100644 --- a/Common/Cdy.Tag.Common/Common/FixedMemoryBlock.cs +++ b/Common/Cdy.Tag.Common/Common/FixedMemoryBlock.cs @@ -1478,7 +1478,7 @@ namespace Cdy.Tag /// public static void MakeMemoryBusy(this FixedMemoryBlock memory) { - LoggerService.Service.Info("FixedMemoryBlock", memory.Name + " is busy....."); + //LoggerService.Service.Info("FixedMemoryBlock", memory.Name + " is busy....."); memory.IncRef(); //memory.StartMemory[0] = 1; } @@ -1489,7 +1489,7 @@ namespace Cdy.Tag /// public static void MakeMemoryNoBusy(this FixedMemoryBlock memory) { - LoggerService.Service.Info("FixedMemoryBlock", memory.Name+ " is ready !"); + //LoggerService.Service.Info("FixedMemoryBlock", memory.Name+ " is ready !"); memory.DecRef(); //memory.StartMemory[0] = 0; } diff --git a/Common/Cdy.Tag.Common/Common/MarshalFixedMemoryBlock.cs b/Common/Cdy.Tag.Common/Common/MarshalFixedMemoryBlock.cs index f096e6d856d8e76d0c7a6e8691145a53b5b9317f..d361a05dd32589ee80c780e0f6457b4d9886fb6a 100644 --- a/Common/Cdy.Tag.Common/Common/MarshalFixedMemoryBlock.cs +++ b/Common/Cdy.Tag.Common/Common/MarshalFixedMemoryBlock.cs @@ -1512,7 +1512,7 @@ namespace Cdy.Tag public static void MakeMemoryBusy(this MarshalFixedMemoryBlock memory) { memory.IncRef(); - LoggerService.Service.Info("MemoryBlock","make "+ memory.Name + " is busy....."); + //LoggerService.Service.Info("MemoryBlock","make "+ memory.Name + " is busy....."); //memory.IsBusy = true; //memory.StartMemory[0] = 1; @@ -1525,7 +1525,7 @@ namespace Cdy.Tag public static void MakeMemoryNoBusy(this MarshalFixedMemoryBlock memory) { memory.DecRef(); - LoggerService.Service.Info("MemoryBlock", "make " + memory.Name+ " is ready !"); + //LoggerService.Service.Info("MemoryBlock", "make " + memory.Name+ " is ready !"); //memory.StartMemory[0] = 0; } diff --git a/Common/Cdy.Tag.Common/Common/MarshalMemoryBlock.cs b/Common/Cdy.Tag.Common/Common/MarshalMemoryBlock.cs index 40616a29ce0a03e94f6534257d4ef0f3ef193fe9..55b7320bd2bafd78a2402baded3cfe03200abf0c 100644 --- a/Common/Cdy.Tag.Common/Common/MarshalMemoryBlock.cs +++ b/Common/Cdy.Tag.Common/Common/MarshalMemoryBlock.cs @@ -382,6 +382,12 @@ namespace Cdy.Tag if (mIsDisposed) break; Marshal.Copy(zoreData, 0, vv + i * zoreData.Length, zoreData.Length); } + + if(BufferItemSize % zoreData.Length >0) + { + Marshal.Copy(zoreData, 0, vv + (BufferItemSize > zoreData.Length ? BufferItemSize - zoreData.Length : 0), BufferItemSize > zoreData.Length ? zoreData.Length : BufferItemSize); + } + } } catch @@ -2490,7 +2496,7 @@ namespace Cdy.Tag public static void MakeMemoryBusy(this MarshalMemoryBlock memory) { memory.IncRef(); - LoggerService.Service.Info("MemoryBlock","make "+ memory.Name + " is busy....."); + //LoggerService.Service.Info("MemoryBlock","make "+ memory.Name + " is busy....."); //memory.IsBusy = true; //memory.StartMemory[0] = 1; @@ -2503,7 +2509,7 @@ namespace Cdy.Tag public static void MakeMemoryNoBusy(this MarshalMemoryBlock memory) { memory.DecRef(); - LoggerService.Service.Info("MemoryBlock", "make " + memory.Name+ " is ready !"); + //LoggerService.Service.Info("MemoryBlock", "make " + memory.Name+ " is ready !"); //memory.StartMemory[0] = 0; } diff --git a/Common/Cdy.Tag.Common/Common/MemoryBlock.cs b/Common/Cdy.Tag.Common/Common/MemoryBlock.cs index 09879b5314ec42beb99d323a1cef8f569957cde5..e3668c11d6721e5f6a7bb13372bd0641e9837fb4 100644 --- a/Common/Cdy.Tag.Common/Common/MemoryBlock.cs +++ b/Common/Cdy.Tag.Common/Common/MemoryBlock.cs @@ -1797,7 +1797,7 @@ namespace Cdy.Tag /// public static void MakeMemoryBusy(this MemoryBlock memory) { - LoggerService.Service.Info("MemoryBlock", memory.Name + " is busy....."); + //LoggerService.Service.Info("MemoryBlock", memory.Name + " is busy....."); memory.IsBusy = true; //memory.StartMemory[0] = 1; } @@ -1808,7 +1808,7 @@ namespace Cdy.Tag /// public static void MakeMemoryNoBusy(this MemoryBlock memory) { - LoggerService.Service.Info("MemoryBlock", memory.Name+ " is ready !"); + //LoggerService.Service.Info("MemoryBlock", memory.Name+ " is ready !"); memory.IsBusy = false; //memory.StartMemory[0] = 0; } diff --git a/Common/Cdy.Tag.Driver/IRealTagProducter.cs b/Common/Cdy.Tag.Driver/IRealTagProducter.cs index dbd5455ffda82c84ce2336c13b183fc6e80c9557..89f6eb7d8a4fdfd33e8fc75f262e44b4d3a8ebcd 100644 --- a/Common/Cdy.Tag.Driver/IRealTagProducter.cs +++ b/Common/Cdy.Tag.Driver/IRealTagProducter.cs @@ -99,7 +99,13 @@ namespace Cdy.Tag.Driver /// bool SetTagValue(Tagbase tag, object value, DateTime time, byte quality); - + /// + /// + /// + /// + /// + /// + /// bool SetTagValue(Tagbase tag, object value, byte quality); /// diff --git a/Common/Cdy.Tag.Driver/ITagHisvalueProducter.cs b/Common/Cdy.Tag.Driver/ITagHisvalueProducter.cs index ff3b07fba43220bf2ae701a8a53afaf6ac5039d5..e7f1730d7ad23d273a3a9b634ceb3d2e6177ae73 100644 --- a/Common/Cdy.Tag.Driver/ITagHisvalueProducter.cs +++ b/Common/Cdy.Tag.Driver/ITagHisvalueProducter.cs @@ -36,6 +36,19 @@ namespace Cdy.Tag.Driver #region ... Methods ... + /// + /// 获取手工记录的变量的Id + /// + /// + List GetManualRecordTagId(); + + /// + /// 获取变量的记录类型 + /// + /// + /// + Dictionary GetTagRecordType(List id); + /// /// 设置变量的历史值 /// @@ -55,6 +68,26 @@ namespace Cdy.Tag.Driver /// bool SetTagHisValue(int id, TagValue value, int timeUnit); + /// + /// + /// + /// + /// + /// + /// + /// + /// + bool SetTagHisValue(int id, DateTime time, object value, byte quality, int timeUnit); + + /// + /// + /// + /// + /// + /// + /// + bool SetTagHisValue(int id, object value, int timeUnit); + /// /// 设置变量的历史值 /// @@ -64,6 +97,12 @@ namespace Cdy.Tag.Driver bool SetTagHisValues(Dictionary> values, int timeUnit); + /// + /// + /// + /// + /// + /// bool SetTagHisValues(Dictionary values, int timeUnit); /// diff --git a/Driver/SimDriver/Driver.cs b/Driver/SimDriver/Driver.cs index 402a1b7ab7cc5b217fb328eb74563d1073aba54a..f9087154a6cbe2ae2f4626fce914aff6ea18a277 100644 --- a/Driver/SimDriver/Driver.cs +++ b/Driver/SimDriver/Driver.cs @@ -5,6 +5,7 @@ using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.IO; +using System.Linq; using System.Threading; namespace SimDriver @@ -14,7 +15,9 @@ namespace SimDriver #region ... Variables ... - System.Collections.Generic.Dictionary> mTagIdCach = new Dictionary>(); + Dictionary> mTagIdCach = new Dictionary>(); + + Dictionary> mManualRecordTagCach = new Dictionary>(); //private System.Timers.Timer mScanTimer; @@ -102,6 +105,14 @@ namespace SimDriver private void InitTagCach(IRealTagProduct tagQuery) { mTagIdCach = tagQuery.GetTagsByLinkAddress(new List() { "Sim:cos", "Sim:sin", "Sim:step", "Sim:steppoint", "Sim:square" }); + + mTagHisValueService = ServiceLocator.Locator.Resolve(); + + foreach (var vv in mTagIdCach) + { + mManualRecordTagCach.Add(vv.Key, mTagHisValueService.GetTagRecordType(vv.Value.Select(e => e.Id).ToList()).Where(e=>e.Value == RecordType.Manual).Select(e=>e.Key).ToList()); + } + } /// @@ -184,6 +195,41 @@ namespace SimDriver }); mTagService.SubmiteNotifyChanged(); + long llsw = sw.ElapsedMilliseconds; + + System.Threading.Tasks.Parallel.ForEach(mManualRecordTagCach, (vv) => { + if (vv.Key == "Sim:cos") + { + TagValue tv = new TagValue() { Quality = 0, Time = DateTime.Now, Value = fval }; + mTagHisValueService.SetTagHisValues(vv.Value.ToDictionary(e => e, e => tv), 1000); + } + else if (vv.Key == "Sim:sin") + { + TagValue tv = new TagValue() { Quality = 0, Time = DateTime.Now, Value = sval }; + mTagHisValueService.SetTagHisValues(vv.Value.ToDictionary(e => e, e => tv), 1000); + //mTagService.SetTagValue(vv.Value, sval); + } + else if (vv.Key == "Sim:step") + { + TagValue tv = new TagValue() { Quality = 0, Time = DateTime.Now, Value = mNumber }; + mTagHisValueService.SetTagHisValues(vv.Value.ToDictionary(e => e, e => tv), 1000); + //mTagService.SetTagValue(vv.Value, mNumber); + } + else if (vv.Key == "Sim:steppoint") + { + TagValue tv = new TagValue() { Quality = 0, Time = DateTime.Now, Value = fval }; + mTagHisValueService.SetTagHisValues(vv.Value.ToDictionary(e => e, e => tv), 1000); + // mTagService.SetPointValue(vv.Value, mNumber, mNumber, mNumber); + } + else if (vv.Key == "Sim:square") + { + TagValue tv = new TagValue() { Quality = 0, Time = DateTime.Now, Value = mBoolNumber }; + mTagHisValueService.SetTagHisValues(vv.Value.ToDictionary(e => e, e => tv), 1000); + //mTagService.SetTagValue(vv.Value, mBoolNumber); + } + }); + + int delay = (int)(500 - (DateTime.Now - mLastProcessTime).TotalMilliseconds); if(delay < 0) { @@ -192,7 +238,7 @@ namespace SimDriver #if DEBUG sw.Stop(); if (mNumber%10 == 0) - LoggerService.Service.Debug("Sim Driver", "set value elapsed:" + sw.ElapsedMilliseconds); + LoggerService.Service.Debug("Sim Driver", "set value elapsed:" + sw.ElapsedMilliseconds+", set his value elapsed:"+(sw.ElapsedMilliseconds-llsw)); #endif Thread.Sleep(delay); } diff --git a/RunTime/DBInRun/Properties/launchSettings.json b/RunTime/DBInRun/Properties/launchSettings.json index 21cb4218ac8cdf7641c4cf095a7db6c95139cbf5..d74015f88dab164d7be203163f5a025e72959e8e 100644 --- a/RunTime/DBInRun/Properties/launchSettings.json +++ b/RunTime/DBInRun/Properties/launchSettings.json @@ -2,7 +2,7 @@ "profiles": { "DBInRun": { "commandName": "Executable", - "executablePath": "C:\\Users\\cdy81\\source\\repos\\mars\\Output\\DBInRun.exe" + "executablePath": "C:\\Users\\chongdaoyang\\source\\repos\\mars\\Output\\DBInRun.exe" } } } \ No newline at end of file diff --git a/RunTime/DBRuntime/His/Compress/CompressMemory2.cs b/RunTime/DBRuntime/His/Compress/CompressMemory2.cs index 20db9ff49fa9487bf25fc6fbc97d9ac5f79f1d18..ae83fd02d595afd046e841b97ffe0d1723a2cea6 100644 --- a/RunTime/DBRuntime/His/Compress/CompressMemory2.cs +++ b/RunTime/DBRuntime/His/Compress/CompressMemory2.cs @@ -168,7 +168,7 @@ namespace Cdy.Tag vpp = mMemoryCach.Dequeue(); } Compress(vpp); - Thread.Sleep(1); + //Thread.Sleep(1); } mIsRunning = false; } @@ -389,7 +389,7 @@ namespace Cdy.Tag int datasize = 0; var targetPosition = 28; - + block.WriteInt(0,data.Id); block.WriteDatetime(4, data.Time); //时间 block.WriteDatetime(12, data.EndTime); //结束时间 //block.WriteInt(20, 0); //写入数据大小 diff --git a/RunTime/DBRuntime/His/Compress/Special/LosslessCompressUnit2.cs b/RunTime/DBRuntime/His/Compress/Special/LosslessCompressUnit2.cs index 90b678d467caf23274ccd95c9011edd4e0f432f1..f0d32c2ec8bd63b1c1b6a6e024ab9b600b6e2ce2 100644 --- a/RunTime/DBRuntime/His/Compress/Special/LosslessCompressUnit2.cs +++ b/RunTime/DBRuntime/His/Compress/Special/LosslessCompressUnit2.cs @@ -68,7 +68,7 @@ namespace Cdy.Tag { target.WriteDatetime(targetAddr, this.StartTime); - LoggerService.Service.Debug("LosslessCompressUnit2", "Record time: "+this.StartTime.ToString("yyyy-MM-dd HH:mm:ss.fff")); + //LoggerService.Service.Debug("LosslessCompressUnit2", "Record time: "+this.StartTime.ToString("yyyy-MM-dd HH:mm:ss.fff")); target.Write(TimeTick); switch (TagType) diff --git a/RunTime/DBRuntime/His/HisEnginer2.cs b/RunTime/DBRuntime/His/HisEnginer2.cs index 564bc1dc4116d42fa71f91e65f27cffae4e3acd8..dc9a032919c7140ee6020e3cd81bfb574780626c 100644 --- a/RunTime/DBRuntime/His/HisEnginer2.cs +++ b/RunTime/DBRuntime/His/HisEnginer2.cs @@ -1385,14 +1385,17 @@ namespace Cdy.Tag if (mHisTags.ContainsKey(id) && mHisTags[id].Type == RecordType.Manual) { - if (mManualHisDataCach.ContainsKey(id)) + lock (mManualHisDataCach) { - datacach = mManualHisDataCach[id]; - } - else - { - datacach = new SortedDictionary(); - mManualHisDataCach.Add(id, datacach); + if (mManualHisDataCach.ContainsKey(id)) + { + datacach = mManualHisDataCach[id]; + } + else + { + datacach = new SortedDictionary(); + mManualHisDataCach.Add(id, datacach); + } } var tag = mHisTags[id]; @@ -1558,6 +1561,187 @@ namespace Cdy.Tag /// /// private bool ManualRecordHisValues(long id, Cdy.Tag.TagValue value, int timeUnit = 100) + { + return ManualRecordHisValues(id, value.Time, value.Value, value.Quality, timeUnit); + //if (mIsClosed) return false; + + //int valueOffset, qulityOffset = 0; + + //DateTime mLastTime = DateTime.MinValue; + + //SortedDictionary datacach; + + //if (mHisTags.ContainsKey(id) && mHisTags[id].Type == RecordType.Manual) + //{ + // if (mManualHisDataCach.ContainsKey(id)) + // { + // datacach = mManualHisDataCach[id]; + // } + // else + // { + // datacach = new SortedDictionary(); + // mManualHisDataCach.Add(id, datacach); + // } + + // var tag = mHisTags[id]; + // ManualHisDataMemoryBlock hb = null; + + // var vdata = value.Time.Date; + // var mms = (int)(value.Time.Subtract(vdata).TotalSeconds / MergeMemoryTime); + // var time = vdata.AddSeconds(mms * MergeMemoryTime); + // if (datacach.ContainsKey(time)) + // { + // hb = datacach[time]; + // } + // else + // { + // var css = CalCachDatablockSize(tag.TagType, 0, MergeMemoryTime * 1000 / timeUnit, out valueOffset, out qulityOffset); + // hb = ManualHisDataMemoryBlockPool.Pool.Get(css); + // hb.Time = time; + // hb.MaxCount = MergeMemoryTime * 1000 / timeUnit; + // hb.TimeUnit = timeUnit; + // hb.TimeLen = 4; + // hb.TimerAddress = 0; + // hb.ValueAddress = valueOffset; + // hb.QualityAddress = qulityOffset; + // hb.Id = (int)id; + // hb.CurrentCount = 0; + + // datacach.Add(time, hb); + // } + // mLastTime = time; + + // if (hb.CurrentCount < hb.MaxCount && value.Time > hb.EndTime) + // { + // hb.Lock(); + // var vtime = (int)((value.Time - hb.Time).TotalMilliseconds / timeUnit); + // //写入时间戳 + // hb.WriteInt(hb.TimerAddress + hb.CurrentCount * 4, vtime); + // switch (tag.TagType) + // { + // case TagType.Bool: + // hb.WriteByteDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, Convert.ToByte(Convert.ToBoolean(value.Value))); + // break; + // case TagType.Byte: + // hb.WriteByteDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, Convert.ToByte(value.Value)); + // break; + // case TagType.Short: + // hb.WriteShortDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, Convert.ToInt16(value.Value)); + // break; + // case TagType.UShort: + // hb.WriteUShortDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, Convert.ToUInt16(value.Value)); + // break; + // case TagType.Int: + // hb.WriteIntDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, Convert.ToInt32(value.Value)); + // break; + // case TagType.UInt: + // hb.WriteUIntDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, Convert.ToUInt32(value.Value)); + // break; + // case TagType.Long: + // hb.WriteLongDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, Convert.ToInt64(value.Value)); + // break; + // case TagType.ULong: + // hb.WriteULongDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, Convert.ToUInt64(value.Value)); + // break; + // case TagType.Float: + // hb.WriteFloatDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, Convert.ToSingle(value.Value)); + // break; + // case TagType.Double: + // hb.WriteDoubleDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, Convert.ToDouble(value.Value)); + // break; + // case TagType.String: + // hb.WriteStringDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, Convert.ToString(value.Value), Encoding.Unicode); + // break; + // case TagType.DateTime: + // hb.WriteDatetime(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, Convert.ToDateTime(value.Value)); + // break; + // case TagType.UIntPoint: + // UIntPointData data = (UIntPointData)value.Value; + // hb.WriteUIntDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, data.X); + // hb.WriteUIntDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue + 4, data.Y); + // break; + // case TagType.IntPoint: + // IntPointData idata = (IntPointData)value.Value; + // hb.WriteIntDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, idata.X); + // hb.WriteIntDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue + 4, idata.Y); + // break; + // case TagType.UIntPoint3: + // UIntPoint3Data udata3 = (UIntPoint3Data)value.Value; + // hb.WriteUIntDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, udata3.X); + // hb.WriteUIntDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue + 4, udata3.Y); + // hb.WriteUIntDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue + 8, udata3.Z); + // break; + // case TagType.IntPoint3: + // IntPoint3Data idata3 = (IntPoint3Data)value.Value; + // hb.WriteIntDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, idata3.X); + // hb.WriteIntDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue + 4, idata3.Y); + // hb.WriteIntDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue + 8, idata3.Z); + // break; + + // case TagType.ULongPoint: + // ULongPointData udata = (ULongPointData)value.Value; + // hb.WriteULongDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, udata.X); + // hb.WriteULongDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue + 8, udata.Y); + // break; + // case TagType.LongPoint: + // LongPointData lidata = (LongPointData)value.Value; + // hb.WriteLongDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, lidata.X); + // hb.WriteLongDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue + 8, lidata.Y); + // break; + // case TagType.ULongPoint3: + // ULongPoint3Data ludata3 = (ULongPoint3Data)value.Value; + // hb.WriteULongDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, ludata3.X); + // hb.WriteULongDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue + 16, ludata3.Y); + // hb.WriteULongDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue + 24, ludata3.Z); + // break; + // case TagType.LongPoint3: + // LongPoint3Data lidata3 = (LongPoint3Data)value.Value; + // hb.WriteLongDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, lidata3.X); + // hb.WriteLongDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue + 16, lidata3.Y); + // hb.WriteLongDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue + 24, lidata3.Z); + // break; + // } + // hb.WriteInt(hb.QualityAddress + hb.CurrentCount, value.Quality); + // hb.EndTime = value.Time; + // hb.CurrentCount++; + // hb.Relase(); + + // HisDataMemoryQueryService.Service.RegistorManual(id, hb.Time, hb.EndTime, hb); + + // } + + // bool isNeedSubmite = false; + + // foreach (var vv in datacach.ToArray()) + // { + // if (vv.Key < mLastTime || vv.Value.CurrentCount >= vv.Value.MaxCount) + // { + // ServiceLocator.Locator.Resolve().RequestManualToCompress(vv.Value); + // datacach.Remove(vv.Key); + // isNeedSubmite = true; + // } + // } + // if (isNeedSubmite) + // ServiceLocator.Locator.Resolve().SubmitManualToCompress(); + + // return true; + //} + //else + //{ + // return false; + //} + } + + /// + /// + /// + /// + /// + /// + /// + /// + /// + private bool ManualRecordHisValues(long id,DateTime datetime ,object value,byte quality=0, int timeUnit = 100) { if (mIsClosed) return false; @@ -1569,21 +1753,24 @@ namespace Cdy.Tag if (mHisTags.ContainsKey(id) && mHisTags[id].Type == RecordType.Manual) { - if (mManualHisDataCach.ContainsKey(id)) - { - datacach = mManualHisDataCach[id]; - } - else + lock (mManualHisDataCach) { - datacach = new SortedDictionary(); - mManualHisDataCach.Add(id, datacach); + if (mManualHisDataCach.ContainsKey(id)) + { + datacach = mManualHisDataCach[id]; + } + else + { + datacach = new SortedDictionary(); + mManualHisDataCach.Add(id, datacach); + } } var tag = mHisTags[id]; ManualHisDataMemoryBlock hb = null; - var vdata = value.Time.Date; - var mms = (int)(value.Time.Subtract(vdata).TotalSeconds / MergeMemoryTime); + var vdata = datetime.Date; + var mms = (int)(datetime.Subtract(vdata).TotalSeconds / MergeMemoryTime); var time = vdata.AddSeconds(mms * MergeMemoryTime); if (datacach.ContainsKey(time)) { @@ -1607,98 +1794,98 @@ namespace Cdy.Tag } mLastTime = time; - if (hb.CurrentCount < hb.MaxCount && value.Time > hb.EndTime) + if (hb.CurrentCount < hb.MaxCount && datetime > hb.EndTime) { hb.Lock(); - var vtime = (int)((value.Time - hb.Time).TotalMilliseconds / timeUnit); + var vtime = (int)((datetime - hb.Time).TotalMilliseconds / timeUnit); //写入时间戳 hb.WriteInt(hb.TimerAddress + hb.CurrentCount * 4, vtime); switch (tag.TagType) { case TagType.Bool: - hb.WriteByteDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, Convert.ToByte(Convert.ToBoolean(value.Value))); + hb.WriteByteDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, Convert.ToByte(Convert.ToBoolean(value))); break; case TagType.Byte: - hb.WriteByteDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, Convert.ToByte(value.Value)); + hb.WriteByteDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, Convert.ToByte(value)); break; case TagType.Short: - hb.WriteShortDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, Convert.ToInt16(value.Value)); + hb.WriteShortDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, Convert.ToInt16(value)); break; case TagType.UShort: - hb.WriteUShortDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, Convert.ToUInt16(value.Value)); + hb.WriteUShortDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, Convert.ToUInt16(value)); break; case TagType.Int: - hb.WriteIntDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, Convert.ToInt32(value.Value)); + hb.WriteIntDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, Convert.ToInt32(value)); break; case TagType.UInt: - hb.WriteUIntDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, Convert.ToUInt32(value.Value)); + hb.WriteUIntDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, Convert.ToUInt32(value)); break; case TagType.Long: - hb.WriteLongDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, Convert.ToInt64(value.Value)); + hb.WriteLongDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, Convert.ToInt64(value)); break; case TagType.ULong: - hb.WriteULongDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, Convert.ToUInt64(value.Value)); + hb.WriteULongDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, Convert.ToUInt64(value)); break; case TagType.Float: - hb.WriteFloatDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, Convert.ToSingle(value.Value)); + hb.WriteFloatDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, Convert.ToSingle(value)); break; case TagType.Double: - hb.WriteDoubleDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, Convert.ToDouble(value.Value)); + hb.WriteDoubleDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, Convert.ToDouble(value)); break; case TagType.String: - hb.WriteStringDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, Convert.ToString(value.Value), Encoding.Unicode); + hb.WriteStringDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, Convert.ToString(value), Encoding.Unicode); break; case TagType.DateTime: - hb.WriteDatetime(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, Convert.ToDateTime(value.Value)); + hb.WriteDatetime(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, Convert.ToDateTime(value)); break; case TagType.UIntPoint: - UIntPointData data = (UIntPointData)value.Value; + UIntPointData data = (UIntPointData)value; hb.WriteUIntDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, data.X); hb.WriteUIntDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue + 4, data.Y); break; case TagType.IntPoint: - IntPointData idata = (IntPointData)value.Value; + IntPointData idata = (IntPointData)value; hb.WriteIntDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, idata.X); hb.WriteIntDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue + 4, idata.Y); break; case TagType.UIntPoint3: - UIntPoint3Data udata3 = (UIntPoint3Data)value.Value; + UIntPoint3Data udata3 = (UIntPoint3Data)value; hb.WriteUIntDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, udata3.X); hb.WriteUIntDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue + 4, udata3.Y); hb.WriteUIntDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue + 8, udata3.Z); break; case TagType.IntPoint3: - IntPoint3Data idata3 = (IntPoint3Data)value.Value; + IntPoint3Data idata3 = (IntPoint3Data)value; hb.WriteIntDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, idata3.X); hb.WriteIntDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue + 4, idata3.Y); hb.WriteIntDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue + 8, idata3.Z); break; case TagType.ULongPoint: - ULongPointData udata = (ULongPointData)value.Value; + ULongPointData udata = (ULongPointData)value; hb.WriteULongDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, udata.X); hb.WriteULongDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue + 8, udata.Y); break; case TagType.LongPoint: - LongPointData lidata = (LongPointData)value.Value; + LongPointData lidata = (LongPointData)value; hb.WriteLongDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, lidata.X); hb.WriteLongDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue + 8, lidata.Y); break; case TagType.ULongPoint3: - ULongPoint3Data ludata3 = (ULongPoint3Data)value.Value; + ULongPoint3Data ludata3 = (ULongPoint3Data)value; hb.WriteULongDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, ludata3.X); hb.WriteULongDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue + 16, ludata3.Y); hb.WriteULongDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue + 24, ludata3.Z); break; case TagType.LongPoint3: - LongPoint3Data lidata3 = (LongPoint3Data)value.Value; + LongPoint3Data lidata3 = (LongPoint3Data)value; hb.WriteLongDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, lidata3.X); hb.WriteLongDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue + 16, lidata3.Y); hb.WriteLongDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue + 24, lidata3.Z); break; } - hb.WriteInt(hb.QualityAddress + hb.CurrentCount, value.Quality); - hb.EndTime = value.Time; + hb.WriteInt(hb.QualityAddress + hb.CurrentCount, quality); + hb.EndTime = datetime; hb.CurrentCount++; hb.Relase(); @@ -1756,6 +1943,31 @@ namespace Cdy.Tag return ManualRecordHisValues(id, value, timeUnit); } + /// + /// + /// + /// + /// + /// + /// + /// + /// + public bool SetTagHisValue(int id, DateTime time, object value, byte quality, int timeUnit=100) + { + return ManualRecordHisValues(id, time, value, quality, timeUnit); + } + + /// + /// + /// + /// + /// + /// + /// + public bool SetTagHisValue(int id, object value, int timeUnit) + { + return SetTagHisValue(id, DateTime.Now, value, 0, timeUnit); + } /// /// /// @@ -1796,6 +2008,33 @@ namespace Cdy.Tag return true; } + /// + /// + /// + /// + public List GetManualRecordTagId() + { + return mManager.HisTags.Where(e => e.Value.Type == RecordType.Manual).Select(e=>e.Value.Id).ToList(); + } + + /// + /// + /// + /// + /// + public Dictionary GetTagRecordType(List id) + { + Dictionary re = new Dictionary(); + foreach(var vv in id) + { + if(mManager.HisTags.ContainsKey(vv)) + { + re.Add(vv,mManager.HisTags[vv].Type); + } + } + return re; + } + #endregion ...Methods... #region ... Interfaces ... diff --git a/RunTime/DBRuntime/His/ManualHisDataMemoryBlockPool.cs b/RunTime/DBRuntime/His/ManualHisDataMemoryBlockPool.cs index af3565289f9510baa5534286cb621685c8a70956..603c3cd3e26bbed7e620c042050319d2d7a745de 100644 --- a/RunTime/DBRuntime/His/ManualHisDataMemoryBlockPool.cs +++ b/RunTime/DBRuntime/His/ManualHisDataMemoryBlockPool.cs @@ -26,7 +26,7 @@ namespace DBRuntime.His /// /// /// - private Dictionary> mPools = new Dictionary>(); + private Dictionary> mFreePools = new Dictionary>(); #endregion ...Variables... @@ -44,6 +44,30 @@ namespace DBRuntime.His #region ... Methods ... + /// + /// + /// + /// + public void PreAlloc(int size) + { + lock (mFreePools) + { + if (mFreePools.ContainsKey(size)) + { + var pp = mFreePools[size]; + var bnb = NewBlock(size); + pp.Enqueue(bnb); + } + else + { + var bnb = NewBlock(size); + Queue dd = new Queue(); + dd.Enqueue(bnb); + mFreePools.Add(size,dd); + } + } + } + /// /// /// @@ -51,31 +75,27 @@ namespace DBRuntime.His /// public ManualHisDataMemoryBlock Get(int size) { - lock (mPools) + lock (mFreePools) { - if (mPools.ContainsKey(size)) + if (mFreePools.ContainsKey(size)) { - var pp = mPools[size].Where(e => !e.Value); - if (pp.Count() > 0) + var pp = mFreePools[size]; + if (pp.Count > 0) { - var bnb = pp.First().Key; - mPools[size][bnb] = true; + var bnb = pp.Dequeue(); return bnb; } else { var bnb = NewBlock(size); - mPools[size].Add(bnb, true); - return bnb; } } else { var bnb = NewBlock(size); - Dictionary dd = new Dictionary(); - dd.Add(bnb, true); - mPools.Add(size, dd); + Queue dd = new Queue(); + mFreePools.Add(size, dd); return bnb; } } @@ -98,13 +118,13 @@ namespace DBRuntime.His public void Release(ManualHisDataMemoryBlock block) { - var size = (int)block.AllocSize; - if(mPools.ContainsKey(size)) + lock (mFreePools) { - var vv = mPools[size]; - if(vv.ContainsKey(block)) + var size = (int)block.AllocSize; + if (mFreePools.ContainsKey(size)) { - vv[block] = false; + var vv = mFreePools[size]; + vv.Enqueue(block); block.Clear(); } } diff --git a/RunTime/DBRuntime/His/MarshalMemoryBlockPool.cs b/RunTime/DBRuntime/His/MarshalMemoryBlockPool.cs index f409e37860a6949d911dd2ce9d3ac9b8aea7ecd6..87854b2092ec15ee87a2bbac97e87b13dd442f15 100644 --- a/RunTime/DBRuntime/His/MarshalMemoryBlockPool.cs +++ b/RunTime/DBRuntime/His/MarshalMemoryBlockPool.cs @@ -27,7 +27,7 @@ namespace DBRuntime.His /// /// /// - private Dictionary mPools = new Dictionary(); + private Dictionary> mFreePools = new Dictionary>(); #endregion ...Variables... @@ -52,19 +52,27 @@ namespace DBRuntime.His /// public MarshalMemoryBlock Get(long size) { - lock (mPools) + lock (mFreePools) { - var mms = mPools.Where(e => !e.Value && e.Key.AllocSize == size); - if (mms.Count() > 0) + if (mFreePools.ContainsKey(size)) { - var vv = mms.First().Key; - mPools[vv] = true; - return vv; + var pp = mFreePools[size]; + if (pp.Count > 0) + { + var bnb = pp.Dequeue(); + return bnb; + } + else + { + var bnb = NewBlock(size); + return bnb; + } } else { var bnb = NewBlock(size); - mPools.Add(bnb, true); + Queue dd = new Queue(); + mFreePools.Add(size, dd); return bnb; } } @@ -77,7 +85,7 @@ namespace DBRuntime.His /// private MarshalMemoryBlock NewBlock(long size) { - return new MarshalMemoryBlock(size).Clear(); + return new MarshalMemoryBlock(size,(int)size).Clear(); } /// @@ -87,11 +95,16 @@ namespace DBRuntime.His public void Release(MarshalMemoryBlock block) { - if(mPools.ContainsKey(block)) + lock (mFreePools) { - mPools[block] = false; + var size = (int)block.AllocSize; + if (mFreePools.ContainsKey(size)) + { + var vv = mFreePools[size]; + vv.Enqueue(block); + block.Clear(); + } } - block.Clear(); } #endregion ...Methods... diff --git a/RunTime/DBRuntime/His/SeriseEnginer2.cs b/RunTime/DBRuntime/His/SeriseEnginer2.cs index e207b81a9b9e01e0e2bfb64f3300f7810718dc8f..9cbb351f9695311a5da5a91f71b5377aabae8a27 100644 --- a/RunTime/DBRuntime/His/SeriseEnginer2.cs +++ b/RunTime/DBRuntime/His/SeriseEnginer2.cs @@ -17,6 +17,7 @@ using System.Buffers; using DotNetty.Common; using DBRuntime.His; using System.Collections; +using System.Drawing; /* * ****文件结构**** @@ -268,6 +269,12 @@ namespace Cdy.Tag lock (resetEvent) resetEvent.Reset(); + //#if DEBUG + Stopwatch sw = new Stopwatch(); + sw.Start(); + LoggerService.Service.Info("SeriseEnginer", "********开始执行存储********", ConsoleColor.Cyan); + //#endif + if (mWaitForProcessMemory.Count > 0) { SaveToFile(); @@ -284,6 +291,11 @@ namespace Cdy.Tag } } + //#if DEBUG + sw.Stop(); + LoggerService.Service.Info("SeriseEnginer", ">>>>>>>>>完成执行存储>>>>>>> ElapsedMilliseconds:" + sw.ElapsedMilliseconds, ConsoleColor.Cyan); + //#endif + if (mIsClosed) break; } @@ -300,11 +312,7 @@ namespace Cdy.Tag 2. 拷贝数据块 3. 更新数据块指针 */ - //#if DEBUG - Stopwatch sw = new Stopwatch(); - sw.Start(); - LoggerService.Service.Info("SeriseEnginer", "********开始执行存储********", ConsoleColor.Cyan); - //#endif + HisDataPath = SelectHisDataPath(); List mtmp; lock (mWaitForProcessMemory) @@ -320,10 +328,7 @@ namespace Cdy.Tag vv.MakeMemoryNoBusy(); } - //#if DEBUG - sw.Stop(); - LoggerService.Service.Info("SeriseEnginer", ">>>>>>>>>完成执行存储>>>>>>> Count:" + mtmp.Count + " ElapsedMilliseconds:" + sw.ElapsedMilliseconds, ConsoleColor.Cyan); - //#endif + } /// @@ -351,7 +356,7 @@ namespace Cdy.Tag foreach (var vv in mSeriserFiles) { - if (id >= vv.Value.Id * TagCountOneFile && id < (vv.Value.Id + 1) * TagCountOneFile) + if (id >= vv.Value.IdStart && id < vv.Value.IdEnd) { vv.Value.ManualRequestToSeriseFile(id, data); } @@ -370,7 +375,7 @@ namespace Cdy.Tag HisDataPath = SelectHisDataPath(); lock (mCachSeriseMemoryBlock) mCachSeriseMemoryBlock.Enqueue(data); - + if (mCachSeriseMemoryBlock.Count > 10) RequestToSave(); } #endregion ...Methods... @@ -444,9 +449,10 @@ namespace Cdy.Tag private List mTagIdsCach; - private Dictionary> mPointerCach = new Dictionary>(); + private int mId = 0; + #endregion ...Variables... #region ... Events ... @@ -458,10 +464,21 @@ namespace Cdy.Tag #endregion ...Constructor... #region ... Properties ... + /// /// /// - public int Id { get; set; } + public int IdStart { get; set; } + + /// + /// + /// + public int IdEnd { get; set; } + + /// + /// + /// + public int Id { get { return mId; } set { mId = value; IdStart = value * TagCountOneFile;IdEnd = (value + 1) * TagCountOneFile; } } /// /// @@ -506,6 +523,8 @@ namespace Cdy.Tag public bool IsNeedInit { get; set; } + private Dictionary mManualHisDataCach = new Dictionary(); + #endregion ...Properties... #region ... Methods ... @@ -815,6 +834,48 @@ namespace Cdy.Tag return preoffset; } + /// + /// + /// + /// + /// + public void AppendManualSeriseFile(int id,MarshalMemoryBlock datablock) + { + mManualHisDataCach.Add(id, datablock); + } + + /// + /// + /// + public void FreshManualDataToDisk() + { + string oldFile = string.Empty; + DataFileSeriserbase mwriter; + + Dictionary mHeadAddress = new Dictionary(); + Dictionary mHeadValue = new Dictionary(); + + foreach (var vv in mManualHisDataCach) + { + var id = vv.Key; + var datablock = vv.Value; + DateTime time = datablock.ReadDateTime(4); + DateTime endTime = datablock.ReadDateTime(12); + int size = datablock.ReadInt(20); + mTagCount = datablock.ReadInt(24);//变量个数 + string sfile = GetFileName(time); + if(sfile!=oldFile) + { + mHeadAddress = new Dictionary(); + mHeadValue = new Dictionary(); + + var heads = GetDataRegionHeadPoint(id, time, out mwriter); + mHeadAddress.Add(id, heads); + } + + } + } + /// /// 通过手动更新的方式,提交历史记录 ///