public static void ReportProgress(UcwaAppProgressReportEventHandler progressReporter, string msg, HttpStatusCode status = HttpStatusCode.OK)
 {
     if (progressReporter != null)
     {
         progressReporter(msg, status);
     }
 }
Exemple #2
0
        public async Task <HttpStatusCode> SignIn(string userName, string password, UcwaAppAuthenticationTypes authType = UcwaAppAuthenticationTypes.Password)
        {
            this.userName           = userName;
            this.password           = password;
            this.authenticationType = authType;
            try
            {
                var opResult = await DiscoverRootResource(this.discoverFromInternalDomain);

                if (opResult.Resource == null)
                {
                    UcwaAppUtils.ReportProgress(OnProgressReported, "GetRootResource returns null result.", opResult.StatusCode);
                    return(opResult.StatusCode);
                }

                opResult = await GetUserResource(opResult.Resource.GetLinkUri("user"), userName, password, authType);

                if (opResult.Resource == null)
                {
                    UcwaAppUtils.ReportProgress(OnProgressReported,
                                                userName + " cannot be authenticated, with the " + authType.ToString() + " grant_type.",
                                                opResult.StatusCode);
                    return(opResult.StatusCode);
                }
                // Create the UCWA application bound to the specified user
                opResult = await GetApplicationResource(opResult.Resource);

                if (opResult.Resource == null)
                {
                    UcwaAppUtils.ReportProgress(OnProgressReported, "Failed to create the UCWA application resource.", opResult.StatusCode);
                    return(opResult.StatusCode);
                }

                this.ApplicationResource = opResult.Resource;
                UcwaAppUtils.ReportProgress(OnProgressReported, "Succeded in creating the application resource: " + this.ApplicationResource.Uri);

                // Setup and start event channel
                var eventsUri = this.ApplicationResource.GetLinkUri("events");
                this.EventChannel = new UcwaAppEventChannel(eventsUri, this.Transport);
                this.EventChannel.OnEventNotificationsReceived += EventChannel_OnEventNotificationsReceived;
                this.EventChannel.OnErrorReported      += EventChannel_OnErrorReported;
                this.EventChannel.OnEventChannelClosed += EventChannel_OnEventChannelClosed;
                this.EventChannel.OnProgressReported   += EventChannel_OnProgressReported;
                this.EventChannel.Start();
                UcwaAppUtils.ReportProgress(OnProgressReported, "Event channel started on " + eventsUri);

                // Make me available to receiving incoming alerts
                this.Me = new UcwaAppMe(this);
                var result = await this.Me.PostMakeMeAvailable("4255552222", "Online", new string[] { "Plain", "Html" }, new string[] { "PhoneAudio", "Messaging" });

                if (result.StatusCode != HttpStatusCode.NoContent)
                {
                    UcwaAppUtils.ReportProgress(OnProgressReported, "Failed to post to makeMeAvailable resource.", result.StatusCode);
                    return(result.StatusCode);
                }

                // Get application resource again to receive any updates triggered by the POST request to making me available
                opResult = await GetApplicationResource(this.ApplicationResource.Uri);

                if (opResult.Resource == null)
                {
                    UcwaAppUtils.ReportProgress(OnProgressReported, "Failed to get the updated application resource", opResult.StatusCode);
                    return(opResult.StatusCode);
                }
                this.ApplicationResource = opResult.Resource;
                await this.Me.Refresh(this.ApplicationResource.GetEmbeddedResourceUri("me"));

                // Set up a timer to post on reportMyActivity every four minutes
                timer = new Timer((e) =>
                                  { ReportMyActivity(this.ApplicationResource.GetEmbeddedResource("me").GetLinkUri("reportMyActivity")); },
                                  null,
                                  new TimeSpan(0, 0, 0),
                                  new TimeSpan(0, 4, 0)
                                  );
            }
            catch (Exception ex)
            {
                UcwaAppUtils.ReportError(OnErrorReported, ex);
                return(HttpStatusCode.BadRequest);
            }
            return(HttpStatusCode.OK);
        }