Exemple #1
0
        /// <inheritdoc/>
        public async Task <PagedResult <Sample> > GetSamplesAsync(Guid objectId, int attributeId, TimeFilter filter)
        {
            List <Sample> objectSamples = new List <Sample>();
            // Perform a generic call using objects resource valid for Network Devices as well
            var response = await GetPagedResultsAsync <JToken>("objects", ToDictionary(filter), objectId, "attributes", attributeId, "samples").ConfigureAwait(false);

            // Read full attribute from url
            foreach (JToken s in response.Items)
            {
                Sample sample   = new Sample();
                string unitsUrl = string.Empty;
                try
                {
                    if (Version < ApiVersion.v3)
                    {
                        sample.Timestamp  = s["timestamp"].Value <DateTime>();
                        sample.IsReliable = s["isReliable"].Value <Boolean>();
                        sample.Value      = s["value"]["value"].Value <double>();
                        unitsUrl          = s["value"]["units"].Value <string>();
                        sample.Unit       = ResourceManager.Localize(unitsUrl, _CultureInfo);
                    }
                    else
                    {
                        // Since Api v3 the schema has changed
                        sample.Timestamp  = s["result"]["timestamp"].Value <DateTime>();
                        sample.IsReliable = s["result"]["isReliable"].Value <Boolean>();
                        sample.Value      = s["result"]["value"]["value"]["value"].Value <double>();
                        sample.Unit       = s["result"]["value"]["units"].Value <string>();
                        sample.Unit       = ResourceManager.Localize(sample.Unit, _CultureInfo);
                    }
                }
                catch (ArgumentNullException e)
                {
                    // Something went wrong on object parsing
                    throw new MetasysObjectException(e);
                }
                if (Version < ApiVersion.v3)
                {
                    // On Api v2 and v1 there was the url endpoint of the enum instead of the fully qualified enumeration string
                    var unitId = unitsUrl.Split('/').Last();
                    // Read full url if not cached previously
                    if (!Units.ContainsKey(unitId))
                    {
                        var unit = await GetWithFullUrl(unitsUrl).ConfigureAwait(false);

                        Units.Add(unitId, unit["description"].Value <string>());
                    }
                    sample.Unit = Units[unitId];
                }
                objectSamples.Add(sample);
            }
            // Type the response as Sample List
            return(new PagedResult <Sample>
            {
                Items = objectSamples,
                CurrentPage = response.CurrentPage,
                PageCount = response.PageCount,
                PageSize = response.PageSize,
                Total = response.Total
            });
        }
Exemple #2
0
 /// <inheritdoc/>
 public PagedResult <Sample> GetSamples(Guid objectId, int attributeId, TimeFilter filter)
 {
     return(GetSamplesAsync(objectId, attributeId, filter).GetAwaiter().GetResult());
 }