public async Task SetLightState(int row, int col, Color color, bool onOff) { var requestUri = $"http://{_panelBaseAddress}/led"; using (var request = new HttpRequestMessage(HttpMethod.Put, requestUri)) { var colorString = $"rgba({Convert.ToInt32(color.R * 255)},{Convert.ToInt32(color.G * 255)},{Convert.ToInt32(color.B * 255)},{color.A})"; if (onOff) { request.Content = new StringContent($"{{\"row\":{row},\"column\":{col},\"color\":\"{colorString}\",\"state\":\"{(onOff ? "on" : "off")}\"}}", Encoding.UTF8, "application/json"); } else { request.Content = new StringContent($"{{\"row\":{row},\"column\":{col},\"state\":\"{(onOff ? "on" : "off")}\"}}", Encoding.UTF8, "application/json"); } using (var response = await _httpClient.SendAsync(request)) { if (response.IsSuccessStatusCode) { LastErrorMessage = $"Light turned {(onOff ? "on" : "off")} to color {colorString} successfully"; } else { LastErrorMessage = $"Failed to contact panel: {response.ReasonPhrase}"; } } } OperationComplete?.Invoke(this, EventArgs.Empty); return; }
public async Task StopLine(int lineNumber, bool reset = false) { if (!reset && (!_lineRunning.ContainsKey(lineNumber) || !_lineRunning[lineNumber])) { return; } var requestUri = $"http://{_panelBaseAddress}/line/{lineNumber}?reset={reset.ToString().ToLower()}"; using (var request = new HttpRequestMessage(HttpMethod.Delete, requestUri)) { using (var response = await _httpClient.SendAsync(request)) { if (response.IsSuccessStatusCode) { LastErrorMessage = "Line stopped set successfully"; } else { LastErrorMessage = $"Failed to contact panel: {response.ReasonPhrase}"; } _lineRunning[lineNumber] = false; } } OperationComplete?.Invoke(this, EventArgs.Empty); return; }
public async Task <bool> Connect(string panelBaseAddress) { var requestUri = $"http://{panelBaseAddress}/connect"; using (var request = new HttpRequestMessage(HttpMethod.Put, requestUri)) { using (var response = await _httpClient.SendAsync(request)) { IsConnected = false; if (response.IsSuccessStatusCode) { LastErrorMessage = "Panel Contacted... waiting for connected status"; } else if (response.StatusCode == System.Net.HttpStatusCode.NotModified) { // Assume panel is already connected LastErrorMessage = "Panel already connected, disconnect + reconnect if not working"; } else { LastErrorMessage = $"Failed to contact panel: {response.ReasonPhrase}"; OperationComplete?.Invoke(this, EventArgs.Empty); return(false); } } } int retryCount = 0; while (retryCount <= 5) { using (var response = await _httpClient.GetAsync($"http://{panelBaseAddress}/state")) { var result = await response.Content.ReadAsStringAsync(); var stateResult = Newtonsoft.Json.JsonConvert.DeserializeObject <StateResult>(result); if (stateResult.State > 1) { _panelBaseAddress = panelBaseAddress; LastErrorMessage = "Success - Connected!"; IsConnected = true; OperationComplete?.Invoke(this, EventArgs.Empty); return(true); } } await Task.Delay(TimeSpan.FromSeconds(10)); } LastErrorMessage = "Panel controller could not connect to light panel"; OperationComplete?.Invoke(this, EventArgs.Empty); return(false); }
public async Task Disconnect() { var requestUri = $"http://{_panelBaseAddress}/connect"; using (var request = new HttpRequestMessage(HttpMethod.Delete, requestUri)) { using (var response = await _httpClient.SendAsync(request)) { IsConnected = false; if (response.IsSuccessStatusCode) { LastErrorMessage = "Panel Contacted... waiting for disconnected status"; } else { LastErrorMessage = $"Failed to contact panel: {response.ReasonPhrase}"; OperationComplete?.Invoke(this, EventArgs.Empty); return; } } } int retryCount = 0; while (retryCount <= 5) { using (var response = await _httpClient.GetAsync($"http://{_panelBaseAddress}/state")) { var result = await response.Content.ReadAsStringAsync(); var stateResult = Newtonsoft.Json.JsonConvert.DeserializeObject <StateResult>(result); if (stateResult.State <= 1) { LastErrorMessage = "Success - Disconnected"; IsConnected = false; _panelBaseAddress = null; OperationComplete?.Invoke(this, EventArgs.Empty); return; } } await Task.Delay(TimeSpan.FromSeconds(10)); } LastErrorMessage = "Panel controller could not disconnect from light panel"; OperationComplete?.Invoke(this, EventArgs.Empty); return; }
public async Task StartLine(int lineNumber, string command) { var isRunning = false; if (String.IsNullOrWhiteSpace(command) && _lineRunning.TryGetValue(lineNumber, out isRunning)) { if (isRunning) { return; } } var requestUri = $"http://{_panelBaseAddress}/line/{lineNumber}"; using (var request = new HttpRequestMessage(HttpMethod.Post, requestUri)) { if (!String.IsNullOrWhiteSpace(command)) { request.Content = new StringContent(command, Encoding.UTF8, "application/json"); } using (var response = await _httpClient.SendAsync(request)) { if (response.IsSuccessStatusCode) { LastErrorMessage = "Line started set successfully"; isRunning = true; } else { LastErrorMessage = $"Failed to contact panel: {response.ReasonPhrase}"; } if (_lineRunning.ContainsKey(lineNumber)) { _lineRunning[lineNumber] = isRunning; } else { _lineRunning.Add(lineNumber, isRunning); } } } OperationComplete?.Invoke(this, EventArgs.Empty); return; }
private async Task <DataModel> GetModelFromFileAsync(string text) { try { OperationStarted?.Invoke("Reading model data...", new EventArgs()); var model = DataModel.FromJsonFile(text); return(await Task.FromResult(model)); } catch (Exception exc) { throw new Exception($"Error reading model data: {exc.Message}", exc); } finally { OperationComplete?.Invoke(this, new EventArgs()); } }
private async Task <DataModel> GetAssemblyModelAsync(string text) { try { // help from https://docs.microsoft.com/en-us/dotnet/framework/deployment/best-practices-for-assembly-loading OperationStarted?.Invoke("Analyzing assembly...", new EventArgs()); var assembly = Assembly.LoadFrom(text); var result = new AssemblyModelBuilder().GetDataModel(assembly); return(await Task.FromResult(result)); } catch (Exception exc) { throw new Exception($"Error analyzing assembly: {exc.Message}"); } finally { OperationComplete?.Invoke(this, new EventArgs()); } }
private async Task <DataModel> GetConnectionModelAsync(string text) { try { OperationStarted?.Invoke("Analyzing database...", new EventArgs()); using (var cn = GetConnection(text)) { return(await new SqlServerModelBuilder().GetDataModelAsync(cn)); } } catch (Exception exc) { throw new Exception($"Error analyzing database: {exc.Message}", exc); } finally { OperationComplete?.Invoke(this, new EventArgs()); } }
private async Task ExecuteInner(EventArgs e, string statusBarMessage, bool commit, string successMessage = null) { try { OperationStarted?.Invoke(statusBarMessage, e); using (var cn = GetConnection.Invoke(tbDest.Text)) { await SqlDialect.ExecuteAsync(cn, tbScriptOutput.Text, commit); if (commit) { ScriptExecuted?.Invoke(this, new EventArgs()); if (_manualEdits) { MessageBox.Show("Changes applied successfully.", "Script Executed", MessageBoxButtons.OK, MessageBoxIcon.Information); } } if (!string.IsNullOrEmpty(successMessage)) { MessageBox.Show(successMessage, "SQL Test", MessageBoxButtons.OK, MessageBoxIcon.Information); } if (!_manualEdits && commit) { await GenerateScriptAsync(); } } } catch (Exception exc) { if (MessageBox.Show(exc.Message + "\r\n\r\nClick OK to create test case.", "Script Error", MessageBoxButtons.OKCancel) == DialogResult.OK) { frmSaveTestCase.PromptSaveZipFile(false, exc.Message, _sourceModel, _destModel, _diff); } } finally { OperationComplete?.Invoke(this, new EventArgs()); } }
public async Task TurnPanelOff() { var requestUri = $"http://{_panelBaseAddress}/leds"; using (var request = new HttpRequestMessage(HttpMethod.Delete, requestUri)) { using (var response = await _httpClient.SendAsync(request)) { if (response.IsSuccessStatusCode) { LastErrorMessage = $"Panel turned off successfully"; } else { LastErrorMessage = $"Failed to contact panel: {response.ReasonPhrase}"; } _lineRunning.Keys.ToList().ForEach(k => _lineRunning[k] = false); } } OperationComplete?.Invoke(this, EventArgs.Empty); return; }
public async Task SetTempo(int newTempo) { var requestUri = $"http://{_panelBaseAddress}/tempo"; using (var request = new HttpRequestMessage(HttpMethod.Put, requestUri)) { request.Content = new StringContent($"{{\"bpm\":{newTempo}}}", Encoding.UTF8, "application/json"); using (var response = await _httpClient.SendAsync(request)) { if (response.IsSuccessStatusCode) { LastErrorMessage = "Tempo set successfully"; } else { LastErrorMessage = $"Failed to contact panel: {response.ReasonPhrase}"; } } } OperationComplete?.Invoke(this, EventArgs.Empty); return; }
private void OnOperationComplete(OperationEventArgs args) { OperationComplete?.Invoke(this, args); }