数据正常显示

master
mzhifa 1 year ago
parent bf42d19e39
commit 80fe6835e1

@ -25,7 +25,7 @@ namespace StartServerWPF.Modules.MseedChart.Models
//添加Z分量波形
if (item.dz.Count > 0)
{
axisY.SetRange(2000, 5000);
axisY.SetRange(0, 5000);
}
axisY.Title.Text = string.Format("Ch{0}.Z", item.Name);
axisY.Title.DistanceToAxis = distancetoX;
@ -34,7 +34,7 @@ namespace StartServerWPF.Modules.MseedChart.Models
axisY = new AxisY(curChart.ViewXY);
if (item.dn.Count > 0)
{
axisY.SetRange(2000, 5000);
axisY.SetRange(0, 5000);
}
axisY.Title.Text = string.Format("Ch{0}.N", item.Name);
axisY.Title.DistanceToAxis = distancetoX;

@ -1,4 +1,5 @@
using System;
using ServiceStack;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
@ -37,4 +38,21 @@ namespace StartServerWPF.Modules.MseedChart
public char samplesize;
public System.IntPtr datasamples; //!< Data samples, \a numsamples of type \a sampletype
}
public class ASCiiData
{
public int Index { set; get; }
public string sid { set; get; }
public double samprate { set; get; }
public Int64 numsamples { set; get; }
public Dictionary<double, double[]> datas { get; }=new Dictionary<double, double[]>();
public void AddData(double oaTime, double[] da)
{
if(datas.Count>=100)
{
datas.RemoveKey(0);
}
datas.Add(oaTime, da);
}
}
}

@ -247,6 +247,7 @@ namespace ZhaoXi.Advanced.MyRedis.Service
public void Subscribe(string channel, Action<string, byte[], IRedisSubscription> actionOnMessage)
{
var subscription = base.iClient.CreateSubscription();
subscription.OnSubscribe = c =>
{
Console.WriteLine($"订阅频道{c}");
@ -266,7 +267,6 @@ namespace ZhaoXi.Advanced.MyRedis.Service
Console.WriteLine($"开始启动监听 {channel}");
subscription.SubscribeToChannels(channel); //blocking
}
public void UnSubscribeFromChannels(string channel)
{
var subscription = base.iClient.CreateSubscription();
@ -274,7 +274,7 @@ namespace ZhaoXi.Advanced.MyRedis.Service
//subscription.UnSubscribeFromAllChannels();
}
public static bool ByteToFile(byte[] byteArray, string fileName)
public bool ByteToFile(byte[] byteArray, string fileName)
{
bool result = false;
try

@ -3,25 +3,20 @@ using Arction.Wpf.Charting.Axes;
using Arction.Wpf.Charting.SeriesXY;
using Arction.Wpf.Charting.Views.ViewXY;
using Microsoft.Win32;
using StartServerWPF.Modules.MseedChart.Models;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using SharpDX.Direct2D1;
using SharpDX.DirectWrite;
using Prism.Regions;
using ServiceStack;
using StartServerWPF.Assets;
using StartServerWPF.Modules.MseedChart.Models;
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Http.Headers;
using System.Reflection;
using System.Security.Cryptography;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
@ -29,18 +24,9 @@ using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using System.Xml.Linq;
using Prism.Regions;
using static System.Collections.Specialized.BitVector32;
using ZhaoXi.Advanced.MyRedis.Service;
using ServiceStack;
using ServiceStack.Redis;
using Arction.Licensing;
using System.Runtime.InteropServices;
using StartServerWPF.Assets;
using ServiceStack.Text;
namespace StartServerWPF.Modules.MseedChart.ViewModels
{
@ -71,6 +57,9 @@ namespace StartServerWPF.Modules.MseedChart.ViewModels
public ConcurrentQueue<StationModel> smList=new ConcurrentQueue<StationModel>();
public List<AxisY> _chartAxisY=new List<AxisY>();
// Dictionary<string, ASCiiData> cacheChartDic=new Dictionary<string, ASCiiData>();
Dictionary<string, ASCiiData> subNameList;
#endregion
#region 属性
@ -260,26 +249,24 @@ namespace StartServerWPF.Modules.MseedChart.ViewModels
public DelegateCommand IntervalSureCommand => new DelegateCommand(IntervalSure);
public DelegateCommand<object> RealTimeDataCommand => new DelegateCommand<object>(RealTimeData);
Dictionary<string,int> subNameList;
private void Loaded()
{
//N08,N25,N26
string station = "N05,N10";
var stations = GlobalData.StationNames;// station.Split(',');
smList = _wavesModel.ReadWavesFromJson(stations);
subNameList = new Dictionary<string, ASCiiData>();
}
private void UnLoaded()
{
time.Stop();
using (RedisListService service = new RedisListService())
{
if (subNameList != null)
{
if(subNameList == null) return;
foreach (var subName in subNameList)
{
service.UnSubscribeFromChannels(subName.Key);
}
using (RedisListService service = new RedisListService())
{
//停止订阅
service.Publish(subName.Key, "shutoff");
}
}
}
@ -488,10 +475,9 @@ namespace StartServerWPF.Modules.MseedChart.ViewModels
_lChartAll.EndUpdate();
}
}
int i = 1;
private void RealTimeData(object isCheck)
{
_channelCount = smList.Count * 3;
CurrentTime = smList.First().BeginTime.ToShortDateString();
StationsName = smList.Select(a => new StationAxis { Name = a.Name, IsChecked = true, SelectCommand = new DelegateCommand<object>(StationsNameVisible) }).ToList();
@ -515,26 +501,33 @@ namespace StartServerWPF.Modules.MseedChart.ViewModels
time.Elapsed -= CompositionTarget_Rendering;
time.Elapsed += CompositionTarget_Rendering;
LChartALL.ViewXY.XAxes[0].ScrollMode = XAxisScrollMode.Scrolling;
Task.Run(() =>
{
Thread.Sleep(5000);
time.Start();
});
using (RedisListService service = new RedisListService())
{
foreach (var subName in subNameList)
{
service.UnSubscribeFromChannels(subName.Key);
}
service.FlushAll();
}
foreach (var subName in subNameList)
{
Task.Run(() =>
{
Debug.WriteLine($"注册{1}:{subName.Key}");
using (RedisListService service = new RedisListService())
{
Debug.WriteLine($"注册{1}:{subName.Key}");
{
service.Subscribe(subName.Key, (c, message, iRedisSubscription) =>
{
if(message.Length==7 && System.Text.Encoding.Default.GetString(message)== "shutoff")
{
iRedisSubscription.UnSubscribeFromChannels(subName.Key);
}
Mseed2asciiApi.bufferMseedData(message.Length, message);
});
}
}
});
}
}
@ -543,11 +536,11 @@ namespace StartServerWPF.Modules.MseedChart.ViewModels
time.Stop();
time.Elapsed -= CompositionTarget_Rendering;
LChartALL.ViewXY.XAxes[0].ScrollMode = XAxisScrollMode.None;
using (RedisListService service = new RedisListService())
{
foreach (var subName in subNameList)
{
service.UnSubscribeFromChannels(subName.Key);
using (RedisListService service = new RedisListService())
{
service.Publish(subName.Key, "shutoff");
}
}
}
@ -560,8 +553,6 @@ namespace StartServerWPF.Modules.MseedChart.ViewModels
{
DateTime startTime = Convert.ToDateTime(asciiData.endtime).AddHours(8);
DateTime endTime = Convert.ToDateTime(asciiData.endtime).AddHours(8);
Debug.WriteLine($"注册{asciiData.sid}:{startTime}");
var time = endTime - startTime;
byte[] bytes = new byte[asciiData.numsamples * asciiData.samplesize];
// IntPtr bufferHandler = Marshal.AllocHGlobal((int)asciiData.datasize);
Marshal.Copy(asciiData.datasamples, bytes, 0, bytes.Length);
@ -595,8 +586,8 @@ namespace StartServerWPF.Modules.MseedChart.ViewModels
lines.Add(b);
}
}
Debug.WriteLine($"接收:{asciiData.sid}:{startTime},count:{lines.Count}");
string lineStr = String.Join(",", lines);
using (RedisListService service = new RedisListService())
{
service.LPush(name, lineStr);
@ -661,7 +652,6 @@ namespace StartServerWPF.Modules.MseedChart.ViewModels
private void StartChart()
{
DisposeAllAndClear(_chartAxisY);
subNameList = new Dictionary<string, int>();
ViewXY v = LChartALL.ViewXY;
LChartALL.SizeChanged -= LChartALL_SizeChanged;
LChartALL.SizeChanged += LChartALL_SizeChanged;
@ -702,7 +692,11 @@ namespace StartServerWPF.Modules.MseedChart.ViewModels
int count = stationModel.Dzne.Count;
int number= smList.Count*3;
bool isAdd = false;
if (subNameList.Count == 0)
{
isAdd = true;
}
for (int i = 0; i < number; i++)
{
int seriesIndex = i;
@ -755,7 +749,10 @@ namespace StartServerWPF.Modules.MseedChart.ViewModels
subName = $"HA.{name}.06.SHE";
series.LineStyle.Color = System.Windows.Media.Colors.ForestGreen;
}
subNameList.Add(subName,i);
if (isAdd)
{
subNameList.Add(subName, new ASCiiData { Index = i, sid = subName });
}
series.LineStyle.Width = 0.2;
series.ScrollModePointsKeepLevel = 1;
//series.ScrollingStabilizing = true;
@ -867,73 +864,166 @@ namespace StartServerWPF.Modules.MseedChart.ViewModels
{
RenderNextFrame();
}
static double currentOATime = 0;
List<string> tempData = new List<string>();
private void RenderNextFrame()
{
Debug.WriteLine("timeFrame:{0}", DateTime.Now);
Stopwatch stopwatch = Stopwatch.StartNew();
string minName = string.Empty;
if (_lChartAll == null)
{
return;
}
using (RedisListService service = new RedisListService())
List<double> datasStr = new List<double>();
//计算数据中最小时间
if (currentOATime == 0)
{
double minTime = DateTime.Now.AddHours(1).ToOADate();
string name = string.Empty;
foreach (var item in subNameList)
{
List<double> dataDouList = new List<double>();
using (RedisListService service = new RedisListService())
{
var data = service.Get(item.Key, 0, 0).FirstOrDefault();
if (data != null)
{
IEnumerable<double> datasStr = data.Split(',').Select(a => Convert.ToDouble(a));
datasStr = data.Split(',').Select(a => Convert.ToDouble(a)).ToList();
double oaTime = datasStr.First();
if (oaTime < DateTime.Now.AddSeconds(-3).ToOADate())
if (oaTime < minTime)
{
minTime = oaTime;
currentOATime = oaTime;
name=item.Key;
}
}
}
}
if (currentOATime == 0) return;
Debug.WriteLine("currentTimeFirst:{0},now:{1},sid:{2},count:{3}", DateTime.FromOADate(currentOATime),DateTime.Now, name, datasStr.Count);
}
else
{
//更新图表数据时间,每秒刷新一次
currentOATime = DateTime.FromOADate(currentOATime).AddSeconds(1).ToOADate();
}
foreach (var item in subNameList)
{
using (RedisListService service = new RedisListService())
{
var data = service.Get(item.Key, 0, 0).FirstOrDefault();
if (data != null)
{
datasStr = data.Split(',').Select(a => Convert.ToDouble(a)).ToList();
DateTime firstTime = DateTime.FromOADate(datasStr.First());
DateTime currentTime = DateTime.FromOADate(currentOATime);
datasStr.RemoveAt(0);
if (Math.Abs((currentTime - firstTime).TotalSeconds) < 1 && firstTime.Second == currentTime.Second)
{
//取出队列数据
service.RemoveStartFromList(item.Key);
var DouData = data.Split(',').Select(a => Convert.ToDouble(a)).ToList();
DouData.RemoveAt(0);
dataDouList.AddRange(DouData);
_data[item.Value.Index] = datasStr.ToArray();
}
else if ((currentTime - firstTime).TotalSeconds < 0)
{
_data[item.Value.Index] = new double[datasStr.Count];
}
else if ((currentTime - firstTime).TotalSeconds > 0)
{
firstTime.AddMilliseconds(-firstTime.Millisecond);
//出更数据需要重新绘图表点
_data[item.Value.Index] = new double[datasStr.Count];
int offset = (int)(currentTime - firstTime).TotalSeconds;
while (offset > 0)
{
service.RemoveStartFromList(item.Key);
tempData.Add(item.Key+ firstTime);
if (offset < 100)
{
// 不能超过缓存数, 更新数据
var time = currentTime.AddSeconds(-offset);
var tem = item.Value.datas[time.ToOADate()];
item.Value.datas[time.ToOADate()] = datasStr.ToArray();
}
data = service.Get(item.Key, 0, 0).FirstOrDefault();
if (data == null) break;
datasStr = data.Split(',').Select(a => Convert.ToDouble(a)).ToList();
firstTime = DateTime.FromOADate(datasStr.First());
offset = (int)(currentTime - firstTime).TotalSeconds;
datasStr.RemoveAt(0);
Debug.WriteLine("timeMinData:{0},Time1:{1},sid:{2},offset:{3}", currentTime, firstTime, item.Key, offset);
}
}
else
{
break;
throw new Exception($"数据解析异常:{item.Key},{currentTime},{firstTime}");
}
//继续查看数据
// data = service.Get(item.Key, 0, 0).FirstOrDefault();
}
// data =service.BlockingDequeueItemFromList(item.Key, TimeSpan.FromMilliseconds(5));
if (dataDouList.Count!=0)
else
{
_data[item.Value] =dataDouList.ToArray();
_data[item.Value.Index] = new double[500];
}
}
item.Value.AddData(currentOATime, _data[item.Value.Index]);
}
// if (_data.ToList().Any(d => d != null))
stopwatch.Stop();
double time1 = stopwatch.ElapsedMilliseconds;
stopwatch.Start();
bool isRefresh = false;
//有新数据开始刷新
if (tempData.Count > 5)
{
List<double> temDou = new List<double>();
foreach (var item in subNameList)
{
var d = item.Value.datas.Keys.OrderBy(a => a).ToArray();
for (int i = 0; i < d.Count(); i++)
{
temDou.AddRange(item.Value.datas[d[i]]);
}
// _data[item.Value.Index] = temDou.ToArray();
// DateTime time1 = DateTime.Now.AddMinutes(i++);
// _lChartAll.ViewXY.XAxes[0].SetRange(_lChartAll.ViewXY.XAxes[0].DateTimeToAxisValue(time1),
// _lChartAll.ViewXY.XAxes[0].DateTimeToAxisValue(time1.AddSeconds(60)));
}
// isRefresh = true;
tempData.Clear();
}
_dispatcher.Invoke(() =>
{
FeedData(/*chartTitleText*/);
FeedData(isRefresh /*chartTitleText*/);
});
}
stopwatch.Stop();
double time2 = stopwatch.ElapsedMilliseconds;
Debug.WriteLine("timeFrame:{0},time1:{1},time1:{2}", DateTime.FromOADate(currentOATime), time1, time2);
}
int _iRound = 0;
double _pointsAppended = 0;
double[][] _data;
private void FeedData()
private void FeedData(bool isRefresh)
{
if (_lChartAll != null)
{
_lChartAll.BeginUpdate();
if(isRefresh)
{
for (int seriesIndex = 0; seriesIndex < _channelCount; seriesIndex++)
{
_lChartAll.ViewXY.SampleDataSeries[seriesIndex].Clear();
}
}
//Append data to series
for (int seriesIndex = 0; seriesIndex < _channelCount; seriesIndex++)
{
double[] thisSeriesData = _data[seriesIndex];
if (thisSeriesData == null) continue;
if(thisSeriesData==null)
{
}
_lChartAll.ViewXY.SampleDataSeries[seriesIndex].AddSamples(thisSeriesData, false);
_data[seriesIndex] = null;
System.Diagnostics.Debug.WriteLine("***********index:{0}, pointCount:{1},time:{2}", seriesIndex,
_lChartAll.ViewXY.SampleDataSeries[seriesIndex].PointCount, DateTime.Now);
// System.Diagnostics.Debug.WriteLine("***********index:{0}, pointCount:{1},time:{2}", seriesIndex,
// _lChartAll.ViewXY.SampleDataSeries[seriesIndex].PointCount, DateTime.Now);
}
//System.Threading.Tasks.Parallel.For(0, _channelCount, (seriesIndex) =>
//{

@ -45,9 +45,9 @@
<ComboBoxItem Content="SHN"/>
<ComboBoxItem Content="SHE"/>
</ComboBox>
<TextBlock VerticalAlignment="Center" Margin="20,0,0,0">时间间隔:</TextBlock>
<!--<TextBlock VerticalAlignment="Center" Margin="20,0,0,0">时间间隔:</TextBlock>
<TextBox Text="{Binding XaisInterval}" Width="60" VerticalAlignment="Center"/>
<Button Command="{Binding IntervalSureCommand}" Background="#FF66B1FF" HorizontalAlignment="Right">确认</Button>
<Button Command="{Binding IntervalSureCommand}" Background="#FF66B1FF" HorizontalAlignment="Right">确认</Button>-->
<CheckBox Command="{Binding AxesYVisibleCommand}" Visibility="Collapsed" CommandParameter="{Binding RelativeSource={RelativeSource Mode=Self},Path=IsChecked}" Content="Y轴显示" VerticalContentAlignment="Center" IsChecked="True" FontSize="15" Foreground="Black" Margin="5,0,5,0" FontWeight="Bold" />
<CheckBox Command="{Binding RealTimeDataCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=Self},Path=IsChecked}" Visibility="Collapsed" Content="实时" VerticalContentAlignment="Center" FontSize="15" Foreground="Black" FontWeight="Bold" DockPanel.Dock="Right"></CheckBox>
<TextBox Text="{Binding IntervalTime}" Visibility="Collapsed" Width="60" VerticalAlignment="Center"></TextBox>

@ -34,4 +34,5 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ProjectExtensions><VisualStudio><UserProperties systemconfig_1json__JsonSchema="https://developer.1password.com/schema/ssh-agent-config.json" /></VisualStudio></ProjectExtensions>
</Project>

@ -2,7 +2,7 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:prism="http://prismlibrary.com/" WindowState="Maximized"
WindowStyle="None" WindowStartupLocation="CenterScreen" MouseDown="Window_MouseDown"
WindowStartupLocation="CenterScreen" MouseDown="Window_MouseDown"
prism:ViewModelLocator.AutoWireViewModel="True" Closing="Window_Closing"
Title="{Binding Title}" Height="900" Width="1700" >
<Window.Resources>

Loading…
Cancel
Save