'---------------------------------------------------------------------------- ' 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 = DirectCast(Application.Current.MainWindow, MainWindow) ' Dati Private m_bCameraLink As Boolean = False Private m_nCameraCount As Integer = 0 Private m_sCameraPath As String = String.Empty Private m_sCameraPath2 As String = String.Empty Private m_sCameraProcName As String = String.Empty Private m_sCameraProcName2 As String = String.Empty Private m_sImage As String = String.Empty Private m_sImage2 As String = String.Empty Private m_sInfo As String = String.Empty Private m_sInfo2 As String = String.Empty Private m_sResult As String = String.Empty Private m_sResult2 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) m_nCameraCount = GetPrivateProfileInt(S_CAMERA, K_CAM_COUNT, 1, m_MainWindow.GetIniFile()) 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()) GetPrivateProfileString(S_CAMERA, K_CAM_EXEPATH2, "", m_sCameraPath2, m_MainWindow.GetIniFile()) GetPrivateProfileString(S_CAMERA, K_CAM_IMAGE2, "", m_sImage2, m_MainWindow.GetIniFile()) GetPrivateProfileString(S_CAMERA, K_CAM_INFO2, "", m_sInfo2, m_MainWindow.GetIniFile()) GetPrivateProfileString(S_CAMERA, K_CAM_RESULT2, "", m_sResult2, 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) m_sCameraProcName2 = Path.GetFileNameWithoutExtension(m_sCameraPath2) ' Se camera abilitata, lancio l'esecuzione in cieco If m_bCameraLink Then If m_nCameraCount <> 2 Then Dim bOk As Boolean = True If Not CameraHide(1) Then bOk = False EgtOutLog("CameraMng not starting") End If Return bOk Else Dim bOk As Boolean = True If Not CameraHide(1) Then bOk = False EgtOutLog("CameraMng 1 not starting") End If If Not CameraHide(2) Then bOk = False EgtOutLog("CameraMng 2 not starting") End If Return bOk End If Else Return True End If End Function Public Function Close() As Boolean If m_bBusy Then Return False If m_bCameraLink Then If m_nCameraCount <> 2 Then KillProcess(1) Else KillProcess(1) KillProcess(2) End If End If Return True End Function Public Function GetCameraLink() As Boolean Return m_bCameraLink End Function Public Function CameraHide(nInd As Integer) As Boolean ' Lancio il programma in cieco, se già attivo lo nascondo Try Process.Start(If(nInd <> 2, m_sCameraPath, m_sCameraPath2), "0") Return True Catch ex As Exception Return False End Try End Function Public Function CameraShow(nInd As Integer) As Boolean ' Lancio il programma in modo visibile, se già attivo lo rendo visibile Try Process.Start(If(nInd <> 2, m_sCameraPath, m_sCameraPath2), "1") Return True Catch ex As Exception Return False End Try End Function Public Function CameraTest(nInd As Integer) As Boolean ' Cancello il risultato If My.Computer.FileSystem.FileExists(If(nInd <> 2, m_sResult, m_sResult2)) Then My.Computer.FileSystem.DeleteFile(If(nInd <> 2, m_sResult, m_sResult2)) End If ' Lancio il programma per sapere se macchina fotografica collegata Try ' Interrogo Process.Start(If(nInd <> 2, m_sCameraPath, m_sCameraPath2), "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(nInd, 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 ' Determino la camera da utilizzare, se più di una Dim nInd As Integer = 1 If m_nCameraCount = 2 Then Dim sTabName = MAIN_TAB EgtGetTableName(sTabName) If sTabName = SECOND_TAB Then nInd = 2 End If End If ' Se gestore macchina non attivo, lo lancio in modo cieco If Not ProcessIsRunning(nInd) Then If Not CameraHide(nInd) Then Return False End If ' Aspetto 5000 ms Thread.Sleep(5000) ' Altrimenti richiedo verifica di camera connessa Else If Not CameraTest(nInd) Then Return False End If ' Aspetto 100 ms Thread.Sleep(100) End If ' Visualizzo progressbar m_bBusy = True m_MainWindow.m_CurrentProjectPageUC.PhotoProgress.Visibility = Visibility.Visible m_MainWindow.m_CurrentProjectPageUC.PhotoProgress.Value = 1 ' Cancellazione eventuali vecchi file rimasti Try If My.Computer.FileSystem.FileExists(If(nInd <> 2, m_sResult, m_sResult2)) Then My.Computer.FileSystem.DeleteFile(If(nInd <> 2, m_sResult, m_sResult2)) End If If My.Computer.FileSystem.FileExists(If(nInd <> 2, m_sImage, m_sImage2)) Then My.Computer.FileSystem.DeleteFile(If(nInd <> 2, m_sImage, m_sImage2)) End If If My.Computer.FileSystem.FileExists(If(nInd <> 2, m_sInfo, m_sInfo2)) Then My.Computer.FileSystem.DeleteFile(If(nInd <> 2, m_sInfo, m_sInfo2)) 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(If(nInd <> 2, m_sCameraPath, m_sCameraPath2), "2 0") bOk = WaitPhoto(nInd) Catch ex As Exception bOk = False End Try ' Nascondo progressbar m_MainWindow.m_CurrentProjectPageUC.PhotoProgress.Visibility = Visibility.Hidden m_bBusy = False Return bOk End Function Private Function WaitPhoto(nInd As Integer) 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(nInd, nErr) Then If nErr = 0 Then ' Copio i file Dim sImageDest As String = m_sImageDir & "\" & Path.GetFileName(If(nInd <> 2, m_sImage, m_sImage2)) Dim sInfoDest As String = Path.ChangeExtension(sImageDest, "txt") File.Copy(If(nInd <> 2, m_sImage, m_sImage2), sImageDest, True) File.Copy(If(nInd <> 2, m_sInfo, m_sInfo2), 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 = CInt(nThick * 100 / nMaxThick) m_MainWindow.m_CurrentProjectPageUC.PhotoProgress.Value = nProgress ' Costringo ad aggiornare UI UpdateUI() ' Aspetto 100 ms Thread.Sleep(100) End If Next EgtOutLog("Camera generic error") ' Chiudo il gestore della macchina per resettarlo KillProcess(nInd) Return False End Function Private Function VerifyResult(nInd As Integer, ByRef nErr As Integer) As Boolean ' Se non esiste il file con il risultato If Not My.Computer.FileSystem.FileExists(If(nInd <> 2, m_sResult, m_sResult2)) 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(If(nInd <> 2, m_sResult, m_sResult2)) 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(nInd As Integer) As Boolean Dim Procs() As Process Procs = Process.GetProcessesByName(If(nInd <> 2, m_sCameraProcName, m_sCameraProcName2)) Return (Procs.Length() > 0) End Function Private Sub KillProcess(nInd As Integer) Dim Procs() As Process Procs = Process.GetProcessesByName(If(nInd <> 2, m_sCameraProcName, m_sCameraProcName2)) For i As Integer = 0 To Procs.Length() - 1 Procs(i).Kill() Procs(i).WaitForExit(2000) Next i End Sub End Class