/// <summary> /// Initializes DeviceContext /// </summary> /// <param name="productKey">Product key of the device</param> /// <param name="accessKey">Access key of the device</param> /// <param name="apiBaseAddress">Base address of the Arnon Sky API</param> public DeviceContext(string productKey, byte[] accessKey, string apiBaseAddress = DefaultApiBaseAddress) { ValidateProductKey(); if (accessKey == null || accessKey.Length < 32) { throw new ArgumentException($"Invalid {nameof(accessKey)}"); } ProductKey = productKey; _protectedAccessKey = Security.ProtectData(accessKey); _apiBaseAddress = apiBaseAddress; _apiClient = new ApiClient(productKey, accessKey, apiBaseAddress); _configuration = new DeviceConfigurationContainer(); void ValidateProductKey() { if (productKey == null || productKey.Length < 16) { throw new ArgumentException($"{nameof(productKey)} is too short."); } if (!System.Text.RegularExpressions.Regex.IsMatch(productKey, @"^[A-Z0-9-]+$")) { throw new ArgumentException($"{nameof(productKey)} has invalid characters, only letters A-Z, numerals 0-9 and character '-' is allowed."); } } }
private void UpdateConfiguration(GetDeviceDetailsModel newConfiguration) { var replaceConf = new DeviceConfigurationContainer() { ReadTimestamp = DateTime.UtcNow, Current = newConfiguration.ToDeviceConfiguration() }; Interlocked.Exchange(ref _configuration, replaceConf); }