初始化提交
仓库转移到Gitea,初始化提交,可能丢失以前的git版本日志
This commit is contained in:
392
UTS_Core/UTSModule/Service/AppRegister.vb
Normal file
392
UTS_Core/UTSModule/Service/AppRegister.vb
Normal file
@@ -0,0 +1,392 @@
|
||||
Imports System.Text
|
||||
Imports UTS_Core.Database
|
||||
Imports UTS_Core.UTSModule.DbConnect
|
||||
Imports UTS_Core.UTSModule.DbTableModel.Manage
|
||||
|
||||
Namespace UTSModule.Service
|
||||
Public Class AppRegister
|
||||
Implements IDisposable
|
||||
|
||||
''' <summary>
|
||||
''' 当前App所属服务索引
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public ReadOnly Property ServiceIndex As Integer
|
||||
|
||||
''' <summary>
|
||||
''' 当前App名称
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public ReadOnly Property AppName As String
|
||||
|
||||
''' <summary>
|
||||
''' 当前App版本
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public ReadOnly Property AppVersion As String
|
||||
|
||||
''' <summary>
|
||||
''' 当前App运行信息
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property AppMsg As String
|
||||
|
||||
''' <summary>
|
||||
''' 当前App状态,默认0为空闲
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property AppStatus As Integer
|
||||
|
||||
|
||||
Private ReadOnly _logLock As Object = New Object()
|
||||
Private ReadOnly _logQueue As New Queue(Of Dictionary(Of String, String))
|
||||
|
||||
|
||||
Enum LogTypeEnum
|
||||
''' <summary>
|
||||
''' 调试信息
|
||||
''' </summary>
|
||||
Debug
|
||||
|
||||
''' <summary>
|
||||
''' 流程信息
|
||||
''' </summary>
|
||||
Info
|
||||
|
||||
''' <summary>
|
||||
''' 警告信息
|
||||
''' </summary>
|
||||
Warn
|
||||
|
||||
''' <summary>
|
||||
''' 错误信息
|
||||
''' </summary>
|
||||
[Error]
|
||||
|
||||
''' <summary>
|
||||
''' 致命信息
|
||||
''' </summary>
|
||||
Fatal
|
||||
End Enum
|
||||
|
||||
|
||||
Sub New(serviceIndex As Integer, appName As String, appVersion As String)
|
||||
Me.ServiceIndex = serviceIndex
|
||||
Me.AppName = appName
|
||||
Me.AppVersion = appVersion
|
||||
|
||||
InitApp().GetAwaiter().GetResult()
|
||||
End Sub
|
||||
|
||||
Public Property ProjectName() As String
|
||||
Public Property StationName() As String
|
||||
Public Property TestPlan() As String
|
||||
|
||||
Private Async Function InitApp() As Task
|
||||
If AppRegistered() Then
|
||||
'更新App版本
|
||||
Try
|
||||
Await UpdateAppVersion()
|
||||
Catch ex As Exception
|
||||
Throw New Exception($"UpdateAppVersion Error: {ex.Message}")
|
||||
End Try
|
||||
Return
|
||||
End If
|
||||
|
||||
Try
|
||||
RegisterApp() '注册App
|
||||
Catch ex As Exception
|
||||
Throw New Exception($"RegisterApp Error: {ex.Message}")
|
||||
End Try
|
||||
|
||||
End Function
|
||||
|
||||
|
||||
Private Function AppRegistered() As Boolean
|
||||
Dim result As Integer
|
||||
|
||||
Dim colName As String = $"count(*)"
|
||||
Dim tableName As String = $"{AppListTable.TableName}"
|
||||
Dim condition As String = $"`{AppListTable.ColNamesEnum.ServiceID}` = {ServiceIndex} and
|
||||
`{AppListTable.ColNamesEnum.AppName}` = '{AppName}'"
|
||||
|
||||
Using db As New DbExecutor(UtsDb.LocalDbType, UtsDb.LocalConnString)
|
||||
db.Open()
|
||||
|
||||
Dim cmdText As String = db.CmdHelper.Search(colName, tableName, condition)
|
||||
result = CInt(db.ExecuteScalar(cmdText))
|
||||
|
||||
db.Close()
|
||||
End Using
|
||||
|
||||
Return result > 0
|
||||
End Function
|
||||
|
||||
|
||||
Private Sub RegisterApp()
|
||||
Dim tableName As String = AppListTable.TableName
|
||||
Dim filed As New Dictionary(Of String, String) From {
|
||||
{$"{AppListTable.ColNamesEnum.ServiceID}", ServiceIndex.ToString()},
|
||||
{$"{AppListTable.ColNamesEnum.AppName}", AppName},
|
||||
{$"{AppListTable.ColNamesEnum.AppVersion}", AppVersion},
|
||||
{$"{AppListTable.ColNamesEnum.RegisterDateTime}", Now.ToString("yyyy-MM-dd HH:mm:ss")},
|
||||
{$"{AppListTable.ColNamesEnum.LastActiveDateTime}", Now.ToString("yyyy-MM-dd HH:mm:ss")}
|
||||
}
|
||||
|
||||
Dim saved As Boolean
|
||||
Dim cmdText As String
|
||||
|
||||
Try
|
||||
'远程注册App
|
||||
Using db As New DbExecutor(UtsDb.RemoteDbType, UtsDb.RemoteConnString)
|
||||
db.Open()
|
||||
|
||||
cmdText = $"call `uts_manage`.`RegisterApp` ({ServiceIndex},'{AppName}','{AppVersion}','{Now:yyyy-MM-dd HH:mm:ss}')"
|
||||
|
||||
db.ExecuteNonQuery(cmdText)
|
||||
|
||||
db.Close()
|
||||
End Using
|
||||
|
||||
saved = True
|
||||
Catch ex As Exception
|
||||
saved = False
|
||||
Console.WriteLine("Register Remote App Error:" & ex.Message)
|
||||
End Try
|
||||
|
||||
|
||||
'本地注册App
|
||||
Using db As New DbExecutor(UtsDb.LocalDbType, UtsDb.LocalConnString)
|
||||
db.Open()
|
||||
|
||||
cmdText = db.CmdHelper.Insert(tableName, filed)
|
||||
db.ExecuteNonQuery(cmdText)
|
||||
|
||||
If saved = False Then
|
||||
cmdText = $"call `uts_manage`.`RegisterApp` ({ServiceIndex},'{AppName}','{AppVersion}','{Now:yyyy-MM-dd HH:mm:ss}')"
|
||||
DbConnector.SaveCmdStringToCacheTable(db, cmdText)
|
||||
End If
|
||||
|
||||
db.Close()
|
||||
End Using
|
||||
End Sub
|
||||
|
||||
Private Async Function UpdateAppVersion() As Task
|
||||
Dim tableName As String = AppListTable.TableName
|
||||
Dim filed As New Dictionary(Of String, String) From {
|
||||
{$"{AppListTable.ColNamesEnum.AppVersion}", AppVersion},
|
||||
{$"{AppListTable.ColNamesEnum.LastActiveDateTime}", Now.ToString("yyyy-MM-dd HH:mm:ss")}
|
||||
}
|
||||
Dim condition As String = $"`{AppListTable.ColNamesEnum.ServiceID}` = {ServiceIndex} and `{AppListTable.ColNamesEnum.AppName}` = '{AppName}'"
|
||||
|
||||
Dim saved As Boolean
|
||||
Dim cmdText As String
|
||||
|
||||
Try
|
||||
'远程更新App版本
|
||||
Using db As New DbExecutor(UtsDb.RemoteDbType, UtsDb.RemoteConnString)
|
||||
cmdText = db.CmdHelper.DbUpdate(UtsDb.RemotePublicDb, tableName, filed, condition)
|
||||
|
||||
Await db.OpenAsync()
|
||||
|
||||
Await db.ExecuteNonQueryAsync(cmdText)
|
||||
|
||||
db.Close()
|
||||
End Using
|
||||
saved = True
|
||||
Catch ex As Exception
|
||||
saved = False
|
||||
Console.WriteLine("Update Remote AppVersion Error:" & ex.Message)
|
||||
End Try
|
||||
|
||||
|
||||
'本地更新App版本
|
||||
Using db As New DbExecutor(UtsDb.LocalDbType, UtsDb.LocalConnString)
|
||||
Await db.OpenAsync()
|
||||
|
||||
cmdText = db.CmdHelper.Update(tableName, filed, condition)
|
||||
Await db.ExecuteNonQueryAsync(cmdText)
|
||||
|
||||
'保存至缓冲队列
|
||||
If saved = False Then
|
||||
cmdText = New DbExecutor(UtsDb.RemoteDbType, UtsDb.RemoteConnString).CmdHelper.DbUpdate(UtsDb.RemotePublicDb, tableName, filed, condition)
|
||||
DbConnector.SaveCmdStringToCacheTable(db, cmdText)
|
||||
End If
|
||||
|
||||
db.Close()
|
||||
End Using
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 定期更新APP的活动时间,调用则会定期基于本地调用存储过程至缓存表
|
||||
''' </summary>
|
||||
Public Sub UpdateAliveTime()
|
||||
Dim sb As New StringBuilder
|
||||
sb.Append($"update `{UtsDb.RemotePublicDb}`.`{AppListTable.TableName}` set ")
|
||||
sb.Append($"`{AppListTable.ColNamesEnum.AppVersion}` = '{AppVersion}' ")
|
||||
sb.Append($",`{AppListTable.ColNamesEnum.LastActiveDateTime}` = current_timestamp() ")
|
||||
If String.IsNullOrEmpty(AppMsg) = False Then
|
||||
Select Case AppStatus
|
||||
Case 0 '空闲状态
|
||||
sb.Append($",`{AppListTable.ColNamesEnum.LastInfomation}` = '{AppMsg},Status:Idle' ")
|
||||
Case 1 '测试中
|
||||
sb.Append($",`{AppListTable.ColNamesEnum.LastInfomation}` = '{AppMsg},Status:Testing'")
|
||||
End Select
|
||||
End If
|
||||
|
||||
sb.Append($"where `{AppListTable.ColNamesEnum.ServiceID}` = {ServiceIndex} ")
|
||||
sb.Append($"and `{AppListTable.ColNamesEnum.AppName}` = '{AppName}' ")
|
||||
|
||||
Try
|
||||
Using db As New DbExecutor(UtsDb.RemoteDbType, UtsDb.RemoteConnString)
|
||||
db.Open()
|
||||
|
||||
db.ExecuteNonQuery(sb.ToString)
|
||||
|
||||
db.Close()
|
||||
End Using
|
||||
Catch ex As Exception
|
||||
Console.WriteLine($"Update AppInfo Error:{ex.Message}")
|
||||
End Try
|
||||
|
||||
End Sub
|
||||
|
||||
|
||||
Public Shared Sub CreateAppLogTable()
|
||||
Try
|
||||
Using db As New DbExecutor(UtsDb.LocalDbType, UtsDb.LocalConnString)
|
||||
db.Open()
|
||||
|
||||
Dim cmdText As String = AppLogTable.CreateTableString("", DbExecutor.DbTypeEnum.Sqlite)
|
||||
|
||||
db.ExecuteNonQuery(cmdText)
|
||||
|
||||
db.Close()
|
||||
End Using
|
||||
|
||||
Catch ex As Exception
|
||||
Throw New Exception($"CreateAppLogTable Error:{ex.Message}")
|
||||
End Try
|
||||
End Sub
|
||||
|
||||
|
||||
Public Shared Sub CreateAppListTable()
|
||||
Try
|
||||
Using db As New DbExecutor(UtsDb.LocalDbType, UtsDb.LocalConnString)
|
||||
db.Open()
|
||||
|
||||
Dim cmdText As String = AppListTable.CreateTableString("", DbExecutor.DbTypeEnum.Sqlite)
|
||||
|
||||
db.ExecuteNonQuery(cmdText)
|
||||
|
||||
db.Close()
|
||||
End Using
|
||||
|
||||
Catch ex As Exception
|
||||
Throw New Exception($"CreateAppListTable Error:{ex.Message}")
|
||||
End Try
|
||||
End Sub
|
||||
|
||||
|
||||
Public Sub AppendDebugLog(logText As String)
|
||||
AppendLogToLogQueue(LogTypeEnum.Debug, logText)
|
||||
End Sub
|
||||
|
||||
Public Sub AppendTraceLog(logText As String)
|
||||
AppendLogToLogQueue(LogTypeEnum.Info, logText)
|
||||
End Sub
|
||||
|
||||
Public Sub AppendWarningLog(logText As String)
|
||||
AppendLogToLogQueue(LogTypeEnum.Warn, logText)
|
||||
End Sub
|
||||
|
||||
Public Sub AppendErrorLog(logText As String)
|
||||
AppendLogToLogQueue(LogTypeEnum.Error, logText)
|
||||
End Sub
|
||||
|
||||
Public Sub AppendFatalLog(logText As String)
|
||||
AppendLogToLogQueue(LogTypeEnum.Fatal, logText)
|
||||
End Sub
|
||||
|
||||
''' <summary>
|
||||
''' 添加日志内容至缓存的日志队列中
|
||||
''' 调用SaveLogQueueToDb将队列中的数据写入到数据库
|
||||
'''
|
||||
''' Tip:缓存队列仅记录每条日志不同的内容,例如写入时间,日志类型,日志内容等
|
||||
''' </summary>
|
||||
''' <param name="logType"></param>
|
||||
''' <param name="logText"></param>
|
||||
Public Sub AppendLogToLogQueue(logType As LogTypeEnum, logText As String)
|
||||
Dim dicFiled As New Dictionary(Of String, String)
|
||||
|
||||
dicFiled.Add($"{AppLogTable.ColNamesEnum.DateTime}", $"{Now:yyyy-MM-dd HH:mm:ss}")
|
||||
|
||||
dicFiled.Add($"{AppLogTable.ColNamesEnum.LogType}", $"{logType}")
|
||||
dicFiled.Add($"{AppLogTable.ColNamesEnum.LogText}", $"{logText}")
|
||||
|
||||
SyncLock _logLock
|
||||
_logQueue.Enqueue(dicFiled) '将日志信息放入缓冲队列
|
||||
End SyncLock
|
||||
|
||||
End Sub
|
||||
|
||||
|
||||
Public Sub SaveLogQueueToDb()
|
||||
If _logQueue.Count <= 0 Then Return
|
||||
|
||||
Dim tableName As String = AppLogTable.TableName
|
||||
|
||||
Using db As New DbExecutor(UtsDb.LocalDbType, UtsDb.LocalConnString)
|
||||
db.Open()
|
||||
|
||||
db.BeginTransaction()
|
||||
|
||||
Dim dicFiled As Dictionary(Of String, String)
|
||||
Dim cmdText As String
|
||||
Dim cmdHelper As DbCmdHelper = DbCmdHelper.CreateCmdHelper(UtsDb.RemoteDbType)
|
||||
|
||||
SyncLock _logLock
|
||||
While _logQueue.Count > 0
|
||||
dicFiled = _logQueue.Dequeue()
|
||||
|
||||
dicFiled.Add($"{AppLogTable.ColNamesEnum.ServiceID}", $"{ServiceIndex}")
|
||||
dicFiled.Add($"{AppLogTable.ColNamesEnum.AppName}", $"{AppName}")
|
||||
dicFiled.Add($"{AppLogTable.ColNamesEnum.AppVersion}", $"{AppVersion}")
|
||||
|
||||
dicFiled.Add($"{AppLogTable.ColNamesEnum.ProjectName}", $"{ProjectName}")
|
||||
dicFiled.Add($"{AppLogTable.ColNamesEnum.StationName}", $"{StationName}")
|
||||
dicFiled.Add($"{AppLogTable.ColNamesEnum.TestPlan}", $"{TestPlan}")
|
||||
|
||||
Try
|
||||
cmdText = db.CmdHelper.Insert(tableName, dicFiled)
|
||||
db.ExecuteNonQuery(cmdText) '执行语句
|
||||
Catch ex As Exception
|
||||
Console.WriteLine($"Write LogQueue To Local DB Error: {ex.Message}")
|
||||
Continue While
|
||||
End Try
|
||||
|
||||
|
||||
Try
|
||||
cmdText = cmdHelper.DbInsert(UtsDb.RemotePublicDb, tableName, dicFiled)
|
||||
DbConnector.SaveCmdStringToCacheTable(db, cmdText) '执行命令入缓存库
|
||||
Catch ex As Exception
|
||||
Console.WriteLine($"Write LogQueue To Chche DB Error: {ex.Message}")
|
||||
Continue While
|
||||
End Try
|
||||
End While
|
||||
End SyncLock
|
||||
|
||||
db.CommitTransaction()
|
||||
db.Close()
|
||||
End Using
|
||||
End Sub
|
||||
|
||||
|
||||
|
||||
|
||||
Public Sub Dispose() Implements IDisposable.Dispose
|
||||
|
||||
|
||||
End Sub
|
||||
End Class
|
||||
End Namespace
|
||||
97
UTS_Core/UTSModule/Service/AutsTcpClient.vb
Normal file
97
UTS_Core/UTSModule/Service/AutsTcpClient.vb
Normal file
@@ -0,0 +1,97 @@
|
||||
Imports System.Net.Sockets
|
||||
Imports System.Text
|
||||
|
||||
Namespace UTSModule.Service
|
||||
|
||||
Public Class AutsTcpClient
|
||||
Implements IDisposable
|
||||
|
||||
Private _tcpClient As TcpClient
|
||||
Private _tcpOpen As Boolean
|
||||
|
||||
Private ReadOnly _hostName As String
|
||||
Private ReadOnly _hostPort As Integer
|
||||
|
||||
|
||||
Sub New(hostName As String, port As Integer)
|
||||
_hostName = hostName
|
||||
|
||||
_hostPort = port
|
||||
|
||||
_tcpOpen = False
|
||||
End Sub
|
||||
|
||||
|
||||
Public ReadOnly Property IsOpen() As Boolean
|
||||
Get
|
||||
Return _tcpOpen
|
||||
End Get
|
||||
End Property
|
||||
|
||||
|
||||
Public Sub Open()
|
||||
If _tcpOpen = True Then Return
|
||||
|
||||
_tcpClient = New TcpClient(_hostName, _hostPort)
|
||||
_tcpOpen = True
|
||||
End Sub
|
||||
|
||||
|
||||
Public Sub Close()
|
||||
If _tcpOpen = False Then Return
|
||||
|
||||
_tcpClient.Close()
|
||||
_tcpClient.Dispose()
|
||||
_tcpOpen = False
|
||||
End Sub
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 网络发送Json字符串,字符串以回车换行结尾
|
||||
''' </summary>
|
||||
''' <param name="jsonString"></param>
|
||||
Public Sub WriteJsonString(jsonString As String)
|
||||
Dim buf() As Byte = Encoding.UTF8.GetBytes(jsonString)
|
||||
_tcpClient.GetStream().Write(buf, 0, buf.Length)
|
||||
End Sub
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 超时接收回复信息,以回车换行结尾
|
||||
''' </summary>
|
||||
''' <param name="timeout">超时时间,单位毫秒</param>
|
||||
''' <returns>接收到的字符串,超时返回空字符串</returns>
|
||||
Public Function ReadJsonString(Optional timeout As Integer = 1000) As String
|
||||
Dim replayStr As New StringBuilder
|
||||
|
||||
Dim time As Date = Now
|
||||
While (Now - time).TotalMilliseconds < timeout
|
||||
If _tcpClient.Available > 0 Then
|
||||
Dim length As Integer = _tcpClient.Available
|
||||
Dim buf(length - 1) As Byte
|
||||
_tcpClient.GetStream().Read(buf, 0, length) '读取数据
|
||||
|
||||
replayStr.Append(Encoding.UTF8.GetString(buf))
|
||||
|
||||
If replayStr.ToString().EndsWith(vbCrLf) Then '校验,以回车换行结尾
|
||||
Exit While
|
||||
End If
|
||||
End If
|
||||
End While
|
||||
|
||||
Return replayStr.ToString()
|
||||
End Function
|
||||
|
||||
Public Function Connected() As Boolean
|
||||
If _tcpClient Is Nothing Then Return False
|
||||
If _tcpClient.Client Is Nothing Then Return False
|
||||
Return _tcpClient.Connected
|
||||
End Function
|
||||
Public Sub Dispose() Implements IDisposable.Dispose
|
||||
If IsOpen() Then
|
||||
Close()
|
||||
End If
|
||||
End Sub
|
||||
|
||||
End Class
|
||||
End Namespace
|
||||
36
UTS_Core/UTSModule/Service/IServiceTask.vb
Normal file
36
UTS_Core/UTSModule/Service/IServiceTask.vb
Normal file
@@ -0,0 +1,36 @@
|
||||
|
||||
Namespace UTSModule.Service
|
||||
''' <summary>
|
||||
''' 任务类型接口,每个任务必须实现的功能
|
||||
''' </summary>
|
||||
Public Interface IServiceTask
|
||||
''' <summary>
|
||||
''' 任务开始
|
||||
''' </summary>
|
||||
Sub Start()
|
||||
|
||||
''' <summary>
|
||||
''' 任务退出
|
||||
''' </summary>
|
||||
Sub [Stop]()
|
||||
|
||||
''' <summary>
|
||||
''' 重启任务
|
||||
''' </summary>
|
||||
Sub Restart()
|
||||
|
||||
''' <summary>
|
||||
''' 设置任务参数
|
||||
''' </summary>
|
||||
''' <param name="params"></param>
|
||||
Sub SetParams(params As Dictionary(Of String, String))
|
||||
|
||||
''' <summary>
|
||||
''' 获取任务参数
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Function GetParams() As Dictionary(Of String, String)
|
||||
|
||||
|
||||
End Interface
|
||||
End Namespace
|
||||
88
UTS_Core/UTSModule/Service/ServiceTask.vb
Normal file
88
UTS_Core/UTSModule/Service/ServiceTask.vb
Normal file
@@ -0,0 +1,88 @@
|
||||
Imports System.Xml.Serialization
|
||||
|
||||
Namespace UTSModule.Service
|
||||
|
||||
<XmlInclude(GetType(ServiceTask))>
|
||||
Public MustInherit Class ServiceTask
|
||||
Implements IServiceTask
|
||||
|
||||
''' <summary>
|
||||
''' 服务任务类型枚举集合
|
||||
''' </summary>
|
||||
Enum ServiceTaskTypeEnum
|
||||
''' <summary>
|
||||
''' 数据库同步
|
||||
''' </summary>
|
||||
DbSync
|
||||
|
||||
''' <summary>
|
||||
''' 监听Json文件
|
||||
''' </summary>
|
||||
ListenJsonFile
|
||||
End Enum
|
||||
|
||||
''' <summary>
|
||||
''' 服务任务状态枚举值
|
||||
''' </summary>
|
||||
Enum ServiceTaskStatusEnum
|
||||
''' <summary>
|
||||
''' 启动状态
|
||||
''' </summary>
|
||||
Start
|
||||
|
||||
''' <summary>
|
||||
''' 停止状态
|
||||
''' </summary>
|
||||
[Stop]
|
||||
End Enum
|
||||
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 服务任务类型
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property TaskType() As ServiceTaskTypeEnum
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 服务任务名,服务任务的唯一索引
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property TaskName() As String
|
||||
|
||||
''' <summary>
|
||||
''' 服务任务的状态
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property TaskStatus() As ServiceTaskStatusEnum
|
||||
|
||||
''' <summary>
|
||||
''' 任务开启
|
||||
''' </summary>
|
||||
Public MustOverride Sub Start() Implements IServiceTask.Start
|
||||
|
||||
''' <summary>
|
||||
''' 任务停止
|
||||
''' </summary>
|
||||
Public MustOverride Sub [Stop]() Implements IServiceTask.[Stop]
|
||||
|
||||
''' <summary>
|
||||
''' 任务重启
|
||||
''' </summary>
|
||||
Public MustOverride Sub Restart() Implements IServiceTask.Restart
|
||||
|
||||
''' <summary>
|
||||
''' 任务参数集合设置
|
||||
''' </summary>
|
||||
''' <param name="params">任务参数键值对</param>
|
||||
Public MustOverride Sub SetParams(params As Dictionary(Of String, String)) Implements IServiceTask.SetParams
|
||||
|
||||
''' <summary>
|
||||
''' 任务参数集合获取
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public MustOverride Function GetParams() As Dictionary(Of String, String) Implements IServiceTask.GetParams
|
||||
|
||||
End Class
|
||||
End Namespace
|
||||
356
UTS_Core/UTSModule/Service/TaskJsonParam.vb
Normal file
356
UTS_Core/UTSModule/Service/TaskJsonParam.vb
Normal file
@@ -0,0 +1,356 @@
|
||||
Imports Newtonsoft.Json
|
||||
Imports Newtonsoft.Json.Converters
|
||||
|
||||
Namespace UTSModule.Service
|
||||
|
||||
''' <summary>
|
||||
''' 应用程序与服务通讯协议类,可将此类序列化为Json字符串或反序列化Json字符串为此类
|
||||
'''
|
||||
''' 更改须知:
|
||||
''' 当需要增加不参与序列化的共有字段时,
|
||||
''' 在变量名上一行,参照JsonFilter字段增加JsonIgnore特性.
|
||||
'''
|
||||
''' 当需要增加命令名称类型时,
|
||||
''' 首先在CmdNamesEnum中增加枚举字段,
|
||||
''' 之后在FilterFiled函数中,增加指定命令名称中需要序列话的字段名数组.
|
||||
'''
|
||||
''' 2021-03-26
|
||||
''' 增加软件与设备通讯命令,用于注册软件 InitApp
|
||||
'''
|
||||
''' </summary>
|
||||
Public Class TaskJsonParam
|
||||
|
||||
'发送
|
||||
''' <summary>
|
||||
''' 发送控制命令时,通信的用户账号
|
||||
''' </summary>
|
||||
Public User As String
|
||||
|
||||
''' <summary>
|
||||
''' 发送控制命令时,执行程序名
|
||||
''' </summary>
|
||||
Public AppName As String
|
||||
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 通讯时使用的命令名称
|
||||
''' </summary>
|
||||
<JsonConverter(GetType(StringEnumConverter))>
|
||||
Public CmdName As CmdNamesEnum
|
||||
|
||||
''' <summary>
|
||||
''' 用户存储服务中任务信息
|
||||
''' </summary>
|
||||
Public TasksInfo As List(Of Dictionary(Of String, String))
|
||||
|
||||
''' <summary>
|
||||
''' 指定需要控制的任务名称集合
|
||||
''' </summary>
|
||||
Public TasksName As List(Of String)
|
||||
|
||||
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 回复命令时,指示命令执行的状态
|
||||
''' </summary>
|
||||
Public CmdStatus As String
|
||||
|
||||
''' <summary>
|
||||
''' 回复命令时,指定命令执行后的提示信息
|
||||
''' </summary>
|
||||
Public CmdMsg As String
|
||||
|
||||
''' <summary>
|
||||
''' 回复命令时,获取当前服务的版本号
|
||||
''' </summary>
|
||||
Public ServiceVersion As String
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 发送与恢复时,软件信息
|
||||
''' </summary>
|
||||
Public AppInfo As Dictionary(Of String, String)
|
||||
|
||||
''' <summary>
|
||||
''' 是否为回复数据
|
||||
''' </summary>
|
||||
<JsonIgnore>
|
||||
Public IsReply As Boolean
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 是否启用筛选功能,默认为启用筛选功能
|
||||
''' 此字段不参与Json序列化
|
||||
''' </summary>
|
||||
<JsonIgnore>
|
||||
Public Shared Property JsonFilter As Boolean
|
||||
|
||||
''' <summary>
|
||||
''' Json序列化时的显示格式,分行对齐显示或是单行无格式显示,默认单行显示
|
||||
''' 此字段不参与Json序列化
|
||||
''' </summary>
|
||||
<JsonIgnore>
|
||||
Public Shared Property JsonFormat As Formatting
|
||||
|
||||
|
||||
''' <summary> 任务命令执行状态列表 </summary>
|
||||
Enum CmdStatusEnum
|
||||
Pass
|
||||
Fail
|
||||
End Enum
|
||||
|
||||
|
||||
''' <summary> 任务命令列表 </summary>
|
||||
Enum CmdNamesEnum
|
||||
''' <summary>添加任务</summary>
|
||||
AddTasks
|
||||
|
||||
''' <summary>设置任务</summary>
|
||||
SetTasks
|
||||
|
||||
''' <summary>获取任务</summary>
|
||||
GetTasks
|
||||
|
||||
''' <summary>获取所有任务</summary>
|
||||
GetAllTasks
|
||||
|
||||
''' <summary>删除任务</summary>
|
||||
DeleteTasks
|
||||
|
||||
''' <summary>删除所有任务</summary>
|
||||
DeleteAllTasks
|
||||
|
||||
''' <summary>开启任务</summary>
|
||||
StartTasks
|
||||
|
||||
''' <summary>开启所有任务</summary>
|
||||
StartAllTasks
|
||||
|
||||
''' <summary>暂停任务</summary>
|
||||
StopTasks
|
||||
|
||||
''' <summary>暂停所有任务</summary>
|
||||
StopAllTasks
|
||||
|
||||
''' <summary>重启任务</summary>
|
||||
RestartTasks
|
||||
|
||||
''' <summary>重启所有任务</summary>
|
||||
RestartAllTasks
|
||||
|
||||
''' <summary>获取DataService版本信息</summary>
|
||||
GetServiceVersion
|
||||
|
||||
''' <summary>初始化APP,从服务端获取APP运行参数</summary>
|
||||
InitApp
|
||||
|
||||
''' <summary>App上报状态至服务</summary>
|
||||
AppMsg
|
||||
|
||||
''' <summary>服务下发通知App</summary>
|
||||
ServiceMsg
|
||||
End Enum
|
||||
|
||||
''' <summary>
|
||||
''' App上报时包含的类型
|
||||
''' </summary>
|
||||
Enum AppMsgTypes
|
||||
''' <summary>
|
||||
''' APP已启动
|
||||
''' </summary>
|
||||
AppStart
|
||||
|
||||
''' <summary>
|
||||
''' APP已关闭
|
||||
''' </summary>
|
||||
AppClose
|
||||
|
||||
''' <summary>
|
||||
''' APP站位切换
|
||||
''' </summary>
|
||||
StationChanged
|
||||
|
||||
''' <summary>
|
||||
''' APP心跳包
|
||||
''' </summary>
|
||||
AppAlive
|
||||
|
||||
''' <summary>
|
||||
''' Sn总表发生变化
|
||||
''' </summary>
|
||||
SnListChanged
|
||||
|
||||
End Enum
|
||||
|
||||
''' <summary>
|
||||
''' 服务下发时包含的类型
|
||||
''' </summary>
|
||||
Enum ServiceMsgTypes
|
||||
DbHostChange
|
||||
FtpHostChange
|
||||
End Enum
|
||||
|
||||
|
||||
Sub New()
|
||||
IsReply = False
|
||||
|
||||
JsonFilter = True
|
||||
JsonFormat = Formatting.None
|
||||
TasksName = New List(Of String)()
|
||||
TasksInfo = New List(Of Dictionary(Of String, String))
|
||||
AppInfo = New Dictionary(Of String, String)
|
||||
End Sub
|
||||
|
||||
Sub New(reply As Boolean)
|
||||
IsReply = reply
|
||||
|
||||
JsonFilter = True
|
||||
JsonFormat = Formatting.None
|
||||
TasksName = New List(Of String)()
|
||||
TasksInfo = New List(Of Dictionary(Of String, String))
|
||||
AppInfo = New Dictionary(Of String, String)
|
||||
End Sub
|
||||
|
||||
Sub New(cmd As CmdNamesEnum)
|
||||
IsReply = False
|
||||
|
||||
JsonFilter = True
|
||||
JsonFormat = Formatting.None
|
||||
TasksName = New List(Of String)()
|
||||
TasksInfo = New List(Of Dictionary(Of String, String))
|
||||
AppInfo = New Dictionary(Of String, String)
|
||||
|
||||
CmdName = cmd
|
||||
End Sub
|
||||
|
||||
Sub New(cmd As CmdNamesEnum, reply As Boolean)
|
||||
IsReply = reply
|
||||
|
||||
JsonFilter = True
|
||||
JsonFormat = Formatting.None
|
||||
TasksName = New List(Of String)()
|
||||
TasksInfo = New List(Of Dictionary(Of String, String))
|
||||
AppInfo = New Dictionary(Of String, String)
|
||||
|
||||
CmdName = cmd
|
||||
End Sub
|
||||
|
||||
|
||||
Sub New(cmd As CmdNamesEnum, usr As String, app As String)
|
||||
IsReply = False
|
||||
|
||||
JsonFilter = True
|
||||
JsonFormat = Formatting.None
|
||||
TasksName = New List(Of String)()
|
||||
TasksInfo = New List(Of Dictionary(Of String, String))
|
||||
AppInfo = New Dictionary(Of String, String)
|
||||
|
||||
CmdName = cmd
|
||||
User = usr
|
||||
AppName = app
|
||||
End Sub
|
||||
|
||||
Sub New(cmd As CmdNamesEnum, usr As String, app As String, reply As Boolean)
|
||||
IsReply = reply
|
||||
|
||||
JsonFilter = True
|
||||
JsonFormat = Formatting.None
|
||||
TasksName = New List(Of String)()
|
||||
TasksInfo = New List(Of Dictionary(Of String, String))
|
||||
AppInfo = New Dictionary(Of String, String)
|
||||
|
||||
CmdName = cmd
|
||||
User = usr
|
||||
AppName = app
|
||||
End Sub
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 序列化类中字段为Json字符串
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Shared Function SerializeToJson(jsonParam As TaskJsonParam) As String
|
||||
If JsonFilter Then
|
||||
Dim jSetting As New JsonSerializerSettings
|
||||
jSetting.ContractResolver = FilterFiled(jsonParam.CmdName, jsonParam.IsReply)
|
||||
Return JsonConvert.SerializeObject(jsonParam, JsonFormat, jSetting)
|
||||
Else
|
||||
Return JsonConvert.SerializeObject(jsonParam, JsonFormat)
|
||||
End If
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 反序列Json字符串填充当前类中
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Shared Function DeserializeFormJson(jsonString As String) As TaskJsonParam
|
||||
Return JsonConvert.DeserializeObject(Of TaskJsonParam)(jsonString)
|
||||
End Function
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 根据任务名称,设置参与序列化的字段名称
|
||||
''' 增加任务名称时,需要更新该函数
|
||||
''' </summary>
|
||||
''' <param name="cmd"></param>
|
||||
''' <returns></returns>
|
||||
Private Shared Function FilterFiled(cmd As CmdNamesEnum, Optional isReply As Boolean = False) As TaskJsonSettings
|
||||
If isReply Then
|
||||
Return FilterReplyFiled(cmd)
|
||||
Else
|
||||
Return FilterSendFiled(cmd)
|
||||
End If
|
||||
End Function
|
||||
|
||||
Private Shared Function FilterSendFiled(cmd As CmdNamesEnum) As TaskJsonSettings
|
||||
Dim lst As New List(Of String)
|
||||
lst.Add("User")
|
||||
lst.Add("AppName")
|
||||
lst.Add("CmdName")
|
||||
|
||||
Select Case cmd
|
||||
Case CmdNamesEnum.AddTasks,
|
||||
CmdNamesEnum.SetTasks
|
||||
|
||||
lst.Add("TasksInfo")
|
||||
Case CmdNamesEnum.DeleteTasks,
|
||||
CmdNamesEnum.GetTasks,
|
||||
CmdNamesEnum.StartTasks,
|
||||
CmdNamesEnum.StopTasks,
|
||||
CmdNamesEnum.RestartTasks
|
||||
|
||||
lst.Add("TasksName")
|
||||
Case CmdNamesEnum.InitApp, CmdNamesEnum.AppMsg, CmdNamesEnum.ServiceMsg
|
||||
lst.Add("AppInfo")
|
||||
Case CmdNamesEnum.GetServiceVersion
|
||||
lst.Add("ServiceVersion")
|
||||
End Select
|
||||
|
||||
Return New TaskJsonSettings(lst.ToArray())
|
||||
End Function
|
||||
|
||||
|
||||
Private Shared Function FilterReplyFiled(cmd As CmdNamesEnum) As TaskJsonSettings
|
||||
Dim lst As New List(Of String)
|
||||
lst.Add("CmdName")
|
||||
lst.Add("CmdStatus")
|
||||
lst.Add("CmdMsg")
|
||||
|
||||
Select Case cmd
|
||||
Case CmdNamesEnum.GetTasks
|
||||
lst.Add("TasksInfo")
|
||||
Case CmdNamesEnum.GetAllTasks
|
||||
lst.Add("TasksInfo")
|
||||
Case CmdNamesEnum.GetServiceVersion
|
||||
lst.Add("ServiceVersion")
|
||||
Case CmdNamesEnum.InitApp, CmdNamesEnum.AppMsg, CmdNamesEnum.ServiceMsg
|
||||
lst.Add("AppInfo")
|
||||
End Select
|
||||
|
||||
Return New TaskJsonSettings(lst.ToArray())
|
||||
End Function
|
||||
End Class
|
||||
|
||||
End Namespace
|
||||
43
UTS_Core/UTSModule/Service/TaskJsonSettings.vb
Normal file
43
UTS_Core/UTSModule/Service/TaskJsonSettings.vb
Normal file
@@ -0,0 +1,43 @@
|
||||
Imports Newtonsoft.Json
|
||||
Imports Newtonsoft.Json.Serialization
|
||||
|
||||
Namespace UTSModule.Service
|
||||
''' <summary>
|
||||
''' 针对服务任务的Json设置
|
||||
''' 当前作用:筛选指定类中字段参与Json序列化
|
||||
''' </summary>
|
||||
Public Class TaskJsonSettings
|
||||
Inherits DefaultContractResolver
|
||||
|
||||
''' <summary>
|
||||
''' 需要操作的字段名
|
||||
''' 与_retain参数结合使用,实现指定类中某些字段的Json序列化
|
||||
''' </summary>
|
||||
Private ReadOnly _name() As String
|
||||
|
||||
''' <summary>
|
||||
''' 是否显示指定字段
|
||||
''' 为真时,仅显示指定的字段名
|
||||
''' 为假时,仅不显示指定的字段名
|
||||
''' </summary>
|
||||
Private ReadOnly _retain As Boolean
|
||||
|
||||
Sub New(name2() As String, Optional retain2 As Boolean = True)
|
||||
_name = name2
|
||||
_retain = retain2
|
||||
End Sub
|
||||
|
||||
|
||||
Protected Overrides Function CreateProperties(type As Type, memberSerialization As MemberSerialization) As IList(Of JsonProperty)
|
||||
Dim lst As IList(Of JsonProperty) = MyBase.CreateProperties(type, memberSerialization)
|
||||
|
||||
Return lst.Where(Function(x As JsonProperty)
|
||||
If _retain Then
|
||||
Return _name.Contains(x.PropertyName)
|
||||
Else
|
||||
Return Not _name.Contains(x.PropertyName)
|
||||
End If
|
||||
End Function).ToList()
|
||||
End Function
|
||||
End Class
|
||||
End Namespace
|
||||
Reference in New Issue
Block a user