Example #1
0
        /// <summary>
        /// Register a device with cloud infrastructure.
        /// </summary>
        public async Task RegisterCloudScanner()
        {
            var client = new TwainCloudClient(CloudManager.GetCloudApiRoot());
            var registrationManager = new RegistrationManager(client);

            var scannerInfo = new ScannerInformation
            {
                Name        = GetTwainLocalTy(),
                Description = GetTwainLocalNote()
            };

            var result = await registrationManager.Register(scannerInfo);

            var registrationDialog = new RegistrationForm(registrationManager, result);

            registrationDialog.ShowDialog();

            var pollResult = registrationDialog.PollResponse;

            if (pollResult != null)
            {
                SaveScannerRegistration(new CloudScanner
                {
                    Id   = result.ScannerId,
                    Name = scannerInfo.Name,
                    AuthorizationToken = pollResult.AuthorizationToken,
                    RefreshToken       = pollResult.RefreshToken
                });
            }
        }
Example #2
0
        private void LoadCloudScanners(TwainCloudTokens cloudTokens)
        {
            var apiRoot            = CloudManager.GetCloudApiRoot();
            var client             = new TwainCloudClient(apiRoot, cloudTokens);
            var applicationManager = new ApplicationManager(client);

            applicationManager.GetScanners().ContinueWith(task =>
            {
                var scanners = task.Result;
                foreach (var s in scanners)
                {
                    ListViewItem listviewitem = new ListViewItem
                                                (
                        new[] {
                        s.Name,
                        s.Description,
                        s.Manufacturer,
                        CloudManager.GetScannerCloudUrl(s),
                        "(no ip)"
                    }
                                                );

                    listviewitem.Tag = s;
                    AddCloudScanner(listviewitem);

                    // Fix our buttons...
                    SetButtons(ButtonState.Devices);
                }
            });
        }
Example #3
0
        /// <summary>
        /// Start polling for tasks...
        /// </summary>
        /// <returns>true on success</returns>
        public async Task <bool> MonitorTasksStart()
        {
            bool blSuccess;

            CloudScanner cloudScanner = null;

            using (var context = new CloudContext())
            {
                cloudScanner = context.Scanners.First();
            }

            var cloudClient = new TwainCloudClient(CloudManager.GetCloudApiRoot(), new TwainCloudTokens(cloudScanner.AuthorizationToken, cloudScanner.RefreshToken));

            cloudClient.TokensRefreshed += (sender, args) =>
            {
                cloudScanner.AuthorizationToken = args.Tokens.AuthorizationToken;
                cloudScanner.RefreshToken       = args.Tokens.RefreshToken;
                SaveScannerRegistration(cloudScanner);
            };

            // Start monitoring for commands...
            blSuccess = await m_twainlocalscannerdevice.DeviceHttpServerStart(new DeviceSession(cloudClient, cloudScanner.Id));

            if (!blSuccess)
            {
                Log.Error("DeviceHttpServerStart failed...");
                return(false);
            }

            // All done...
            return(true);
        }
Example #4
0
        private async void registerButton_Click(object sender, EventArgs e)
        {
            var client = new TwainCloudClient(Constants.ApiRoot);
            var registrationManager = new RegistrationManager(client);

            var scannerInfo = new RegistrationRequest
            {
                Name         = "Virtual Scanner",
                Description  = "Home Virtual Scanner",
                Manufacturer = "Home & Co"
            };

            var result = await registrationManager.Register(scannerInfo);

            var registrationDialog = new RegistrationForm(registrationManager, result);

            registrationDialog.ShowDialog();

            var pollResult = registrationDialog.PollResponse;

            if (pollResult != null)
            {
                SaveScannerRegistration(new CloudScanner
                {
                    Id   = result.ScannerId,
                    Name = scannerInfo.Name,
                    AuthorizationToken = pollResult.AuthorizationToken,
                    RefreshToken       = pollResult.RefreshToken
                });

                LoadRegisteredScanners();
            }
        }
Example #5
0
        private void loginButton_Click(object sender, EventArgs e)
        {
            var loginPopup = new FacebookLoginForm(Constants.LoginUrl);

            loginPopup.Authorized += async(_, args) =>
            {
                loginPopup.Close();
                var client = new TwainCloudClient(Constants.ApiRoot, args.Tokens);
                _applicationManager = new ApplicationManager(client);
                await LoadScanners();
            };

            loginPopup.ShowDialog();
        }
Example #6
0
        /// <summary>
        /// Start polling for tasks...
        /// </summary>
        /// <returns>true on success</returns>
        public async Task <bool> MonitorTasksStart()
        {
            DeviceSession devicesession    = null;
            string        szCloudApiRoot   = "";
            string        szCloudScannerId = "";

            var cloudScanner = GetCurrentCloudScanner();

            // If cloud fails, we should keep going so that the
            // user can still run TWAIN Local...
            if (cloudScanner != null)
            {
                try
                {
                    szCloudApiRoot = CloudManager.GetCloudApiRoot();
                    var twaincloudtokens = new TwainCloudTokens(cloudScanner.AuthorizationToken, cloudScanner.RefreshToken);
                    var twaincloudclient = new TwainCloudClient(szCloudApiRoot, twaincloudtokens);
                    twaincloudclient.TokensRefreshed += (sender, args) =>
                    {
                        cloudScanner.AuthorizationToken = args.Tokens.AuthorizationToken;
                        cloudScanner.RefreshToken       = args.Tokens.RefreshToken;
                        SaveScannerRegistration(cloudScanner);
                    };
                    devicesession    = new DeviceSession(twaincloudclient, cloudScanner.Id);
                    szCloudScannerId = cloudScanner.Id;
                }
                catch (Exception exception)
                {
                    Log.Error("MonitorTasksStart: failed to initialize cloud, has it been registered? - " + exception.Message);
                    devicesession = null;
                }
            }

            // Start monitoring for commands...
            var blSuccess = await m_twainlocalscannerdevice.DeviceHttpServerStart(devicesession, szCloudApiRoot, szCloudScannerId);

            if (!blSuccess)
            {
                Log.Error("MonitorTasksStart: DeviceHttpServerStart failed...");
                return(false);
            }

            // All done...
            return(true);
        }
Example #7
0
        /// <summary>
        /// Take us through the signin process...
        /// </summary>
        /// <param name="a_szApiRoot">our cloud url</param>
        /// <param name="a_szSigninUrl">our signin url</param>
        /// <returns></returns>
        public TwainLocalScannerClient Signin(string a_szApiRoot, string a_szSigninUrl)
        {
            // Cleanup...
            m_twaincloudclient = null;
            m_twaincloudtokens = null;
            if (m_twainlocalscannerclient != null)
            {
                m_twainlocalscannerclient.Dispose();
                m_twainlocalscannerclient = null;
            }

            // Remember stuff...
            m_szApiRoot   = a_szApiRoot;
            m_szSigninUrl = a_szSigninUrl;

            // Signin...
            AutoResetEvent autoresetevent = new AutoResetEvent(false);

            Invoke(new MethodInvoker(delegate {
                FacebookLoginForm facebookloginform = new FacebookLoginForm(m_szSigninUrl);
                facebookloginform.Authorized       += async(_, args) =>
                {
                    // Form goes bye-bye...
                    facebookloginform.Close();

                    // Squirrel this away...
                    m_twaincloudtokens = args.Tokens;

                    // Put the authorization bearer token where we can get it...
                    m_twainlocalscannerclient = new TwainLocalScannerClient(EventCallback, this, false);
                    m_twaincloudclient        = new TwainCloudClient(m_szApiRoot, m_twaincloudtokens);
                    await m_twainlocalscannerclient.ConnectToCloud(m_twaincloudclient);
                    m_twainlocalscannerclient.m_dictionaryExtraHeaders.Add("Authorization", m_twaincloudtokens.AuthorizationToken);
                    autoresetevent.Set();
                };
            }));
            autoresetevent.WaitOne();

            // All done...
            return(m_twainlocalscannerclient);
        }
Example #8
0
        private bool LoadCloudScanners(TwainCloudTokens a_twaincloudtokens)
        {
            // The user hasn't identified themselves...
            if (a_twaincloudtokens == null)
            {
                return(false);
            }

            var apiRoot            = CloudManager.GetCloudApiRoot();
            var client             = new TwainCloudClient(apiRoot, a_twaincloudtokens);
            var applicationManager = new ApplicationManager(client);

            applicationManager.GetScanners().ContinueWith(task =>
            {
                var scanners = task.Result;
                foreach (var s in scanners)
                {
                    ListViewItem listviewitem = new ListViewItem
                                                (
                        new[] {
                        s.Name,
                        s.Id,
                        s.Description,
                        CloudManager.GetScannerCloudUrl(s),
                        "(no ip)"
                    }
                                                );

                    listviewitem.Tag = s;
                    AddCloudScanner(listviewitem);

                    // Fix our buttons...
                    SetButtons(ButtonState.Devices);
                }
            });

            // All done, there's no guarantee that we have any
            // cloud scanners, but since the user identified
            // themselves, we can proceed...
            return(true);
        }
Example #9
0
        /// <summary>
        /// Register a device with cloud infrastructure.
        /// </summary>
        public async Task RegisterCloudScanner()
        {
            try
            {
                var client = new TwainCloudClient(CloudManager.GetCloudApiRoot());
                var registrationManager = new RegistrationManager(client);

                var scannerInfo = new RegistrationRequest
                {
                    Name        = GetTwainLocalTy(),
                    Description = GetTwainLocalNote()
                };

                var result = await registrationManager.Register(scannerInfo);

                var registrationDialog = new RegistrationForm(registrationManager, result);
                registrationDialog.ShowDialog();

                var pollResult = registrationDialog.PollResponse;
                if (pollResult != null)
                {
                    var cloudScanner = new CloudScanner
                    {
                        Id   = result.ScannerId,
                        Name = $"{scannerInfo.Name} ({scannerInfo.Description})",
                        AuthorizationToken = pollResult.AuthorizationToken,
                        RefreshToken       = pollResult.RefreshToken
                    };

                    SaveScannerRegistration(cloudScanner);
                    SetCurrentCloudScanner(cloudScanner);
                }
            }
            catch (Exception exception)
            {
                Log.Error("RegisterCloudScanner exception - " + exception.Message);
            }
        }
Example #10
0
        private async void connectButton_Click_1(object sender, EventArgs e)
        {
            var scanner = registeredDevicesComboBox.SelectedItem as CloudScanner;

            var tokens = new TwainCloudTokens(scanner.AuthorizationToken, scanner.RefreshToken);
            var client = new TwainCloudClient(Constants.ApiRoot, tokens);

            client.TokensRefreshed += (o, args) =>
            {
                scanner.AuthorizationToken = args.Tokens.AuthorizationToken;
                scanner.RefreshToken       = args.Tokens.RefreshToken;
                SaveScannerRegistration(scanner);
            };

            var deviceSession = new DeviceSession(client, scanner.Id);

            deviceSession.Received += async(o, message) =>
            {
                Debug.WriteLine("cloud message received: " + message);
                await deviceSession.Send("message received");
            };

            await deviceSession.Connect(scanner.Id);
        }
Example #11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DeviceSession"/> class.
 /// </summary>
 /// <param name="client">Initialized TWAIN Cloud client.</param>
 /// <param name="scannerId">Scanner Id.</param>
 public DeviceSession(TwainCloudClient client, string scannerId)
 {
     _client    = client;
     _scannerId = scannerId;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="RegistrationManager"/> class.
 /// </summary>
 /// <param name="client">Initialized TWAIN Cloud client.</param>
 public RegistrationManager(TwainCloudClient client)
 {
     _client = client;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="ApplicationManager"/> class.
 /// </summary>
 /// <param name="client">Initialized TWAIN Cloud client.</param>
 public ApplicationManager(TwainCloudClient client)
 {
     _client = client;
 }