MessageProcesser.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Concurrent;
namespace TestSeverDemo
{
//可以根据自己需要在这里修改消息的处理方式
public class MessageProcesser
{
//public Dictionary<string, THMessage> RealtimeDataQueue;
//public Dictionary<string, List<THMessage>> AlarmDataQueue;
//public Dictionary<string, List<THMessage>> HistoryDataQueue;
public MessageProcesser()
{
//RealtimeDataQueue = new Dictionary<string, THMessage>();
//AlarmDataQueue = new Dictionary<string, List<THMessage>>();
//HistoryDataQueue = new Dictionary<string, List<THMessage>>();
}
public bool ConsumeMessage(ref THMessage th)
{
//收到的包是否无法解析
bool unexpected = false;
//回复实时数据时需要带这个参数,比如记录间隔,上报间隔,报警范围...,时间
THDeviceParams thdp = null;
if (th.msgType == MessageType.Realtime)
{
Console.WriteLine("收到实时数据:" + th.hexStringPyaload);
//thdp 参数在这下面进行初始化对仪表参数进行配置,暂时用默认参数(null)
if (th.deviceType == DeviceType.TH)
{
RealtimeTHMessage rth = new RealtimeTHMessage(th.receivepayload);
th = rth;
Console.WriteLine("当前温度:" + rth.temp.ToString() + " 当前湿度:" + rth.humidity.ToString());
}
else if (th.deviceType == DeviceType.T)
{
RealtimeTMessage rt = new RealtimeTMessage(th.receivepayload);
th = rt;
Console.WriteLine("当前温度:" + rt.temp.ToString());
}
//try
//{
// RealtimeDataQueue[th.sn] = th;
//}
//catch(Exception ex)
//{
// RealtimeDataQueue.Add(th.sn, th);
//}
}
else if (th.msgType == MessageType.Alarm)
{
Console.WriteLine("收到告警数据:" + th.hexStringPyaload);
AlarmTHMessage alm = new AlarmTHMessage(th.receivepayload);
th = alm;
//try
//{
// AlarmDataQueue[th.sn].Add(th);
//}
//catch (Exception ex)
//{
// List<THMessage> lthmessage = new List<THMessage>();
// lthmessage.Add(th);
// AlarmDataQueue.Add(th.sn, lthmessage);
//}
}
else if (th.msgType == MessageType.History)
{
Console.WriteLine("收到历史数据:" + th.hexStringPyaload);
HistoryTHMessage hthm = null;
HistoryTMessage htm = null;
if (th.deviceType == DeviceType.TH)
{
hthm = new HistoryTHMessage(th.receivepayload);
for (int i = 1; i <= hthm.historyReocrds.Count; i++)
{
Console.WriteLine("收到历史记录:" + i.ToString() + ":温度:" + hthm.historyReocrds[i - 1].temp.ToString() + " 湿度:" + hthm.historyReocrds[i - 1].humidity.ToString() + " 时间:" + hthm.historyReocrds[i - 1].time.ToString("F"));
}
th = hthm;
}
if (th.deviceType == DeviceType.T)
{
htm = new HistoryTMessage(th.receivepayload);
for (int i = 1; i <= htm.historyReocrds.Count; i++)
{
Console.WriteLine("收到历史记录:" + i.ToString() + ":温度:" + htm.historyReocrds[i - 1].temp.ToString() + " 时间:" + htm.historyReocrds[i - 1].time.ToString("F"));
}
th = htm;
}
//try
//{
// HistoryDataQueue[th.sn].Add(th);
//}
//catch (Exception ex)
//{
// List<THMessage> lthmessage = new List<THMessage>();
// lthmessage.Add(th);
// HistoryDataQueue.Add(th.sn, lthmessage);
//}
}
else if (th.msgType == MessageType.Register7)
{
Console.WriteLine("收到注册数据:" + th.hexStringPyaload);
RegisterTHMessage regth = new RegisterTHMessage(th.receivepayload);
th = regth;
}
else
{
//Console.WriteLine("收到未识别数据:" + th.hexStringPyaload);
unexpected = true;
}
//生成发送数据
if (!unexpected)
{
th.GenerateMessage(thdp);
return true;
}
else
{
return false;
}
}
}
}