예제 #1
0
        public void UpdateGlobal( )
        {
            Context.NotifySpeckleFrame("client-log", StreamId, JsonConvert.SerializeObject("Global update received."));
            try
            {
                var streamGetResponse = Client.StreamGetAsync(StreamId, null).Result;
                if (streamGetResponse.Success == false)
                {
                    Context.NotifySpeckleFrame("client-error", StreamId, streamGetResponse.Message);
                    // TODO
                    // Try and get from cache
                    // First stream
                    // Then objects
                }
                else
                {
                    Client.Stream = streamGetResponse.Resource;
                    Context.NotifySpeckleFrame("client-metadata-update", StreamId, Client.Stream.ToJson());
                    Context.NotifySpeckleFrame("client-is-loading", StreamId, "");

                    // add or update the newly received stream in the cache.
                    LocalContext.AddOrUpdateStream(Client.Stream, Client.BaseUrl);

                    // pass the object list through a cache check
                    LocalContext.GetCachedObjects(Client.Stream.Objects, Client.BaseUrl);

                    // filter out the objects that were not in the cache and still need to be retrieved
                    var payload = Client.Stream.Objects.Where(o => o.Type == "Placeholder").Select(obj => obj._id).ToArray();

                    // how many objects to request from the api at a time
                    int maxObjRequestCount = 20;

                    // list to hold them into
                    var newObjects = new List <SpeckleObject>();

                    // jump in `maxObjRequestCount` increments through the payload array
                    for (int i = 0; i < payload.Length; i += maxObjRequestCount)
                    {
                        // create a subset
                        var subPayload = payload.Skip(i).Take(maxObjRequestCount).ToArray();

                        // get it sync as this is always execed out of the main thread
                        var res = Client.ObjectGetBulkAsync(subPayload, "omit=displayValue").Result;

                        // put them in our bucket
                        newObjects.AddRange(res.Resources);
                        Context.NotifySpeckleFrame("client-log", StreamId, JsonConvert.SerializeObject(String.Format("Got {0} out of {1} objects.", i, payload.Length)));
                    }

                    // populate the retrieved objects in the original stream's object list
                    foreach (var obj in newObjects)
                    {
                        var locationInStream = Client.Stream.Objects.FindIndex(o => o._id == obj._id);
                        try { Client.Stream.Objects[locationInStream] = obj; } catch { }
                    }

                    // add objects to cache async
                    Task.Run(() =>
                    {
                        foreach (var obj in newObjects)
                        {
                            LocalContext.AddCachedObject(obj, Client.BaseUrl);
                        }
                    });

                    DisplayContents();
                    Context.NotifySpeckleFrame("client-done-loading", StreamId, "");
                }
            }
            catch (Exception err)
            {
                Context.NotifySpeckleFrame("client-error", Client.Stream.StreamId, JsonConvert.SerializeObject(err.Message));
                Context.NotifySpeckleFrame("client-done-loading", StreamId, "");
                return;
            }
        }
예제 #2
0
        public virtual void UpdateGlobal( )
        {
            if (IsUpdating)
            {
                this.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "New update received while update was in progress. Please refresh.");
                return;
            }

            IsUpdating = true;

            var getStream = Client.StreamGetAsync(Client.StreamId, null).Result;

            NickName = getStream.Resource.Name;
            Layers   = getStream.Resource.Layers.ToList();

            Client.Stream = getStream.Resource;

            // add or update the newly received stream in the cache.
            LocalContext.AddOrUpdateStream(Client.Stream, Client.BaseUrl);

            this.Message = "Getting objects!";

            // pass the object list through a cache check
            LocalContext.GetCachedObjects(Client.Stream.Objects, Client.BaseUrl);

            // filter out the objects that were not in the cache and still need to be retrieved
            var payload = Client.Stream.Objects.Where(o => o.Type == "Placeholder").Select(obj => obj._id).ToArray();

            // how many objects to request from the api at a time
            int maxObjRequestCount = 42;

            // list to hold them into
            var newObjects = new List <SpeckleObject>();

            // jump in `maxObjRequestCount` increments through the payload array
            for (int i = 0; i < payload.Length; i += maxObjRequestCount)
            {
                // create a subset
                var subPayload = payload.Skip(i).Take(maxObjRequestCount).ToArray();

                // get it sync as this is always execed out of the main thread
                var res = Client.ObjectGetBulkAsync(subPayload, "omit=displayValue").Result;

                // put them in our bucket
                newObjects.AddRange(res.Resources);
                this.Message = JsonConvert.SerializeObject(String.Format("{0}/{1}", i, payload.Length));
            }

            foreach (var obj in newObjects)
            {
                var locationInStream = Client.Stream.Objects.FindIndex(o => o._id == obj._id);
                try { Client.Stream.Objects[locationInStream] = obj; } catch { }
            }

            // add objects to cache async
            Task.Run(() =>
            {
                foreach (var obj in newObjects)
                {
                    LocalContext.AddCachedObject(obj, Client.BaseUrl);
                }
            });

            // set ports
            UpdateOutputStructure();

            this.Message = "Converting...";

            SpeckleObjects.Clear();

            ConvertedObjects = SpeckleCore.Converter.Deserialise(Client.Stream.Objects);

            this.Message = "Got data\n@" + DateTime.Now.ToString("hh:mm:ss");

            IsUpdating = false;
            Rhino.RhinoApp.MainApplicationWindow.Invoke(expireComponentAction);
        }