Пример #1
0
        private async Task <RemoteEndpoint> GetStateObject(string RemoteEndpoint, string Protocol)
        {
            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)
            {
                EP = new RemoteEndpoint()
                {
                    Endpoint     = RemoteEndpoint,
                    LastProtocol = Protocol,
                    Creted       = 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);
        }