Imports Microsoft.Win32.Registry

Namespace UTSModule

    ''' <summary>
    ''' 从注册表中获取UTS软件运行所需的变量值
    '''
    ''' 注意：
    ''' 使用时避免反复读写注册表,应将获取的字段值保存至变量中;
    ''' 获取字段失败时会程序异,使用时做好异常保护;
    ''' 只读的变量是从注册表中获取的字段根据规则拼接而成的;
    ''' </summary>
    Public Class UtsRegistry
        Enum RegistryDirNameEnum
            ''' <summary>数据服务</summary>
            AUTS_DataService

            ''' <summary>更新服务</summary>
            AUTS_UpdateService

            ''' <summary>鉴权文件</summary>
            License

            ''' <summary>数据库文件夹</summary>
            LocalDb

            ''' <summary>产品相关</summary>
            Product
        End Enum


        ''' <summary>
        ''' 注册表存放根路径配置信息
        ''' </summary>
        Private Shared ReadOnly AutsRegistryRootPath As String = $"software\AUTS"


        ''' <summary>
        ''' 获取注册表是否存在
        ''' </summary>
        ''' <returns></returns>
        Public Shared Function RootDirExists() As Boolean
            Return LocalMachine.OpenSubKey(AutsRegistryRootPath) IsNot Nothing
        End Function


        ''' <summary>
        ''' 获取和设置UTS软件存放位置的根文件路径,如C:/AUTS
        ''' </summary>
        ''' <returns></returns>
        Public Shared Property RootPath() As String
            Get
                Return LocalMachine.OpenSubKey($"{AutsRegistryRootPath}").GetValue("Path", "").ToString()
            End Get

            Set(value As String)
                LocalMachine.CreateSubKey($"{AutsRegistryRootPath}").SetValue("Path", value, Microsoft.Win32.RegistryValueKind.String)
            End Set
        End Property


        ''' <summary>
        ''' 获取和设置UTS软件Ftp所在地址
        ''' </summary>
        ''' <returns></returns>
        Public Shared Property FtpHost() As String
            Get
                Return LocalMachine.OpenSubKey($"{AutsRegistryRootPath}").GetValue("FtpHost", "").ToString()
            End Get
            Set(value As String)
                LocalMachine.CreateSubKey($"{AutsRegistryRootPath}").SetValue("FtpHost", value, Microsoft.Win32.RegistryValueKind.String)
            End Set
        End Property

        ''' <summary>
        ''' 获取和设置数据库URL所在地址
        ''' </summary>
        ''' <returns></returns>
        Public Shared Property DbHost() As String
            Get
                Return LocalMachine.OpenSubKey($"{AutsRegistryRootPath}").GetValue("DbHost", "").ToString()
            End Get
            Set(value As String)
                LocalMachine.CreateSubKey($"{AutsRegistryRootPath}").SetValue("DbHost", value, Microsoft.Win32.RegistryValueKind.String)
            End Set
        End Property

        ''' <summary>
        ''' 子网名称，用于过滤多播时非相同子网名称数据包
        ''' </summary>
        ''' <returns></returns>
        Public Shared Property BarnchNet As String
            Get
                Return LocalMachine.OpenSubKey($"{AutsRegistryRootPath}").GetValue("BarnchNet", "").ToString()
            End Get
            Set(value As String)
                LocalMachine.CreateSubKey($"{AutsRegistryRootPath}").SetValue("BarnchNet", value, Microsoft.Win32.RegistryValueKind.String)
            End Set
        End Property

        ''' <summary>
        ''' 设备在子网中的角色,0为普通设备，1为服务器设备
        ''' </summary>
        ''' <returns></returns>
        Public Shared Property Roles As String
            Get
                Return LocalMachine.OpenSubKey($"{AutsRegistryRootPath}").GetValue("Roles", "0").ToString()
            End Get
            Set(value As String)
                LocalMachine.CreateSubKey($"{AutsRegistryRootPath}").SetValue("Roles", value, Microsoft.Win32.RegistryValueKind.String)
            End Set
        End Property


        ''' <summary>
        ''' 获取和设置License文件名
        ''' </summary>
        ''' <returns></returns>
        Public Shared Property LicenseFileName() As String
            Get
                Return LocalMachine.OpenSubKey($"{AutsRegistryRootPath}\{RegistryDirNameEnum.License}").GetValue("Name", "").ToString()
            End Get

            Set(value As String)
                LocalMachine.CreateSubKey($"{AutsRegistryRootPath}\{RegistryDirNameEnum.License}").SetValue("Name", value, Microsoft.Win32.RegistryValueKind.String)
            End Set
        End Property

        Public Shared ReadOnly Property LicenseDirPath() As String
            Get
                Return $"{RootPath}\{RegistryDirNameEnum.License}"
            End Get
        End Property

        Public Shared ReadOnly Property LicenseFilePath() As String
            Get
                Return $"{LicenseDirPath}\{LicenseFileName}"
            End Get
        End Property


        ''' <summary>
        ''' 获取和设置DataService名称
        ''' </summary>
        ''' <returns></returns>
        Public Shared Property DataServiceName() As String
            Get
                Return LocalMachine.OpenSubKey($"{AutsRegistryRootPath}\{RegistryDirNameEnum.AUTS_DataService}").GetValue("Name", "").ToString()
            End Get

            Set(value As String)
                LocalMachine.CreateSubKey($"{AutsRegistryRootPath}\{RegistryDirNameEnum.AUTS_DataService}").SetValue("Name", value, Microsoft.Win32.RegistryValueKind.String)
            End Set
        End Property


        ''' <summary>
        ''' 获取和设置DataService版本信息
        ''' </summary>
        ''' <returns></returns>
        Public Shared Property DataServiceVersion() As String
            Get
                Return LocalMachine.OpenSubKey($"{AutsRegistryRootPath}\{RegistryDirNameEnum.AUTS_DataService}").GetValue("Version", "").ToString()
            End Get
            Set(value As String)
                LocalMachine.CreateSubKey($"{AutsRegistryRootPath}\{RegistryDirNameEnum.AUTS_DataService}").SetValue("Version", value, Microsoft.Win32.RegistryValueKind.String)

            End Set
        End Property

        ''' <summary>
        ''' 获取或设置服务的索引,默认值为-1
        ''' </summary>
        ''' <returns></returns>
        Public Shared Property DataServiceIndex() As String
            Get
                Return LocalMachine.OpenSubKey($"{AutsRegistryRootPath}\{RegistryDirNameEnum.AUTS_DataService}").GetValue("Index", "-1").ToString()
            End Get

            Set(value As String)
                LocalMachine.CreateSubKey($"{AutsRegistryRootPath}\{RegistryDirNameEnum.AUTS_DataService}").SetValue("Index", value, Microsoft.Win32.RegistryValueKind.String)
            End Set
        End Property


        ''' <summary>
        ''' 获取或设置服务的通讯端口,默认端口55533
        ''' </summary>
        ''' <returns></returns>
        Public Shared Property DataServicePort() As String
            Get
                Return LocalMachine.OpenSubKey($"{AutsRegistryRootPath}\{RegistryDirNameEnum.AUTS_DataService}").GetValue("Port", "55533").ToString()
            End Get

            Set(value As String)
                LocalMachine.CreateSubKey($"{AutsRegistryRootPath}\{RegistryDirNameEnum.AUTS_DataService}").SetValue("Port", value, Microsoft.Win32.RegistryValueKind.String)
            End Set
        End Property



        ''' <summary>
        ''' 服务注册时使用的自定义标识名
        ''' </summary>
        ''' <returns></returns>
        Public Shared Property DataServiceAlias() As String
            Get
                Return LocalMachine.OpenSubKey($"{AutsRegistryRootPath}\{RegistryDirNameEnum.AUTS_DataService}").GetValue("Alias", "").ToString()
            End Get

            Set(value As String)
                LocalMachine.CreateSubKey($"{AutsRegistryRootPath}\{RegistryDirNameEnum.AUTS_DataService}").SetValue("Alias", value, Microsoft.Win32.RegistryValueKind.String)
            End Set
        End Property

        ''' <summary>
        ''' 存放数据服务文件夹,不包含版本文件夹
        ''' </summary>
        ''' <returns></returns>
        Public Shared ReadOnly Property DataServiceDirPath() As String
            Get
                Return $"{RootPath}\{RegistryDirNameEnum.AUTS_DataService}"
            End Get
        End Property

        ''' <summary>
        ''' 数据服务版本文件夹
        ''' </summary>
        ''' <returns></returns>
        Public Shared ReadOnly Property DataServiceVersionDirPath() As String
            Get
                Return $"{RootPath}\{RegistryDirNameEnum.AUTS_DataService}"
            End Get
        End Property


        ''' <summary>
        ''' 获取数据服务执行文件的路径
        ''' </summary>
        ''' <returns></returns>
        Public Shared ReadOnly Property DataServiceFilePath() As String
            Get
                Return $"{RootPath}\{RegistryDirNameEnum.AUTS_DataService}\{DataServiceVersion}\{DataServiceName}.exe"
            End Get
        End Property



        ''' <summary>
        ''' 获取和设置UpdateService名称
        ''' </summary>
        ''' <returns></returns>
        Public Shared Property UpdateServiceName() As String
            Get
                Return LocalMachine.OpenSubKey($"{AutsRegistryRootPath}\{RegistryDirNameEnum.AUTS_UpdateService}").GetValue("Name", "").ToString()
            End Get
            Set(value As String)
                LocalMachine.CreateSubKey($"{AutsRegistryRootPath}\{RegistryDirNameEnum.AUTS_UpdateService}").SetValue("Name", value, Microsoft.Win32.RegistryValueKind.String)
            End Set
        End Property


        ''' <summary>
        ''' 获取和设置UpdateService版本信息
        ''' </summary>
        ''' <returns></returns>
        Public Shared Property UpdateServiceVersion() As String
            Get
                Return LocalMachine.OpenSubKey($"{AutsRegistryRootPath}\{RegistryDirNameEnum.AUTS_UpdateService}").GetValue("Version", "").ToString()
            End Get
            Set(value As String)
                LocalMachine.CreateSubKey($"{AutsRegistryRootPath}\{RegistryDirNameEnum.AUTS_UpdateService}").SetValue("Version", value, Microsoft.Win32.RegistryValueKind.String)

            End Set
        End Property

        ''' <summary>
        ''' 获取和设置UpdateService文件夹路径,不包含版本文件夹
        ''' </summary>
        ''' <returns></returns>
        Public Shared ReadOnly Property UpdateServiceDirPath() As String
            Get
                Return $"{RootPath}\{RegistryDirNameEnum.AUTS_UpdateService}"
            End Get
        End Property

        ''' <summary>
        ''' 获取和设置UpdateService当前版本文件夹路径
        ''' </summary>
        ''' <returns></returns>
        Public Shared ReadOnly Property UpdateServiceVersionDirPath() As String
            Get
                Return $"{RootPath}\{RegistryDirNameEnum.AUTS_UpdateService}\{UpdateServiceVersion}"
            End Get
        End Property


        ''' <summary>
        ''' 获取数据服务执行文件的路径
        ''' </summary>
        ''' <returns></returns>
        Public Shared ReadOnly Property UpdateServiceFilePath() As String
            Get
                Return $"{RootPath}\{RegistryDirNameEnum.AUTS_UpdateService}\{UpdateServiceVersion}\{UpdateServiceName}.exe"
            End Get
        End Property

        ''' <summary>
        ''' 本地数据库文件夹
        ''' </summary>
        ''' <returns></returns>
        Public Shared ReadOnly Property LocalDbDirPath() As String
            Get
                Return $"{RootPath}\{RegistryDirNameEnum.LocalDb}"
            End Get
        End Property

        ''' <summary>
        ''' 本地产品图像文件夹
        ''' </summary>
        ''' <returns></returns>
        Public Shared ReadOnly Property ProductDirPath() As String
            Get
                Return $"{RootPath}\{RegistryDirNameEnum.Product}"
            End Get
        End Property

    End Class
End Namespace