Exemplo n.º 1
0
 public SoftNodesController(ISoftNodesServer server, SerialGateway gateway)
 {
     this.server = server;
     this.gateway = gateway;
     server.OnReceivedMessage+= OnReceivedSoftNodeMessage;
     gateway.OnMessageSendEvent+= OnSendGatewayMessage;
 }
        public void ConnectToGateway(SerialGateway gateway)
        {
            this.gateway = gateway;

            gateway.OnSensorUpdatedEvent += OnSensorUpdatedEvent;

            updateDbTimer.Elapsed += UpdateDbTimer;

            updateDbTimer.Interval = writeInterval;
            updateDbTimer.Start();
        }
Exemplo n.º 3
0
        public SensorsLinksEngine(SerialGateway gateway, ISensorsLinksRepository db)
        {
            this.db = db;
            this.gateway = gateway;

            gateway.OnClearNodesListEvent += OnClearNodesListEvent;
            gateway.OnSensorUpdatedEvent += OnSensorUpdatedEvent;

            db.CreateDb();
            GetLinksFromRepository();
        }
Exemplo n.º 4
0
        public SensorsTasksEngine(SerialGateway gateway, ISensorsTasksRepository db)
        {
            this.db = db;
            this.gateway = gateway;

            gateway.OnClearNodesListEvent += OnClearNodesListEvent;

            updateTasksTimer.Elapsed += UpdateTasks;
            updateTasksTimer.Interval = updateTasksInterval;

            db.CreateDb();
            GetTasksFromRepository();

            Start();
        }
Exemplo n.º 5
0
        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;
            }
        }
Exemplo n.º 6
0
        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);
        }
Exemplo n.º 7
0
        public void ConnectToGateway(SerialGateway gateway)
        {
            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 (writeInterval > 0)
            {
                updateDbTimer.Interval = writeInterval;
                updateDbTimer.Start();
            }
        }