private async Task CreateLocationDataRequest(bool urgent)
        {
            // Don't send anything unless we're setup
            if (!Settings.WeatherLoaded)
            {
                return;
            }

            if (mWearNodesWithApp == null)
            {
                // Create requests if nodes exist with app support
                mWearNodesWithApp = await FindWearDevicesWithApp();

                if (mWearNodesWithApp == null || mWearNodesWithApp.Count == 0)
                {
                    return;
                }
            }

            PutDataMapRequest mapRequest = PutDataMapRequest.Create(WearableHelper.LocationPath);
            var homeData = Settings.HomeData;

            mapRequest.DataMap.PutString("locationData", homeData?.ToJson());
            mapRequest.DataMap.PutLong("update_time", DateTime.UtcNow.Ticks);
            PutDataRequest request = mapRequest.AsPutDataRequest();

            if (urgent)
            {
                request.SetUrgent();
            }
            await WearableClass.DataApi.PutDataItem(mGoogleApiClient, request);

            Logger.WriteLine(LoggerLevel.Info, "{0}: CreateLocationDataRequest(): urgent: {1}", TAG, urgent.ToString());
        }
Пример #2
0
        /**
         * Transfer the required data over to the wearable
         * @param attractions list of attraction data to transfer over
         */
        private void SendDataToWearable(List <Attraction> attractions)
        {
            GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this)
                                              .AddApi(WearableClass.API)
                                              .Build();

            // It's OK to use blockingConnect() here as we are running in an
            // IntentService that executes work on a separate (background) thread.
            ConnectionResult connectionResult = googleApiClient.BlockingConnect(
                Constants.GOOGLE_API_CLIENT_TIMEOUT_S, TimeUnit.Seconds);

            // Limit attractions to send
            int count = attractions.Count > Constants.MAX_ATTRACTIONS ?
                        Constants.MAX_ATTRACTIONS : attractions.Count;

            List <DataMap> attractionsData = new List <DataMap>(count);

            for (int i = 0; i < count; i++)
            {
                Attraction attraction = attractions[i];

                Bitmap image          = null;
                Bitmap secondaryImage = null;

                try
                {
                    //TODO:
                    // Fetch and resize attraction image bitmap
                    //image = Glide.with(this)
                    //		.load(attraction.imageUrl)
                    //		.asBitmap()
                    //		.diskCacheStrategy(DiskCacheStrategy.SOURCE)
                    //		.into(Constants.WEAR_IMAGE_SIZE_PARALLAX_WIDTH, Constants.WEAR_IMAGE_SIZE)
                    //		.get();

                    //secondaryImage = Glide.with(this)
                    //		.load(attraction.secondaryImageUrl)
                    //		.asBitmap()
                    //		.diskCacheStrategy(DiskCacheStrategy.SOURCE)
                    //		.into(Constants.WEAR_IMAGE_SIZE_PARALLAX_WIDTH, Constants.WEAR_IMAGE_SIZE)
                    //		.get();
                }
                catch (Exception e)
                {
                    Log.Error("UtilityService", "Exception loading bitmap from network");
                }

                if (image != null && secondaryImage != null)
                {
                    DataMap attractionData = new DataMap();

                    string distance = Utils.FormatDistanceBetween(
                        Utils.GetLocation(this), attraction.Location);

                    attractionData.PutString(Constants.EXTRA_TITLE, attraction.Name);
                    attractionData.PutString(Constants.EXTRA_DESCRIPTION, attraction.Description);
                    attractionData.PutDouble(
                        Constants.EXTRA_LOCATION_LAT, attraction.Location.Latitude);
                    attractionData.PutDouble(
                        Constants.EXTRA_LOCATION_LNG, attraction.Location.Longitude);
                    attractionData.PutString(Constants.EXTRA_DISTANCE, distance);
                    attractionData.PutString(Constants.EXTRA_CITY, attraction.City);

                    //TODO:
                    //	attractionData.PutAsset(Constants.EXTRA_IMAGE,
                    //		Utils.CreateAssetFromBitmap(image));
                    //attractionData.PutAsset(Constants.EXTRA_IMAGE_SECONDARY,
                    //		Utils.CreateAssetFromBitmap(secondaryImage));

                    attractionsData.Add(attractionData);
                }
            }

            if (connectionResult.IsSuccess && googleApiClient.IsConnected &&
                attractionsData.Count > 0)
            {
                PutDataMapRequest dataMap = PutDataMapRequest.Create(Constants.ATTRACTION_PATH);
                dataMap.DataMap.PutDataMapArrayList(Constants.EXTRA_ATTRACTIONS, attractionsData);
                //TODO:
                //dataMap.DataMap.PutLong(Constants.EXTRA_TIMESTAMP, DateTime.Now.ToFileTime);
                PutDataRequest request = dataMap.AsPutDataRequest();
                request.SetUrgent();

                //TODO: Async/Await
                // Send the data over
                //            var result =
                //					WearableClass.DataApi.PutDataItem(googleApiClient, request));

                //            if (!result.).isSuccess()) {
                //                Log.e(TAG, String.format("Error sending data using DataApi (error code = %d)",
                //                        result.getStatus().getStatusCode()));
                //            }

                //} else {
                //            Log.e(TAG, String.format(Constants.GOOGLE_API_CLIENT_ERROR_MSG,
                //                    connectionResult.getErrorCode()));
                //        }
                googleApiClient.Disconnect();
            }
        }
        private async Task CreateWeatherDataRequest(bool urgent)
        {
            // Don't send anything unless we're setup
            if (!Settings.WeatherLoaded)
            {
                return;
            }

            if (mWearNodesWithApp == null)
            {
                // Create requests if nodes exist with app support
                mWearNodesWithApp = await FindWearDevicesWithApp();

                if (mWearNodesWithApp == null || mWearNodesWithApp.Count == 0)
                {
                    return;
                }
            }

            PutDataMapRequest mapRequest = PutDataMapRequest.Create(WearableHelper.WeatherPath);
            var homeData    = Settings.HomeData;
            var weatherData = await Settings.GetWeatherData(homeData.query);

            var alertData = await Settings.GetWeatherAlertData(homeData.query);

            if (weatherData != null)
            {
                weatherData.weather_alerts = alertData;

                // location
                // update_time
                // forecast
                // hr_forecast
                // txt_forecast
                // condition
                // atmosphere
                // astronomy
                // precipitation
                // ttl
                // source
                // query
                // locale

                mapRequest.DataMap.PutString("weatherData", weatherData?.ToJson());
                List <String> alerts = new List <String>();
                if (weatherData.weather_alerts.Count > 0)
                {
                    foreach (WeatherData.WeatherAlert alert in weatherData.weather_alerts)
                    {
                        alerts.Add(alert.ToJson());
                    }
                }
                mapRequest.DataMap.PutStringArrayList("weatherAlerts", alerts);
                mapRequest.DataMap.PutLong("update_time", weatherData.update_time.UtcTicks);
                PutDataRequest request = mapRequest.AsPutDataRequest();
                if (urgent)
                {
                    request.SetUrgent();
                }
                await WearableClass.DataApi.PutDataItem(mGoogleApiClient, request);

                Logger.WriteLine(LoggerLevel.Info, "{0}: CreateWeatherDataRequest(): urgent: {1}", TAG, urgent.ToString());
            }
        }