123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using static IoTIntegrationPlatform.Model.Enum.EnumMqtt;
- using IoTIntegrationPlatform.Common;
- using IoTIntegrationPlatform.Interface;
- using IoTIntegrationPlatform.Model.common;
- using IoTIntegrationPlatform.Model.Model;
- using SqlSugar;
- namespace IoTIntegrationPlatform.Services
- {
- /// <summary>
- /// 日志服务
- /// </summary>
- public class LogService : ILogService
- {
- private ISqlSugarClient db { get; }
- public LogService(ISqlSugarClient _db)
- {
- db = _db;
- }
- #region 类句柄操作
- private IntPtr handle;
- private bool disposed = false;
- //关闭句柄
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1060:MovePInvokesToNativeMethodsClass"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), System.Runtime.InteropServices.DllImport("Kernel32")]
- public extern static Boolean CloseHandle(IntPtr handle);
- /// <summary>
- /// 释放对象资源
- /// </summary>
- public void Dispose()
- {
- Dispose(true);
- GC.SuppressFinalize(this);
- }
- /// <summary>
- /// 释放对象资源
- /// </summary>
- /// <param name="disposing"></param>
- protected virtual void Dispose(bool disposing)
- {
- if (!this.disposed)
- {
- if (handle != IntPtr.Zero)
- {
- CloseHandle(handle);
- handle = IntPtr.Zero;
- }
- }
- disposed = true;
- }
- ~LogService()
- {
- Dispose(false);
- }
- #endregion
- /// <summary>
- /// 获取mqtt通讯日志列表
- /// </summary>
- /// <param name="parm"></param>
- /// <param name="totalPage"></param>
- /// <returns></returns>
- public List<MqttLog> GetMqttLogList(BaseParm parm, ref int totalPage)
- {
- List<MqttLog> list = new List<MqttLog>();
- try
- {
- list = db.Queryable<MqttLog>()
- .WhereIF(!string.IsNullOrEmpty(parm.Param), it => it.ClientId.Contains(parm.Param) || it.Content.Contains(parm.Param))
- .WhereIF(parm.Int1 > 0, it => it.MessagePattern == parm.Int1)
- .WhereIF(parm.Int2 > 0, it => it.TopicType == (EnumMqttTopicType)parm.Int2)
- .OrderBy(it => it.CreateTime, OrderByType.Desc)//倒序
- .ToPageList(parm.PageIndex, parm.PageSize, ref totalPage);
- }
- catch (Exception ex)
- {
- Logging.Error(ex, "GetMqttLogList");
- }
- return list;
- }
- /// <summary>
- /// 添加mqtt通讯日志
- /// </summary>
- /// <param name="level"></param>
- /// <param name="content"></param>
- /// <returns></returns>
- public bool AddMqttLog(MqttLog model)
- {
- bool result = false;
- try
- {
- model.Uuid = Guid.NewGuid().ToString();
- model.TopicTypeName = EnumExtension.GetDescription(model.TopicType);
- model.CreateTime = TimeHelper.GetCurrentTimestampTicks();
- int row = db.Insertable(model).ExecuteCommand();
- if (row > 0)
- {
- result = true;
- }
- }
- catch (Exception ex)
- {
- Logging.Error(ex, ex.Message);
- }
- return result;
- }
-
- }
- }
|