static async Task RemoveRobotAsync([NotNull] UpdateRobot srv) { string id = srv.Request.Id; if (id.Length == 0) { srv.Response.Success = false; srv.Response.Message = "EE Id field is empty"; return; } ModuleData moduleData; if ((moduleData = ModuleDatas.FirstOrDefault(data => data.Configuration.Id == id)) == null) { srv.Response.Success = true; srv.Response.Message = $"WW There is no node with name '{id}'"; return; } if (moduleData.ModuleType != ModuleType.Robot) { srv.Response.Success = false; srv.Response.Message = $"EE Another module of the same id already exists, but it has type {moduleData.ModuleType}"; return; } using (SemaphoreSlim signal = new SemaphoreSlim(0)) { GameThread.Post(() => { try { Logger.Info($"ControllerService: Removing robot"); ModuleListPanel.Instance.RemoveModule(moduleData); } catch (Exception e) { srv.Response.Success = false; srv.Response.Message = $"EE An exception was raised: {e.Message}"; } finally { signal.Release(); } }); if (!await signal.WaitAsync(DefaultTimeoutInMs)) { srv.Response.Success = false; srv.Response.Message = "EE Request timed out!"; return; } if (string.IsNullOrEmpty(srv.Response.Message)) { srv.Response.Success = true; } } }
static async Task AddRobotAsync([NotNull] UpdateRobot srv) { string id = srv.Request.Id; ModuleData moduleData; if (id.Length != 0) { moduleData = ModuleDatas.FirstOrDefault(data => data.Configuration.Id == id); if (moduleData != null && moduleData.ModuleType != ModuleType.Robot) { srv.Response.Success = false; srv.Response.Message = $"EE Another module of the same id already exists, but it has type {moduleData.ModuleType}"; return; } } else { moduleData = null; } using (var signal = new SemaphoreSlim(0)) { GameThread.Post(() => { try { //Logger.Info($"ControllerService: Creating robot"); var newModuleData = moduleData ?? ModuleListPanel.Instance.CreateModule( ModuleType.Robot, requestedId: id.Length != 0 ? id : null); srv.Response.Success = true; ((SimpleRobotModuleData)newModuleData).UpdateConfiguration( srv.Request.Configuration, srv.Request.ValidFields); } catch (Exception e) { srv.Response.Success = false; srv.Response.Message = $"EE An exception was raised: {e.Message}"; } finally { signal.Release(); } }); if (!await signal.WaitAsync(DefaultTimeoutInMs)) { srv.Response.Success = false; srv.Response.Message = "EE Request timed out!"; return; } if (string.IsNullOrEmpty(srv.Response.Message)) { srv.Response.Success = true; } } }
internal static Task UpdateRobotAsync([NotNull] UpdateRobot srv) { switch (srv.Request.Operation) { case OperationType.Remove: return(RemoveRobotAsync(srv)); case OperationType.AddOrUpdate: return(AddRobotAsync(srv)); default: srv.Response.Success = false; srv.Response.Message = "EE Unknown operation"; return(Task.CompletedTask); } }