/// <summary> /// Handles sending regular calls to the heartbeat API, in order to keep the connection alive. /// </summary> /// <exception cref="RestException">Thrown if there is an error calling the REST API.</exception> private void SendHeartbeat() { #if DEBUG Log.Trace("Sending heartbeat"); #endif var response = _client.PutAsync <HeartbeatResponse>("/heartbeat").Result; if (!response.IsSuccessful) { var ex = new RestException( "Call to heartbeat API failed", Result.RzFailed, new Uri(_client.BaseAddress, "/heartbeat"), response.Status); Log.Error(ex, "Heartbeat call failed"); throw ex; } if (response.Data == null) { var ex = new RestException( "Got NULL data from heartbeat call", Result.RzFailed, new Uri(_client.BaseAddress, "/heartbeat"), response.Status); Log.Error(ex, "Got NULL data from heartbeat call"); throw ex; } #if DEBUG Log.TraceFormat("Heartbeat complete, tick: {0}", response.Data.Tick); #endif }
/// <summary> /// Creates a Chroma effect using the specified API endpoint and effect data. /// </summary> /// <param name="endpoint">Device endpoint to create effect at.</param> /// <param name="data">Effect data.</param> /// <returns>A <see cref="Guid" /> identifying the newly created effect.</returns> /// <exception cref="RestException">Thrown if there is an error calling the REST API.</exception> /// <exception cref="ApiException">Thrown if the SDK returns an exception creating the effect.</exception> private async Task <Guid> CreateEffectAsync(string endpoint, object data) { var response = await _client.PostAsync <SdkEffectResponse>(endpoint, data).ConfigureAwait(false); if (!response.IsSuccessful) { var ex = new RestException( $"Failed to create effect at {endpoint}", response.Data?.Result ?? Result.RzFailed, new Uri(_client.BaseAddress, endpoint), response.Status, response.Data?.ToString()); Log.Error(ex, "Failed to create effect"); throw ex; } var responseData = response.Data; if (responseData == null) { throw new ApiException("Effect creation API returned NULL response"); } if (!responseData.Result) { throw new ApiException("Exception when calling SetEffect API", responseData.Result); } if (responseData.EffectId == null) { throw new ApiException("Got NULL GUID from creating effect", responseData.Result); } return(responseData.EffectId.Value); }
/// <inheritdoc /> /// <summary> /// Initializes the Chroma SDK by sending a POST request to <c>/razer/chromasdk</c>. /// </summary> /// <param name="info">Information about the application.</param> /// <returns>An object representing the progress of this asynchronous task.</returns> /// <exception cref="ArgumentNullException">Thrown if <paramref name="info" /> is <c>null</c>.</exception> /// <exception cref="RestException">Thrown if there is an error calling the REST API.</exception> public async Task InitializeAsync(AppInfo info) { if (info == null) { throw new ArgumentNullException(nameof(info)); } Log.Info("Initializing SDK via /razer/chromasdk endpoint"); _client.BaseAddress = _baseUri; var response = await _client.PostAsync <SdkInitResponse>("/razer/chromasdk", info).ConfigureAwait(false); if (!response.IsSuccessful) { var ex = new RestException( "Failed to initialize Chroma REST API", Result.RzFailed, new Uri(_client.BaseAddress, "/razer/chromasdk"), response.Status); Log.Error(ex, "Chroma SDK initialization failed"); throw ex; } var data = response.Data; if (data == null) { var ex = new RestException( "REST API returned NULL data", Result.RzFailed, new Uri(_client.BaseAddress, "/razer/chromasdk"), response.Status); Log.Error(ex, "Got NULL data from REST API"); throw ex; } if (data.Session == 0 || data.Uri == default) { // Attempt to deserialize into SdkResponse to handle Razer's invalid REST API if (!string.IsNullOrWhiteSpace(response.Content)) { var sdkResponse = response.Deserialize <SdkResponse>(); if (sdkResponse != default) { throw new ApiException("Exception when calling initialize API", sdkResponse.Result); } } var ex = new RestException( "REST API returned invalid session ID or URL", Result.RzFailed, new Uri(_client.BaseAddress, "/razer/chromasdk"), response.Status, data.ToString()); Log.Error(ex, "Got invalid session response from REST init API"); throw ex; } _session = data.Session; _client.BaseAddress = data.Uri; Log.InfoFormat("New REST API session {0} at {1}", _session, _client.BaseAddress); Log.Debug("Starting heartbeat timer"); _heartbeatTimer.Change(HeartbeatInterval, HeartbeatInterval); }