/// <summary> /// Select the mode for this session, batch or interactive, /// based on the arguments. Don't be weirded out by this /// function calling TWAIN Local On Sane and then that function using /// Sword. This is a static function, it's just a convenience /// to use the Sword object to hold it, it could go anywhere /// and later on probably will (like as a function inside of /// the Program module). /// </summary> /// <returns>true for batch mode, false for interactive mode</returns> public static bool SelectMode() { int iPid = 0; string szIpc = null; string szTaskFile = ""; string szWriteFolder; bool blTestTwainLocalOnSane = false; // Sleep so we can attach and debug stuff... int iDelay = (int)Config.Get("delayTwainDirectOnSane", 0); if (iDelay > 0) { Thread.Sleep(iDelay); } // Check the arguments... szWriteFolder = Config.Get("writeFolder", null); szTaskFile = Config.Get("task", null); blTestTwainLocalOnSane = (Config.Get("testtwainlocalonsane", null) != null); szIpc = Config.Get("ipc", null); iPid = int.Parse(Config.Get("parentpid", "0")); // Handle the SANE list... string szSaneList = Config.Get("sanelist", null); if (szSaneList != null) { if (szSaneList == "") { szSaneList = Path.Combine(Config.Get("writeFolder", ""), "sanelist.txt"); } System.IO.File.WriteAllText(szSaneList, Sword.SaneListDrivers()); return(true); } // Test TWAIN Local on SANE... if (blTestTwainLocalOnSane) { TestTwainLocalOnSane testtwainlocalonsane; // List the scanners... Sword.SaneListDrivers(); // Create our object... testtwainlocalonsane = new TestTwainLocalOnSane(Config.Get("scanner", null), szWriteFolder, iPid, szTaskFile, szIpc); // Do the test... testtwainlocalonsane.Test(); // All done... return(true); } // Run in IPC mode. The caller has set up a 'pipe' for us, so we'll use // that to send commands back and forth... if (szIpc != null) { TwainLocalOnSane twainlocalonsane; // Create our object... twainlocalonsane = new TwainLocalOnSane(szWriteFolder, szIpc, iPid); // Run our object... twainlocalonsane.Run(); // All done... return(true); } // If we have a file, then take the shortcut, this is the path to use // when running under another program, like an HTTP service. if (File.Exists(szTaskFile)) { bool blSetAppCapabilities = false; string szScanImageArguments = ""; Sword sword; SwordTask swordtask; // Init stuff... swordtask = new SwordTask(); // Create our object... sword = new Sword(); // Run our task... sword.BatchMode(Config.Get("scanner", null), szTaskFile, false, ref swordtask, ref blSetAppCapabilities, out szScanImageArguments); // All done... return(true); } // Otherwise let the user interact with us... TwainDirectSupport.Log.Info("Interactive mode..."); return(false); }
/// <summary> /// Test our ability to run the TWAIN driver using the TWAIN Direct Client-Scanner API /// as the controlling API. This is easier to debug than running stuff /// across more than one process with the cloud involved... /// </summary> /// <returns></returns> public bool Test() { int ii; int[] aiImageBlockNum; long lResponseCharacterOffset; bool blSts; bool blEndOfJob; string szJson; Thread thread; Ipc ipc; JsonLookup jsonlookup; // Create our objects... m_twainlocalonsane = new TwainLocalOnSane(m_szWriteFolder, m_szIpc, Process.GetCurrentProcess().Id); jsonlookup = new JsonLookup(); ipc = new Ipc(m_szIpc, true); // Run in a thread... thread = new Thread(RunTest); thread.Start(); // Wait for a connection... ipc.Accept(); // Create Session... #region Create Session... // Open the scanner... blSts = ipc.Write ( "{" + "\"method\":\"createSession\"," + "\"scanner\":\"" + m_szScanner + "\"" + "}" ); if (!blSts) { TwainDirectSupport.Log.Error("Lost our process..."); goto ABORT; } // Get the result... szJson = ipc.Read(); // Analyze the result... try { jsonlookup.Load(szJson, out lResponseCharacterOffset); if (jsonlookup.Get("status") != "success") { TwainDirectSupport.Log.Error("createSession failed: " + jsonlookup.Get("status")); return(false); } } catch { TwainDirectSupport.Log.Error("createSession failed: JSON error"); return(false); } #endregion // Set TWAIN Direct Options... #region Set TWAIN Direct Options... string szTwainDirectOptions = File.ReadAllText(m_szTask); blSts = ipc.Write ( "{" + "\"method\":\"setTwainDirectOptions\"," + "\"task\":" + szTwainDirectOptions + "}" ); if (!blSts) { TwainDirectSupport.Log.Error("Lost our process..."); goto ABORT; } // Get the result... szJson = ipc.Read(); if (szJson == null) { TwainDirectSupport.Log.Error("Lost our process..."); goto ABORT; } #endregion // Start Capturing... #region Start Capturing... blSts = ipc.Write ( "{" + "\"method\":\"startCapturing\"" + "}" ); if (!blSts) { TwainDirectSupport.Log.Error("Lost our process..."); goto ABORT; } // Get the result... szJson = ipc.Read(); if (szJson == null) { TwainDirectSupport.Log.Error("Lost our process..."); goto ABORT; } #endregion // Loop until we run out of images... blEndOfJob = false; aiImageBlockNum = null; while (true) { // Get Session (wait for image)... #region GetSession (wait for images)... // Stay in this loop unti we get an image or an error... while (true) { // Get the current session info... blSts = ipc.Write ( "{" + "\"method\":\"getSession\"" + "}" ); if (!blSts) { TwainDirectSupport.Log.Error("Lost our process..."); goto ABORT; } // Get the result... szJson = ipc.Read(); if (szJson == null) { TwainDirectSupport.Log.Error("Lost our process..."); goto ABORT; } // Parse it... jsonlookup = new JsonLookup(); jsonlookup.Load(szJson, out lResponseCharacterOffset); // Bail if we're end of job... if (jsonlookup.Get("endOfJob") == "true") { blEndOfJob = true; break; } // Collect the data... try { aiImageBlockNum = null; for (ii = 0; ; ii++) { // Get the data... string szNum = jsonlookup.Get("session.imageBlocks[" + ii + "]"); if ((szNum == null) || (szNum == "")) { break; } // Convert it... int iTmp = int.Parse(szNum); // Add it to the list... if (aiImageBlockNum == null) { aiImageBlockNum = new int[1]; aiImageBlockNum[0] = iTmp; } else { int[] aiTmp = new int[aiImageBlockNum.Length + 1]; aiImageBlockNum.CopyTo(aiTmp, 0); aiTmp[aiTmp.Length - 1] = iTmp; aiImageBlockNum = aiTmp; } } } catch { // don't need to do anything... } // We got one! if (aiImageBlockNum != null) { break; } // Snooze a bit... Thread.Sleep(100); } // Bail if we're end of job... if (blEndOfJob) { break; } #endregion // Read Image Block Metadata... #region Read Image Block Metadata... // Get this image's metadata... blSts = ipc.Write ( "{" + "\"method\":\"readImageBlockMetadata\"," + "\"imageBlockNum\":" + aiImageBlockNum[0] + "}" ); if (!blSts) { TwainDirectSupport.Log.Error("Lost our process..."); goto ABORT; } // Get the result... szJson = ipc.Read(); if (szJson == null) { TwainDirectSupport.Log.Error("Lost our process..."); goto ABORT; } #endregion // Read Image Block... #region Read Image Block... // Get this image... blSts = ipc.Write ( "{" + "\"method\":\"readImageBlock\"," + "\"imageBlockNum\":" + aiImageBlockNum[0] + "}" ); if (!blSts) { TwainDirectSupport.Log.Error("Lost our process..."); goto ABORT; } // Get the result... szJson = ipc.Read(); if (szJson == null) { TwainDirectSupport.Log.Error("Lost our process..."); goto ABORT; } #endregion // Release Image Block... #region Release Image Block... // Release this image... blSts = ipc.Write ( "{" + "\"method\":\"releaseImageBlocks\"," + "\"imageBlockNum\":" + aiImageBlockNum[0] + "," + "\"lastImageBlockNum\":" + aiImageBlockNum[0] + "}" ); if (!blSts) { TwainDirectSupport.Log.Error("Lost our process..."); goto ABORT; } // Get the result... szJson = ipc.Read(); if (szJson == null) { TwainDirectSupport.Log.Error("Lost our process..."); goto ABORT; } #endregion } // Stop Capturing... #region Stop Capturing... blSts = ipc.Write ( "{" + "\"method\":\"stopCapturing\"" + "}" ); if (!blSts) { TwainDirectSupport.Log.Error("Lost our process..."); goto ABORT; } // Get the result... szJson = ipc.Read(); if (szJson == null) { TwainDirectSupport.Log.Error("Lost our process..."); goto ABORT; } #endregion // Close Session... #region Close Session... // Close the scanner... blSts = ipc.Write ( "{" + "\"method\":\"closeSession\"" + "}" ); if (!blSts) { TwainDirectSupport.Log.Error("Lost our process..."); goto ABORT; } // Get the result... szJson = ipc.Read(); if (szJson == null) { TwainDirectSupport.Log.Error("Lost our process..."); goto ABORT; } // Exit the process... blSts = ipc.Write ( "{" + "\"method\":\"exit\"" + "}" ); if (!blSts) { TwainDirectSupport.Log.Error("Lost our process..."); goto ABORT; } #endregion // All done... ABORT: thread.Join(); return(true); }