Пример #1
0
        // execute the requested command (from web service)
        private void migService_ServiceRequestPreProcess(MIGClientRequest request, MIGInterfaceCommand migCommand)
        {
            LogBroadcastEvent(
                "MIG.Gateways.WebServiceGateway",
                request.RequestOrigin,
                request.RequestMessage,
                request.SubjectName,
                request.SubjectValue
            );

            #region Interconnection (Remote Node Command Routing)

            Module target = systemModules.Find(m => m.Domain == migCommand.Domain && m.Address == migCommand.NodeId);
            bool isRemoteModule = (target != null && !String.IsNullOrWhiteSpace(target.RoutingNode));
            if (isRemoteModule)
            {
                // ...
                try
                {
                    string domain = migCommand.Domain;
                    if (domain.StartsWith("HGIC:")) domain = domain.Substring(domain.IndexOf(".") + 1);
                    string serviceurl = "http://" + target.RoutingNode + "/api/" + domain + "/" + migCommand.NodeId + "/" + migCommand.Command + "/" + migCommand.OptionsString;
                    Automation.Scripting.NetHelper neth = new Automation.Scripting.NetHelper(this).WebService(serviceurl);
                    if (systemConfiguration.HomeGenie.UserLogin != "" && systemConfiguration.HomeGenie.UserPassword != "")
                    {
                        neth.WithCredentials(
                            systemConfiguration.HomeGenie.UserLogin,
                            systemConfiguration.HomeGenie.UserPassword
                        );
                    }
                    neth.Call();
                }
                catch (Exception ex)
                {
                    HomeGenieService.LogEvent(
                        Domains.HomeAutomation_HomeGenie,
                        "Interconnection:" + target.RoutingNode,
                        ex.Message,
                        "Exception.StackTrace",
                        ex.StackTrace
                    );
                }
                return;
            }

            #endregion

            // HomeGenie Web Service domain API
            if (migCommand.Domain == Domains.HomeAutomation_HomeGenie)
            {
                // domain == HomeAutomation.HomeGenie
                switch (migCommand.NodeId)
                {
                case "Logging":

                    wshLogging.ProcessRequest(request, migCommand);
                    break;

                case "Config":

                    wshConfig.ProcessRequest(request, migCommand);
                    break;

                case "Automation":

                    wshAutomation.ProcessRequest(request, migCommand);
                    break;

                case "Interconnection":

                    wshInterconnection.ProcessRequest(request, migCommand);
                    break;

                case "Statistics":

                    wshStatistics.ProcessRequest(request, migCommand);
                    break;
                }
            }
            else if (migCommand.Domain == Domains.HomeAutomation_HomeGenie_Automation)
            {
                int n;
                bool nodeIdIsNumeric = int.TryParse(migCommand.NodeId, out n);
                if (nodeIdIsNumeric)
                {
                    switch (migCommand.Command)
                    {
                    case "Control.Run":
                        wshAutomation.ProgramRun(migCommand.NodeId, migCommand.GetOption(0));
                        break;
                    case "Control.Break":
                        wshAutomation.ProgramBreak(migCommand.NodeId);
                        break;
                    }
                }
            }
        }
Пример #2
0
        private void migService_ServiceRequestPreProcess(object sender, ProcessRequestEventArgs args)
        {
            // Currently we only support requests coming from WebServiceGateway
            // TODO: in the future, add support for any MigGateway channel (eg. WebSocketGateway as well)
            if (args.Request.Context.Source != ContextSource.WebServiceGateway)
                return;

            var migCommand = args.Request.Command;

            #region Interconnection (Remote Node Command Routing)

            Module target = systemModules.Find(m => m.Domain == migCommand.Domain && m.Address == migCommand.Address);
            bool isRemoteModule = (target != null && !String.IsNullOrWhiteSpace(target.RoutingNode));
            if (isRemoteModule)
            {
                try
                {
                    string domain = migCommand.Domain;
                    if (domain.StartsWith("HGIC:")) domain = domain.Substring(domain.IndexOf(".") + 1);
                    string serviceurl = "http://" + target.RoutingNode + "/api/" + domain + "/" + migCommand.Address + "/" + migCommand.Command + "/" + migCommand.OptionsString;
                    Automation.Scripting.NetHelper neth = new Automation.Scripting.NetHelper(this).WebService(serviceurl);
                    string username = webGateway.GetOption("Username").Value;
                    string password = webGateway.GetOption("Password").Value;
                    if (!String.IsNullOrWhiteSpace(username) && !String.IsNullOrWhiteSpace(password))
                    {
                        neth.WithCredentials(
                            username,
                            password
                        );
                    }
                    neth.Call();
                }
                catch (Exception ex)
                {
                    LogError(
                        Domains.HomeAutomation_HomeGenie,
                        "Interconnection:" + target.RoutingNode,
                        ex.Message,
                        "Exception.StackTrace",
                        ex.StackTrace
                    );
                }
                return;
            }

            #endregion

            // HomeGenie Web Service domain API
            if (migCommand.Domain == Domains.HomeAutomation_HomeGenie)
            {
                // domain == HomeAutomation.HomeGenie
                switch (migCommand.Address)
                {

                case "Config":
                    wshConfig.ProcessRequest(args.Request);
                    break;

                case "Automation":
                    wshAutomation.ProcessRequest(args.Request);
                    break;

                case "Interconnection":
                    wshInterconnection.ProcessRequest(args.Request);
                    break;

                case "Statistics":
                    wshStatistics.ProcessRequest(args.Request);
                    break;

                }
            }
            else if (migCommand.Domain == Domains.HomeAutomation_HomeGenie_Automation)
            {
                int n;
                bool nodeIdIsNumeric = int.TryParse(migCommand.Address, out n);
                if (nodeIdIsNumeric)
                {
                    switch (migCommand.Command)
                    {

                    case "Control.Run":
                        wshAutomation.ProgramRun(migCommand.Address, migCommand.GetOption(0));
                        break;

                    case "Control.Break":
                        wshAutomation.ProgramBreak(migCommand.Address);
                        break;

                    }
                }
            }
        }
Пример #3
0
 public void InterfaceControl(MIGInterfaceCommand cmd)
 {
     var target = systemModules.Find(m => m.Domain == cmd.Domain && m.Address == cmd.NodeId);
     bool isRemoteModule = (target != null && !String.IsNullOrWhiteSpace(target.RoutingNode));
     if (isRemoteModule)
     {
         // ...
         try
         {
             string domain = cmd.Domain;
             if (domain.StartsWith("HGIC:")) domain = domain.Substring(domain.IndexOf(".") + 1);
             string serviceUrl = "http://" + target.RoutingNode + "/api/" + domain + "/" + cmd.NodeId + "/" + cmd.Command + "/" + cmd.OptionsString;
             Automation.Scripting.NetHelper netHelper = new Automation.Scripting.NetHelper(this).WebService(serviceUrl);
             if (!String.IsNullOrWhiteSpace(systemConfiguration.HomeGenie.UserLogin) && !String.IsNullOrWhiteSpace(systemConfiguration.HomeGenie.UserPassword))
             {
                 netHelper.WithCredentials(
                     systemConfiguration.HomeGenie.UserLogin,
                     systemConfiguration.HomeGenie.UserPassword
                 );
             }
             netHelper.Call();
         }
         catch (Exception ex)
         {
             HomeGenieService.LogEvent(
                 Domains.HomeAutomation_HomeGenie,
                 "Interconnection:" + target.RoutingNode,
                 ex.Message,
                 "Exception.StackTrace",
                 ex.StackTrace
             );
         }
         return;
     }
     //
     object response = null;
     MIGInterface migInterface = GetInterface(cmd.Domain);
     if (migInterface != null)
     {
         try
         {
             response = migInterface.InterfaceControl(cmd);
         }
         catch (Exception ex)
         {
             HomeGenieService.LogEvent(
                 Domains.HomeAutomation_HomeGenie,
                 "InterfaceControl",
                 ex.Message,
                 "Exception.StackTrace",
                 ex.StackTrace
             );
         }
     }
     //
     if (response == null || response.Equals(""))
     {
         migService.WebServiceDynamicApiCall(cmd);
     }
     //
     migService_ServiceRequestPostProcess(null, cmd);
 }