软测单独项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

224 lines
6.7 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography.X509Certificates;
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;
using System.Net.Security;
using Newtonsoft.Json;
using System.IO;
namespace FangYar.FYMQTT
{
public class MQTT
{
MqttClient client;
string brokerHostName = "172.20.123.17";
int brokerPort = 31883;
public string clientId = null;
string username = "smartcamp";
string password = "smartcamp";
byte[] qosLevels = { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE };
public MQTT()
{
clientId = Guid.NewGuid().ToString();
connect();
}
public MQTT(string cid)
{
clientId = cid;
//connect();
}
#region MQTT
/// <summary>
/// 连接MQTT
/// </summary>
public void connect()
{
try
{
//创建客户端实例
if (client == null)
{
client = new MqttClient(brokerHostName,
brokerPort,
false, // 开启TLS
MqttSslProtocols.TLSv1_0, // TLS版本
null,
null
);
//消息接受
//client.MqttMsgPublishReceived += messageReceive;
//消息发送完成
//client.MqttMsgPublished += messagePublished;
//连接Broker
client.Connect(clientId, username, password, false, 60);
}
else if (!client.IsConnected)
{
client.Connect(clientId, username, password, false, 60);
}
}
catch (Exception ex)
{
}
}
/// <summary>
/// 订阅主题
/// </summary>
public void Subscribe(string[] Subscribe)
{
try
{
//创建客户端实例
client.Subscribe(Subscribe, qosLevels);
}
catch (Exception ex)
{
}
}
/// <summary>
/// 断开连接
/// </summary>
public void disconnect()
{
if (client != null && client.IsConnected)
{
client.Disconnect();
}
}
private void messageReceive(object sender, MqttMsgPublishEventArgs e)
{
byte[] cv = Encoding.Convert(Encoding.UTF8, Encoding.UTF8, e.Message);
string msg = System.Text.Encoding.UTF8.GetString(cv);
}
public void messagePublished(string jsonstr, string Topic)
{
if (client == null || !client.IsConnected)
{
connect();
}
try
{
byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(jsonstr);
client.Publish(Topic, byteArray, MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
}
catch { }
}
public void messagePublished2(string title, string content, string uid)
{
var jsonstr = "{\"Title\":\"" + title + "\",\"Type\":\"APPROVAL\",\"Content\":\"" + content + "\"}";
FangYar.OracleDAL.TBL.SysEmpDAL dal = new OracleDAL.TBL.SysEmpDAL();
FangYar.Model.TBL.TBL_SYS_EMP_Model model = dal.GetModelByUID(uid);
string Topic = "XJYQ"+ model.ORG_ID + "/" + uid + "/APP/APPROVAL";
if (client == null || !client.IsConnected)
{
connect();
}
try
{
byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(jsonstr);
client.Publish(Topic, byteArray, MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
}
catch { }
}
public void messagePublished(string type, string jsonstr, string Topic)
{
if (client == null || !client.IsConnected)
{
connect();
}
try
{
string str = "{\"ID\":\"" + Guid.NewGuid().ToString() + "\",\"type\":\"" + type + "\",\"data\":" + jsonstr + "}";
byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(str);
//ushort fg = client.Publish(Topic, byteArray);
ushort fg = client.Publish(Topic, byteArray, MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
WriteLog(str + "MQTT:" + fg);
//client.Disconnect();
}
catch (Exception ex)
{
//WriteLog(ex.Message);
throw (ex);
}
}
#endregion
#region 记录日志
/// <summary>
/// 记录日志
/// </summary>
/// <param name="msg"></param>
private void WriteLog(string msg)
{
//string path = @"C:\log.txt";
//该日志文件会存在windows服务程序目录下
string path = AppDomain.CurrentDomain.BaseDirectory + "log\\" + DateTime.Now.ToString("yyyyMMdd") + "log.txt";
FileInfo file = new FileInfo(path);
if (!file.Exists)
{
FileStream fs;
fs = File.Create(path);
fs.Close();
}
using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write))
{
using (StreamWriter sw = new StreamWriter(fs))
{
sw.WriteLine(DateTime.Now.ToString() + " " + msg);
}
}
}
private void WriteData(string msg)
{
//string path = @"C:\log.txt";
//该日志文件会存在windows服务程序目录下
string path = AppDomain.CurrentDomain.BaseDirectory + "log\\" + DateTime.Now.ToString("yyyyMMdd") + "Data.txt";
FileInfo file = new FileInfo(path);
if (!file.Exists)
{
FileStream fs;
fs = File.Create(path);
fs.Close();
}
using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write))
{
using (StreamWriter sw = new StreamWriter(fs))
{
sw.WriteLine(msg);
}
}
}
#endregion
}
}