Files
2025-07-28 14:42:27 +02:00

479 lines
19 KiB
VB.net

Imports System.Threading
Imports System.Windows.Threading
Imports System.Math
Imports System.IO
Imports EgtUILib
Imports EgtWPFLib5
Imports EgtPHOTOLib
Imports System.Windows.Forms.Integration
Public Class MainWindowM
#Region "FIELDS"
' Timer
Private m_TimerIsBusy As Boolean = False
Private m_RefreshTimer As New DispatcherTimer
' Per lettura modifica direttorio
Private fsWatch As FileSystemWatcher
Private m_RefreshNewFileClick As New DispatcherTimer
Private m_bNewFileClick As Boolean = False
Private m_sNewFileClickPath As String = String.Empty
Private m_sDataRoot As String = String.Empty
Friend ReadOnly Property sDataRoot As String
Get
Return m_sDataRoot
End Get
End Property
Private m_sConfigDir As String = String.Empty
Public ReadOnly Property sConfigDir As String
Get
Return m_sConfigDir
End Get
End Property
Private m_nDebug As Integer = 0
Private m_objMutex As Mutex
Private m_bFirstInstance As Boolean = False
Friend ReadOnly Property bFirstInstance As Boolean
Get
Return m_bFirstInstance
End Get
End Property
Private m_nInstance As Integer = 0
Friend ReadOnly Property nInstance As Integer
Get
Return m_nInstance
End Get
End Property
Private m_nUserLevel As Integer = 1
Friend ReadOnly Property nUserLevel As Integer
Get
Return m_nUserLevel
End Get
End Property
Private m_nKeyLevel As Integer = 0
Friend ReadOnly Property nKeyLevel As Integer
Get
Return m_nKeyLevel
End Get
End Property
Private m_nKeyOptions As UInteger = 0
Friend ReadOnly Property nKeyOptions As Integer
Get
Return m_nKeyOptions
End Get
End Property
Private m_sTempDir As String
Friend ReadOnly Property sTempDir As String
Get
Return m_sTempDir
End Get
End Property
Private m_sLogFile As String
Friend ReadOnly Property sLogFile As String
Get
Return m_sLogFile
End Get
End Property
Friend ReadOnly Property sImgAutoDir As String
Get
Return m_sDataRoot & "\" & IMGAUTO_DIR
End Get
End Property
Private m_sPhotoDir As String
Friend ReadOnly Property sPhotoDir As String
Get
Return m_sPhotoDir
End Get
End Property
' l'indirizzo del file può essere modificato dalla pagina OptionPage
Private m_TemplateFilePrinter As String
Friend Property sTemplateFilePrinter As String
Get
Return m_TemplateFilePrinter
End Get
Set(value As String)
m_TemplateFilePrinter = value
' salvo il nome del nuovo file template in uso
WriteMainPrivateProfileString(S_PRINTER, K_TEMPLATE, m_TemplateFilePrinter)
' comunico alla librearia il nuovo nome del file in uso dal programma
EgtPHOTOLib.MainData.SetTemplateFilePrinter(m_TemplateFilePrinter)
End Set
End Property
Private m_DatFilePrinter As String
Friend ReadOnly Property DataFilePrinter As String
Get
Return m_DatFilePrinter
End Get
End Property
Private m_ZebraUtilitiesExe As String
Friend ReadOnly Property ZebraUtilitiesExe As String
Get
Return m_ZebraUtilitiesExe
End Get
End Property
Private m_PrinterIsVisible As Boolean = False
Friend ReadOnly Property PrinterIsVisible As Boolean
Get
Return m_PrinterIsVisible
End Get
End Property
Private m_sBackUpDir As String
Friend ReadOnly Property sBackUpDir As String
Get
Return m_sBackUpDir
End Get
End Property
Private m_EnableTextId As Boolean = True
Public ReadOnly Property EnableTextId As Boolean
Get
Return m_EnableTextId
End Get
End Property
Private m_LastId As Integer = 10000
Public Property nLastId As Integer
Get
Return m_LastId
End Get
Set(value As Integer)
m_LastId = value
End Set
End Property
Private m_bCapsLock As Boolean
Public ReadOnly Property bCapsLock As Boolean
Get
Return m_bCapsLock
End Get
End Property
#End Region ' FIELDS
#Region "CONSTRUCTOR"
Sub New()
InitializeEgtEnvironment()
End Sub
#End Region ' CONSTRUCTOR
#Region "METHODS"
Private Sub InitializeEgtEnvironment()
' Impostazione path radice per i dati
m_sDataRoot = System.AppDomain.CurrentDomain.BaseDirectory
If EgtUILib.GetPrivateProfileString(S_DATA, K_DATAROOT, "", m_sDataRoot, m_sDataRoot & "\" & DAT_FILE_NAME) = 0 Then
m_sDataRoot = System.AppDomain.CurrentDomain.BaseDirectory
End If
' Impostazione direttorio di configurazione
m_sConfigDir = m_sDataRoot & "\" & CONF_DIR
' Impostazione direttorio per file temporanei
m_sTempDir = m_sDataRoot & "\" & TEMP_DIR
Directory.CreateDirectory(m_sTempDir)
' Impostazione path Ini file
IniFile.m_sIniFile = m_sConfigDir & "\" & INI_FILE_NAME
' Impostazione direttorio per backup
m_sBackUpDir = m_sDataRoot & "\" & BACKUP_DIR
Directory.CreateDirectory(m_sBackUpDir)
' Verifico indice di istanza
ManageInstance()
' Imposto tipo di chiave
EgtSetLockType(KEY_TYPE.HW)
' Leggo e imposto chiave di protezione
Dim sLicFileName As String = String.Empty
GetMainPrivateProfileString(S_GENERAL, K_LICENCE, LIC_FILE_NAME, sLicFileName)
Dim sLicFile As String = m_sConfigDir & "\" & sLicFileName
Dim sKey As String = String.Empty
EgtUILib.GetPrivateProfileString(S_LICENCE, K_KEY, "", sKey, sLicFile)
EgtSetKey(sKey)
Dim bNetHwKey As Boolean = (GetMainPrivateProfileInt(S_GENERAL, K_NETKEY, 0) = 1)
EgtSetNetHwKey(bNetHwKey)
' Recupero livello e opzioni della chiave
Dim bKey As Boolean = EgtGetKeyLevel(9423, 2707, 1, m_nKeyLevel) And
EgtGetKeyOptions(9423, 2707, 1, m_nKeyOptions)
' Verifico abilitazione prodotto
Dim bProd As Boolean = GetKeyOption(KEY_OPT.PHOTO_BASE)
' Inizializzazione generale di EgtInterface
m_nDebug = GetMainPrivateProfileInt(S_GENERAL, K_DEBUG, 0)
m_sLogFile = m_sTempDir & "\" & GENLOG_FILE_NAME.Replace("#", m_nInstance.ToString())
Dim sLogMsg As String = "User " & Environment.MachineName & "\" & Environment.UserName & " (" & m_nInstance.ToString() & ")" & vbLf &
My.Application.Info.Title.ToString() & " ver. " &
My.Application.Info.Version.Major.ToString() &
"." & My.Application.Info.Version.Minor.ToString() &
(ChrW(97 - 1 + My.Application.Info.Version.Build)).ToString() &
My.Application.Info.Version.Revision.ToString()
EgtInit(m_nDebug, m_sLogFile, sLogMsg)
EgtSetTempDir(m_sTempDir)
EgtSetIniFile(IniFile.m_sIniFile)
' Leggo direttorio dei messaggi (se manca uso direttorio di configurazione)
Dim sMsgDir As String = String.Empty
If GetMainPrivateProfileString(S_GENERAL, K_MESSAGESDIR, "", sMsgDir) = 0 Then
sMsgDir = m_sConfigDir
End If
' Leggo lingua corrente
Dim sLanguage As String = String.Empty
GetMainPrivateProfileString(S_GENERAL, K_MESSAGES, "", sLanguage)
' Recupero nome file dei messaggi della lingua corrente
Dim sMsgName As String = "EgalTechIta.txt"
Dim nIndex As Integer = 1
While True
Dim ReadLanguage As Language = GetMainPrivateProfileLanguage(S_LANGUAGES, K_LANGUAGE & nIndex)
If IsNothing(ReadLanguage) Then Exit While
If String.Compare(ReadLanguage.Name, sLanguage, True) = 0 Then
sMsgName = ReadLanguage.FilePath
Exit While
End If
nIndex += 1
End While
' Leggo file messaggi
Dim sMsgFilePath As String = sMsgDir & "\" & sMsgName
If Not EgtLoadMessages(sMsgFilePath) Then
EgtOutLog("Error in EgtLoadMessages")
End If
' Leggo e imposto unità di misura per interfaccia utente
EgtSetUiUnits(GetMainPrivateProfileInt(S_GENERAL, K_MMUNITS, 1) <> 0)
' Leggo e imposto livello utilizzatore
m_nUserLevel = Math.Min(m_nKeyLevel, GetMainPrivateProfileInt(S_GENERAL, K_USERLEVEL, 1))
' Imposto dir font Nfe e font default
Dim sNfeDir As String = String.Empty
GetMainPrivateProfileString(S_GEOMDB, K_NFEFONTDIR, "", sNfeDir)
Dim sDefFont As String = String.Empty
GetMainPrivateProfileString(S_GEOMDB, K_DEFAULTFONT, "", sDefFont)
EgtSetFont(sNfeDir, sDefFont)
' imposto dir di default per libreria Lua e lancio libreria di base
Dim sLuaLibsDir As String = String.Empty
GetMainPrivateProfileString(S_LUA, K_LIBSDIR, "", sLuaLibsDir)
EgtSetLuaLibs(sLuaLibsDir)
Dim sLuaBaseLib As String = String.Empty
GetMainPrivateProfileString(S_LUA, K_BASELIB, "EgtBase", sLuaBaseLib)
EgtLuaRequire(sLuaBaseLib)
' Leggo nome cartella delle foto
GetMainPrivateProfileString(S_GENERAL, K_PHOTODIR, sDataRoot & "\Data", m_sPhotoDir)
' leggo nome file prn (per stampante zebra)
GetMainPrivateProfileString(S_PRINTER, K_TEMPLATE, sDataRoot & "\Label\Default.prn", m_TemplateFilePrinter)
' leggo nome del direttorio in cui scrivere il file ini (per stampante zebra) -- solo se si desidera scrivere il file in un direttorio diverso
GetMainPrivateProfileString(S_PRINTER, K_DAT, sDataRoot & "\Temp", m_DatFilePrinter)
' leggo nome file exe (per stampante zebra)
GetMainPrivateProfileString(S_PRINTER, K_ZEBRAUTILITIES, System.AppDomain.CurrentDomain.BaseDirectory & "ZebraPrinterUtilitiesR32.exe", m_ZebraUtilitiesExe)
' verifico che sia abilitata la stampante con flag
m_PrinterIsVisible = (GetMainPrivateProfileInt(S_PRINTER, K_ENABLE_PRINTER, 0) = 1)
' verifico che sia abilitata il CapsLock
m_bCapsLock = (GetMainPrivateProfileInt(S_GENERAL, K_CAPSLOCK, 0) = 1)
' Creo connessione al Db
If Not ManageDb.ConnectToDb(m_sPhotoDir & "\" & DB_FILENAME) Then
EgtOutLog("Error connecting to DB")
MessageBox.Show(EgtMsg(MSG_OMAGPHOTO + 1) & EgtMsg(MSG_OMAGPHOTO + 2), EgtMsg(MSG_EGTMSGBOX + 15), MessageBoxButton.OK, MessageBoxImage.Error)
End
End If
' Info su opzioni chiave
EgtOutLog("KeyOptions : " & bKey.ToString() & " " & m_nKeyOptions.ToString() & " " & bProd.ToString())
' Imposto parametri EgtPHOTOLib
EgtPHOTOLib.MainData.SetDebugLevel(m_nDebug)
EgtPHOTOLib.MainData.SetConfigDir(m_sConfigDir)
EgtPHOTOLib.MainData.SetKeyLevel(m_nKeyLevel)
EgtPHOTOLib.MainData.SetKeyOptions(m_nKeyOptions)
EgtPHOTOLib.MainData.SetUser(Environment.MachineName & "\" & Environment.UserName & " (" & nInstance.ToString() & ")")
EgtPHOTOLib.MainData.SetPhotoDir(m_sPhotoDir)
EgtPHOTOLib.MainData.SetBackUpDir(m_sBackUpDir)
EgtPHOTOLib.MainData.SetTemplateFilePrinter(m_TemplateFilePrinter)
EgtPHOTOLib.MainData.SetDataFilePrinter(m_DatFilePrinter)
EgtPHOTOLib.MainData.SetZebraPrinterExe(m_ZebraUtilitiesExe)
EgtPHOTOLib.MainData.SetPrinterVisibility(m_PrinterIsVisible)
EgtPHOTOLib.MainData.SetCapsLock(m_bCapsLock)
Dim sLastBlock As String = String.Empty
If GetMainPrivateProfileString(S_GENERAL, "LastBlock", "", sLastBlock) <> 0 Then EgtPHOTOLib.MainData.SetLastBlock(sLastBlock)
Dim sIdKey As String = String.Empty
EgtGetKeyInfo(sIdKey)
EgtPHOTOLib.MainData.SetKey(sIdKey)
' lancio il timer di aggiornamento dell'interfaccia, per la gestione di due OmagPHOTO che accedono allo stesso SQL
AddHandler m_RefreshTimer.Tick, AddressOf RefreshTimer_Tick
m_RefreshTimer.Interval = TimeSpan.FromMilliseconds(2000)
m_RefreshTimer.Start()
' sec onfigurato lancio il controllo di uno specifico direttorio per leggere quando scattare una nuova foto
Dim sClidkDir As String = String.Empty
Dim bEnableLineProduction As Boolean = GetMainPrivateProfileString(S_AUTOCLICK, K_CLICKDIR, "", sClidkDir) <> 0
If bEnableLineProduction Then
fsWatch = New FileSystemWatcher(sClidkDir) With {.EnableRaisingEvents = True}
'fsWatch.EnableRaisingEvents = True
AddHandler fsWatch.Created, AddressOf AutoClick
AddHandler fsWatch.Changed, AddressOf AutoClick
AddHandler fsWatch.Renamed, AddressOf AutoClick
m_bNewFileClick = False
AddHandler m_RefreshNewFileClick.Tick, AddressOf AutoClick_Cmd
m_RefreshNewFileClick.Interval = TimeSpan.FromMilliseconds(2000)
m_RefreshNewFileClick.Start()
End If
End Sub
Private Sub AutoClick(ByVal sender As Object, ByVal e As FileSystemEventArgs)
EgtOutLog("File '" & e.FullPath & "' has been created: ready to Click")
m_sNewFileClickPath = e.FullPath
m_bNewFileClick = True
End Sub
Private Sub AutoClick_Cmd()
If Not m_bNewFileClick Then Return
' deseleziono la precedente lastra
If Not IsNothing(EgtPHOTOLib.PhotoMap.refProjectVM.SelSlab) Then
EgtPHOTOLib.PhotoMap.refProjectVM.SelSlab.IsSelected = 0
End If
' verifico se esite una lastra corrente in modifica, quindi chiedo se salvare
Map.refTopCommandBarVM.NewCmd(True)
' apro la pagina per inserire una nuova lastra, se esiste un oggetto di tipo lastra allora procedo
If Not IsNothing(EgtPHOTOLib.PhotoMap.refProjectVM.SelSlab) Then
' Autocompilazione dei campi con i dati dell'ultima lastra salvata
ReadLastInfoAutoClick(EgtPHOTOLib.PhotoMap.refProjectVM.SelSlab)
' scatto la foto
EgtPHOTOLib.PhotoMap.refOptionPanelVM.Photo(Nothing)
' notifico che è stata scattata una foto
Try
File.Delete(m_sNewFileClickPath)
EgtOutLog("File '" & m_sNewFileClickPath & "' has been deleted!")
m_bNewFileClick = False
Catch ex As Exception
EgtOutLog("File '" & m_sNewFileClickPath & "' can't be deleted! " & ex.ToString)
End Try
' se è stata scattata una foto provvedo a salvare l'inserimento
If Not m_bNewFileClick Then
' salvo i dati inseriti ( nel file ini e nel DB )
EgtPHOTOLib.PhotoMap.refOptionPanelVM.Save()
' ricarico la pagina
EgtPHOTOLib.PhotoMap.refProjectVM.SelProjectMode = ProjectSlabVM.ProjectModeOpt.DETAIL
EgtPHOTOLib.PhotoMap.refOptionPanelVM.Ok_IsEnabled = False
' stampo etichetta
EgtPHOTOLib.PhotoMap.refOptionPanelVM.PrintLabel()
End If
End If
End Sub
Private Sub ReadLastInfoAutoClick(CurrSlab As Slab)
Dim nIndState As Integer = 1
Dim sMaterial As String = ""
Dim dThick As Double = 0
Dim sWarehouse As String = ""
nIndState = GetMainPrivateProfileInt(S_AUTOCLICK, K_STATE, nIndState)
GetMainPrivateProfileString(S_AUTOCLICK, K_AUTOMATERIAL, "", sMaterial)
dThick = GetMainPrivateProfileDouble(S_AUTOCLICK, K_THICKNESS, 1)
GetMainPrivateProfileString(S_AUTOCLICK, K_WAREHAOUSE, "", sWarehouse)
CurrSlab.State = nIndState
For IndexMat As Integer = 0 To EgtPHOTOLib.PhotoMap.refProjectVM.MaterialList.Count - 1
If EgtPHOTOLib.PhotoMap.refProjectVM.MaterialList(IndexMat).Trim = sMaterial.Trim Then
CurrSlab.SetMaterial(EgtPHOTOLib.PhotoMap.refProjectVM.MaterialList(IndexMat))
Exit For
End If
Next
CurrSlab.SetThickness(dThick)
CurrSlab.WarehousePosition = sWarehouse
End Sub
' per la gestione di due OmagPHOTO che accedono allo stesso SQL
Private Sub RefreshTimer_Tick()
If Not m_TimerIsBusy AndAlso Not IsNothing(PhotoMap.refProjectVM) AndAlso
PhotoMap.refProjectVM.SelProjectMode = ProjectSlabVM.ProjectModeOpt.LIST Then
m_TimerIsBusy = True
m_RefreshTimer.Stop()
EgtPHOTOLib.PhotoMap.refListPageVM.RefreshLisPage()
m_TimerIsBusy = False
m_RefreshTimer.Start()
End If
End Sub
Private Sub ManageInstance()
Dim bCreated As Boolean
Try
m_objMutex = New Mutex(False, "Global\OmagPHOTO", bCreated)
Catch
bCreated = False
End Try
m_bFirstInstance = bCreated
If bCreated Then
' Prima istanza
m_nInstance = 1
' Aggiorno stato istanze attive
WriteMainPrivateProfileString(S_GENERAL, K_INSTANCES, m_nInstance.ToString())
Else
' Leggo il massimo numero di istanze ammesse
Const MAX_INST As Integer = 32
Dim nMaxInst As Integer = GetMainPrivateProfileInt(S_GENERAL, K_MAXINST, 1)
nMaxInst = Max(1, Min(nMaxInst, MAX_INST))
' Cerco il primo indice di istanza libero
Dim nTmp As Integer = GetMainPrivateProfileInt(S_GENERAL, K_INSTANCES, 0)
m_nInstance = 1
Dim nMask As Integer = 1
While (nTmp And nMask) <> 0 And m_nInstance < MAX_INST
m_nInstance += 1
nMask *= 2
End While
' Se l'indice supera il massimo
If m_nInstance > nMaxInst Then
' porto in primo piano la prima istanza
Dim bFound As Boolean = False
' processi del programma a 32 bit
Dim localProc As Process() = Process.GetProcessesByName("OmagPHOTOR32")
For Each p As Process In localProc
If p.Id <> Process.GetCurrentProcess().Id Then
bFound = True
ShowWindow(p.MainWindowHandle, 1)
Exit For
End If
Next
' se non trovati processi a 32 bit provo a 64 bit
If Not bFound Then
localProc = Process.GetProcessesByName("OmagPHOTOR64")
For Each p As Process In localProc
If p.Id <> Process.GetCurrentProcess().Id Then
bFound = True
ShowWindow(p.MainWindowHandle, SW.RESTORE)
Exit For
End If
Next
End If
' esco dal programma
End
End If
' Aggiorno stato istanze attive
nTmp += (1 << (m_nInstance - 1))
WriteMainPrivateProfileString(S_GENERAL, K_INSTANCES, nTmp.ToString())
End If
End Sub
Friend Function GetKeyOption(nKeyOpt As KEY_OPT) As Boolean
Return ((m_nKeyOptions And nKeyOpt) <> 0)
End Function
Friend Sub Close()
' Terminazione generale di EgtInterface
EgtExit()
' Rilascio mutex
If Not IsNothing(m_objMutex) Then m_objMutex.Close()
' Aggiorno istanze usate
Dim nTmp As Integer = GetMainPrivateProfileInt(S_GENERAL, K_INSTANCES, 0)
nTmp -= (1 << (m_nInstance - 1))
WriteMainPrivateProfileString(S_GENERAL, K_INSTANCES, nTmp.ToString())
End Sub
#End Region ' METHODS
End Class