'---------------------------------------------------------------------------- ' EgalTech 2015-2015 '---------------------------------------------------------------------------- ' File : Camera.vb Data : 08.10.15 Versione : 1.6j1 ' Contenuto : Classe Camera (gestione della macchina fotografica). ' ' ' ' Modifiche : 08.10.15 DS Creazione modulo. ' ' '---------------------------------------------------------------------------- Imports System.Threading Imports System.IO Imports EgtUILib Public Class Camera ' Riferimento alla MainWindow Private m_MainWindow As MainWindow = Application.Current.MainWindow ' Dati Private m_bCameraLink As Boolean = False Private m_sCameraPath As String = String.Empty Private m_sCameraProcName As String = String.Empty Private m_sImage As String = String.Empty Private m_sInfo As String = String.Empty Private m_sResult As String = String.Empty Private m_nTimeout As Integer = 30 Private m_sImageDir As String = String.Empty ' Flag per foto in esecuzione Friend m_bBusy As Boolean = False Public Function Init() As Boolean ' Lettura dati di configurazione da file Ini m_bCameraLink = (GetPrivateProfileInt(S_GENERAL, K_CAMERALINK, 0, m_MainWindow.GetIniFile()) <> 0) GetPrivateProfileString(S_CAMERA, K_CAM_EXEPATH, "", m_sCameraPath, m_MainWindow.GetIniFile()) GetPrivateProfileString(S_CAMERA, K_CAM_IMAGE, "", m_sImage, m_MainWindow.GetIniFile()) GetPrivateProfileString(S_CAMERA, K_CAM_INFO, "", m_sInfo, m_MainWindow.GetIniFile()) GetPrivateProfileString(S_CAMERA, K_CAM_RESULT, "", m_sResult, m_MainWindow.GetIniFile()) m_nTimeout = GetPrivateProfileInt(S_CAMERA, K_CAM_TIMEOUT, 30, m_MainWindow.GetIniFile()) GetPrivateProfileString(S_GENERAL, K_IMAGEDIR, "", m_sImageDir, m_MainWindow.GetIniFile()) ' Ricavo il nome del processo associato m_sCameraProcName = Path.GetFileNameWithoutExtension(m_sCameraPath) ' Se camera abilitata, lancio l'esecuzione in cieco If m_bCameraLink Then Return CameraHide() End If Return True End Function Public Function Close() As Boolean If m_bBusy Then Return False End If KillProcess() Return True End Function Public Function GetCameraLink() As Boolean Return m_bCameraLink End Function Public Function CameraHide() As Boolean ' Lancio il programma in cieco, se già attivo lo nascondo Try Process.Start(m_sCameraPath, "0") Return True Catch ex As Exception Return False End Try End Function Public Function CameraShow() As Boolean ' Lancio il programma in modo visibile, se già attivo lo rendo visibile Try Process.Start(m_sCameraPath, "1") Return True Catch ex As Exception Return False End Try End Function Public Function CameraTest() As Boolean ' Cancello il risultato If My.Computer.FileSystem.FileExists(m_sResult) Then My.Computer.FileSystem.DeleteFile(m_sResult) End If ' Lancio il programma per sapere se macchina fotografica collegata Try ' Interrogo Process.Start(m_sCameraPath, "3") ' Ciclo di attesa risultato Dim nMaxThick = 10 * 4 For nThick As Integer = 0 To nMaxThick ' Se esiste il file di risultato Dim nErr = 999 If VerifyResult(nErr) Then Return (nErr = 0) End If ' Aspetto 100 ms Thread.Sleep(100) Next Catch ex As Exception ' End Try Return False End Function Public Function CameraClick() As Boolean ' Se gestore macchina non attivo, lo lancio in modo cieco If Not ProcessIsRunning() Then If Not CameraHide() Then Return False End If ' Aspetto 5000 ms Thread.Sleep(5000) ' Altrimenti richiedo verifica di camera connessa Else If Not CameraTest() Then Return False End If ' Aspetto 100 ms Thread.Sleep(100) End If ' Visualizzo progressbar m_bBusy = True m_MainWindow.m_CadCutPageUC.PhotoProgress.Visibility = Visibility.Visible m_MainWindow.m_CadCutPageUC.PhotoProgress.Value = 1 ' Cancellazione eventuali vecchi file rimasti Try If My.Computer.FileSystem.FileExists(m_sResult) Then My.Computer.FileSystem.DeleteFile(m_sResult) End If If My.Computer.FileSystem.FileExists(m_sImage) Then My.Computer.FileSystem.DeleteFile(m_sImage) End If If My.Computer.FileSystem.FileExists(m_sInfo) Then My.Computer.FileSystem.DeleteFile(m_sInfo) End If Catch ex As Exception End Try ' Scatto una foto (il programma deve essere già attivo) Dim bOk As Boolean = False Try Process.Start(m_sCameraPath, "2 0") bOk = WaitPhoto() Catch ex As Exception bOk = False End Try ' Nascondo progressbar m_MainWindow.m_CadCutPageUC.PhotoProgress.Visibility = Visibility.Hidden m_bBusy = False Return bOk End Function Private Function WaitPhoto() As Boolean ' Ciclo di ricerca foto scattata Dim nMaxThick = 10 * m_nTimeout For nThick As Integer = 0 To nMaxThick ' Se esiste il file di risultato Dim nErr = 999 If VerifyResult(nErr) Then If nErr = 0 Then ' Copio i file Dim sImageDest As String = m_sImageDir & "\" & Path.GetFileName(m_sImage) Dim sInfoDest As String = Path.ChangeExtension(sImageDest, "txt") File.Copy(m_sImage, sImageDest, True) File.Copy(m_sInfo, sInfoDest, True) ' Lancio caricamento della foto m_MainWindow.m_CadCutPageUC.PostPhoto(sImageDest) Return True Else EgtOutLog("Camera err=" & nErr.ToString()) Return False End If ' Altrimenti aspetto Else ' Imposto ProgressBar Dim nProgress As Integer = nThick * 100 / nMaxThick m_MainWindow.m_CadCutPageUC.PhotoProgress.Value = nProgress ' Costringo ad aggiornare Dim nDummy As Integer Application.Current.Dispatcher.Invoke(Windows.Threading.DispatcherPriority.Background, _ New Action(Function() nDummy = 0)) ' Aspetto 100 ms Thread.Sleep(100) End If Next EgtOutLog("Camera generic error") ' Chiudo il gestore della macchina per resettarlo KillProcess() Return False End Function Private Function VerifyResult(ByRef nErr As Integer) As Boolean ' Se non esiste il file con il risultato If Not My.Computer.FileSystem.FileExists(m_sResult) Then Return False End If ' Leggo il file Dim bOk As Boolean = False Try ' Controllo errori nel file di info Dim sLine As String = String.Empty Dim sr As StreamReader = New StreamReader(m_sResult) Do While sr.Peek() > -1 sLine = sr.ReadLine() sLine = sLine.Replace(" ", "") If sLine.StartsWith("Err=") Then If Int32.TryParse(sLine.Substring(4), nErr) Then bOk = True Exit Do End If End If Loop sr.Close() Catch ex As Exception bOk = False End Try Return bOk End Function Private Function ProcessIsRunning() As Boolean Dim Procs() As Process Procs = Process.GetProcessesByName(m_sCameraProcName) Return (Procs.Length() > 0) End Function Private Sub KillProcess() Dim Procs() As Process Procs = Process.GetProcessesByName(m_sCameraProcName) For i As Integer = 0 To Procs.Length() - 1 Procs(i).Kill() Procs(i).WaitForExit(2000) Next i End Sub End Class