Files
effector.main/Effector.Main/MainWindow/MainWindowVM.vb
T
2025-06-12 20:34:31 +02:00

261 lines
9.8 KiB
VB.net

Imports System.IO
Imports System.Runtime.InteropServices
Imports System.Security
Imports System.Windows.Threading
Imports Effector.Plugin.Lib
Imports Effector.Plugin.Interface
Public Module EgtInterface
#If DEBUG Then
Const EgtIntDll32 As String = "EgtBasisD32.dll"
#Else
Const EgtIntDll32 As String = "EgtBasisR32.dll"
#End If
<DllImport(EgtIntDll32, CharSet:=CharSet.Unicode, EntryPoint:="EgtSetKey"), SuppressUnmanagedCodeSecurity()>
Public Function EgtSetKey(sKey As String) As Boolean
End Function
<DllImport(EgtIntDll32, CharSet:=CharSet.Unicode, EntryPoint:="EgtGetKeyLevel"), SuppressUnmanagedCodeSecurity()>
Public Function EgtGetKeyLevel(nProd As Integer, nVer As Integer, nLev As Integer, ByRef nKLev As Integer) As Boolean
End Function
<DllImport(EgtIntDll32, CharSet:=CharSet.Unicode, EntryPoint:="EgtGetKeyOptions"), SuppressUnmanagedCodeSecurity()>
Public Function EgtGetKeyOptions(nProd As Integer, nVer As Integer, nLev As Integer,
ByRef nOpt1 As UInteger, ByRef nOpt2 As UInteger) As Boolean
End Function
<DllImport(EgtIntDll32, CharSet:=CharSet.Unicode, EntryPoint:="EgtGetKeyLeftDays"), SuppressUnmanagedCodeSecurity()>
Public Function EgtGetKeyLeftDays(ByRef nLeftDays As Integer) As Boolean
End Function
<DllImport(EgtIntDll32, CharSet:=CharSet.Unicode, EntryPoint:="EgtGetKeyAssLeftDays"), SuppressUnmanagedCodeSecurity()>
Public Function EgtGetKeyAssLeftDays(ByRef nAssLeftDays As Integer) As Boolean
End Function
<DllImport(EgtIntDll32, CharSet:=CharSet.Unicode, EntryPoint:="EgtGetKeyOptLeftDays"), SuppressUnmanagedCodeSecurity()>
Public Function EgtGetKeyOptLeftDays(ByRef nOptLeftDays As Integer) As Boolean
End Function
<DllImport(EgtIntDll32, CharSet:=CharSet.Unicode, EntryPoint:="EgtFreeMemory"), SuppressUnmanagedCodeSecurity()>
Public Function EgtFreeMemory(sB As IntPtr) As Boolean
End Function
<DllImport(EgtIntDll32, CharSet:=CharSet.Unicode, EntryPoint:="EgtLoadMessages"), SuppressUnmanagedCodeSecurity()>
Public Function EgtLoadMessages(sMsgFilePath As String) As Boolean
End Function
<DllImport(EgtIntDll32, CharSet:=CharSet.Unicode, EntryPoint:="EgtGetMsg"), SuppressUnmanagedCodeSecurity()>
Private Function EgtGetMsg_32(nId As Integer) As IntPtr
End Function
Public Function EgtMsg(nId As Integer) As String
Return Marshal.PtrToStringUni(EgtGetMsg_32(nId))
' Non è necessario liberare la memoria nativa perchè usa un buffer statico
End Function
<DllImport(EgtIntDll32, CharSet:=CharSet.Unicode, EntryPoint:="EgtInitLogger"), SuppressUnmanagedCodeSecurity()>
Public Function EgtInitLogger( nDebug As Integer, sLogFilePath As String) As Boolean
End Function
<DllImport(EgtIntDll32, CharSet:=CharSet.Unicode, EntryPoint:="EgtOutLog"), SuppressUnmanagedCodeSecurity()>
Private Function EgtOutLog(sMsg As String, nDebugLevel As Integer) As Boolean
End Function
<DllImport(EgtIntDll32, CharSet:=CharSet.Unicode, EntryPoint:="EgtGetStringUtf8FromIni"), SuppressUnmanagedCodeSecurity()>
Private Function EgtGetStringUtf8FromIni_32(sSec As String, sKey As String, sDef As String, ByRef sVal As IntPtr, sIniFile As String) As Boolean
End Function
Public Function EgtGetStringUtf8FromIni(sSec As String, sKey As String, sDef As String, ByRef sVal As String, sIniFile As String) As Boolean
Dim psVal As IntPtr
Dim bOk As Boolean = EgtGetStringUtf8FromIni_32(sSec, sKey, sDef, psVal, sIniFile)
If bOk Then
sVal = Marshal.PtrToStringUni(psVal)
EgtFreeMemory(psVal)
Else
sVal = String.Empty
End If
Return bOk
End Function
<DllImport(EgtIntDll32, CharSet:=CharSet.Unicode, EntryPoint:="EgtWriteStringUtf8ToIni"), SuppressUnmanagedCodeSecurity()>
Public Function EgtWriteStringUtf8toIni(sSec As String, sKey As String, sVal As String, sIniFile As String) As Boolean
End Function
End Module
Public Class MainWindowVM
Inherits VMBase
Friend m_PluginTestInfo As Integer = 764
' Riferimento al Model della MainWindow
Private m_MainWindowM As MainWindowM
Friend ReadOnly Property MainWindowM As MainWindowM
Get
Return m_MainWindowM
End Get
End Property
' Riferimento al gestore delle macchine a stati
Private m_FiniteStateMachineManager As FiniteStateMachineManager
Friend ReadOnly Property FiniteStateMachineManager As FiniteStateMachineManager
Get
Return m_FiniteStateMachineManager
End Get
End Property
' Riferimento al gestore del della comunicazione macchine
Private m_MachineManager As MachineManager
Friend ReadOnly Property MachineManager As MachineManager
Get
Return m_MachineManager
End Get
End Property
' Riferimento al gestore del server Redis
Private m_RedisManager As RedisManager
Friend ReadOnly Property RedisManager As RedisManager
Get
Return m_RedisManager
End Get
End Property
Private m_ContentPanel As Panel
Public ReadOnly Property ContentPanel As Panel
Get
Return m_ContentPanel
End Get
End Property
Private m_ContentMenu As Panel
Public ReadOnly Property ContentMenu As Panel
Get
Return m_ContentMenu
End Get
End Property
Private m_SplashScreen_Timer As New DispatcherTimer
Private m_WaitAfterRender As Integer = 0
Private m_Window_Opacity As Double = 0.2
Public ReadOnly Property Window_Opacity As Double
Get
Return m_Window_Opacity
End Get
End Property
' Titolo
Public ReadOnly Property sTitle As String
Get
Return "Effector"
End Get
End Property
Sub New()
' Avvio l'inizializzazione della mappa passandogli il riferimento al MainWindowVM
Map.BeginInit(Me)
' imposto e avvio contatore SplashScreen
m_SplashScreen_Timer.Interval = New TimeSpan(0, 0, 0, 0, 500)
AddHandler m_SplashScreen_Timer.Tick, AddressOf SplashScreenTimer_Tick
If Not IsNothing(Map.refSplashScreen) Then
m_SplashScreen_Timer.Start()
End If
' Creo Model della MainWindow
m_MainWindowM = New MainWindowM
' creo gestore della comunicazione
m_MachineManager = New MachineManager
m_MachineManager.Init()
' recupero nome del plugin
Dim sPluginName As String = ""
If GetMainPrivateProfileString(S_GENERAL, K_PLUGINNAME, "", sPluginName) > 0 AndAlso Not String.IsNullOrWhiteSpace(sPluginName) Then
' aggiungo Dictionary del plugin
Dim PluginDictionary As IPluginControl = GetDictionary(Of IPluginControl)(sPluginName)
Application.Current.Resources.MergedDictionaries.Add(PluginDictionary)
'' aggiungo MainMenu dal plugin
Dim PluginMainMenu As IPluginControl = GetControlByName(Of IPluginControl)(sPluginName, "MainMenu")
m_ContentMenu = PluginMainMenu
'' aggiungo manager delle funzioni Lua del Plugin
Dim PluginLuaManager As IPluginLuaManager = Map.refMainWindowVM.GetLuaManager(Of IPluginLuaManager)(sPluginName)
PluginLuaManager.PlgInit(LuaManager.state)
' aggiungo Project dal plugin
Dim ProjectPlugin As IPluginControl = GetControlByName(Of IPluginControl)(sPluginName, "Project")
m_ContentPanel = ProjectPlugin
End If
' creo variabile globale Effector per dati programma
LuaManager.CreateGlobTable("EFFECTOR")
LuaManager.SetGlobVar("EFFECTOR.INIPATH", Map.refMainWindowVM.MainWindowM.sConfigDir & "\" & INI_FILE_NAME)
' creo gestore delle macchine a stati
m_FiniteStateMachineManager = New FiniteStateMachineManager
If GetMainPrivateProfileInt(S_REDIS, K_ENABLED, 0) = 1 Then
m_RedisManager = New RedisManager
End If
End Sub
Friend Sub ContentRendered()
' segno su contatore splashscreen render finito
m_WaitAfterRender = 1
End Sub
Friend Function OnClose() As Boolean
If Not IsNothing(FiniteStateMachineManager) Then FiniteStateMachineManager.ResetFiniteStateMachineTimer()
LuaManager.Close()
' chiudo comunicazione macchine
m_MachineManager.Close()
' Termino il Model
m_MainWindowM.Close()
Return True
End Function
Private Sub SplashScreenTimer_Tick()
If m_WaitAfterRender > 1 Then
m_Window_Opacity = 1
NotifyPropertyChanged(NameOf(Window_Opacity))
' chiudo SplashScreen
Map.refSplashScreen.Close()
Map.SetRefSplashScreen(Nothing)
m_SplashScreen_Timer.Stop()
ElseIf m_WaitAfterRender > 0 Then
m_WaitAfterRender += 1
End If
End Sub
#Region "Plugin"
Private m_Loader As MEFLoader = New MEFLoader()
Private Function GetPathByName(sPluginFileName As String) As String
Return MainWindowM.sDataRoot & "\Plugin\" & sPluginFileName
End Function
Friend Function GetControlByName(Of T)(sPluginFileName As String, sPluginTagName As String) As Panel
Dim PlugInControl As Object = m_Loader.LoadByTag(Of T)(GetPathByName(sPluginFileName), sPluginTagName).FirstOrDefault()
Dim PluginPanel As Panel = Nothing
If PlugInControl.GetType().BaseType.FullName = GetType(Panel).FullName OrElse PlugInControl.GetType().BaseType.FullName = GetType(Grid).FullName Then
PluginPanel = TryCast(PlugInControl, Panel)
End If
Return PluginPanel
End Function
Friend Function GetDictionary(Of T)(sPluginFileName As String) As ResourceDictionary
Dim DictionaryControl As Object = m_Loader.LoadByTag(Of T)(GetPathByName(sPluginFileName), "Dictionary").FirstOrDefault()
Dim Dictionary As ResourceDictionary = Nothing
Dictionary = TryCast(DictionaryControl, ResourceDictionary)
Return DictionaryControl
End Function
Friend Function GetLuaManager(Of T)(sPluginFileName As String) As IPluginLuaManager
Dim LuaManager As Object = m_Loader.LoadByTag(Of T)(GetPathByName(sPluginFileName), "LuaManager").FirstOrDefault()
Return LuaManager
End Function
#End Region ' Plugin
End Class