Exemple #1
0
        public static async Task <DBlocalContact> contactLookup(string contactUID)
        {
            Console.Error.WriteLine("Lookup started at: {0:HH:mm:ss.ffff}", DateTime.Now);

            DBAppInfo appInfo = DBLocalDataStore.GetInstance().GetAppInfo();

            var user = DBLocalDataStore.GetInstance().GetLocalUserInfo();

            LookupContext lookup = new LookupContext(contactUID);

            lookup.context = new RequestData();
            var output = lookup.context;

            output.password          = user.password;
            output.username          = user.username;
            output.profile           = DBLocalDataStore.GetInstance().GetSelectProfile().shortName;
            output.tags              = new string[] { user.tags };
            output.campaignReference = appInfo.campaignReference;

            var jsonUser = JsonConvert.SerializeObject(lookup);

            var httpWebRequest = (HttpWebRequest)WebRequest.Create(ServerURLs.contactLookupURL);

            try
            {
                //throw new Exception("TEST EXCEPTION");
                httpWebRequest.ContentType = "application/json";
                httpWebRequest.Method      = "POST";
                httpWebRequest.Timeout     = 30000;
                httpWebRequest.KeepAlive   = false;

                using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                {
                    streamWriter.Write(jsonUser);
                }

                var response = await httpWebRequest.GetResponseAsync();

                return(NetworkRequests.getContactFromLookup(response));
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex.Message);
                throw;
            }
            finally
            {
                Console.Error.WriteLine("Lookup finished at: {0:HH:mm:ss.ffff}", DateTime.Now);
                httpWebRequest.Abort();
            }
        }
Exemple #2
0
        public static async Task GetContacts(StatusDownload loghandler)
        {
            SyncContext scr = new SyncContext();

            DBAppInfo appInfo = DBLocalDataStore.GetInstance().GetAppInfo();

            var user = DBLocalDataStore.GetInstance().GetLocalUserInfo();

            scr.context = new RequestData();
            var output = scr.context;

            output.password          = user.password;
            output.username          = user.username;
            output.profile           = DBLocalDataStore.GetInstance().GetSelectProfile().shortName;
            output.tags              = new string[] { user.tags };
            output.campaignReference = appInfo.campaignReference;

            var jsonUser = JsonConvert.SerializeObject(scr);

            var httpWebRequest = (HttpWebRequest)WebRequest.Create(ServerURLs.contactsURLNew);

            try
            {
                //throw new Exception("TEST EXCEPTION");
                httpWebRequest.ContentType = "application/json";
                httpWebRequest.Method      = "POST";
                httpWebRequest.Timeout     = 30000;
                httpWebRequest.KeepAlive   = false;

                using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                {
                    streamWriter.Write(jsonUser);
                }

                var response = await httpWebRequest.GetResponseAsync();

                NetworkRequests.GetContactsFromResponse(response, loghandler);
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex.Message);
                throw;
            }
            finally
            {
                httpWebRequest.Abort();
            }
        }
Exemple #3
0
        public static async Task GetFilesAndForms(StatusDownloadFilesAndForms loghandler)
        {
            RequestData requestData = new RequestData();
            var         user        = DBLocalDataStore.GetInstance().GetLocalUserInfo();

            requestData.password = user.password;
            requestData.username = user.username;
            requestData.profile  = DBLocalDataStore.GetInstance().GetSelectProfile().shortName;
            requestData.tags     = new string[] { user.tags };

            var jsonUser = JsonConvert.SerializeObject(requestData);

            var httpWebRequest = (HttpWebRequest)WebRequest.Create(ServerURLs.filesAndFormsURL);

            try
            {
                httpWebRequest.ContentType = "application/json";
                httpWebRequest.Method      = "POST";
                httpWebRequest.ServicePoint.ConnectionLimit = 3;
                httpWebRequest.Timeout   = 30000;
                httpWebRequest.KeepAlive = false;

                using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                {
                    streamWriter.Write(jsonUser);
                }

                var response = await httpWebRequest.GetResponseAsync();

                NetworkRequests.GetFilesAndFormsFromResponse(response, loghandler);
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex.Message);
                throw;
            }
            finally
            {
                httpWebRequest.Abort();
            }
        }
Exemple #4
0
        public static async Task SyncDataServer()
        {
            var SyncData = DBLocalDataStore.GetInstance().getSyncRequests().Where(s => !s.isSent).ToList();
            var user     = DBLocalDataStore.GetInstance().GetLocalUserInfo();
            List <CustomerType> receivedContacts = new List <CustomerType>();

            if (user.invalidPassword)
            {
                return;
            }

            if (SyncData.Count > 0 && NetworkRequests.onBeginSync != null)
            {
                NetworkRequests.onBeginSync();
            }

            var           appInfo     = DBLocalDataStore.GetInstance().GetAppInfo();
            DBAppSettings appSettings = DBLocalDataStore.GetInstance().GetAppSettings();

            for (int i = 0; i < SyncData.Count; i++)
            {
                string jsonUser = SyncData[i].serializedSyncContext;
                var    data     = JsonConvert.DeserializeObject <SyncContext>(jsonUser);

                data.context = new RequestData();
                var context = data.context;
                context.password = user.password;
                //context.password = "******";
                context.username          = user.username;
                context.profile           = DBLocalDataStore.GetInstance().GetSelectProfile().shortName;
                context.tags              = new string[] { user.tags };
                context.campaignReference = appInfo.campaignReference;

                JsonSerializerSettings serializationSettings = new JsonSerializerSettings();
                serializationSettings.DefaultValueHandling = DefaultValueHandling.Ignore;

                jsonUser = JsonConvert.SerializeObject(data, Formatting.Indented, serializationSettings);

                Console.WriteLine("Sending to sync server:\n{0}", jsonUser);

                var httpWebRequest = (HttpWebRequest)WebRequest.Create(ServerURLs.contactsURLNew);
                try
                {
                    //throw new Exception("TEST EXCEPTION");
                    httpWebRequest.ContentType     = "application/json";
                    httpWebRequest.Method          = "POST";
                    httpWebRequest.Timeout         = 30000;
                    httpWebRequest.KeepAlive       = false;
                    httpWebRequest.PreAuthenticate = false;

                    using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                    {
                        streamWriter.Write(jsonUser);
                    }

                    var response = await httpWebRequest.GetResponseAsync();

                    using (var streamReader = new StreamReader(response.GetResponseStream()))
                    {
                        var result = streamReader.ReadToEnd();

                        Console.Error.WriteLine("Received from sync server:\n{0}", result);

                        var receivedData = JsonConvert.DeserializeObject <CustomerSyncResult>(result);
                        if (receivedData.success)
                        {
                            SyncData[i].isSent = true;
                            DBLocalDataStore.GetInstance().updateSyncReqest(SyncData[i]);
                            if (appSettings.getSharedContacts)
                            {
                                receivedContacts.AddRange(receivedData.contacts);
                            }
                        }
                        else
                        {
                            user.invalidPassword = true;
                            DBLocalDataStore.GetInstance().AddUserInfo(user);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Sync exception: {0}", ex.Message);
                    if (NetworkRequests.onFailSync != null)
                    {
                        onFailSync();
                    }
                    throw;
                }
                finally
                {
                    httpWebRequest.Abort();
                }
            }

            if (receivedContacts.Count > 0)
            {
                //CustomerTypeComparer comparer = new CustomerTypeComparer();
                //receivedContacts = receivedContacts.Distinct(comparer).ToList();

                NetworkRequests.UpdateContactsFromResponse(receivedContacts);

                if (NetworkRequests.onFinishSync != null)
                {
                    onFinishSync();
                }
            }
            else
            {
                if (NetworkRequests.onFailSync != null)
                {
                    onFailSync();
                }
            }
        }
Exemple #5
0
        public static async Task sendBadge(DBlocalContact contact, string URL)
        {
            try
            {
                var uri = new Uri(URL);
                uri.ToString();
            }
            catch
            {
                throw new ArgumentException("Invalid webhook URL");
            }
            SyncContext scr = new SyncContext();

            scr.context  = new RequestData();
            scr.contacts = new List <CustomerType>();
            scr.contacts.Add(new CustomerType(contact));

            var appInfo = DBLocalDataStore.GetInstance().GetAppInfo();
            var user    = DBLocalDataStore.GetInstance().GetLocalUserInfo();
            var context = scr.context;

            context.password          = user.password;
            context.username          = user.username;
            context.profile           = DBLocalDataStore.GetInstance().GetSelectProfile().shortName;
            context.tags              = new string[] { user.tags };
            context.campaignReference = appInfo.campaignReference;

            string json = JsonConvert.SerializeObject(scr, Formatting.Indented);

            Console.WriteLine("Trying to send to webhook:\n{0}", json);

            //var httpWebRequest = (HttpWebRequest)WebRequest.Create(URL);
            var httpWebRequest = WebRequest.CreateHttp(URL);

            try
            {
                //throw new Exception("TEST EXCEPTION");
                httpWebRequest.ContentType = "application/json";
                httpWebRequest.Method      = "POST";
                httpWebRequest.Timeout     = 30000;
                httpWebRequest.ServicePoint.ConnectionLimit = 3;
                httpWebRequest.KeepAlive = false;

                using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                {
                    streamWriter.Write(json);
                }

                var response = await httpWebRequest.GetResponseAsync();

                using (var streamReader = new StreamReader(response.GetResponseStream()))
                {
                    var result = streamReader.ReadToEnd();

                    Console.WriteLine("Received from webhook:\n{0}", result);

                    var settings = new JsonSerializerSettings();
                    settings.MissingMemberHandling = MissingMemberHandling.Ignore;
                    var receivedData = JsonConvert.DeserializeObject <CustomerSyncResult>(result, settings);
                    if (NetworkRequests._onFinishPrint != null)
                    {
                        NetworkRequests._onFinishPrint(receivedData.success);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Webhook exception: {0}", ex.Message);
                if (NetworkRequests._onFinishPrint != null)
                {
                    NetworkRequests._onFinishPrint(false);
                }
                //throw;
            }
            finally
            {
                httpWebRequest.Abort();
            }
        }
Exemple #6
0
        public static async Task <DBlocalContact> didScanBarcode(string barcode_, string symbology, StatusLookup callback)
        {
            if (callback == null)
            {
                throw new ArgumentNullException("Callback must not be null to handle result");
            }

            var barcode = barcode_.Trim();

            DBlocalContact temporaryContact = null;

            if (symbology == "QR")
            {
                if (!OfflineLogic.isBarcodeValid(barcode))
                {
                    throw new InvalidOperationException("The QR code cannot be used by BoaBee because it is not a contact or an ID.");
                }
                else if (barcode.Contains("BEGIN:VCARD"))
                {
                    //VCard
                    temporaryContact = OfflineLogic.parseAsVCard(barcode);
                }
                else if (barcode.StartsWith("[[") && barcode.EndsWith("]]"))
                {
                    //Artexis
                    temporaryContact = OfflineLogic.parseAsArtexis(barcode);
                }
                else
                {
                    //unknown format
                    temporaryContact     = new DBlocalContact();
                    temporaryContact.uid = barcode;
                }
            }
            else
            {
                //unknown format
                temporaryContact     = new DBlocalContact();
                temporaryContact.uid = barcode;
            }
            DBAppSettings appSettings = DBLocalDataStore.GetInstance().GetAppSettings();

            if (appSettings == null)
            {
                appSettings = new DBAppSettings();
                appSettings.instantContactCheck = true;
                DBLocalDataStore.GetInstance().SetAppSettings(appSettings);
            }

            string message = string.Empty;

            if (appSettings.instantContactCheck)
            {
                var localContact = DBLocalDataStore.GetInstance().GetLocalContactsByUID(temporaryContact.uid);
                if (localContact != null)
                {
                    temporaryContact   &= localContact;
                    temporaryContact.Id = localContact.Id;
                }

                if (await Reachability.isConnected())
                {
                    try
                    {
                        int timeout = 10000;
                        var timeoutCancellationTokenSource = new CancellationTokenSource();
                        var timeoutTask = Task.Delay(timeout, timeoutCancellationTokenSource.Token);

                        var task = NetworkRequests.contactLookup(temporaryContact.uid);

                        DBlocalContact serverContact = null;
                        if (await Task.WhenAny(task, timeoutTask) == task)
                        {
                            // Task completed within timeout.
                            // Consider that the task may have faulted or been canceled.
                            // We re-await the task so that any exceptions/cancellation is rethrown.

                            timeoutCancellationTokenSource.Cancel();

                            serverContact = await task;
                            if (serverContact != null)
                            {
                                if (serverContact.hasOnlyUID() || serverContact.hasOnlyUIDAndName())
                                {
                                    message = "Check for additional contact details can't be executed at this moment. More details will be added to your report if available.";
                                }
                                else
                                {
                                    message = "These are the details we have found for you. Feel free to complete the rest below.";
                                }
                            }
                            else
                            {
                                message = "Check for additional contact details can't be executed at this moment. More details will be added to your report if available.";
                            }
                            Console.Error.WriteLine("Server has contact: {0}", serverContact != null);
                        }
                        else
                        {
                            // timeout/cancellation logic
                            message = "Check for additional contact details can't be executed at this moment. More details will be added to your report if available.";
                            Console.Error.WriteLine("Timed out");
                        }

                        temporaryContact *= serverContact;
                    }
                    catch (Exception e)
                    {
                        message = "Check for additional contact details can't be executed at this moment. More details will be added to your report if available.";
                        Console.Error.WriteLine("contactLookup exception: {0}", e.Message);
                    }
                }
                else
                {
                    message = "Check for additional contact details can't be executed at this moment. More details will be added to your report if available.";
                }
            }
            else
            {
                message = "Instant contact checking is turned off";
            }
            callback(message);
            return(temporaryContact);
        }