public bool Connect(Gateway gateway, string serverUrl, string connectionPassword)
        {
            DebugState(String.Format("Connecting to server {0}... ", serverUrl));

            isAuthorized = false;

            var querystringData = new Dictionary<string, string>();
            querystringData.Add("IsGateway", "true");
            querystringData.Add("ConnectionPassword", connectionPassword);

            hubConnection = new HubConnection(serverUrl, querystringData);
            hubProxy = hubConnection.CreateHubProxy("gatewayHub");

            try
            {
                hubProxy.On<string>("clearLog", ClearLog);
                hubProxy.On("clearNodes", ClearNodes);
                hubProxy.On<string>("getLog", GetLog);
                hubProxy.On<string>("getNodes", GetNodes);
                hubProxy.On<string>("getGatewayInfo", GetGatewayInfo);
                hubProxy.On<string>("getGatewayHardwareConnected", GetGatewayHardwareConnected);
                hubProxy.On("authorizationFailed", AuthorizationFailed);
                hubProxy.On("authorizationCompleted", AuthorizationCompleted);
                hubProxy.On<string, string>("sendMessage", SendMessage);

                hubConnection.Start().Wait();
                hubConnection.Closed += Disconnect;

                this.gateway = gateway;
                gateway.OnMessageRecievedEvent += OnMessageRecievedEvent;
                gateway.OnMessageSendEvent += OnMessageSendEvent;
                gateway.messagesLog.OnClearMessages += OnClearMessages;
                gateway.OnNewNodeEvent += OnNewNodeEvent;
                gateway.OnNodeLastSeenUpdatedEvent += OnNodeLastSeenUpdatedEvent;
                gateway.OnNodeUpdatedEvent += OnNodeUpdatedEvent;
                gateway.OnNodeBatteryUpdatedEvent += OnNodeBatteryUpdatedEvent;
                gateway.OnNewSensorEvent += OnNewSensorEvent;
                gateway.OnSensorUpdatedEvent += OnSensorUpdatedEvent;
                gateway.OnClearNodesListEvent += OnClearNodesListEvent;
                gateway.OnDisconnectedEvent += OnGatewayDisconnectedEvent;
                gateway.OnConnectedEvent += OnGatewayConnectedEvent;

                if (OnConnected != null && IsConnected())
                    OnConnected(this, null);

                // DebugState("Connected.");

                return true;
            }
            catch
            {
                DebugState("Can`t connect.");
                if (OnConnectionFailed != null)
                    OnConnectionFailed(this, null);
                return false;
            }
        }
        public void Connect(Gateway gateway, string connectionString)
        {
            InitializeDB(connectionString);

            this.gateway = gateway;

            List<Message> messages = GetMessages();
            foreach (var message in messages)
                gateway.messagesLog.AddNewMessage(message);

            List<Node> nodes = GetNodes();
            foreach (var node in nodes)
                gateway.AddNode(node);

            gateway.messagesLog.OnNewMessageLogged += OnNewMessage;
            gateway.messagesLog.OnClearMessages += OnClearMessages;

            gateway.OnClearNodesListEvent += OnClearNodesListEvent;

            gateway.OnNewNodeEvent += OnNodeUpdated;
            gateway.OnNodeUpdatedEvent += OnNodeUpdated;
            gateway.OnNewSensorEvent += OnSensorUpdated;
            gateway.OnSensorUpdatedEvent += OnSensorUpdated;

            updateDbTimer.Elapsed += UpdateDbTimer;

            if (storeTimeInterval > 0)
            {
                updateDbTimer.Interval = storeTimeInterval;
                updateDbTimer.Start();
            }
        }
        public void Disconnect()
        {
            DebugState("Disconnected.");

            if (gateway != null)
            {
                gateway.OnMessageRecievedEvent -= OnMessageRecievedEvent;
                gateway.OnMessageSendEvent -= OnMessageSendEvent;
                gateway.messagesLog.OnClearMessages -= OnClearMessages;
                gateway.OnNewNodeEvent -= OnNewNodeEvent;
                gateway.OnNodeLastSeenUpdatedEvent -= OnNodeLastSeenUpdatedEvent;
                gateway.OnNodeUpdatedEvent -= OnNodeUpdatedEvent;
                gateway.OnNodeBatteryUpdatedEvent -= OnNodeBatteryUpdatedEvent;
                gateway.OnNewSensorEvent -= OnNewSensorEvent;
                gateway.OnSensorUpdatedEvent -= OnSensorUpdatedEvent;
                gateway.OnClearNodesListEvent -= OnClearNodesListEvent;
                gateway.OnDisconnectedEvent -= OnGatewayDisconnectedEvent;
                gateway.OnConnectedEvent -= OnGatewayConnectedEvent;
                gateway = null;
            }

            isAuthorized = false;

            if (hubConnection != null && hubConnection.State == ConnectionState.Connected)
                hubConnection.Stop();

            if (OnDisconnected != null)
                OnDisconnected(this, null);
        }