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