public static object TryApiCall(MigInterfaceCommand command)
 {
     object response = "";
     // Dynamic Interface API 
     var registeredApi = command.Domain + "/" + command.Address + "/" + command.Command;
     var handler = ProgramDynamicApi.Find(registeredApi);
     if (handler != null)
     {
         // explicit command API handlers registered in the form <domain>/<address>/<command>
         // receives only the remaining part of the request after the <command>
         var args = command.OriginalRequest.Substring(registeredApi.Length).Trim('/');
         response = handler(args);
     }
     else
     {
         handler = ProgramDynamicApi.FindMatching(command.OriginalRequest.Trim('/'));
         if (handler != null)
         {
             // other command API handlers
             // receives the full request string
             response = handler(command.OriginalRequest.Trim('/'));
         }
     }
     return response;
 }
Exemple #2
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;
 }
Exemple #3
0
 // TODO: move this to a better location
 public bool ExecuteAutomationRequest(MigInterfaceCommand command)
 {
     string levelValue, commandValue;
     // check for certain commands
     if (command.Command == Commands.Groups.GroupsLightsOff)
     {
         levelValue = "0";
         commandValue = Commands.Control.ControlOff;
     }
     else if (command.Command == Commands.Groups.GroupsLightsOn)
     {
         levelValue = "1";
         commandValue = Commands.Control.ControlOn;
     }
     else
     {
         return false;
     }
     //loop, turning off lights
     try
     {
         var group = Groups.Find(z => z.Name == command.GetOption(0));
         for (int m = 0; m < group.Modules.Count; m++)
         {
             var module = Modules.Find(mod => mod.Domain == group.Modules[m].Domain && mod.Address == group.Modules[m].Address);
             if (module != null && (module.DeviceType == MIG.ModuleTypes.Light || module.DeviceType == MIG.ModuleTypes.Dimmer))
             {
                 try
                 {
                     var icmd = new MigInterfaceCommand(module.Domain + "/" + module.Address + "/" + commandValue);
                     InterfaceControl(icmd);
                     Service.Utility.ModuleParameterGet(module, Properties.StatusLevel).Value = levelValue;
                 }
                 catch (Exception e)
                 {
                     LogError(e);
                 }
             }
         }
     }
     catch
     {
         // TODO: handle exception here
     }
     return true;
 }
Exemple #4
0
        private void migService_ServiceRequestPostProcess(object sender, ProcessRequestEventArgs args)
        {
            var command = args.Request.Command;
            if (command.Domain ==  Domains.MigService_Interfaces && command.Command.EndsWith(".Set"))
            {
                systemConfiguration.Update();
            }

            // Let automation programs process the request; we append eventual POST data (RequestText) to the MigInterfaceCommand
            if (!String.IsNullOrWhiteSpace(args.Request.RequestText))
                command = new MigInterfaceCommand(command.OriginalRequest + "/" + args.Request.RequestText);
            args.Request.ResponseData = ProgramDynamicApi.TryApiCall(command);

            // Macro Recording
            if (masterControlProgram != null && masterControlProgram.MacroRecorder.IsRecordingEnabled && command != null && command.Command != null && (command.Command.StartsWith("Control.") || (command.Command.StartsWith("AvMedia.") && command.Command != "AvMedia.Browse" && command.Command != "AvMedia.GetUri")))
            {
                masterControlProgram.MacroRecorder.AddCommand(command);
            }
        }
Exemple #5
0
 public MigClientRequest(MigContext context, MigInterfaceCommand command)
 {
     Command = command;
     Context = context;
 }
Exemple #6
0
 private void ExecuteCommand(ProgramCommand programCommand)
 {
     string command = programCommand.Domain + "/" + programCommand.Target + "/" + programCommand.CommandString + "/" + System.Uri.EscapeDataString(programCommand.CommandArguments);
     var interfaceCommand = new MigInterfaceCommand(command);
     homegenie.InterfaceControl(interfaceCommand);
 }
        public void AddCommand(MigInterfaceCommand cmd)
        {
            double delay = 0;
            switch (delayType)
            {
            case MacroDelayType.Mimic:
                // calculate pause between current and previous command
                delay = new TimeSpan(DateTime.Now.Ticks - currentTimestamp.Ticks).TotalSeconds;
                break;

            case MacroDelayType.Fixed:
                // put a fixed pause
                delay = delaySeconds;
                break;
            }
            //
            try
            {
                if (delay > 0 && macroCommands.Count > 0)
                {
                    // add a pause command to the macro
                    macroCommands.Add(new MigInterfaceCommand(Domains.HomeAutomation_HomeGenie + "/Automation/Program.Pause/" + delay.ToString(System.Globalization.CultureInfo.InvariantCulture)));
                }
                macroCommands.Add(cmd);
            }
            catch
            {
                //HomeGenieService.LogEvent(Domains.HomeAutomation_HomeGenie, "migservice_ServiceRequestPostProcess(...)", ex.Message, "Exception.StackTrace", ex.StackTrace);
            }
            //
            currentTimestamp = DateTime.Now;
        }
 private object InterfaceControl(Module module, MigInterfaceCommand migCommand)
 {
     migCommand.Domain = module.Domain;
     migCommand.Address = module.Address;
     return homegenie.InterfaceControl(migCommand);
 }
 public MigClientRequest(MigContext context, MigInterfaceCommand command)
 {
     Command = command;
     Context = context;
 }
Exemple #10
0
 private object InterfaceControl(Module module, MigInterfaceCommand migCommand)
 {
     object response = null;
     migCommand.Domain = module.Domain;
     migCommand.Address = module.Address;
     try
     {
         response = homegenie.InterfaceControl(migCommand);
     }
     catch(Exception e)
     {
         // TODO: should report the error?
     }
     return response;
 }