chore: 初始化平芝126KV总装线 WebApi
This commit is contained in:
29
Helpers/Gl.cs
Normal file
29
Helpers/Gl.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using WebApi.Models;
|
||||
|
||||
namespace WebApi.Helpers
|
||||
{
|
||||
/// <summary>
|
||||
/// 全局变量类
|
||||
/// </summary>
|
||||
public class Gl
|
||||
{
|
||||
/// <summary>
|
||||
/// SQL Server连接状态
|
||||
/// </summary>
|
||||
public static bool OnLine_Sql { get; set; } = false;
|
||||
|
||||
/// <summary>
|
||||
/// AGV连接状态
|
||||
/// </summary>
|
||||
public static bool OnLine_AGV { get; set; } = false;
|
||||
|
||||
/// <summary>
|
||||
/// MQTT连接状态
|
||||
/// </summary>
|
||||
public static bool OnLine_MQTT { get; set; } = false;
|
||||
|
||||
public static ConcurrentDictionary<string, TightenTool> TightenList { get; set; } = new ConcurrentDictionary<string, TightenTool>();
|
||||
}
|
||||
}
|
||||
147
Helpers/Mqtt.cs
Normal file
147
Helpers/Mqtt.cs
Normal file
@@ -0,0 +1,147 @@
|
||||
using Logger;
|
||||
using MQTTnet;
|
||||
using MQTTnet.Client;
|
||||
using MQTTnet.Extensions.ManagedClient;
|
||||
using MQTTnet.Server;
|
||||
using System;
|
||||
using System.Net.Sockets;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace WebApi.Helpers
|
||||
{
|
||||
|
||||
public class Mqtt
|
||||
{
|
||||
public static IMqttClient mqttClient = null;
|
||||
|
||||
public static event Action<bool> ConnectionStatusChanged; // 连接状态改变事件
|
||||
/// <summary>
|
||||
/// Param1:ClientId
|
||||
/// Param2:Topic
|
||||
/// Param3:Message
|
||||
/// </summary>
|
||||
public static event Action<string,string,string> MessageReceived; // 接收到消息事件
|
||||
|
||||
public static async Task RunMqttStart(string ip,int port,string username, string password)
|
||||
{
|
||||
try
|
||||
{
|
||||
string clientId = Guid.NewGuid().ToString();
|
||||
// Create a MQTT client factory
|
||||
var factory = new MqttFactory();
|
||||
|
||||
// Create a MQTT client instance
|
||||
mqttClient = factory.CreateMqttClient();
|
||||
|
||||
// 连接成功
|
||||
mqttClient.ConnectedAsync += (e =>
|
||||
{
|
||||
ConnectionStatusChanged?.Invoke(true);
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
// 连接断开
|
||||
mqttClient.DisconnectedAsync += (e =>
|
||||
{
|
||||
ConnectionStatusChanged?.Invoke(false);
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
// 收到消息
|
||||
mqttClient.ApplicationMessageReceivedAsync += (e =>
|
||||
{
|
||||
string messagePayload = null;
|
||||
if (e.ApplicationMessage.PayloadSegment.Count > 0)
|
||||
{
|
||||
byte[] payload = e.ApplicationMessage.PayloadSegment.Array;
|
||||
int payloadOffset = e.ApplicationMessage.PayloadSegment.Offset;
|
||||
int payloadLength = e.ApplicationMessage.PayloadSegment.Count;
|
||||
messagePayload = Encoding.UTF8.GetString(payload, payloadOffset, payloadLength);
|
||||
}
|
||||
else
|
||||
{
|
||||
messagePayload = "";
|
||||
}
|
||||
|
||||
MessageReceived?.Invoke(e.ClientId, e.ApplicationMessage.Topic, messagePayload);
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
|
||||
// Create MQTT client options
|
||||
var options = new MqttClientOptionsBuilder()
|
||||
.WithTcpServer(ip, port) // MQTT broker address and port
|
||||
.WithCredentials(username, password) // Set username and password
|
||||
.WithClientId(clientId)
|
||||
.WithCleanSession()
|
||||
.Build();
|
||||
|
||||
// Connect to MQTT broker
|
||||
var connectResult = await mqttClient.ConnectAsync(options);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.FunError(ex, MethodBase.GetCurrentMethod().Name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 断开连接
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static async Task RunMqttStop()
|
||||
{
|
||||
if (mqttClient != null)
|
||||
{
|
||||
await mqttClient.DisconnectAsync();
|
||||
mqttClient.Dispose();
|
||||
mqttClient = null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 推送消息
|
||||
/// </summary>
|
||||
/// <param name="topic"></param>
|
||||
/// <param name="message"></param>
|
||||
/// <returns></returns>
|
||||
public static async Task RunMqttPublish(string topic, string message)
|
||||
{
|
||||
if (mqttClient != null)
|
||||
{
|
||||
var publishOptions = new MqttApplicationMessageBuilder()
|
||||
.WithTopic(topic)
|
||||
.WithPayload(message)
|
||||
.WithRetainFlag(false)
|
||||
.Build();
|
||||
|
||||
await mqttClient.PublishAsync(publishOptions);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加订阅
|
||||
/// </summary>
|
||||
/// <param name="topic"></param>
|
||||
/// <returns></returns>
|
||||
public static async Task RunMqttSubscribe(string topic)
|
||||
{
|
||||
if (mqttClient != null)
|
||||
{
|
||||
await mqttClient.SubscribeAsync(topic);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 取消订阅
|
||||
/// </summary>
|
||||
/// <param name="topic"></param>
|
||||
/// <returns></returns>
|
||||
public static async Task RunMqttUnSubscribe(string topic)
|
||||
{
|
||||
if (mqttClient != null)
|
||||
{
|
||||
await mqttClient.UnsubscribeAsync(topic);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
223
Helpers/SqlServer.cs
Normal file
223
Helpers/SqlServer.cs
Normal file
@@ -0,0 +1,223 @@
|
||||
using System;
|
||||
using System.Data.SqlClient;
|
||||
using System.Data;
|
||||
using System.Reflection;
|
||||
using Logger;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace WebApi.Helpers
|
||||
{
|
||||
public class SqlServer
|
||||
{
|
||||
public static bool ExecuteProcedure(string ProcedureName, SqlParameter[] sqlParameters, out DataTable dt, out string errorMessage)
|
||||
{
|
||||
bool result = false;
|
||||
dt = new DataTable();
|
||||
errorMessage = "";
|
||||
using (SqlConnection sqlConnection = new SqlConnection(Tools.AppConfigManage.ReadConfig("ConnectionString")))
|
||||
{
|
||||
try
|
||||
{
|
||||
sqlConnection.Open();
|
||||
// 开始事务
|
||||
SqlTransaction sqlTransaction = sqlConnection.BeginTransaction();
|
||||
try
|
||||
{
|
||||
using (SqlCommand sqlCommand = sqlConnection.CreateCommand())
|
||||
{
|
||||
sqlCommand.Transaction = sqlTransaction; // 将事务附加到SqlCommand对象
|
||||
sqlCommand.CommandType = CommandType.StoredProcedure;
|
||||
sqlCommand.CommandText = ProcedureName;
|
||||
|
||||
if (sqlParameters != null)
|
||||
{
|
||||
for (int i = 0; i < sqlParameters.Length; i++)
|
||||
{
|
||||
sqlCommand.Parameters.Add(sqlParameters[i]);
|
||||
}
|
||||
}
|
||||
|
||||
// 创建SqlDataAdapter并关联SqlCommand
|
||||
using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand))
|
||||
{
|
||||
// 填充DataTable
|
||||
sqlDataAdapter.Fill(dt);
|
||||
}
|
||||
// 如果执行成功,提交事务
|
||||
sqlTransaction.Commit();
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// 如果执行过程中出现异常,回滚事务
|
||||
try
|
||||
{
|
||||
sqlTransaction.Rollback();
|
||||
errorMessage = ProcedureName + "存储过程执行失败,事务已回滚。错误信息:" + ex.Message;
|
||||
Log.Error(ProcedureName + "存储过程执行失败,事务已回滚。错误信息:" + ex.Message);
|
||||
}
|
||||
catch (Exception exRollback)
|
||||
{
|
||||
// 如果回滚操作也失败,记录回滚异常
|
||||
errorMessage = ProcedureName + "事务回滚失败。错误信息:" + exRollback.Message;
|
||||
Log.Error(ProcedureName + "事务回滚失败。错误信息:" + exRollback.Message);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (sqlConnection.State == ConnectionState.Open)
|
||||
{
|
||||
sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
errorMessage = ex.Message;
|
||||
Log.FunError(ex, MethodBase.GetCurrentMethod().Name);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (sqlConnection.State == ConnectionState.Open)
|
||||
{
|
||||
sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
public static bool ExecuteSql(string sql, out DataTable dt, out string errorMessage)
|
||||
{
|
||||
bool result = false;
|
||||
dt = new DataTable();
|
||||
errorMessage = "";
|
||||
using (SqlConnection sqlConnection = new SqlConnection(Tools.AppConfigManage.ReadConfig("ConnectionString")))
|
||||
{
|
||||
try
|
||||
{
|
||||
sqlConnection.Open();
|
||||
// 开始事务
|
||||
using (SqlTransaction sqlTransaction = sqlConnection.BeginTransaction())
|
||||
{
|
||||
using (SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection, sqlTransaction))
|
||||
{
|
||||
sqlCommand.CommandType = CommandType.Text; // 设置为执行文本命令
|
||||
|
||||
// 创建SqlDataAdapter并关联SqlCommand
|
||||
using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand))
|
||||
{
|
||||
// 填充DataTable
|
||||
sqlDataAdapter.Fill(dt);
|
||||
}
|
||||
|
||||
// 如果执行成功,提交事务
|
||||
sqlTransaction.Commit();
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
errorMessage = ex.Message;
|
||||
Log.FunError(ex, MethodBase.GetCurrentMethod().Name);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (sqlConnection.State == ConnectionState.Open)
|
||||
{
|
||||
sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static bool ExecuteProcedure(string ProcedureName, Dictionary<string, Object> sqlParametersDictionary, out DataTable dt, out string errorMessage)
|
||||
{
|
||||
// 处理字典数据 转为 SqlParameter
|
||||
List <SqlParameter> parameterList = new List <SqlParameter> ();
|
||||
foreach (KeyValuePair<string,Object> param in sqlParametersDictionary)
|
||||
{
|
||||
parameterList.Add(new SqlParameter("@" + param.Key, param.Value));
|
||||
}
|
||||
SqlParameter[] sqlParameters = parameterList.ToArray();
|
||||
|
||||
bool result = false;
|
||||
dt = new DataTable();
|
||||
errorMessage = "";
|
||||
using (SqlConnection sqlConnection = new SqlConnection(Tools.AppConfigManage.ReadConfig("ConnectionString")))
|
||||
{
|
||||
try
|
||||
{
|
||||
sqlConnection.Open();
|
||||
// 开始事务
|
||||
SqlTransaction sqlTransaction = sqlConnection.BeginTransaction();
|
||||
try
|
||||
{
|
||||
using (SqlCommand sqlCommand = sqlConnection.CreateCommand())
|
||||
{
|
||||
sqlCommand.Transaction = sqlTransaction; // 将事务附加到SqlCommand对象
|
||||
sqlCommand.CommandType = CommandType.StoredProcedure;
|
||||
sqlCommand.CommandText = ProcedureName;
|
||||
|
||||
if (sqlParameters != null)
|
||||
{
|
||||
for (int i = 0; i < sqlParameters.Length; i++)
|
||||
{
|
||||
sqlCommand.Parameters.Add(sqlParameters[i]);
|
||||
}
|
||||
}
|
||||
|
||||
// 创建SqlDataAdapter并关联SqlCommand
|
||||
using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand))
|
||||
{
|
||||
// 填充DataTable
|
||||
sqlDataAdapter.Fill(dt);
|
||||
}
|
||||
// 如果执行成功,提交事务
|
||||
sqlTransaction.Commit();
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// 如果执行过程中出现异常,回滚事务
|
||||
try
|
||||
{
|
||||
sqlTransaction.Rollback();
|
||||
errorMessage = ProcedureName + "存储过程执行失败,事务已回滚。错误信息:" + ex.Message;
|
||||
Log.Error(ProcedureName + "存储过程执行失败,事务已回滚。错误信息:" + ex.Message);
|
||||
}
|
||||
catch (Exception exRollback)
|
||||
{
|
||||
// 如果回滚操作也失败,记录回滚异常
|
||||
errorMessage = ProcedureName + "事务回滚失败。错误信息:" + exRollback.Message;
|
||||
Log.Error(ProcedureName + "事务回滚失败。错误信息:" + exRollback.Message);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (sqlConnection.State == ConnectionState.Open)
|
||||
{
|
||||
sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
errorMessage = ex.Message;
|
||||
Log.FunError(ex, MethodBase.GetCurrentMethod().Name);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (sqlConnection.State == ConnectionState.Open)
|
||||
{
|
||||
sqlConnection.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user