public ControllerProxy ConnectToControllerProxy() { if (controllerProxy == null) { // Request headtracking endpoint address bool success = controlSocket.TrySendAsJson(new RequestEndpoint(EndpointNames.Controller), 1000); if (!success) { HandleControlConnectionException(new Exception("API server timeout.")); } EndpointCreated response; success = controlSocket.TryReceiveJson(out response, 1000); if (!success) { HandleControlConnectionException(new Exception("API server timeout.")); } if (response.Code == (int)ControlResponseCode.InUse) { HandleControlConnectionException(new Exception("API already in use by another client.")); } // Initialize the proxy controllerProxy = new ControllerProxy(response.EndpointAddress, false); } return(controllerProxy); }
public async Task <IActionResult> DeleteConfirmed(Guid id) { CabinetController cabinetController = await GetController(id); Cabinet cabinet = await GetCabinet(cabinetController); if (cabinet != null) { _context.Cabinets.Remove(cabinet); } ControllerProxy proxy = GetControllerProxy(cabinetController); // If the controller isn't connected, force un-enroll it if (proxy.ValidateController(cabinetController)) { // Notify the controller that it should delete its JWT token and reset await proxy.SendCommandToController("clientDelete"); } // Wait a second or two for completion await Task.Delay(TimeSpan.FromSeconds(2)); _context.Controllers.Remove(cabinetController); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); }
/// <summary> /// Closes controller API connection and lets other API clients use it. /// </summary> public void DisconnectControllerProxy() { if (controllerProxy == null) { return; } controllerProxy.Disconnect(); controllerProxy = null; }
public ControllerService(ControllerProxy proxy) { this.proxy = proxy; var sendingThread = new Thread(StateSendingLoop) { IsBackground = true }; // Set initial state SetControllerState(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, false, false, false); sendingThread.Start(); }
public async Task <IActionResult> Post(Guid id, [FromBody] ScheduledCommand _scheduledCommand) { CabinetController cabinetController = await GetController(id); if (cabinetController == null) { return(GetCommandError(_scheduledCommand, ErrorStrings.ControllerNotFound)); } // Make sure the controller is online ControllerProxy proxy = GetControllerProxy(cabinetController); if (!proxy.ValidateController(cabinetController)) { return(GetCommandError(_scheduledCommand, ErrorStrings.ControllerOffline)); } // Send the command to the proxy ScheduledCommand command = await proxy.SendCommandToController(_scheduledCommand.Name, _scheduledCommand.Payload); return(new JsonResult(command)); }