diff --git a/RawPhoto/Camera.vb b/RawPhoto/Camera.vb index 27c8289..7b95680 100644 --- a/RawPhoto/Camera.vb +++ b/RawPhoto/Camera.vb @@ -14,6 +14,7 @@ Imports System.Threading Imports System.IO Imports EgtUILib +Imports System.Reflection Public Class Camera @@ -25,24 +26,27 @@ Public Class Camera Private m_bCalcContour 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_sCameraPath2 As String = String.Empty Private m_sCameraProcName As String = String.Empty - Private m_sCameraProcName2 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_sImage2 As String = String.Empty Private m_sInfo As String = String.Empty - Private m_sInfo2 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_sResult2 As String = String.Empty Private m_sContour As String = String.Empty - Private m_sContour2 As String = String.Empty + 'Private m_sContour2 As String = String.Empty Private m_nThreshold As Integer = 60 Private m_dTolerance As Double = 5 Private m_nTimeout As Integer = 30 Private m_sImageDir As String = String.Empty + Private Const CAMERAMNG As String = "CameraMng" + 'Public m_ProcessCmg As New Process() Public m_ProcessCmgList As New List(Of Process) + Private m_bIsRunnigProc As Boolean = False ' Flag per foto in esecuzione Friend m_bBusy As Boolean = False @@ -57,11 +61,6 @@ Public Class Camera 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_CONTOUR, "", m_sContour, 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()) - GetPrivateProfileString(S_CAMERA, K_CAM_CONTOUR2, "", m_sContour2, m_MainWindow.GetIniFile()) m_nThreshold = GetPrivateProfileInt(S_CAMERA, K_CAM_THRESHOLD, 60, m_MainWindow.GetIniFile()) m_dTolerance = GetPrivateProfileDouble(S_CAMERA, K_CAM_TOLERANCE, 5, m_MainWindow.GetIniFile()) m_nTimeout = GetPrivateProfileInt(S_CAMERA, K_CAM_TIMEOUT, 30, m_MainWindow.GetIniFile()) @@ -71,56 +70,15 @@ Public Class Camera (GetPrivateProfileInt(S_GENERAL, K_CONTOURFROMCAMERA, 1, m_MainWindow.GetIniFile()) <> 0) ' Ricavo il nome del processo associato m_sCameraProcName = Path.GetFileNameWithoutExtension(m_sCameraPath) - m_sCameraProcName2 = Path.GetFileNameWithoutExtension(m_sCameraPath2) - For Index As Integer = 0 To m_nCameraCount - 1 - Dim m_ProcessCmg As New Process() - m_ProcessCmg.StartInfo.FileName = m_sCameraPath - m_ProcessCmg.StartInfo.RedirectStandardInput = True - m_ProcessCmg.StartInfo.RedirectStandardOutput = True - m_ProcessCmg.StartInfo.Arguments = "0" & " " & Index.ToString() - m_ProcessCmg.StartInfo.UseShellExecute = False - m_ProcessCmg.StartInfo.CreateNoWindow = True - AddHandler m_ProcessCmg.OutputDataReceived, AddressOf Thread_OutputDataReceived - If m_ProcessCmg.Start() Then - m_ProcessCmg.BeginOutputReadLine() - End If - m_ProcessCmgList.Add(m_ProcessCmg) - Next - ' 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 + ' Avvio processi del camera manager per ogni tavola + CamerasHide() + Return True 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 + KillProcess() End If Return True End Function @@ -171,7 +129,7 @@ Public Class Camera End Set End Property - Public Function CameraHide(nInd As Integer) As Boolean + Public Function CameraHide_0(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") @@ -181,26 +139,70 @@ Public Class Camera End Try End Function - Private Sub Thread_OutputDataReceived(sender As Object, e As DataReceivedEventArgs) - If Not IsNothing(e.Data) Then - EgtOutLog("Ricevuto: " & e.Data) - End If - End Sub - - Public Function CameraShow(nInd As Integer) As Boolean - ' Lancio il programma in modo visibile, se già attivo lo rendo visibile + Public Function CamerasHide() As Boolean + ' Lancio il programma in cieco, se già attivo lo nascondo Try - Process.Start(If(nInd <> 2, m_sCameraPath, m_sCameraPath2), "1") + For Index As Integer = 0 To m_nCameraCount - 1 + Dim m_ProcessCmg As New Process() + m_ProcessCmg.StartInfo.FileName = m_sCameraPath + m_ProcessCmg.StartInfo.RedirectStandardInput = True + m_ProcessCmg.StartInfo.RedirectStandardOutput = True + ' Assegno argomento 0 per avvio in cieco, index per specificare il numero di processo + m_ProcessCmg.StartInfo.Arguments = "0" & " " & Index.ToString() + m_ProcessCmg.StartInfo.UseShellExecute = False + m_ProcessCmg.StartInfo.CreateNoWindow = True + AddHandler m_ProcessCmg.OutputDataReceived, AddressOf Thread_OutputDataReceived + If m_ProcessCmg.Start() Then + m_ProcessCmg.BeginOutputReadLine() + ' m_ProcessCmg.StandardInput.WriteLine("0") + m_ProcessCmgList.Add(m_ProcessCmg) + Else + Return False + End If + Next Return True Catch ex As Exception + EgtOutLog(ex.Message) Return False End Try End Function - Public Function CameraTest(nInd As Integer) As Boolean + Public Function ReloadCameraHide(nInd As Integer) As Boolean + Dim m_ProcessCmg As New Process() + m_ProcessCmg.StartInfo.FileName = m_sCameraPath + m_ProcessCmg.StartInfo.RedirectStandardInput = True + m_ProcessCmg.StartInfo.RedirectStandardOutput = True + ' Assegno argomento 0 per avvio in cieco, index per specificare il numero di processo + m_ProcessCmg.StartInfo.Arguments = "0" & " " & nInd.ToString() + m_ProcessCmg.StartInfo.UseShellExecute = False + m_ProcessCmg.StartInfo.CreateNoWindow = True + AddHandler m_ProcessCmg.OutputDataReceived, AddressOf Thread_OutputDataReceived + If m_ProcessCmg.Start() Then + m_ProcessCmg.BeginOutputReadLine() + m_ProcessCmgList(nInd) = m_ProcessCmg + Return True + End If + Return False + End Function + + ' Ogni processo cameramanager che restituisce un messaggio passa di qua + Private Sub Thread_OutputDataReceived(sender As Object, e As DataReceivedEventArgs) + ' Il formato del messaggio è 'esempio messaggio: #IndProc' + If Not IsNothing(e.Data) Then + Dim sMsg As String() = e.Data.Split(":"c) + If sMsg.Length > 1 Then + If sMsg(0).Contains("PROCESSO ATTIVO") Then + m_bIsRunnigProc = True + End If + EgtOutLog(" § " & sMsg(0) & " Process_" & sMsg(1)) + End If + End If + End Sub + + Public Function CameraTest_0(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)) + 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 @@ -238,15 +240,15 @@ Public Class Camera If nInd <> 1 And nInd <> 2 Then Return 0 End If ' Se gestore macchina non attivo, lo lancio in modo cieco - If Not ProcessIsRunning(nInd) Then - If Not CameraHide(nInd) Then + If Not ProcessIsRunning() Then + If Not CameraHide_0(nInd) Then Return 0 End If ' Aspetto 5000 ms Thread.Sleep(5000) ' Altrimenti richiedo verifica di camera connessa Else - If Not CameraTest(nInd) Then + If Not CameraTest_0(nInd) Then Return 0 End If ' Aspetto 100 ms @@ -255,29 +257,65 @@ Public Class Camera Return nInd End Function + ' Ottengo l'indice del processo associato alla tavola e la configurazione del processo Private Function PrepareCamera() As Integer - Return GetCurrentTable() + m_bIsRunnigProc = False + Dim sArgs As String = "7" + Dim nInd As Integer = GetCurrentTable() + ' Invio richiesta al processo per vedere se la comunicazione è aperta + Try + m_ProcessCmgList(nInd - 1).StandardInput.WriteLine(sArgs) + Catch ex As Exception + EgtOutLog(ex.Message()) + End Try + ' Attendo massimo 5 secondi per avere una risposta dal programma (la risposta arriva al metodo Thread_OutputDataReceived) + For i As Integer = 0 To 5 + Thread.Sleep(1000) + If m_bIsRunnigProc Then Exit For + Next + ' Se processo non attivo allora provo a rilanciarlo + If Not m_bIsRunnigProc Then + EgtOutLog(" ~ PROCESSO NON RISPONDE: Process_" & (nInd - 1).ToString()) + ReloadCameraHide(nInd - 1) + For i As Integer = 0 To 5 + Thread.Sleep(1000) + ' Invio richiesta al processo per vedere se la comunicazione è aperta + m_ProcessCmgList(nInd - 1).StandardInput.WriteLine(sArgs) + If m_bIsRunnigProc Then Exit For + Next + End If + + ' ----------------------------------------------------------------- + ' ----- PREPARAZIONE PERCORSO FILE ----- + ' ----------------------------------------------------------------- + + m_sImage = m_sImage.Replace(CAMERAMNG, CAMERAMNG & (nInd - 1).ToString()) + m_sInfo = m_sInfo.Replace(CAMERAMNG, CAMERAMNG & (nInd - 1).ToString()) + m_sResult = m_sResult.Replace(CAMERAMNG, CAMERAMNG & (nInd - 1).ToString()) + m_sContour = m_sContour.Replace(CAMERAMNG, CAMERAMNG & (nInd - 1).ToString()) + + Return If(m_bIsRunnigProc, nInd, -1) End Function Public Function CameraBackImage() As Boolean ' Verifiche preliminari Dim nInd As Integer = PrepareCamera() - If nInd = 0 Then Return False + If nInd = -1 Then Return False ' 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)) + If My.Computer.FileSystem.FileExists(m_sResult) Then + My.Computer.FileSystem.DeleteFile(m_sResult) End If Catch ex As Exception End Try ' Scatto una foto come sfondo (il programma deve essere già attivo) Dim bOk As Boolean = False Try - Process.Start(If(nInd <> 2, m_sCameraPath, m_sCameraPath2), "4") + 'Process.Start(If(nInd <> 2, m_sCameraPath, m_sCameraPath2), "4") bOk = WaitBackImage(nInd) Catch ex As Exception bOk = False @@ -314,13 +352,15 @@ Public Class Camera Next EgtOutLog("Camera generic error") ' Chiudo il gestore della macchina per resettarlo - KillProcess(nInd) + KillProcess() Return False End Function Public Function CameraClick() As Boolean ' Verifiche preliminari Dim nInd As Integer = PrepareCamera() + ' Se non è stato trovato il processo associato alla tavola allora esco + If nInd < 0 Then Return False ' Visualizzo progressbar m_bBusy = True m_MainWindow.m_CurrentProjectPageUC.PhotoProgress.Visibility = Visibility.Visible @@ -384,8 +424,7 @@ Public Class Camera Dim sArgs As String = "2 0" If m_bCalcContour Then sArgs = " 5 0 " & m_nThreshold.ToString() & " 0" Try - 'Process.Start(If(nInd <> 2, m_sCameraPath, m_sCameraPath2), sArgs) - m_ProcessCmgList(nInd - 1).StandardInput.WriteLine("5") + m_ProcessCmgList(nInd - 1).StandardInput.WriteLine("2") bOk = WaitPhoto_0(nInd) Catch ex As Exception EgtOutLog(ex.Message()) @@ -471,15 +510,15 @@ Public Class Camera 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 sImageDest As String = m_sImageDir & "\" & Path.GetFileName(m_sImage) 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) + File.Copy(m_sImage, sImageDest, True) + File.Copy(m_sInfo, sInfoDest, True) ' Se richiesto il riconoscimento del contorno Dim sContourDest As String = String.Empty If m_bCalcContour Then sContourDest = Path.ChangeExtension(sImageDest, "dxf") - File.Copy(If(nInd <> 2, m_sContour, m_sContour2), sContourDest, True) + File.Copy(m_sContour, sContourDest, True) ' altrimenti cancello eventuale contorno presente Else m_MainWindow.m_CurrentProjectPageUC.RemoveContour() @@ -504,13 +543,13 @@ Public Class Camera Next EgtOutLog("Camera generic error") ' Chiudo il gestore della macchina per resettarlo - KillProcess(nInd) + KillProcess() 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 + If Not My.Computer.FileSystem.FileExists(m_sResult) Then Return False End If ' Leggo il file @@ -518,7 +557,7 @@ Public Class Camera 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)) + Dim sr As StreamReader = New StreamReader(m_sResult) Do While sr.Peek() > -1 sLine = sr.ReadLine() sLine = sLine.Replace(" ", "") @@ -536,15 +575,15 @@ Public Class Camera Return bOk End Function - Private Function ProcessIsRunning(nInd As Integer) As Boolean + Private Function ProcessIsRunning() As Boolean Dim Procs() As Process - Procs = Process.GetProcessesByName(If(nInd <> 2, m_sCameraProcName, m_sCameraProcName2)) + Procs = Process.GetProcessesByName(m_sCameraProcName) Return (Procs.Length() > 0) End Function - Private Sub KillProcess(nInd As Integer) + Private Sub KillProcess() Dim Procs() As Process - Procs = Process.GetProcessesByName(If(nInd <> 2, m_sCameraProcName, m_sCameraProcName2)) + Procs = Process.GetProcessesByName(m_sCameraProcName) For i As Integer = 0 To Procs.Length() - 1 Procs(i).Kill() Procs(i).WaitForExit(2000)