我正在使用SignalR,以便实时生成波形图和FFT图,以进行振动监测分析。最初的想法是在ASP.NET环境中显示这些实时图形,以便可以在一系列与Web兼容的设备上访问这些实时趋势,最终转向使用机器学习进行复杂事件处理和智能数据分析的方法。以获得更多增强的分析方法。当前,我在后面的C#代码中使用随机数生成器来模拟传入的数据流。我有两个集线器,一个用于绘制原始正弦波形,另一个用于绘制快速傅立叶变换。这些都使用Plotly.JS绘制。但是,当我尝试同时访问两个集线器时,一个集线器的输出(即FFT)会覆盖另一个集线器的输出(原始正弦数据)。下面是我关于集线器和javascript的代码,它们从客户端访问服务器上的集线器:
Javascript:
function drawGraph(graph, graphType) {
Plotly.plot(graph, [{
y: [1, 2, 3].map(rand),
mode: graphType,
line: { color: getRandomColor() }
}, {
y: [1, 2, 3].map(rand),
mode: graphType,
line: { color: getRandomColor() }
}], {
xaxis: {
autorange: true
}
});
vibrationGraph(graph);
generateFFT();
$.connection.hub.start();
}
function vibrationGraph(graph) {
var systmr = $.connection.systemTime;
systmr.client.logmessage = function (msg) {
Plotly.extendTraces(graph, {
y: [[msg], [msg * rand()]]
}, [0, 1])
};
}
function generateFFT() {
var fastFourier = $.connection.fastFourier;
fastFourier.client.displayResult = function (output) {
var data = [
{
x: ['x'],
y: [output],
type: 'bar'
}
];
Plotly.newPlot('FFTGraph', data);
}
}
C#背后的代码:
public class SystemTime : Hub
{
public static readonly System.Timers.Timer _Timer = new System.Timers.Timer();
static SystemTime()
{
Random rnd = new Random();
generateWaveForm(rnd.Next(60, 300));
_Timer.Interval = 500;
_Timer.Elapsed += GenerateFFT;
_Timer.Start();
}
static int count = 0;
public static Complex[] sampleList = new Complex[1000];
static void GenerateFFT(object sender, System.Timers.ElapsedEventArgs e)
{
if (count < 1000)
{
var hub = GlobalHost.ConnectionManager.GetHubContext("SystemTime");
hub.Clients.All.logMessage(sampleList[count].Real);
count++;
}
}
static void generateWaveForm(double input)
{
double[] fundamental = Generate.Sinusoidal(1000, 2000, input, 10.0);
for (int i = 0; i < sampleList.Length; i++)
{
sampleList[i] = new Complex(fundamental[i], 0);
}
}
}
public class FastFourier : Hub
{
public static readonly System.Timers.Timer _Timer = new System.Timers.Timer();
static int count = 0;
static int numSamples = 1000;
static int sampleRate = 2000;
static FastFourier()
{
_Timer.Interval = 500;
_Timer.Elapsed += GenerateWave;
_Timer.Start();
}
static void GenerateWave(object sender, System.Timers.ElapsedEventArgs e)
{
if (count < 1000)
{
var hub = GlobalHost.ConnectionManager.GetHubContext("FastFourier");
//hub.Clients.All.logMessage(generateFFTData(count));
hub.Clients.All.displayResult(generateFFTData(count));
count++;
}
}
static double generateFFTData(int index)
{
Fourier.Forward(SystemTime.sampleList, FourierOptions.NoScaling);
double mag = (2.0 / numSamples) * (Math.Abs(Math.Sqrt(Math.Pow(SystemTime.sampleList[count].Real, 2) +
Math.Pow(SystemTime.sampleList[count].Imaginary, 2))));
//double hzPerSample = 2000 / 1000;
return mag;
}
}
我将如何获得它,以便拥有多个具有多个唯一数据流的集线器?
任何帮助将不胜感激。
非常感谢,
本。