Beispiel #1
0
 public object InterfaceControl(MigInterfaceCommand cmd)
 {
     object response = null;
     var target = systemModules.Find(m => m.Domain == cmd.Domain && m.Address == cmd.Address);
     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.Address + "/" + cmd.Command + "/" + cmd.OptionsString;
             Automation.Scripting.NetHelper netHelper = 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))
             {
                 netHelper.WithCredentials(username, password);
             }
             response = netHelper.GetData();
         }
         catch (Exception ex)
         {
             LogError(Domains.HomeAutomation_HomeGenie, "Interconnection:" + target.RoutingNode, ex.Message, "Exception.StackTrace", ex.StackTrace);
         }
     }
     else
     {
         var migInterface = migService.GetInterface(cmd.Domain);
         if (migInterface != null)
         {
             try
             {
                 response = migInterface.InterfaceControl(cmd);
             }
             catch (Exception ex)
             {
                 LogError(Domains.HomeAutomation_HomeGenie, "InterfaceControl", ex.Message, "Exception.StackTrace", ex.StackTrace);
             }
         }
         //
         // If the command was not already handled, let automation programs process it
         if (response == null || String.IsNullOrWhiteSpace(response.ToString()))
         {
             response = ProgramDynamicApi.TryApiCall(cmd);
         }
         //
         // Macro Recording
         //
         // TODO: find a better solution for this....
         // TODO: it was: migService_ServiceRequestPostProcess(this, new ProcessRequestEventArgs(cmd));
         // TODO: !IMPORTANT!
         if (masterControlProgram != null && masterControlProgram.MacroRecorder.IsRecordingEnabled && cmd != null && cmd.Command != null && (cmd.Command.StartsWith("Control.") || (cmd.Command.StartsWith("AvMedia.") && cmd.Command != "AvMedia.Browse" && cmd.Command != "AvMedia.GetUri")))
         {
             masterControlProgram.MacroRecorder.AddCommand(cmd);
         }
     }
     return response;
 }
Beispiel #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;

                    }
                }
            }
        }
Beispiel #3
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;
                    }
                }
            }
        }
Beispiel #4
0
 public object InterfaceControl(MIGInterfaceCommand cmd)
 {
     object response = null;
     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);
             }
             response = netHelper.GetData();
         }
         catch (Exception ex)
         {
             HomeGenieService.LogEvent(Domains.HomeAutomation_HomeGenie, "Interconnection:" + target.RoutingNode, ex.Message, "Exception.StackTrace", ex.StackTrace);
         }
     }
     else
     {
         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(""))
         {
             response = migService.WebServiceDynamicApiCall(cmd);
         }
         // let HG post process the local command
         migService_ServiceRequestPostProcess(null, cmd);
     }
     return response;
 }
Beispiel #5
0
 public void InterfaceControl(MIGInterfaceCommand cmd)
 {
     var target = systemModules.Find(m => m.Domain == cmd.Domain && m.Address == cmd.NodeId);
     bool isRemoteModule = (target != null && target.RoutingNode.Trim() != "");
     if (isRemoteModule)
     {
         // ...
         try
         {
             string serviceUrl = "http://" + target.RoutingNode + "/api/" + cmd.Domain + "/" + cmd.NodeId + "/" + cmd.Command + "/" + cmd.OptionsString;
             Automation.Scripting.NetHelper netHelper = new Automation.Scripting.NetHelper(this).WebService(serviceUrl);
             if (systemConfiguration.HomeGenie.UserLogin != "" && 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);
 }