public static void Main (string[] args) { Console.WriteLine("Mig Interface Skelton test APP"); var migService = new MigService(); // Load the configuration from systemconfig.xml file MigServiceConfiguration configuration; // Construct an instance of the XmlSerializer with the type // of object that is being deserialized. XmlSerializer mySerializer = new XmlSerializer(typeof(MigServiceConfiguration)); // To read the file, create a FileStream. FileStream myFileStream = new FileStream("systemconfig.xml", FileMode.Open); // Call the Deserialize method and cast to the object type. configuration = (MigServiceConfiguration)mySerializer.Deserialize(myFileStream); // Set the configuration and start MIG Service migService.Configuration = configuration; migService.StartService(); // Get a reference to the test interface var interfaceDomain = "Example.InterfaceSkelton"; var migInterface = migService.GetInterface(interfaceDomain); // Test an interface API command programmatically <module_domain>/<module_address>/<command>[/<option_0>[/../<option_n>]] var response = migInterface.InterfaceControl(new MigInterfaceCommand(interfaceDomain + "/3/Greet.Hello/Username")); MigService.Log.Debug(response); // <module_domain> ::= "Example.InterfaceSkelton" // <module_address> ::= "3" // <command> ::= "Greet.Hello" // <option_0> ::= "Username" // For more infos about MIG API see: // http://genielabs.github.io/HomeGenie/api/mig/overview.html // http://genielabs.github.io/HomeGenie/api/mig/mig_api_interfaces.html // The same command can be invoked though the WebGateway // http://<server_address>:8080/api/Example.InterfaceSkelton/1/Greet.Hello/Username // Test some other interface API command response = migInterface.InterfaceControl(new MigInterfaceCommand(interfaceDomain + "/1/Control.On")); MigService.Log.Debug(response); response = migInterface.InterfaceControl(new MigInterfaceCommand(interfaceDomain + "/1/Control.Off")); MigService.Log.Debug(response); response = migInterface.InterfaceControl(new MigInterfaceCommand(interfaceDomain + "/2/Temperature.Get")); MigService.Log.Debug(response); Console.WriteLine("\n[Press Enter to Quit]\n"); Console.ReadLine(); }
private void InitializeSystem() { // Setup web service handlers wshConfig = new Handlers.Config(this); wshAutomation = new Handlers.Automation(this); wshInterconnection = new Handlers.Interconnection(this); wshStatistics = new Handlers.Statistics(this); // Initialize MigService, gateways and interfaces migService = new MIG.MigService(); migService.InterfaceModulesChanged += migService_InterfaceModulesChanged; migService.InterfacePropertyChanged += migService_InterfacePropertyChanged; migService.GatewayRequestPreProcess += migService_ServiceRequestPreProcess; migService.GatewayRequestPostProcess += migService_ServiceRequestPostProcess; // Setup other objects used in HG virtualMeter = new VirtualMeter(this); }
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); } } }
public static void Main(string[] args) { string webPort = "8088"; Console.WriteLine("MigService test APP"); Console.WriteLine("URL: http://localhost:{0}", webPort); var migService = new MigService(); // Add and configure the Web gateway var web = migService.AddGateway("WebServiceGateway"); web.SetOption("HomePath", "html"); web.SetOption("BaseUrl", "/pages/"); web.SetOption("Host", "*"); web.SetOption("Port", webPort); web.SetOption("Password", ""); web.SetOption("EnableFileCaching", "False"); // Add and configure the Web Socket gateway var ws = migService.AddGateway("WebSocketGateway"); ws.SetOption("Port", "8181"); // Configuration can also be loaded from a file as shown below /* MigServiceConfiguration configuration; // Construct an instance of the XmlSerializer with the type // of object that is being deserialized. XmlSerializer mySerializer = new XmlSerializer(typeof(MigServiceConfiguration)); // To read the file, create a FileStream. FileStream myFileStream = new FileStream("systemconfig.xml", FileMode.Open); // Call the Deserialize method and cast to the object type. configuration = (MigServiceConfiguration)mySerializer.Deserialize(myFileStream); // Set the configuration migService.Configuration = configuration; */ migService.StartService(); // Enable some interfaces for testing... /* var zwave = migService.AddInterface("HomeAutomation.ZWave", "MIG.HomeAutomation.dll"); zwave.SetOption("Port", "/dev/ttyUSB0"); migService.EnableInterface("HomeAutomation.ZWave"); */ /* var upnp = migService.AddInterface("Protocols.UPnP", "MIG.Protocols.dll"); migService.EnableInterface("Protocols.UPnP"); */ migService.RegisterApi("myapp/demo", (request) => { Console.WriteLine("Received API call from source {0}\n", request.Context.Source); Console.WriteLine("[Context data]\n{0}\n", MigService.JsonSerialize(request.Context.Data, true)); Console.WriteLine("[Mig Command]\n{0}\n", MigService.JsonSerialize(request.Command, true)); var cmd = request.Command; // cmd.Domain is the first element in the API URL (myapp) // cmd.Address is the second element in the API URL (demo) // cmd.Command is the third element in the API URL (greet | echo | ping) // cmd.GetOption(<n>) will give all the subsequent elements in the API URL (0...n) switch (cmd.Command) { case "greet": var name = cmd.GetOption(0); migService.RaiseEvent(typeof(MainClass), cmd.Domain, cmd.Address, "Reply to Greet", "Greet.User", name); break; case "echo": string fullRequestPath = cmd.OriginalRequest; migService.RaiseEvent(typeof(MainClass), cmd.Domain, cmd.Address, "Reply to Echo", "Echo.Data", fullRequestPath); break; case "ping": migService.RaiseEvent(typeof(MainClass), cmd.Domain, cmd.Address, "Reply to Ping", "Ping.Reply", "PONG"); break; } return new ResponseStatus(Status.Ok); }); while (true) { Thread.Sleep(10000); } }
public static void Main(string[] args) { string webPort = "8088"; Console.WriteLine("MigService test APP"); Console.WriteLine("URL: http://localhost:{0}", webPort); var migService = new MigService(); // Add and configure the Web gateway var web = migService.AddGateway("WebServiceGateway"); web.SetOption("HomePath", "html"); web.SetOption("BaseUrl", "/pages/"); web.SetOption("Host", "*"); web.SetOption("Port", webPort); web.SetOption("Password", ""); web.SetOption("EnableFileCaching", "False"); // Add and configure the Web Socket gateway var ws = migService.AddGateway("WebSocketGateway"); ws.SetOption("Port", "8181"); /* var zwave = migService.AddInterface("HomeAutomation.ZWave", "MIG.HomeAutomation.dll"); zwave.SetOption("Port", "/dev/ttyUSB0"); migService.EnableInterface("HomeAutomation.ZWave"); */ /* var upnp = migService.AddInterface("Protocols.UPnP", "MIG.Protocols.dll"); migService.EnableInterface("Protocols.UPnP"); */ migService.StartService(); migService.RegisterApi("myapp/demo", (request) => { Console.WriteLine("Received API call from source {0}\n", request.Context.Source); Console.WriteLine("[Context data]\n{0}\n", MigService.JsonSerialize(request.Context.Data, true)); Console.WriteLine("[Mig Command]\n{0}\n", MigService.JsonSerialize(request.Command, true)); var cmd = request.Command; // cmd.Domain is the first element in the API URL (myapp) // cmd.Address is the second element in the API URL (demo) // cmd.Command is the third element in the API URL (greet | echo | ping) // cmd.GetOption(<n>) will give all the subsequent elements in the API URL (0...n) switch (cmd.Command) { case "greet": var name = cmd.GetOption(0); migService.RaiseEvent(typeof(MainClass), cmd.Domain, cmd.Address, "Reply to Greet", "Greet.User", name); break; case "echo": string fullRequestPath = cmd.OriginalRequest; migService.RaiseEvent(typeof(MainClass), cmd.Domain, cmd.Address, "Reply to Echo", "Echo.Data", fullRequestPath); break; case "ping": migService.RaiseEvent(typeof(MainClass), cmd.Domain, cmd.Address, "Reply to Ping", "Ping.Reply", "PONG"); break; } return new ResponseStatus(Status.Ok); }); while (true) { Thread.Sleep(10000); } }