Exemple #1
0
        /// <summary>
        /// Begins the nest event stream.
        /// Subscribe to the OnNestEventStream_* EventHandlers to do work on events.
        /// </summary>
        /// <returns></returns>
        public async Task BeginNestEventStream()
        {
            using (var stream = await this.GetNestEventStreamAsync())
                using (var reader = new StreamReader(stream))
                {
                    while (reader != null && !reader.EndOfStream)
                    {
                        var currentLine = await reader.ReadLineAsync();

                        if (!String.IsNullOrEmpty(currentLine) && currentLine.StartsWith("data"))
                        {
                            try
                            {
                                // Object needs to be wrapped in curly braces to it's deserialized correctly
                                var           model         = $"{{{currentLine}}}";
                                var           dataModelDict = JsonConvert.DeserializeObject <Dictionary <string, NestStreamEventDataModel> >(model);
                                NestDataModel dataModel     = dataModelDict["data"].Data;
                                this.OnNestEventStream_DeviceEventOccured?.Invoke(this, dataModel);
                            }
                            catch (Exception ex)
                            {
                                System.Diagnostics.Debug.WriteLine(ex);
                            }
                        }
                        else
                        {
                            this.OnNestEventStream_StreamEventOccured?.Invoke(this, currentLine);
                        }
                    }
                }
        }
Exemple #2
0
        public async Task <NestDataModel> GetNestDataAsync()
        {
            try
            {
                VerifyAuthValid();

                HttpClient httpClient = new HttpClient();

                var url = BASE_API_URL + "?auth={0}";

                var data = await httpClient.GetStringAsync(String.Format(url, AccessToken));

                NestDataModel dataModel = JsonConvert.DeserializeObject <NestDataModel>(data);

                return(dataModel);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex);
                throw ex;
            }
        }