Example #1
0
        protected static async Task HandleTrackInfoAsync(LocalSpotifyTrackInfo localTrackInfo)
        {
            try
            {
                if (!SoftwareHttpManager.HasSpotifyAccessToken())
                {
                    // initialize the token
                    await SoftwareHttpManager.InitializeSpotifyClientGrantAsync();
                }
            } catch (Exception e)
            {
                Logger.Error("Code Time: Unable to access spotify, error: " + e.Message);
                return;
            }

            if (!SoftwareHttpManager.HasSpotifyAccessToken())
            {
                return;
            }

            bool hasLocalTrackData = (localTrackInfo.name != null && localTrackInfo.artist != null)
                ? true : false;
            bool hasCurrentTrackData = (CurrentTrackInfo != null && CurrentTrackInfo.name != null && CurrentTrackInfo.artist != null)
                ? true : false;
            bool isNewTrack = true;

            if (hasLocalTrackData && hasCurrentTrackData &&
                localTrackInfo.name.Equals(CurrentTrackInfo.name) &&
                localTrackInfo.artist.Equals(CurrentTrackInfo.artist))
            {
                isNewTrack = false;
            }

            HttpResponseMessage response = null;

            try
            {
                if (isNewTrack && hasLocalTrackData)
                {
                    if (hasCurrentTrackData)
                    {
                        // close the previous track
                        CurrentTrackInfo.end = SoftwareCoUtil.getNowInSeconds();
                        // send it to the app server
                        response = await SoftwareHttpManager.SendRequestAsync(
                            HttpMethod.Post, "/data/music", CurrentTrackInfo.GetAsJson());
                    }
                    // fill in the missing attributes from the spotify API
                    await SoftwareHttpManager.GetSpotifyTrackInfoAsync(localTrackInfo);

                    // send it to the app server
                    response = await SoftwareHttpManager.SendRequestAsync(
                        HttpMethod.Post, "/data/music", localTrackInfo.GetAsJson());

                    CurrentTrackInfo = localTrackInfo.Clone();
                }
                else if (hasCurrentTrackData && !hasLocalTrackData)
                {
                    // send this to close it
                    CurrentTrackInfo.end = SoftwareCoUtil.getNowInSeconds();
                    // send it to the app server
                    response = await SoftwareHttpManager.SendRequestAsync(
                        HttpMethod.Post, "/data/music", CurrentTrackInfo.GetAsJson());

                    CurrentTrackInfo = null;
                }
            } catch (Exception e) {
                Logger.Error("Code Time: Unable to process track information, error: " + e.Message);
            }

            if (response != null && !SoftwareHttpManager.IsOk(response))
            {
                Logger.Error(response.ToString());
            }
        }