/// <summary>
        /// Provisions a device to the Arnon Sky. Each device must have unique ProductKey.
        /// </summary>
        /// <returns>Device context that the device may use to communicate with Arnon Sky</returns>
        public static async Task <DeviceContext> ProvisionDeviceAsync(string provisioningKey, string deviceType = "Sample", string productKey = null, string apiBaseAddress = DefaultApiBaseAddress)
        {
            if (productKey == null)
            {
                productKey = Guid.NewGuid().ToString().ToUpper();
            }

            if (string.IsNullOrEmpty(provisioningKey))
            {
                throw new ArgumentException($"Invalid {nameof(provisioningKey)}");
            }

            if (string.IsNullOrEmpty(deviceType))
            {
                throw new ArgumentException($"Invalid {nameof(deviceType)}");
            }

            if (string.IsNullOrEmpty(productKey))
            {
                throw new ArgumentException($"Invalid {nameof(productKey)}");
            }

            var apiClient = new ApiClient(productKey, null, apiBaseAddress);
            var model     = new PostDeviceModel(provisioningKey, productKey, deviceType);

            var accessKeyBase64 = await apiClient.SendRequestWithoutAuthorizationAsync <string>(HttpMethod.Post, "/devices", model).ConfigureAwait(false);

            var accessKey = Convert.FromBase64String(accessKeyBase64);

            return(new DeviceContext(productKey, accessKey, apiBaseAddress));
        }
Beispiel #2
0
        public async Task <bool> PostDeviceAsync(Device device, string URL)
        {
            var client = new HttpClient();

            client.DefaultRequestHeaders.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json-patch+json");

            var model = new PostDeviceModel()
            {
                Name      = device.Name,
                MaxVolume = device.MaxVolume,
                MaxWeight = device.MaxWeight,
            };

            var json = JsonConvert.SerializeObject(model);

            byte[] messageBytes = System.Text.Encoding.UTF8.GetBytes(json);
            var    content      = new ByteArrayContent(messageBytes);

            content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            var response = await client.PostAsync(URL, content);

            return(response.IsSuccessStatusCode);
        }