Ejemplo n.º 1
0
        public static void ConnectToGateway()
        {
            mySensorsNodesEngine = null;

            if (gatewayConfig.SerialGateway.Enable)
            {
                gatewayConnectionPort = new SerialConnectionPort(
                    gatewayConfig.SerialGateway.SerialPortName,
                    gatewayConfig.SerialGateway.Boudrate);
            }
            else if (gatewayConfig.EthernetGateway.Enable)
            {
                gatewayConnectionPort = new EthernetConnectionPort(
                    gatewayConfig.EthernetGateway.GatewayIP,
                    gatewayConfig.EthernetGateway.GatewayPort);
            }
            else
            {
                return;
            }

            //connecting to gateway
            logs.AddSystemInfo("Connecting to gateway...");

            gateway = new Gateway(gatewayConnectionPort, mySensorsDb, mySensorsMessagesDb);

            gateway.enableAutoAssignId = gatewayConfig.EnableAutoAssignId;
            gateway.IsMetricUnit       = () => gatewayConfig.IsMetric;

            gateway.OnLogDecodedMessage      += logs.AddGatewayDecodedMessage;
            gateway.OnLogMessage             += logs.AddGatewayMessage;
            gateway.OnLogInfo                += logs.AddGatewayInfo;
            gateway.OnLogError               += logs.AddGatewayError;
            gateway.endlessConnectionAttempts = true;
            gateway.OnConnected              += GatewayConnected;
            gateway.OnDisconnected           += GatewayDisconnected;

            gateway.Connect().Wait();

            if (gateway != null && nodesEngine != null)
            {
                mySensorsNodesEngine = new MySensorsNodesEngine(gateway, nodesEngine);
            }

            if (gateway != null && gateway.IsConnected())
            {
                logs.AddSystemInfo("Gateway connected.");
            }
            else
            {
                logs.AddSystemInfo("Gateway is not connected.");
            }
        }
Ejemplo n.º 2
0
        public Gateway(IGatewayConnectionPort connectionPort, IMySensorsRepository db = null, IMySensorsMessagesRepository hisotryDb = null)
        {
            this.db = db;
            this.hisotryDb = hisotryDb;

            nodes = db?.GetNodes() ?? new List<Node>();

            gatewayAliveChecker = new GatewayAliveChecker(this);

            this.connectionPort = connectionPort;
            this.connectionPort.OnDataReceived += RecieveMessage;
            this.connectionPort.OnDisconnected += OnConnectionPortDisconnected;
            this.connectionPort.OnConnected += TryToCommunicateWithGateway;
            this.connectionPort.OnLogError += LogError;
            this.connectionPort.OnLogInfo += LogInfo;
            this.connectionPort.OnLogMessage += LogMessage;
        }
        public static void ConnectToGateway()
        {
            mySensorsNodesEngine = null;

            if (gatewayConfig.SerialGatewayConfig.Enable)
            {
                gatewayConnectionPort = new SerialConnectionPort(
                    gatewayConfig.SerialGatewayConfig.SerialPortName,
                    gatewayConfig.SerialGatewayConfig.Boudrate);
            }
            else if (gatewayConfig.EthernetGatewayConfig.Enable)
            {
                gatewayConnectionPort = new EthernetConnectionPort(
                    gatewayConfig.EthernetGatewayConfig.GatewayIP,
                    gatewayConfig.EthernetGatewayConfig.GatewayPort);
            }
            else return;

            //connecting to gateway
            logs.AddSystemInfo("Connecting to gateway...");

            gateway = new Gateway(gatewayConnectionPort, mySensorsDb, mySensorsMessagesDb);

            gateway.enableAutoAssignId = gatewayConfig.EnableAutoAssignId;

            gateway.OnLogDecodedMessage += logs.AddGatewayDecodedMessage;
            gateway.OnLogMessage += logs.AddGatewayMessage;
            gateway.OnLogInfo += logs.AddGatewayInfo;
            gateway.OnLogError += logs.AddGatewayError;
            gateway.endlessConnectionAttempts = true;
            gateway.OnConnected += GatewayConnected;
            gateway.OnDisconnected += GatewayDisconnected;

            gateway.Connect().Wait();

            if (gateway != null && nodesEngine != null)
                mySensorsNodesEngine = new MySensorsNodesEngine(gateway, nodesEngine);

            if (gateway != null && gateway.IsConnected())
            {
                logs.AddSystemInfo("Gateway connected.");
            }
            else
                logs.AddSystemInfo("Gateway is not connected.");

        }