private async Task <RemoteEndpoint> GetStateObject(string RemoteEndpoint, string Protocol, bool CreateNew) { RemoteEndpoint EP; RemoteEndpoint = this.RemovePort(RemoteEndpoint); lock (this.states) { if (this.states.TryGetValue(RemoteEndpoint, out EP)) { return(EP); } } bool Created = false; bool Updated = false; EP = await Database.FindFirstIgnoreRest <RemoteEndpoint>(new FilterFieldEqualTo("Endpoint", RemoteEndpoint), "Created"); if (EP is null) { if (!CreateNew) { return(null); } EP = new RemoteEndpoint() { Endpoint = RemoteEndpoint, LastProtocol = Protocol, Created = DateTime.Now, Blocked = false, State = new int[this.nrIntervals], Timestamps = new DateTime[this.nrIntervals] }; EP.Reset(false); Created = true; } else { if (EP.State is null || EP.State.Length != this.nrIntervals) { EP.State = new int[this.nrIntervals]; EP.Timestamps = new DateTime[this.nrIntervals]; EP.Reset(false); Updated = true; } } lock (this.states) { if (this.states.TryGetValue(RemoteEndpoint, out RemoteEndpoint EP2)) { return(EP2); } this.states[RemoteEndpoint] = EP; } if (Created) { await Database.Insert(EP); } else if (Updated) { await Database.Update(EP); } return(EP); }