private void Gateway_PostProcessRequest(object sender, ProcessRequestEventArgs args) { var request = args.Request; // Route event OnPostProcessRequest(sender, request); }
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; } } } }
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); } }
private void Gateway_PreProcessRequest(object sender, ProcessRequestEventArgs args) { var request = args.Request; // Route event OnPreProcessRequest(sender, request); if (request.Handled) { return; } var command = request.Command; if (command.Domain == "MIGService.Interfaces") { // This is a MIGService namespace Web API switch (command.Command) { case "IsEnabled.Set": if (command.GetOption(0) == "1") { if (EnableInterface(command.Address) != null) { request.ResponseData = new ResponseStatus(Status.Ok, String.Format("Interface {0} enabled", command.Address)); } else { request.ResponseData = new ResponseStatus(Status.Error, String.Format("Interface {0} not found", command.Address)); } } else { if (DisableInterface(command.Address) != null) { request.ResponseData = new ResponseStatus(Status.Ok, String.Format("Interface {0} disabled", command.Address)); } else { request.ResponseData = new ResponseStatus(Status.Error, String.Format("Interface {0} not found", command.Address)); } } OnInterfacePropertyChanged(sender, new InterfacePropertyChangedEventArgs("MIGService.Interfaces", command.Address, "MIG Interface", "Status.IsEnabled", command.GetOption(0))); break; case "IsEnabled.Get": request.ResponseData = new ResponseText(configuration.GetInterface(command.Address).IsEnabled ? "1" : "0"); break; case "Options.Set": { var iface = GetInterface(command.Address); if (iface != null) { iface.SetOption(command.GetOption(0), command.GetOption(1)); request.ResponseData = new ResponseStatus(Status.Ok, String.Format("{0} option '{1}' set to '{2}'", command.Address, command.GetOption(0), command.GetOption(1))); } else { request.ResponseData = new ResponseStatus(Status.Error, String.Format("Interface {0} not found", command.Address)); } } OnInterfacePropertyChanged(sender, new InterfacePropertyChangedEventArgs("MIGService.Interfaces", command.Address, "MIG Interface", "Options." + command.GetOption(0), command.GetOption(1))); break; case "Options.Get": { var iface = GetInterface(command.Address); if (iface != null) { string optionValue = iface.GetOption(command.GetOption(0)).Value; request.ResponseData = new ResponseText(optionValue); } else { request.ResponseData = new ResponseStatus(Status.Error, String.Format("Interface {0} not found", command.Address)); } } break; default: break; } } else { // Try processing as MigInterface Api or Web Service Dynamic Api var iface = (from miginterface in Interfaces let ns = miginterface.GetType().Namespace let domain = ns.Substring(ns.LastIndexOf(".") + 1) + "." + miginterface.GetType().Name where (command.Domain != null && command.Domain.StartsWith(domain)) select miginterface).FirstOrDefault(); if (iface != null) // && iface.IsEnabled) { //if (iface.IsConnected) //{ try { request.ResponseData = iface.InterfaceControl(command); } catch (Exception ex) { request.ResponseData = new ResponseStatus(Status.Error, MigService.JsonSerialize(ex)); } //} //else //{ // request.ResponseData = new ResponseStatus(Status.Error, String.Format("Interface '{0}' not connected", iface.GetDomain())); //} } // Try processing as Dynamic API if ((request.ResponseData == null || request.ResponseData.Equals(String.Empty))) { request.ResponseData = TryDynamicApi(request); } } }
private void Gateway_PreProcessRequest(object sender, ProcessRequestEventArgs args) { var request = args.Request; // Route event OnPreProcessRequest(sender, request); if (request.Handled) return; var command = request.Command; if (command.Domain == "MIGService.Interfaces") { // This is a MIGService namespace Web API switch (command.Command) { case "IsEnabled.Set": if (command.GetOption(0) == "1") { if (EnableInterface(command.Address) != null) request.ResponseData = new ResponseStatus(Status.Ok, String.Format("Interface {0} enabled", command.Address)); else request.ResponseData = new ResponseStatus(Status.Error, String.Format("Interface {0} not found", command.Address)); } else { if (DisableInterface(command.Address) != null) request.ResponseData = new ResponseStatus(Status.Ok, String.Format("Interface {0} disabled", command.Address)); else request.ResponseData = new ResponseStatus(Status.Error, String.Format("Interface {0} not found", command.Address)); } OnInterfacePropertyChanged(sender, new InterfacePropertyChangedEventArgs("MIGService.Interfaces", command.Address, "MIG Interface", "Status.IsEnabled", command.GetOption(0))); break; case "IsEnabled.Get": request.ResponseData = new ResponseText(configuration.GetInterface(command.Address).IsEnabled ? "1" : "0"); break; case "Options.Set": { var iface = GetInterface(command.Address); if (iface != null) { iface.SetOption(command.GetOption(0), command.GetOption(1)); request.ResponseData = new ResponseStatus(Status.Ok, String.Format("{0} option '{1}' set to '{2}'", command.Address, command.GetOption(0), command.GetOption(1))); } else { request.ResponseData = new ResponseStatus(Status.Error, String.Format("Interface {0} not found", command.Address)); } } OnInterfacePropertyChanged(sender, new InterfacePropertyChangedEventArgs("MIGService.Interfaces", command.Address, "MIG Interface", "Options." + command.GetOption(0), command.GetOption(1))); break; case "Options.Get": { var iface = GetInterface(command.Address); if (iface != null) { string optionValue = iface.GetOption(command.GetOption(0)).Value; request.ResponseData = new ResponseText(optionValue); } else { request.ResponseData = new ResponseStatus(Status.Error, String.Format("Interface {0} not found", command.Address)); } } break; default: break; } } else { // Try processing as MigInterface Api or Web Service Dynamic Api var iface = (from miginterface in Interfaces let ns = miginterface.GetType().Namespace let domain = ns.Substring(ns.LastIndexOf(".") + 1) + "." + miginterface.GetType().Name where (command.Domain != null && command.Domain.StartsWith(domain)) select miginterface).FirstOrDefault(); if (iface != null) // && iface.IsEnabled) { //if (iface.IsConnected) //{ try { request.ResponseData = iface.InterfaceControl(command); } catch (Exception ex) { request.ResponseData = new ResponseStatus(Status.Error, MigService.JsonSerialize(ex)); } //} //else //{ // request.ResponseData = new ResponseStatus(Status.Error, String.Format("Interface '{0}' not connected", iface.GetDomain())); //} } // Try processing as Dynamic API if ((request.ResponseData == null || request.ResponseData.Equals(String.Empty))) { request.ResponseData = TryDynamicApi(request); } } }