示例#1
0
        private static void ConnectSensorsTasks()
        {
            //connecting tasks
            if (!sensorsTasksEnabled) return;

            OnDebugStateMessage("TASK ENGINE: Starting...");

            sensorsTasksEngine = new SensorsTasksEngine(gateway, sensorsTasksDb);
            sensorsTasksEngine.SetUpdateInterval(sensorsTasksUpdateInterval);

            OnDebugStateMessage("TASK ENGINE: Started");
        }
        public bool Connect(
            SerialGateway gateway,
            SensorsTasksEngine tasksEngine, 
            SensorsLinksEngine linksEngine,
            string serverUrl, 
            string connectionPassword)
        {
            DebugState(String.Format("Connecting to server {0}... ", serverUrl));

            this.tasksEngine = tasksEngine;
            this.linksEngine = linksEngine;
            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<Node>("updateNodeSettings", UpdateNodeSettings);
                hubProxy.On("authorizationFailed", AuthorizationFailed);
                hubProxy.On("authorizationCompleted", AuthorizationCompleted);
                hubProxy.On("updateSensorsTasks", UpdateSensorsTasks);
                hubProxy.On("updateSensorsLinks", UpdateSensorsLinks);
                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;
            }
        }
示例#3
0
        private async static Task ConnectSensorsTasks()
        {
            //connecting tasks
            bool connected = false;
            if (Convert.ToBoolean(ConfigurationManager.AppSettings["UseDB"]))
            {
                Console.WriteLine("Starting sensors tasks engine... ");

                string connectionString = ConfigurationManager.ConnectionStrings["DbConnection"].ConnectionString;

                while (!connected)
                {
                    sensorsTasksDb = new SensorsTasksRepositoryDapper(connectionString);
                    sensorsTasksEngine = new SensorsTasksEngine(gateway, sensorsTasksDb);
                    connected = (sensorsTasksDb.IsDbExist());
                    if (!connected) await Task.Delay(5000);
                }
            }
        }