public void ProcessRequest(MIGClientRequest request, MIGInterfaceCommand migCommand) { switch (migCommand.Command) { case "Events.Push": //TODO: implemet security and trust mechanism var stream = new StreamReader(request.InputStream).ReadToEnd(); var moduleEvent = JsonConvert.DeserializeObject<ModuleEvent>( stream, new JsonSerializerSettings(){ Culture = System.Globalization.CultureInfo.InvariantCulture } ); // // prefix remote event domain with HGIC:<remote_node_address>.<domain> moduleEvent.Module.Domain = "HGIC:" + request.RequestOrigin.Replace(".", "_") + "." + moduleEvent.Module.Domain; // var module = homegenie.Modules.Find(delegate(Module o) { return o.Domain == moduleEvent.Module.Domain && o.Address == moduleEvent.Module.Address; }); if (module == null) { module = moduleEvent.Module; homegenie.Modules.Add(module); } else { Utility.ModuleParameterSet(module, moduleEvent.Parameter.Name, moduleEvent.Parameter.Value); } // "<ip>:<port>" remote endpoint port is passed as the first argument from the remote point itself module.RoutingNode = request.RequestOrigin + (migCommand.GetOption(0) != "" ? ":" + migCommand.GetOption(0) : ""); // homegenie.LogBroadcastEvent( moduleEvent.Module.Domain, moduleEvent.Module.Address, request.RequestOrigin, moduleEvent.Parameter.Name, moduleEvent.Parameter.Value ); HomeGenie.Service.HomeGenieService.RoutedEvent eventData = new HomeGenie.Service.HomeGenieService.RoutedEvent() { Sender = request.RequestOrigin, Module = module, Parameter = moduleEvent.Parameter }; ThreadPool.QueueUserWorkItem(new WaitCallback(homegenie.RouteParameterChangedEvent), eventData); break; } }
public object WebServiceDynamicApiCall(MIGInterfaceCommand command) { object response = ""; // Dynamic Interface API var handler = MIG.Interfaces.DynamicInterfaceAPI.Find(command.Domain + "/" + command.NodeId + "/" + command.Command); if (handler != null) { response = handler(command.GetOption(0) + (command.GetOption(1) != "" ? "/" + command.GetOption(1) : "")); } else { handler = MIG.Interfaces.DynamicInterfaceAPI.FindMatching(command.OriginalRequest.Trim('/')); if (handler != null) { response = handler(command.OriginalRequest.Trim('/')); } } return(response); }
public object WebServiceDynamicApiCall(MIGInterfaceCommand cmd) { object response = ""; // Dynamic Interface API Func <object, object> handler = MIG.Interfaces.DynamicInterfaceAPI.Find(cmd.domain + "/" + cmd.nodeid + "/" + cmd.command); if (handler != null) { response = handler(cmd.GetOption(0) + (cmd.GetOption(1) != "" ? "/" + cmd.GetOption(1) : "")); } else { handler = MIG.Interfaces.DynamicInterfaceAPI.FindMatching(cmd.originalRequest.Trim('/')); if (handler != null) { response = handler(cmd.originalRequest.Trim('/')); } } return(response); }
public void ProcessRequest(MIGClientRequest request, MIGInterfaceCommand migCommand) { string domain = ""; string address = ""; int domainSeparator = 0; DateTime dateStart, dateEnd; switch (migCommand.Command) { case "Global.CounterTotal": var counter = homegenie.Statistics.GetTotalCounter(migCommand.GetOption(0), 3600); migCommand.Response = JsonHelper.GetSimpleResponse(counter.ToString("0.000", System.Globalization.CultureInfo.InvariantCulture)); break; case "Global.TimeRange": var totalRange = homegenie.Statistics.GetDateRange(); migCommand.Response = "[{ StartTime : '" + DateToJavascript(totalRange.TimeStart).ToString("0.000", System.Globalization.CultureInfo.InvariantCulture) + "', EndTime : '" + DateToJavascript(totalRange.TimeEnd).ToString("0.000", System.Globalization.CultureInfo.InvariantCulture) + "' }]"; break; case "Database.Reset": homegenie.Statistics.DatabaseReset(); break; case "Configuration.Get": // Just one at the moment. migCommand.Response = "[{ StatisticsUIRefreshSeconds : '" + homegenie.SystemConfiguration.HomeGenie.Statistics.StatisticsUIRefreshSeconds + "' }]"; break; case "Parameter.List": domainSeparator = migCommand.GetOption(0).LastIndexOf(":"); if (domainSeparator > 0) { domain = migCommand.GetOption(0).Substring(0, domainSeparator); address = migCommand.GetOption(0).Substring(domainSeparator + 1); } migCommand.Response = "["; foreach (string statParameter in homegenie.Statistics.GetParametersList(domain, address)) { migCommand.Response += " '" + statParameter + "',\n"; } migCommand.Response = migCommand.Response.TrimEnd(',', '\n'); migCommand.Response += "\n]"; break; case "Parameter.Counter": domainSeparator = migCommand.GetOption(1).LastIndexOf(":"); if (domainSeparator > 0) { domain = migCommand.GetOption(1).Substring(0, domainSeparator); address = migCommand.GetOption(1).Substring(domainSeparator + 1); } // migCommand.Response = "["; migCommand.Response += "[ "; // var hoursAverage = new List<StatisticsEntry>(); dateStart = JavascriptToDate(long.Parse(migCommand.GetOption(2))); dateEnd = JavascriptToDate(long.Parse(migCommand.GetOption(3))); hoursAverage = homegenie.Statistics.GetHourlyCounter(domain, address, migCommand.GetOption(0), 3600, dateStart, dateEnd); // for (int h = 0; h < 24; h++) { StatisticsEntry firstEntry = null; if (hoursAverage != null && hoursAverage.Count > 0) { firstEntry = hoursAverage.Find(se => se.TimeStart.ToLocalTime().Hour == h); } // if (firstEntry != null) { double sum = 0; sum = (double)(hoursAverage.FindAll(se => se.TimeStart.ToLocalTime().Hour == h).Sum(se => se.Value)); // date is normalized to the current date, time info is preserved from original data entry var date = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd") + " " + h.ToString("00") + ":00:00"); migCommand.Response += "[" + DateToJavascript(date).ToString("0.000", System.Globalization.CultureInfo.InvariantCulture) + "," + sum.ToString("0.000", System.Globalization.CultureInfo.InvariantCulture) + "],"; } else { var date = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd") + " " + h.ToString("00") + ":00:00"); migCommand.Response += "[" + DateToJavascript(date).ToString("0.000", System.Globalization.CultureInfo.InvariantCulture) + ",0.000],"; } } migCommand.Response = migCommand.Response.TrimEnd(','); migCommand.Response += " ]"; migCommand.Response += "]"; break; case "Parameter.StatsHour": domainSeparator = migCommand.GetOption(1).LastIndexOf(":"); if (domainSeparator > 0) { domain = migCommand.GetOption(1).Substring(0, domainSeparator); address = migCommand.GetOption(1).Substring(domainSeparator + 1); } // migCommand.Response = "["; // dateStart = JavascriptToDate(long.Parse(migCommand.GetOption(2))); dateEnd = JavascriptToDate(long.Parse(migCommand.GetOption(3))); var hoursAverages = new List<StatisticsEntry>[5]; hoursAverages[0] = homegenie.Statistics.GetHourlyStats(domain, address, migCommand.GetOption(0), "Min", dateStart, dateEnd); hoursAverages[1] = homegenie.Statistics.GetHourlyStats(domain, address, migCommand.GetOption(0), "Max", dateStart, dateEnd); hoursAverages[2] = homegenie.Statistics.GetHourlyStats(domain, address, migCommand.GetOption(0), "Avg", dateStart, dateEnd); hoursAverages[3] = homegenie.Statistics.GetHourlyStatsToday(domain, address, migCommand.GetOption(0), "Avg"); if (migCommand.GetOption(0).StartsWith(Properties.METER_ANY)) { hoursAverages[4] = homegenie.Statistics.GetTodayDetail(domain, address, migCommand.GetOption(0), "Sum"); } else { hoursAverages[4] = homegenie.Statistics.GetTodayDetail(domain, address, migCommand.GetOption(0), "Avg"); } // for (int x = 0; x < 4; x++) { migCommand.Response += "[ "; for (int h = 0; h < 24; h++) { StatisticsEntry firstEntry = null; if (hoursAverages[x] != null && hoursAverages[x].Count > 0) { if (migCommand.GetOption(0).StartsWith(Properties.METER_ANY)) { firstEntry = hoursAverages[x].Find(se => se.TimeStart.ToLocalTime().Hour == h && se.Value > 0); } else { firstEntry = hoursAverages[x].Find(se => se.TimeStart.ToLocalTime().Hour == h); } } // if (firstEntry != null) { double sum = 0; switch (x) { case 0: if (migCommand.GetOption(0).StartsWith(Properties.METER_ANY)) { sum = (double)(hoursAverages[x].FindAll(se => se.TimeStart.ToLocalTime().Hour == h && se.Value > 0).Min(se => se.Value)); } else { sum = (double)(hoursAverages[x].FindAll(se => se.TimeStart.ToLocalTime().Hour == h).Min(se => se.Value)); } break; case 1: sum = (double)(hoursAverages[x].FindAll(se => se.TimeStart.ToLocalTime().Hour == h).Max(se => se.Value)); break; case 2: if (migCommand.GetOption(0).StartsWith(Properties.METER_ANY)) { sum = (double)(hoursAverages[x].FindAll(se => se.TimeStart.ToLocalTime().Hour == h && se.Value > 0).Average(se => se.Value)); } else { sum = (double)(hoursAverages[x].FindAll(se => se.TimeStart.ToLocalTime().Hour == h).Average(se => se.Value)); } break; case 3: if (migCommand.GetOption(0).StartsWith(Properties.METER_ANY)) { sum = (double)(hoursAverages[x].FindAll(se => se.TimeStart.ToLocalTime().Hour == h && se.Value > 0).Average(se => se.Value)); } else { sum = (double)(hoursAverages[x].FindAll(se => se.TimeStart.ToLocalTime().Hour == h).Average(se => se.Value)); } break; } // date is normalized to the current date, time info is preserved from original data entry var date = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd") + " " + h.ToString("00") + ":00:00"); migCommand.Response += "[" + DateToJavascript(date).ToString("0.000", System.Globalization.CultureInfo.InvariantCulture) + "," + sum.ToString("0.000", System.Globalization.CultureInfo.InvariantCulture) + "],"; } else { var date = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd") + " " + h.ToString("00") + ":00:00"); migCommand.Response += "[" + DateToJavascript(date).ToString("0.000", System.Globalization.CultureInfo.InvariantCulture) + ",0.000],"; } } migCommand.Response = migCommand.Response.TrimEnd(','); migCommand.Response += " ],"; } // migCommand.Response += "[ "; foreach (var entry in hoursAverages[4]) { var date = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd") + " " + entry.TimeStart.ToLocalTime().ToString("HH:mm:ss.ffffff")); migCommand.Response += "[" + DateToJavascript(date).ToString("0.000", System.Globalization.CultureInfo.InvariantCulture) + "," + entry.Value.ToString("0.000", System.Globalization.CultureInfo.InvariantCulture) + "],"; } migCommand.Response = migCommand.Response.TrimEnd(','); migCommand.Response += " ]"; // migCommand.Response += "]"; break; } }
public bool ExecuteAutomationRequest(MIGInterfaceCommand command) { bool handled = false; //never assigned string levelValue, commandValue; // check for certain commands if (command.Command == Commands.Groups.GROUPS_LIGHTSOFF) { levelValue = "0"; commandValue = Commands.Control.CONTROL_OFF; } else if (command.Command == Commands.Groups.GROUPS_LIGHTSON) { levelValue = "1"; commandValue = Commands.Control.CONTROL_ON; } else { return handled; } //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 { MIGInterfaceCommand icmd = new MIGInterfaceCommand(module.Domain + "/" + module.Address + "/" + commandValue); InterfaceControl(icmd); Service.Utility.ModuleParameterGet(module, ModuleParameters.MODPAR_STATUS_LEVEL).Value = levelValue; } catch { } } } } catch { // TODO: handle exception here } return handled; }
private void webGateway_ProcessRequest(object gwRequest) { var request = (WebServiceGatewayRequest)gwRequest; var context = request.Context; string requestedUrl = request.UrlRequest; var migRequest = new MIGClientRequest() { Context = context, RequestOrigin = context.Request.RemoteEndPoint.Address.ToString(), RequestMessage = requestedUrl, SubjectName = "HTTP", SubjectValue = context.Request.HttpMethod, InputStream = context.Request.InputStream, OutputStream = context.Response.OutputStream }; // we are expecting url in the forms http://<hgserver>/<hgservicekey>/<servicedomain>/<servicegroup>/<command>/<opt1>/.../<optn> // arguments up to <command> are mandatory. string migCommand = requestedUrl.Substring(requestedUrl.IndexOf('/', 1) + 1); //string section = requestedurl.Substring (0, requestedurl.IndexOf ('/', 1) - 1); TODO: "api" section keyword, ignored for now //TODO: implement "api" keyword in MIGInterfaceCommand? var command = new MIGInterfaceCommand(migCommand); // MIGService namespace Web API if (command.Domain == "MIGService.Interfaces") { context.Response.ContentType = "application/json"; switch (command.Command) { case "IsEnabled.Set": if (command.GetOption(0) == "1") { configuration.GetInterface(command.NodeId).IsEnabled = true; EnableInterface(command.NodeId); } else { configuration.GetInterface(command.NodeId).IsEnabled = false; DisableInterface(command.NodeId); } WebServiceUtility.WriteStringToContext(context, "[{ \"ResponseValue\" : \"OK\" }]"); // if (InterfacePropertyChanged != null) { InterfacePropertyChanged(new InterfacePropertyChangedAction() { Domain = "MIGService.Interfaces", SourceId = command.NodeId, SourceType = "MIG Interface", Path = "Status.IsEnabled", Value = command.GetOption(0) }); } break; case "IsEnabled.Get": WebServiceUtility.WriteStringToContext(context, "[{ \"ResponseValue\" : \"" + (configuration.GetInterface(command.NodeId).IsEnabled ? "1" : "0") + "\" }]"); break; case "Options.Set": Interfaces[command.NodeId].SetOption(command.GetOption(0), command.GetOption(1)); WebServiceUtility.WriteStringToContext(context, "[{ \"ResponseValue\" : \"OK\" }]"); // if (InterfacePropertyChanged != null) { InterfacePropertyChanged(new InterfacePropertyChangedAction() { Domain = "MIGService.Interfaces", SourceId = command.NodeId, SourceType = "MIG Interface", Path = "Options." + command.GetOption(0), Value = command.GetOption(1) }); } break; case "Options.Get": string optionValue = Interfaces[command.NodeId].GetOption(command.GetOption(0)).Value; WebServiceUtility.WriteStringToContext(context, "[{ \"ResponseValue\" : \"" + Uri.EscapeDataString(optionValue) + "\" }]"); break; default: break; } return; } //PREPROCESS request: if domain != html, execute command if (ServiceRequestPreProcess != null) { ServiceRequestPreProcess(migRequest, command); // request was handled by preprocess listener if (!string.IsNullOrEmpty(command.Response)) { // simple automatic json response type detection if (command.Response.StartsWith("[") && command.Response.EndsWith("]") || (command.Response.StartsWith("{") && command.Response.EndsWith("}"))) { // TODO: check the reason why this cause ajax/json error on some browser context.Response.ContentType = "application/json"; context.Response.ContentEncoding = defaultWebFileEncoding; } WebServiceUtility.WriteStringToContext(context, command.Response); return; } } // TODO: move dupe code to WebServiceUtility //if request begins /hg/html, process if (requestedUrl.StartsWith(webServiceConfig.BaseUrl)) { string requestedFile = GetWebFilePath(requestedUrl); if (!System.IO.File.Exists(requestedFile)) { context.Response.StatusCode = 404; //context.Response.OutputStream.WriteByte(); } else { bool isText = false; if (requestedUrl.EndsWith(".js")) // || requestedurl.EndsWith(".json")) { context.Response.ContentType = "text/javascript"; isText = true; } else if (requestedUrl.EndsWith(".css")) { context.Response.ContentType = "text/css"; isText = true; } else if (requestedUrl.EndsWith(".zip")) { context.Response.ContentType = "application/zip"; } else if (requestedUrl.EndsWith(".png")) { context.Response.ContentType = "image/png"; } else if (requestedUrl.EndsWith(".jpg")) { context.Response.ContentType = "image/jpeg"; } else if (requestedUrl.EndsWith(".gif")) { context.Response.ContentType = "image/gif"; } else if (requestedUrl.EndsWith(".mp3")) { context.Response.ContentType = "audio/mp3"; } else { context.Response.ContentType = "text/html"; isText = true; } var file = new System.IO.FileInfo(requestedFile); context.Response.AddHeader("Last-Modified", file.LastWriteTimeUtc.ToString("r")); context.Response.Headers.Set(HttpResponseHeader.LastModified, file.LastWriteTimeUtc.ToString("r")); // PRE PROCESS text output //TODO: add callback for handling caching (eg. function that returns true or false with requestdfile as input and that will return false for widget path) if (isText) { try { WebFileCache cachedItem = GetWebFileCache(requestedFile); context.Response.ContentEncoding = cachedItem.Encoding; context.Response.ContentType += "; charset=" + cachedItem.Encoding.BodyName; string body = cachedItem.Content; // bool tagFound; do { tagFound = false; int ts = body.IndexOf("{include "); if (ts > 0) { int te = body.IndexOf("}", ts); if (te > ts) { string rs = body.Substring(ts + (te - ts) + 1); string cs = body.Substring(ts, te - ts + 1); string ls = body.Substring(0, ts); // try { if (cs.StartsWith("{include ")) { string fileName = cs.Substring(9).TrimEnd('}').Trim(); fileName = GetWebFilePath(fileName); // Encoding fileEncoding = DetectWebFileEncoding(fileName); if (fileEncoding == null) { fileEncoding = defaultWebFileEncoding; } body = ls + System.IO.File.ReadAllText(fileName, fileEncoding) + rs; } } catch { body = ls + "<h5 style=\"color:red\">Error processing '" + cs.Replace("{", "[").Replace("}", "]") + "'</h5>" + rs; } tagFound = true; } } } while (tagFound); // pre processor tag found // if (webServiceConfig.CacheEnable) { PutWebFileCache(requestedFile, body, context.Response.ContentEncoding); } // WebServiceUtility.WriteStringToContext(context, body); } catch (Exception ex) { // TODO: report internal mig interface error context.Response.StatusCode = 500; WebServiceUtility.WriteStringToContext(context, ex.Message + "\n" + ex.StackTrace); Console.WriteLine("\nMIGService ERROR: " + ex.Message + "\n" + ex.StackTrace + "\n"); } } else { WebServiceUtility.WriteBytesToContext(context, System.IO.File.ReadAllBytes(requestedFile)); } } } object responseObject = null; bool wroteBytes = false; //domain == HomeAutomation._Interface_ call InterfaceControl var result = (from miginterface in Interfaces.Values 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 (result != null) { try { responseObject = result.InterfaceControl(command); } catch (Exception ex) { // TODO: report internal mig interface error context.Response.StatusCode = 500; responseObject = ex.Message + "\n" + ex.StackTrace; } } // if (responseObject == null || responseObject.Equals(String.Empty)) { responseObject = WebServiceDynamicApiCall(command); } // if (responseObject != null && responseObject.GetType().Equals(typeof(string))) { command.Response = (string)responseObject; // // simple automatic json response type detection if (command.Response.StartsWith("[") && command.Response.EndsWith("]") || (command.Response.StartsWith("{") && command.Response.EndsWith("}"))) { context.Response.ContentType = "application/json"; context.Response.ContentEncoding = defaultWebFileEncoding; } } else { WebServiceUtility.WriteBytesToContext(context, (Byte[])responseObject); wroteBytes = true; } // //POSTPROCESS if (ServiceRequestPostProcess != null) { ServiceRequestPostProcess(migRequest, command); if (!string.IsNullOrEmpty(command.Response) && !wroteBytes) { // request was handled by postprocess listener WebServiceUtility.WriteStringToContext(context, command.Response); return; } } }
// Check if command was Control.*, update the ModuleParameter. This should happen in a HWInt->HomeGenie pathway private void migService_ServiceRequestPostProcess(MIGClientRequest request, MIGInterfaceCommand command) { // REMARK: No post data is available at this point since it has already beel consumed by ServiceRequestPreProcess switch (command.Domain) { case Domains.HomeAutomation_X10: case Domains.HomeAutomation_ZWave: Module module = null; try { module = Modules.Find(o => o.Domain == command.Domain && o.Address == command.NodeId); } catch { } // // TODO: this should be placed somewhere else (this is specific code for handling interface responses, none of HG business) if (module != null) { // wait for ZWaveLib asynchronous response from node and raise the proper "parameter changed" event if (command.Domain == Domains.HomeAutomation_ZWave) // && (context != null && !context.Request.IsLocal) { if (command.Command == ZWave.Command.BASIC_GET) { command.Response = Utility.WaitModuleParameterChange(module, Properties.ZWAVENODE_BASIC); command.Response = JsonHelper.GetSimpleResponse(command.Response); } else if (command.Command == ZWave.Command.WAKEUP_GET) { command.Response = Utility.WaitModuleParameterChange( module, Properties.ZWAVENODE_WAKEUPINTERVAL ); command.Response = JsonHelper.GetSimpleResponse(command.Response); } else if (command.Command == ZWave.Command.BATTERY_GET) { command.Response = Utility.WaitModuleParameterChange(module, Properties.ZWAVENODE_BATTERY); command.Response = JsonHelper.GetSimpleResponse(command.Response); } else if (command.Command == ZWave.Command.MULTIINSTANCE_GET) { command.Response = Utility.WaitModuleParameterChange( module, Properties.ZWAVENODE_MULTIINSTANCE + "." + command.GetOption(0).Replace(".", "") + "." + command.GetOption(1) ); command.Response = JsonHelper.GetSimpleResponse(command.Response); } else if (command.Command == ZWave.Command.MULTIINSTANCE_GETCOUNT) { command.Response = Utility.WaitModuleParameterChange( module, Properties.ZWAVENODE_MULTIINSTANCE + "." + command.GetOption(0).Replace(".", "") + "." + ".Count" ); command.Response = JsonHelper.GetSimpleResponse(command.Response); } else if (command.Command == ZWave.Command.ASSOCIATION_GET) { command.Response = Utility.WaitModuleParameterChange( module, Properties.ZWAVENODE_ASSOCIATIONS + "." + command.GetOption(0) ); command.Response = JsonHelper.GetSimpleResponse(command.Response); } else if (command.Command == ZWave.Command.CONFIG_PARAMETERGET) { command.Response = Utility.WaitModuleParameterChange( module, Properties.ZWAVENODE_CONFIGVARIABLES + "." + command.GetOption(0) ); command.Response = JsonHelper.GetSimpleResponse(command.Response); } else if (command.Command == ZWave.Command.NODEINFO_GET) { command.Response = Utility.WaitModuleParameterChange(module, Properties.ZWAVENODE_NODEINFO); command.Response = JsonHelper.GetSimpleResponse(command.Response); } else if (command.Command == ZWave.Command.CONTROLLER_NODENEIGHBORUPDATE) { command.Response = Utility.WaitModuleParameterChange(module, Properties.ZWAVENODE_ROUTINGINFO); command.Response = JsonHelper.GetSimpleResponse(command.Response); } else if (command.Command == ZWave.Command.MANUFACTURERSPECIFIC_GET) { command.Response = Utility.WaitModuleParameterChange( module, Properties.ZWAVENODE_MANUFACTURERSPECIFIC ); command.Response = JsonHelper.GetSimpleResponse(command.Response); } else if (command.Command == ZWave.Command.DOORLOCK_SET) { command.Response = Utility.WaitModuleParameterChange( module, Properties.STATUS_DOORLOCK ); command.Response = JsonHelper.GetSimpleResponse(command.Response); } else if (command.Command == ZWave.Command.DOORLOCK_GET) { command.Response = Utility.WaitModuleParameterChange( module, Properties.STATUS_DOORLOCK ); command.Response = JsonHelper.GetSimpleResponse(command.Response); } } } break; case Domains.MigService_Interfaces: if (command.Command.EndsWith(".Set")) { systemConfiguration.Update(); } break; } // // 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); } }
// 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; } } } }
public void ProcessRequest(MIGClientRequest request, MIGInterfaceCommand migCommand) { switch (migCommand.Command) { case "Interfaces.List": migCommand.Response = "[ "; foreach (var kv in homegenie.Interfaces) { var migInterface = kv.Value; var ifaceConfig = homegenie.SystemConfiguration.MIGService.GetInterface(migInterface.Domain); if (ifaceConfig == null || !ifaceConfig.IsEnabled) { continue; } migCommand.Response += "{ \"Domain\" : \"" + migInterface.Domain + "\", \"IsConnected\" : \"" + migInterface.IsConnected + "\" },"; } if (homegenie.UpdateChecker != null && homegenie.UpdateChecker.IsUpdateAvailable) { migCommand.Response += "{ \"Domain\" : \"HomeGenie.UpdateChecker\", \"IsConnected\" : \"True\" }"; migCommand.Response += " ]"; } else { migCommand.Response = migCommand.Response.Substring(0, migCommand.Response.Length - 1) + " ]"; } // break; //TODO: should this be moved somewhere to MIG? case "Interfaces.Configure": switch (migCommand.GetOption(0)) { case "Hardware.SerialPorts": if (Environment.OSVersion.Platform == PlatformID.Unix) { var serialPorts = System.IO.Ports.SerialPort.GetPortNames(); var portList = new List<string>(); for (int p = serialPorts.Length - 1; p >= 0; p--) { if (serialPorts[p].Contains("/ttyS") || serialPorts[p].Contains("/ttyUSB")) { portList.Add(serialPorts[p]); } } if (Raspberry.Board.Current.IsRaspberryPi && !portList.Contains("/dev/ttyAMA0")) { portList.Add("/dev/ttyAMA0"); } migCommand.Response = JsonHelper.GetSimpleResponse(JsonConvert.SerializeObject(portList)); } else { var portNames = System.IO.Ports.SerialPort.GetPortNames(); migCommand.Response = JsonHelper.GetSimpleResponse(JsonConvert.SerializeObject(portNames)); } break; } break; case "System.Configure": if (migCommand.GetOption(0) == "Service.Restart") { Program.Quit(true); migCommand.Response = JsonHelper.GetSimpleResponse("OK"); } else if (migCommand.GetOption(0) == "UpdateManager.UpdatesList") { migCommand.Response = JsonConvert.SerializeObject(homegenie.UpdateChecker.RemoteUpdates); } else if (migCommand.GetOption(0) == "UpdateManager.Check") { homegenie.UpdateChecker.Check(); migCommand.Response = JsonHelper.GetSimpleResponse("OK"); } else if (migCommand.GetOption(0) == "UpdateManager.DownloadUpdate") { var resultMessage = "ERROR"; bool success = homegenie.UpdateChecker.DownloadUpdateFiles(); if (success) { if (homegenie.UpdateChecker.IsRestartRequired) { resultMessage = "RESTART"; } else { resultMessage = "OK"; } } migCommand.Response = JsonHelper.GetSimpleResponse(resultMessage); } else if (migCommand.GetOption(0) == "UpdateManager.InstallUpdate") //UpdateManager.InstallProgramsCommit") { string resultMessage = "OK"; if (!homegenie.UpdateChecker.InstallFiles()) { resultMessage = "ERROR"; } else { if (homegenie.UpdateChecker.IsRestartRequired) { resultMessage = "RESTART"; Utility.RunAsyncTask(() => { Thread.Sleep(2000); Program.Quit(true); }); } else { homegenie.LoadConfiguration(); homegenie.MigService.ClearWebCache(); homegenie.UpdateChecker.Check(); } } migCommand.Response = JsonHelper.GetSimpleResponse(resultMessage); } else if (migCommand.GetOption(0) == "HttpService.SetWebCacheEnabled") { if (migCommand.GetOption(1) == "1") { homegenie.MigService.IsWebCacheEnabled = true; homegenie.SystemConfiguration.MIGService.EnableWebCache = "true"; } else { homegenie.MigService.IsWebCacheEnabled = false; homegenie.SystemConfiguration.MIGService.EnableWebCache = "false"; } homegenie.SystemConfiguration.Update(); migCommand.Response = JsonHelper.GetSimpleResponse("OK"); } else if (migCommand.GetOption(0) == "HttpService.GetWebCacheEnabled") { migCommand.Response = JsonHelper.GetSimpleResponse(homegenie.MigService.IsWebCacheEnabled ? "1" : "0"); } else if (migCommand.GetOption(0) == "HttpService.GetPort") { migCommand.Response = JsonHelper.GetSimpleResponse(homegenie.SystemConfiguration.HomeGenie.ServicePort.ToString()); } else if (migCommand.GetOption(0) == "HttpService.SetPort") { try { homegenie.SystemConfiguration.HomeGenie.ServicePort = int.Parse(migCommand.GetOption(1)); homegenie.SystemConfiguration.Update(); } catch { } } else if (migCommand.GetOption(0) == "SystemLogging.DownloadCsv") { string csvlog = ""; string logpath = Path.Combine("log", "homegenie.log"); if (migCommand.GetOption(1) == "1") { logpath = Path.Combine("log", "homegenie.log.bak"); } if (File.Exists(logpath)) { using (var fs = new FileStream(logpath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using (var sr = new StreamReader(fs, Encoding.Default)) { csvlog = sr.ReadToEnd(); } } (request.Context as HttpListenerContext).Response.AddHeader( "Content-Disposition", "attachment;filename=homegenie_log_" + migCommand.GetOption(1) + ".csv" ); migCommand.Response = csvlog; } else if (migCommand.GetOption(0) == "SystemLogging.Enable") { SystemLogger.Instance.OpenLog(); homegenie.SystemConfiguration.HomeGenie.EnableLogFile = "true"; homegenie.SystemConfiguration.Update(); } else if (migCommand.GetOption(0) == "SystemLogging.Disable") { SystemLogger.Instance.CloseLog(); homegenie.SystemConfiguration.HomeGenie.EnableLogFile = "false"; homegenie.SystemConfiguration.Update(); } else if (migCommand.GetOption(0) == "SystemLogging.IsEnabled") { migCommand.Response = JsonHelper.GetSimpleResponse((homegenie.SystemConfiguration.HomeGenie.EnableLogFile.ToLower().Equals("true") ? "1" : "0")); } else if (migCommand.GetOption(0) == "Security.SetPassword") { // password only for now, with fixed user login 'admin' string pass = migCommand.GetOption(1) == "" ? "" : MIG.Utility.Encryption.SHA1.GenerateHashString(migCommand.GetOption(1)); homegenie.MigService.SetWebServicePassword(pass); homegenie.SystemConfiguration.HomeGenie.UserPassword = pass; // regenerate encrypted files homegenie.SystemConfiguration.Update(); homegenie.UpdateModulesDatabase(); } else if (migCommand.GetOption(0) == "Security.ClearPassword") { homegenie.MigService.SetWebServicePassword(""); homegenie.SystemConfiguration.HomeGenie.UserPassword = ""; // regenerate encrypted files homegenie.SystemConfiguration.Update(); homegenie.UpdateModulesDatabase(); } else if (migCommand.GetOption(0) == "Security.HasPassword") { migCommand.Response = JsonHelper.GetSimpleResponse((homegenie.SystemConfiguration.HomeGenie.UserPassword != "" ? "1" : "0")); } else if (migCommand.GetOption(0) == "System.ConfigurationRestore") { // file uploaded by user string archivename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "tmp", "homegenie_restore_config.zip"); if (!Directory.Exists("tmp")) { Directory.CreateDirectory("tmp"); } try { var downloadedMessageInfo = new DirectoryInfo("tmp"); foreach (var file in downloadedMessageInfo.GetFiles()) { file.Delete(); } foreach (DirectoryInfo directory in downloadedMessageInfo.GetDirectories()) { directory.Delete(true); } } catch { } // try { var encoding = (request.Context as HttpListenerContext).Request.ContentEncoding; string boundary = MIG.Gateways.WebServiceUtility.GetBoundary((request.Context as HttpListenerContext).Request.ContentType); MIG.Gateways.WebServiceUtility.SaveFile(encoding, boundary, request.InputStream, archivename); homegenie.UnarchiveConfiguration(archivename, Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "tmp")); File.Delete(archivename); } catch { } } else if (migCommand.GetOption(0) == "System.ConfigurationRestoreS1") { var serializer = new XmlSerializer(typeof(List<ProgramBlock>)); var reader = new StreamReader(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "tmp", "programs.xml")); var newProgramsData = (List<ProgramBlock>)serializer.Deserialize(reader); reader.Close(); var newProgramList = new List<ProgramBlock>(); foreach (ProgramBlock program in newProgramsData) { if (program.Address >= ProgramEngine.USER_SPACE_PROGRAMS_START) { ProgramBlock p = new ProgramBlock(); p.Address = program.Address; p.Name = program.Name; p.Description = program.Description; newProgramList.Add(p); } } newProgramList.Sort(delegate(ProgramBlock p1, ProgramBlock p2) { string c1 = p1.Address.ToString(); string c2 = p2.Address.ToString(); return c1.CompareTo(c2); }); migCommand.Response = JsonConvert.SerializeObject(newProgramList); } else if (migCommand.GetOption(0) == "System.ConfigurationRestoreS2") { // var serializer = new XmlSerializer(typeof(List<Group>)); var reader = new StreamReader(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "tmp", "automationgroups.xml")); var automationGroups = (List<Group>)serializer.Deserialize(reader); reader.Close(); // foreach (var automationGroup in automationGroups) { if (homegenie.AutomationGroups.Find(g => g.Name == automationGroup.Name) == null) { homegenie.AutomationGroups.Add(automationGroup); } } // homegenie.UpdateGroupsDatabase("Automation"); // //File.Copy(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "tmp", "automationgroups.xml"), "./automationgroups.xml", true); File.Copy(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "tmp", "groups.xml"), Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "groups.xml"), true); File.Copy(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "tmp", "lircconfig.xml"), Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "lircconfig.xml"), true); File.Copy(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "tmp", "modules.xml"), Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "modules.xml"), true); File.Copy(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "tmp", "systemconfig.xml"), Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "systemconfig.xml"), true); // homegenie.LoadConfiguration(); // // Restore automation programs string selectedPrograms = migCommand.GetOption(1); serializer = new XmlSerializer(typeof(List<ProgramBlock>)); reader = new StreamReader(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "tmp", "programs.xml")); var newProgramsData = (List<ProgramBlock>)serializer.Deserialize(reader); reader.Close(); foreach (var program in newProgramsData) { var currentProgram = homegenie.ProgramEngine.Programs.Find(p => p.Address == program.Address); // Only restore user space programs if (selectedPrograms.Contains("," + program.Address.ToString() + ",") && program.Address >= ProgramEngine.USER_SPACE_PROGRAMS_START) { int oldPid = program.Address; if (currentProgram == null) { var newPid = ((currentProgram != null && currentProgram.Address == program.Address) ? homegenie.ProgramEngine.GeneratePid() : program.Address); try { File.Copy(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "tmp", "programs", program.Address + ".dll"), Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "programs", newPid + ".dll"), true); } catch { } program.Address = newPid; homegenie.ProgramEngine.ProgramAdd(program); } else if (currentProgram != null) { homegenie.ProgramEngine.ProgramRemove(currentProgram); try { File.Copy(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "tmp", "programs", program.Address + ".dll"), Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "programs", program.Address + ".dll"), true); } catch { } homegenie.ProgramEngine.ProgramAdd(program); } // Restore Arduino program folder ... // TODO: this is untested yet... if (program.Type.ToLower() == "arduino") { string sourceFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "tmp", "programs", "arduino", oldPid.ToString()); string arduinoFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "programs", "arduino", program.Address.ToString()); if (Directory.Exists(arduinoFolder)) Directory.Delete(arduinoFolder, true); Directory.CreateDirectory(arduinoFolder); foreach (string newPath in Directory.GetFiles(sourceFolder)) { File.Copy(newPath, newPath.Replace(sourceFolder, arduinoFolder), true); } } } else if (currentProgram != null && program.Address < ProgramEngine.USER_SPACE_PROGRAMS_START) { // Only restore Enabled/Disabled status of system programs currentProgram.IsEnabled = program.IsEnabled; } } // homegenie.UpdateProgramsDatabase(); // // regenerate encrypted files homegenie.UpdateModulesDatabase(); homegenie.SystemConfiguration.Update(); } else if (migCommand.GetOption(0) == "System.ConfigurationReset") { homegenie.RestoreFactorySettings(); } else if (migCommand.GetOption(0) == "System.ConfigurationBackup") { homegenie.BackupCurrentSettings(); (request.Context as HttpListenerContext).Response.Redirect("/hg/html/homegenie_backup_config.zip"); } else if (migCommand.GetOption(0) == "System.ConfigurationLoad") { homegenie.LoadConfiguration(); } break; case "Modules.Get": try { var module = homegenie.Modules.Find(m => m.Domain == migCommand.GetOption(0) && m.Address == migCommand.GetOption(1)); migCommand.Response = Utility.Module2Json(module, false); } catch (Exception ex) { migCommand.Response = JsonHelper.GetSimpleResponse("ERROR: \n" + ex.Message + "\n\n" + ex.StackTrace); } break; case "Modules.List": try { homegenie.modules_Sort(); migCommand.Response = homegenie.GetJsonSerializedModules(migCommand.GetOption(0).ToLower() == "short"); } catch (Exception ex) { migCommand.Response = JsonHelper.GetSimpleResponse("ERROR: \n" + ex.Message + "\n\n" + ex.StackTrace); } break; case "Modules.RoutingReset": try { for (int m = 0; m < homegenie.Modules.Count; m++) { homegenie.Modules[m].RoutingNode = ""; } migCommand.Response = JsonHelper.GetSimpleResponse("OK"); } catch (Exception ex) { migCommand.Response = JsonHelper.GetSimpleResponse("ERROR: \n" + ex.Message + "\n\n" + ex.StackTrace); } break; case "Modules.Save": string body = new StreamReader(request.InputStream).ReadToEnd(); var newModules = JsonConvert.DeserializeObject(body) as JArray; for (int i = 0; i < newModules.Count; i++) { try { var module = homegenie.Modules.Find(m => m.Address == newModules[i]["Address"].ToString() && m.Domain == newModules[i]["Domain"].ToString()); module.Name = newModules[i]["Name"].ToString(); // try { module.DeviceType = (MIG.ModuleTypes)Enum.Parse(typeof(MIG.ModuleTypes), newModules[i]["DeviceType"].ToString(), true); } catch { // TODO: check what's wrong here... } // var moduleProperties = newModules[i]["Properties"] as JArray; for (int p = 0; p < moduleProperties.Count; p++) { string propertyName = moduleProperties[p]["Name"].ToString(); string propertyValue = moduleProperties[p]["Value"].ToString(); ModuleParameter parameter = null; parameter = module.Properties.Find(delegate(ModuleParameter mp) { return mp.Name == propertyName; }); // if (propertyName == ModuleParameters.MODPAR_VIRTUALMETER_WATTS) { try { propertyValue = double.Parse(propertyValue.Replace(",", "."), System.Globalization.CultureInfo.InvariantCulture).ToString(); } catch { propertyValue = "0"; } } // if (parameter == null) { module.Properties.Add(new ModuleParameter() { Name = propertyName, Value = propertyValue }); } else //if (true) { if (moduleProperties[p]["NeedsUpdate"] != null && moduleProperties[p]["NeedsUpdate"].ToString() == "true") { parameter.Value = propertyValue; } } } } catch (Exception) { //TODO: notify exception? } } homegenie.UpdateModulesDatabase();//write modules break; case "Modules.Update": string streamContent = new StreamReader(request.InputStream).ReadToEnd(); var newModule = JsonConvert.DeserializeObject<Module>(streamContent); var currentModule = homegenie.Modules.Find(p => p.Domain == newModule.Domain && p.Address == newModule.Address); // if (currentModule == null) { homegenie.Modules.Add(newModule); } else { currentModule.Name = newModule.Name; currentModule.Description = newModule.Description; currentModule.DeviceType = newModule.DeviceType; //cm.Properties = mod.Properties; foreach (var newParameter in newModule.Properties) { var currentParameter = currentModule.Properties.Find(mp => mp.Name == newParameter.Name); if (currentParameter == null) { currentModule.Properties.Add(newParameter); } else if (newParameter.NeedsUpdate) { currentParameter.Value = newParameter.Value; } } // look for deleted properties var deletedParameters = new List<ModuleParameter>(); foreach (var parameter in currentModule.Properties) { var currentParameter = newModule.Properties.Find(mp => mp.Name == parameter.Name); if (currentParameter == null) { deletedParameters.Add(parameter); } } foreach (var parameter in deletedParameters) { currentModule.Properties.Remove(parameter); } deletedParameters.Clear(); } // homegenie.UpdateModulesDatabase(); break; case "Modules.Delete": var deletedModule = homegenie.Modules.Find(m => m.Domain == migCommand.GetOption(0) && m.Address == migCommand.GetOption(1)); if (deletedModule != null) { homegenie.Modules.Remove(deletedModule); } migCommand.Response = JsonHelper.GetSimpleResponse("OK"); // homegenie.UpdateModulesDatabase(); break; case "Groups.ModulesList": var theGroup = homegenie.Groups.Find(z => z.Name.ToLower() == migCommand.GetOption(0).Trim().ToLower()); if (theGroup != null) { string jsonmodules = "["; for (int m = 0; m < theGroup.Modules.Count; m++) { var groupModule = homegenie.Modules.Find(mm => mm.Domain == theGroup.Modules[m].Domain && mm.Address == theGroup.Modules[m].Address); if (groupModule != null) { jsonmodules += Utility.Module2Json(groupModule, false) + ",\n"; } } jsonmodules = jsonmodules.TrimEnd(',', '\n'); jsonmodules += "]"; migCommand.Response = jsonmodules; } break; case "Groups.List": try { migCommand.Response = JsonConvert.SerializeObject(homegenie.GetGroups(migCommand.GetOption(0))); } catch (Exception ex) { migCommand.Response = JsonHelper.GetSimpleResponse("ERROR: \n" + ex.Message + "\n\n" + ex.StackTrace); } break; case "Groups.Rename": string oldName = migCommand.GetOption(1); string newName = new StreamReader(request.InputStream).ReadToEnd(); var currentGroup = homegenie.GetGroups(migCommand.GetOption(0)).Find(g => g.Name == oldName); var newGroup = homegenie.GetGroups(migCommand.GetOption(0)).Find(g => g.Name == newName); // ensure that the new group name is not already defined if (newGroup == null && currentGroup != null) { currentGroup.Name = newName; homegenie.UpdateGroupsDatabase(migCommand.GetOption(0)); //cmd.response = JsonHelper.GetSimpleResponse("OK"); } else { migCommand.Response = JsonHelper.GetSimpleResponse("New name already in use."); } break; case "Groups.Sort": using (var reader = new StreamReader(request.InputStream)) { var newGroupList = new List<Group>(); string[] newPositionOrder = reader.ReadToEnd().Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < newPositionOrder.Length; i++) { newGroupList.Add(homegenie.GetGroups(migCommand.GetOption(0))[int.Parse(newPositionOrder[i])]); } homegenie.GetGroups(migCommand.GetOption(0)).Clear(); homegenie.GetGroups(migCommand.GetOption(0)).RemoveAll(g => true); homegenie.GetGroups(migCommand.GetOption(0)).AddRange(newGroupList); homegenie.UpdateGroupsDatabase(migCommand.GetOption(0)); } // try { migCommand.Response = JsonConvert.SerializeObject(homegenie.GetGroups(migCommand.GetOption(0))); } catch (Exception ex) { migCommand.Response = JsonHelper.GetSimpleResponse("ERROR: \n" + ex.Message + "\n\n" + ex.StackTrace); } break; case "Groups.SortModules": using (var reader = new StreamReader(request.InputStream)) { string groupName = migCommand.GetOption(1); Group sortGroup = null; try { sortGroup = homegenie.GetGroups(migCommand.GetOption(0)).Find(zn => zn.Name == groupName); } catch { } // if (sortGroup != null) { var newModulesReference = new List<ModuleReference>(); string[] newPositionOrder = reader.ReadToEnd().Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < newPositionOrder.Length; i++) { newModulesReference.Add(sortGroup.Modules[int.Parse(newPositionOrder[i])]); } sortGroup.Modules.Clear(); sortGroup.Modules = newModulesReference; homegenie.UpdateGroupsDatabase(migCommand.GetOption(0)); } } try { migCommand.Response = JsonConvert.SerializeObject(homegenie.GetGroups(migCommand.GetOption(0))); } catch (Exception ex) { migCommand.Response = JsonHelper.GetSimpleResponse("ERROR: \n" + ex.Message + "\n\n" + ex.StackTrace); } break; case "Groups.Add": string newGroupName = new StreamReader(request.InputStream).ReadToEnd(); homegenie.GetGroups(migCommand.GetOption(0)).Add(new Group() { Name = newGroupName }); homegenie.UpdateGroupsDatabase(migCommand.GetOption(0));//write groups break; case "Groups.Delete": string deletedGroupName = new StreamReader(request.InputStream).ReadToEnd(); Group deletedGroup = null; try { deletedGroup = homegenie.GetGroups(migCommand.GetOption(0)).Find(zn => zn.Name == deletedGroupName); } catch { } // if (deletedGroup != null) { homegenie.GetGroups(migCommand.GetOption(0)).Remove(deletedGroup); homegenie.UpdateGroupsDatabase(migCommand.GetOption(0));//write groups if (migCommand.GetOption(0).ToLower() == "automation") { var groupPrograms = homegenie.ProgramEngine.Programs.FindAll(p => p.Group.ToLower() == deletedGroup.Name.ToLower()); if (groupPrograms != null) { // delete group association from programs foreach (ProgramBlock program in groupPrograms) { program.Group = ""; } } } } break; case "Groups.Save": string jsonGroups = new StreamReader(request.InputStream).ReadToEnd(); var newGroups = JsonConvert.DeserializeObject<List<Group>>(jsonGroups); for (int i = 0; i < newGroups.Count; i++) { try { var group = homegenie.Groups.Find(z => z.Name == newGroups[i].Name); group.Modules.Clear(); group.Modules = newGroups[i].Modules; } catch { } } homegenie.UpdateGroupsDatabase(migCommand.GetOption(0));//write groups break; } }
public void ProcessRequest(MIGClientRequest request, MIGInterfaceCommand migCommand) { string streamcontent = ""; ProgramBlock currentProgram; ProgramBlock newProgram; // homegenie.ExecuteAutomationRequest(migCommand); if (migCommand.Command.StartsWith("Macro.")) { switch (migCommand.Command) { case "Macro.Record": homegenie.ProgramEngine.MacroRecorder.RecordingEnable(); break; case "Macro.Save": newProgram = homegenie.ProgramEngine.MacroRecorder.SaveMacro(migCommand.GetOption(1)); migCommand.Response = newProgram.Address.ToString(); break; case "Macro.Discard": homegenie.ProgramEngine.MacroRecorder.RecordingDisable(); break; case "Macro.SetDelay": switch (migCommand.GetOption(0).ToLower()) { case "none": homegenie.ProgramEngine.MacroRecorder.DelayType = MacroDelayType.None; break; case "mimic": homegenie.ProgramEngine.MacroRecorder.DelayType = MacroDelayType.Mimic; break; case "fixed": double secs = double.Parse(migCommand.GetOption(1), System.Globalization.CultureInfo.InvariantCulture); homegenie.ProgramEngine.MacroRecorder.DelayType = MacroDelayType.Fixed; homegenie.ProgramEngine.MacroRecorder.DelaySeconds = secs; break; } break; case "Macro.GetDelay": migCommand.Response = "[{ DelayType : '" + homegenie.ProgramEngine.MacroRecorder.DelayType + "', DelayOptions : '" + homegenie.ProgramEngine.MacroRecorder.DelaySeconds + "' }]"; break; } } else if (migCommand.Command.StartsWith("Scheduling.")) { switch (migCommand.Command) { case "Scheduling.Add": case "Scheduling.Update": var item = homegenie.ProgramEngine.SchedulerService.AddOrUpdate(migCommand.GetOption(0), migCommand.GetOption(1).Replace("|", "/")); item.ProgramId = migCommand.GetOption(2); homegenie.UpdateSchedulerDatabase(); break; case "Scheduling.Delete": homegenie.ProgramEngine.SchedulerService.Remove(migCommand.GetOption(0)); homegenie.UpdateSchedulerDatabase(); break; case "Scheduling.Enable": homegenie.ProgramEngine.SchedulerService.Enable(migCommand.GetOption(0)); homegenie.UpdateSchedulerDatabase(); break; case "Scheduling.Disable": homegenie.ProgramEngine.SchedulerService.Disable(migCommand.GetOption(0)); homegenie.UpdateSchedulerDatabase(); break; case "Scheduling.Get": migCommand.Response = JsonConvert.SerializeObject(homegenie.ProgramEngine.SchedulerService.Get(migCommand.GetOption(0))); break; case "Scheduling.List": homegenie.ProgramEngine.SchedulerService.Items.Sort((SchedulerItem s1, SchedulerItem s2) => { return s1.Name.CompareTo(s2.Name); }); migCommand.Response = JsonConvert.SerializeObject(homegenie.ProgramEngine.SchedulerService.Items); break; } } else if (migCommand.Command.StartsWith("Programs.")) { if (migCommand.Command != "Programs.Import") { streamcontent = new StreamReader(request.InputStream).ReadToEnd(); } // switch (migCommand.Command) { case "Programs.Import": string archiveName = "homegenie_program_import.hgx"; if (File.Exists(archiveName)) File.Delete(archiveName); // var encoding = (request.Context as HttpListenerContext).Request.ContentEncoding; string boundary = MIG.Gateways.WebServiceUtility.GetBoundary((request.Context as HttpListenerContext).Request.ContentType); MIG.Gateways.WebServiceUtility.SaveFile(encoding, boundary, request.InputStream, archiveName); // var serializer = new XmlSerializer(typeof(ProgramBlock)); var reader = new StreamReader(archiveName); newProgram = (ProgramBlock)serializer.Deserialize(reader); reader.Close(); // newProgram.Address = homegenie.ProgramEngine.GeneratePid(); newProgram.Group = migCommand.GetOption(0); homegenie.ProgramEngine.ProgramAdd(newProgram); // newProgram.IsEnabled = false; newProgram.ScriptErrors = ""; newProgram.AppAssembly = null; // homegenie.ProgramEngine.CompileScript(newProgram); // homegenie.UpdateProgramsDatabase(); //migCommand.response = JsonHelper.GetSimpleResponse(programblock.Address); migCommand.Response = newProgram.Address.ToString(); break; case "Programs.Export": currentProgram = homegenie.ProgramEngine.Programs.Find(p => p.Address == int.Parse(migCommand.GetOption(0))); var writerSettings = new System.Xml.XmlWriterSettings(); writerSettings.Indent = true; var programSerializer = new System.Xml.Serialization.XmlSerializer(typeof(ProgramBlock)); var builder = new StringBuilder(); var writer = System.Xml.XmlWriter.Create(builder, writerSettings); programSerializer.Serialize(writer, currentProgram); writer.Close(); migCommand.Response = builder.ToString(); // (request.Context as HttpListenerContext).Response.AddHeader("Content-Disposition", "attachment; filename=\"" + currentProgram.Address + "-" + currentProgram.Name.Replace(" ", "_") + ".hgx\""); break; case "Programs.List": var programList = new List<ProgramBlock>(homegenie.ProgramEngine.Programs); programList.Sort(delegate(ProgramBlock p1, ProgramBlock p2) { string c1 = p1.Name + " " + p1.Address; string c2 = p2.Name + " " + p2.Address; return c1.CompareTo(c2); }); migCommand.Response = JsonConvert.SerializeObject(programList); break; case "Programs.Add": newProgram = new ProgramBlock() { Group = migCommand.GetOption(0), Name = streamcontent, Type = "Wizard", ScriptCondition = "// A \"return true;\" statement at any point of this code block, will cause the program to run.\n// For manually activated program, just leave a \"return false\" statement here.\n\nreturn false;\n" }; newProgram.Address = homegenie.ProgramEngine.GeneratePid(); homegenie.ProgramEngine.ProgramAdd(newProgram); homegenie.UpdateProgramsDatabase(); migCommand.Response = JsonHelper.GetSimpleResponse(newProgram.Address.ToString()); break; case "Programs.Delete": currentProgram = homegenie.ProgramEngine.Programs.Find(p => p.Address == int.Parse(migCommand.GetOption(0))); if (currentProgram != null) { //TODO: remove groups associations as well currentProgram.IsEnabled = false; homegenie.ProgramEngine.ProgramRemove(currentProgram); homegenie.UpdateProgramsDatabase(); // remove associated module entry homegenie.Modules.RemoveAll(m => m.Domain == Domains.HomeAutomation_HomeGenie_Automation && m.Address == currentProgram.Address.ToString()); homegenie.UpdateModulesDatabase(); } break; case "Programs.Compile": case "Programs.Update": newProgram = JsonConvert.DeserializeObject<ProgramBlock>(streamcontent); currentProgram = homegenie.ProgramEngine.Programs.Find(p => p.Address == newProgram.Address); // if (currentProgram == null) { newProgram.Address = homegenie.ProgramEngine.GeneratePid(); homegenie.ProgramEngine.ProgramAdd(newProgram); } else { if (currentProgram.Type.ToLower() != newProgram.Type.ToLower()) { currentProgram.AppAssembly = null; // dispose assembly and interrupt current task } currentProgram.Type = newProgram.Type; currentProgram.Group = newProgram.Group; currentProgram.Name = newProgram.Name; currentProgram.Description = newProgram.Description; currentProgram.IsEnabled = newProgram.IsEnabled; currentProgram.ScriptCondition = newProgram.ScriptCondition; currentProgram.ScriptSource = newProgram.ScriptSource; currentProgram.Commands = newProgram.Commands; currentProgram.Conditions = newProgram.Conditions; currentProgram.ConditionType = newProgram.ConditionType; // reset last condition evaluation status currentProgram.LastConditionEvaluationResult = false; } // if (migCommand.Command == "Programs.Compile") { // reset previous error status currentProgram.IsEnabled = false; currentProgram.Stop(); currentProgram.ScriptErrors = ""; // List<ProgramError> errors = homegenie.ProgramEngine.CompileScript(currentProgram); // currentProgram.IsEnabled = newProgram.IsEnabled; currentProgram.ScriptErrors = JsonConvert.SerializeObject(errors); migCommand.Response = currentProgram.ScriptErrors; } // homegenie.UpdateProgramsDatabase(); // homegenie.modules_RefreshPrograms(); homegenie.modules_RefreshVirtualModules(); homegenie.modules_Sort(); break; case "Programs.Run": currentProgram = ProgramRun(migCommand.GetOption(0), migCommand.GetOption(1)); break; case "Programs.Toggle": currentProgram = ProgramToggle(migCommand.GetOption(0), migCommand.GetOption(1)); break; case "Programs.Break": currentProgram = ProgramBreak(migCommand.GetOption(0)); break; case "Programs.Restart": currentProgram = homegenie.ProgramEngine.Programs.Find(p => p.Address == int.Parse(migCommand.GetOption(0))); if (currentProgram != null) { currentProgram.IsEnabled = false; try { currentProgram.Stop(); } catch { } currentProgram.IsEnabled = true; homegenie.UpdateProgramsDatabase(); } break; case "Programs.Enable": currentProgram = homegenie.ProgramEngine.Programs.Find(p => p.Address == int.Parse(migCommand.GetOption(0))); if (currentProgram != null) { currentProgram.IsEnabled = true; homegenie.UpdateProgramsDatabase(); } break; case "Programs.Disable": currentProgram = homegenie.ProgramEngine.Programs.Find(p => p.Address == int.Parse(migCommand.GetOption(0))); if (currentProgram != null) { currentProgram.IsEnabled = false; try { currentProgram.Stop(); } catch { } homegenie.UpdateProgramsDatabase(); } break; } } }
public void ProcessRequest(MIGClientRequest request, MIGInterfaceCommand migCommand) { string streamContent = ""; ProgramBlock currentProgram; ProgramBlock newProgram; string sketchFile = "", sketchFolder = ""; // homegenie.ExecuteAutomationRequest(migCommand); if (migCommand.Command.StartsWith("Macro.")) { switch (migCommand.Command) { case "Macro.Record": homegenie.ProgramEngine.MacroRecorder.RecordingEnable(); break; case "Macro.Save": newProgram = homegenie.ProgramEngine.MacroRecorder.SaveMacro(migCommand.GetOption(1)); migCommand.Response = newProgram.Address.ToString(); break; case "Macro.Discard": homegenie.ProgramEngine.MacroRecorder.RecordingDisable(); break; case "Macro.SetDelay": switch (migCommand.GetOption(0).ToLower()) { case "none": homegenie.ProgramEngine.MacroRecorder.DelayType = MacroDelayType.None; break; case "mimic": homegenie.ProgramEngine.MacroRecorder.DelayType = MacroDelayType.Mimic; break; case "fixed": double secs = double.Parse( migCommand.GetOption(1), System.Globalization.CultureInfo.InvariantCulture ); homegenie.ProgramEngine.MacroRecorder.DelayType = MacroDelayType.Fixed; homegenie.ProgramEngine.MacroRecorder.DelaySeconds = secs; break; } break; case "Macro.GetDelay": migCommand.Response = "[{ DelayType : '" + homegenie.ProgramEngine.MacroRecorder.DelayType + "', DelayOptions : '" + homegenie.ProgramEngine.MacroRecorder.DelaySeconds + "' }]"; break; } } else if (migCommand.Command.StartsWith("Scheduling.")) { switch (migCommand.Command) { case "Scheduling.Add": case "Scheduling.Update": var item = homegenie.ProgramEngine.SchedulerService.AddOrUpdate( migCommand.GetOption(0), migCommand.GetOption(1).Replace( "|", "/" ) ); item.ProgramId = migCommand.GetOption(2); homegenie.UpdateSchedulerDatabase(); break; case "Scheduling.Delete": homegenie.ProgramEngine.SchedulerService.Remove(migCommand.GetOption(0)); homegenie.UpdateSchedulerDatabase(); break; case "Scheduling.Enable": homegenie.ProgramEngine.SchedulerService.Enable(migCommand.GetOption(0)); homegenie.UpdateSchedulerDatabase(); break; case "Scheduling.Disable": homegenie.ProgramEngine.SchedulerService.Disable(migCommand.GetOption(0)); homegenie.UpdateSchedulerDatabase(); break; case "Scheduling.Get": migCommand.Response = JsonConvert.SerializeObject(homegenie.ProgramEngine.SchedulerService.Get(migCommand.GetOption(0))); break; case "Scheduling.List": homegenie.ProgramEngine.SchedulerService.Items.Sort((SchedulerItem s1, SchedulerItem s2) => { return s1.Name.CompareTo(s2.Name); }); migCommand.Response = JsonConvert.SerializeObject(homegenie.ProgramEngine.SchedulerService.Items); break; } } else if (migCommand.Command.StartsWith("Programs.")) { if (migCommand.Command != "Programs.Import") { streamContent = new StreamReader(request.InputStream).ReadToEnd(); } // switch (migCommand.Command) { case "Programs.Import": string archiveName = "homegenie_program_import.hgx"; if (File.Exists(archiveName)) File.Delete(archiveName); // var encoding = (request.Context as HttpListenerContext).Request.ContentEncoding; string boundary = MIG.Gateways.WebServiceUtility.GetBoundary((request.Context as HttpListenerContext).Request.ContentType); MIG.Gateways.WebServiceUtility.SaveFile(encoding, boundary, request.InputStream, archiveName); // int newPid = homegenie.ProgramEngine.GeneratePid(); var reader = new StreamReader(archiveName); char[] signature = new char[2]; reader.Read(signature, 0, 2); reader.Close(); if (signature[0] == 'P' && signature[1] == 'K') { // Read and uncompress zip file content (arduino program bundle) string zipFileName = archiveName.Replace(".hgx", ".zip"); if (File.Exists(zipFileName)) File.Delete(zipFileName); File.Move(archiveName, zipFileName); string destFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "tmp", "import"); if (Directory.Exists(destFolder)) Directory.Delete(destFolder, true); homegenie.UnarchiveConfiguration(zipFileName, destFolder); string bundleFolder = Path.Combine("programs", "arduino", newPid.ToString()); if (Directory.Exists(bundleFolder)) Directory.Delete(bundleFolder, true); Directory.Move(Path.Combine(destFolder, "src"), bundleFolder); reader = new StreamReader(Path.Combine(destFolder, "program.hgx")); } else { reader = new StreamReader(archiveName); } var serializer = new XmlSerializer(typeof(ProgramBlock)); newProgram = (ProgramBlock)serializer.Deserialize(reader); reader.Close(); // newProgram.Address = newPid; newProgram.Group = migCommand.GetOption(0); homegenie.ProgramEngine.ProgramAdd(newProgram); // newProgram.IsEnabled = false; newProgram.ScriptErrors = ""; newProgram.AppAssembly = null; // if (newProgram.Type.ToLower() != "arduino") { homegenie.ProgramEngine.CompileScript(newProgram); } // homegenie.UpdateProgramsDatabase(); //migCommand.response = JsonHelper.GetSimpleResponse(programblock.Address); migCommand.Response = newProgram.Address.ToString(); break; case "Programs.Export": currentProgram = homegenie.ProgramEngine.Programs.Find(p => p.Address == int.Parse(migCommand.GetOption(0))); string filename = currentProgram.Address + "-" + currentProgram.Name.Replace(" ", "_"); // var writerSettings = new System.Xml.XmlWriterSettings(); writerSettings.Indent = true; var programSerializer = new System.Xml.Serialization.XmlSerializer(typeof(ProgramBlock)); var builder = new StringBuilder(); var writer = System.Xml.XmlWriter.Create(builder, writerSettings); programSerializer.Serialize(writer, currentProgram); writer.Close(); // if (currentProgram.Type.ToLower() == "arduino") { string arduinoBundle = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "tmp", "export", filename + ".zip"); if (File.Exists(arduinoBundle)) { File.Delete(arduinoBundle); } else if (!Directory.Exists(Path.GetDirectoryName(arduinoBundle))) { Directory.CreateDirectory(Path.GetDirectoryName(arduinoBundle)); } string mainProgramFile = Path.Combine(Path.GetDirectoryName(arduinoBundle), "program.hgx"); File.WriteAllText( mainProgramFile, builder.ToString() ); Utility.AddFileToZip(arduinoBundle, mainProgramFile, "program.hgx"); sketchFolder = Path.Combine("programs", "arduino", currentProgram.Address.ToString()); foreach (string f in Directory.GetFiles(sketchFolder)) { if (!Path.GetFileName(f).StartsWith("sketch_")) { Utility.AddFileToZip( arduinoBundle, Path.Combine(sketchFolder, Path.GetFileName(f)), Path.Combine( "src", Path.GetFileName(f) ) ); } } // byte[] bundleData = File.ReadAllBytes(arduinoBundle); (request.Context as HttpListenerContext).Response.AddHeader( "Content-Disposition", "attachment; filename=\"" + filename + ".zip\"" ); (request.Context as HttpListenerContext).Response.OutputStream.Write(bundleData, 0, bundleData.Length); } else { (request.Context as HttpListenerContext).Response.AddHeader( "Content-Disposition", "attachment; filename=\"" + filename + ".hgx\"" ); migCommand.Response = builder.ToString(); } break; case "Programs.List": var programList = new List<ProgramBlock>(homegenie.ProgramEngine.Programs); programList.Sort(delegate(ProgramBlock p1, ProgramBlock p2) { string c1 = p1.Name + " " + p1.Address; string c2 = p2.Name + " " + p2.Address; return c1.CompareTo(c2); }); migCommand.Response = JsonConvert.SerializeObject(programList); break; case "Programs.Add": newProgram = new ProgramBlock() { Group = migCommand.GetOption(0), Name = streamContent, Type = "Wizard" }; newProgram.Address = homegenie.ProgramEngine.GeneratePid(); homegenie.ProgramEngine.ProgramAdd(newProgram); homegenie.UpdateProgramsDatabase(); migCommand.Response = JsonHelper.GetSimpleResponse(newProgram.Address.ToString()); break; case "Programs.Delete": currentProgram = homegenie.ProgramEngine.Programs.Find(p => p.Address == int.Parse(migCommand.GetOption(0))); if (currentProgram != null) { //TODO: remove groups associations as well currentProgram.IsEnabled = false; homegenie.ProgramEngine.ProgramRemove(currentProgram); homegenie.UpdateProgramsDatabase(); // remove associated module entry homegenie.Modules.RemoveAll(m => m.Domain == Domains.HomeAutomation_HomeGenie_Automation && m.Address == currentProgram.Address.ToString()); homegenie.UpdateModulesDatabase(); } break; case "Programs.Compile": case "Programs.Update": newProgram = JsonConvert.DeserializeObject<ProgramBlock>(streamContent); currentProgram = homegenie.ProgramEngine.Programs.Find(p => p.Address == newProgram.Address); // if (currentProgram == null) { newProgram.Address = homegenie.ProgramEngine.GeneratePid(); homegenie.ProgramEngine.ProgramAdd(newProgram); } else { if (currentProgram.Type.ToLower() != newProgram.Type.ToLower()) { currentProgram.AppAssembly = null; // dispose assembly and interrupt current task } currentProgram.Type = newProgram.Type; currentProgram.Group = newProgram.Group; currentProgram.Name = newProgram.Name; currentProgram.Description = newProgram.Description; currentProgram.IsEnabled = newProgram.IsEnabled; currentProgram.ScriptCondition = newProgram.ScriptCondition; currentProgram.ScriptSource = newProgram.ScriptSource; currentProgram.Commands = newProgram.Commands; currentProgram.Conditions = newProgram.Conditions; currentProgram.ConditionType = newProgram.ConditionType; // reset last condition evaluation status currentProgram.LastConditionEvaluationResult = false; } // if (migCommand.Command == "Programs.Compile") { // reset previous error status currentProgram.IsEnabled = false; currentProgram.Stop(); currentProgram.ScriptErrors = ""; // List<ProgramError> errors = homegenie.ProgramEngine.CompileScript(currentProgram); // currentProgram.IsEnabled = newProgram.IsEnabled; currentProgram.ScriptErrors = JsonConvert.SerializeObject(errors); migCommand.Response = currentProgram.ScriptErrors; } // homegenie.UpdateProgramsDatabase(); // homegenie.modules_RefreshPrograms(); homegenie.modules_RefreshVirtualModules(); homegenie.modules_Sort(); break; case "Programs.Arduino.FileLoad": sketchFolder = Path.GetDirectoryName(ArduinoAppFactory.GetSketchFile(migCommand.GetOption(0))); sketchFile = migCommand.GetOption(1); if (sketchFile == "main") { // "main" is a special keyword to indicate the main program sketch file sketchFile = ArduinoAppFactory.GetSketchFile(migCommand.GetOption(0)); } sketchFile = Path.Combine(sketchFolder, Path.GetFileName(sketchFile)); migCommand.Response = JsonHelper.GetSimpleResponse(File.ReadAllText(sketchFile)); break; case "Programs.Arduino.FileSave": sketchFolder = Path.GetDirectoryName(ArduinoAppFactory.GetSketchFile(migCommand.GetOption(0))); sketchFile = Path.Combine(sketchFolder, Path.GetFileName(migCommand.GetOption(1))); File.WriteAllText(sketchFile, streamContent); break; case "Programs.Arduino.FileAdd": sketchFolder = Path.GetDirectoryName(ArduinoAppFactory.GetSketchFile(migCommand.GetOption(0))); if (!Directory.Exists(sketchFolder)) Directory.CreateDirectory(sketchFolder); sketchFile = Path.Combine(sketchFolder, Path.GetFileName(migCommand.GetOption(1))); if (File.Exists(sketchFile)) { migCommand.Response = JsonHelper.GetSimpleResponse("EXISTS"); } else if (!ArduinoAppFactory.IsValidProjectFile(sketchFile)) { migCommand.Response = JsonHelper.GetSimpleResponse("INVALID_NAME"); } else { StreamWriter sw = File.CreateText(sketchFile); sw.Close(); sw.Dispose(); sw = null; migCommand.Response = JsonHelper.GetSimpleResponse("OK"); } break; case "Programs.Arduino.FileDelete": sketchFolder = Path.GetDirectoryName(ArduinoAppFactory.GetSketchFile(migCommand.GetOption(0))); sketchFile = Path.Combine(sketchFolder, Path.GetFileName(migCommand.GetOption(1))); if (!File.Exists(sketchFile)) { migCommand.Response = JsonHelper.GetSimpleResponse("NOT_FOUND"); } else { File.Delete(sketchFile); migCommand.Response = JsonHelper.GetSimpleResponse("OK"); } break; case "Programs.Arduino.FileList": sketchFolder = Path.GetDirectoryName(ArduinoAppFactory.GetSketchFile(migCommand.GetOption(0))); List<string> files = new List<string>(); foreach (string f in Directory.GetFiles(sketchFolder)) { if (ArduinoAppFactory.IsValidProjectFile(f)) { files.Add(Path.GetFileName(f)); } } migCommand.Response = JsonConvert.SerializeObject(files); break; case "Programs.Run": currentProgram = homegenie.ProgramEngine.Programs.Find(p => p.Address == int.Parse(migCommand.GetOption(0))); if (currentProgram != null) { // clear any runtime errors before running currentProgram.ScriptErrors = ""; homegenie.ProgramEngine.RaiseProgramModuleEvent( currentProgram, "Runtime.Error", "" ); ProgramRun(migCommand.GetOption(0), migCommand.GetOption(1)); } break; case "Programs.Toggle": currentProgram = ProgramToggle(migCommand.GetOption(0), migCommand.GetOption(1)); break; case "Programs.Break": currentProgram = ProgramBreak(migCommand.GetOption(0)); break; case "Programs.Restart": currentProgram = homegenie.ProgramEngine.Programs.Find(p => p.Address == int.Parse(migCommand.GetOption(0))); if (currentProgram != null) { currentProgram.IsEnabled = false; try { currentProgram.Stop(); } catch { } currentProgram.IsEnabled = true; homegenie.UpdateProgramsDatabase(); } break; case "Programs.Enable": currentProgram = homegenie.ProgramEngine.Programs.Find(p => p.Address == int.Parse(migCommand.GetOption(0))); if (currentProgram != null) { currentProgram.IsEnabled = true; homegenie.UpdateProgramsDatabase(); } break; case "Programs.Disable": currentProgram = homegenie.ProgramEngine.Programs.Find(p => p.Address == int.Parse(migCommand.GetOption(0))); if (currentProgram != null) { currentProgram.IsEnabled = false; try { currentProgram.Stop(); } catch { } homegenie.UpdateProgramsDatabase(); } break; } } }
public void ProcessRequest(MIGClientRequest request, MIGInterfaceCommand migCommand) { string response = ""; switch (migCommand.Command) { case "Interfaces.List": migCommand.Response = "[ "; foreach (var kv in homegenie.Interfaces) { var migInterface = kv.Value; var ifaceConfig = homegenie.SystemConfiguration.MIGService.GetInterface(migInterface.Domain); if (ifaceConfig == null || !ifaceConfig.IsEnabled) { continue; } migCommand.Response += "{ \"Domain\" : \"" + migInterface.Domain + "\", \"IsConnected\" : \"" + migInterface.IsConnected + "\" },"; } if (homegenie.UpdateChecker != null && homegenie.UpdateChecker.IsUpdateAvailable) { migCommand.Response += "{ \"Domain\" : \"" + Domains.HomeGenie_UpdateChecker + "\", \"IsConnected\" : \"True\" }"; migCommand.Response += " ]"; } else { migCommand.Response = migCommand.Response.Substring(0, migCommand.Response.Length - 1) + " ]"; } break; case "Interfaces.ListConfig": migCommand.Response = "[ "; foreach (var kv in homegenie.Interfaces) { var migInterface = kv.Value; var ifaceConfig = homegenie.SystemConfiguration.MIGService.GetInterface(migInterface.Domain); if (ifaceConfig == null) continue; migCommand.Response += JsonConvert.SerializeObject(ifaceConfig) + ","; } migCommand.Response = migCommand.Response.Substring(0, migCommand.Response.Length - 1) + " ]"; break; //TODO: should this be moved somewhere to MIG? case "Interfaces.Configure": switch (migCommand.GetOption(0)) { case "Hardware.SerialPorts": if (Environment.OSVersion.Platform == PlatformID.Unix) { var serialPorts = System.IO.Ports.SerialPort.GetPortNames(); var portList = new List<string>(); for (int p = serialPorts.Length - 1; p >= 0; p--) { if (serialPorts[p].Contains("/ttyS") || serialPorts[p].Contains("/ttyUSB")) { portList.Add(serialPorts[p]); } } if (Raspberry.Board.Current.IsRaspberryPi) { if (!portList.Contains("/dev/ttyAMA0")) portList.Add("/dev/ttyAMA0"); // RaZberry if (!portList.Contains("/dev/ttyACM0")) portList.Add("/dev/ttyACM0"); // ZME_UZB1 } migCommand.Response = JsonHelper.GetSimpleResponse(JsonConvert.SerializeObject(portList)); } else { var portNames = System.IO.Ports.SerialPort.GetPortNames(); migCommand.Response = JsonHelper.GetSimpleResponse(JsonConvert.SerializeObject(portNames)); } break; } break; case "Interface.Import": string downloadUrl = migCommand.GetOption(0); response = ""; string ifaceFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Utility.GetTmpFolder(), "mig_interface_import.zip"); string outputFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Utility.GetTmpFolder(), "mig"); Utility.FolderCleanUp(outputFolder); try { if (String.IsNullOrWhiteSpace(downloadUrl)) { // file uploaded by user MIG.Gateways.WebServiceUtility.SaveFile(request.InputStream, ifaceFileName); } else { // download file from url var client = new WebClient(); client.DownloadFile(downloadUrl, ifaceFileName); client.Dispose(); } } catch { // TODO: report exception } try { if (!Directory.Exists(outputFolder)) { Directory.CreateDirectory(outputFolder); } Utility.UncompressZip(ifaceFileName, outputFolder); File.Delete(ifaceFileName); var migInt = GetInterfaceConfig(Path.Combine(outputFolder, "configuration.xml")); if (migInt != null) { response = String.Format("{0} ({1})\n{2}\n", migInt.Domain, migInt.AssemblyName, migInt.Description); // Check for README notes and append them to the response var readmeFile = Path.Combine(outputFolder, "README.TXT"); if (File.Exists(readmeFile)) { response += File.ReadAllText(readmeFile); } migCommand.Response = JsonHelper.GetSimpleResponse(response); } else { migCommand.Response = JsonHelper.GetSimpleResponse("NOT A VALID ADD-ON PACKAGE"); } } catch { } break; case "Interface.Install": // install the interface package string outFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Utility.GetTmpFolder(), "mig"); string configFile = Path.Combine(outFolder, "configuration.xml"); var iface = GetInterfaceConfig(configFile); if (iface != null) { File.Delete(configFile); // homegenie.MigService.RemoveInterface(iface.Domain); // string configletName = iface.Domain.Substring(iface.Domain.LastIndexOf(".") + 1).ToLower(); string configletPath = Path.Combine("html", "pages", "configure", "interfaces", "configlet", configletName + ".html"); if (File.Exists(configletPath)) { File.Delete(configletPath); } File.Move(Path.Combine(outFolder, "configlet.html"), configletPath); // string logoPath = Path.Combine("html", "images", "interfaces", configletName + ".png"); if (File.Exists(logoPath)) { File.Delete(logoPath); } File.Move(Path.Combine(outFolder, "logo.png"), logoPath); // copy other interface files to mig folder (dll and dependencies) string migFolder = "mig"; Utility.FolderCleanUp(migFolder); DirectoryInfo dir = new DirectoryInfo(outFolder); foreach (var f in dir.GetFiles()) { string destFile = Path.Combine(migFolder, Path.GetFileName(f.FullName)); if (File.Exists(destFile)) { File.Move(destFile, Path.Combine(destFile, ".old")); File.Delete(Path.Combine(destFile, ".old")); } File.Move(f.FullName, destFile); } // homegenie.SystemConfiguration.MIGService.Interfaces.RemoveAll(i => i.Domain == iface.Domain); homegenie.SystemConfiguration.MIGService.Interfaces.Add(iface); homegenie.SystemConfiguration.Update(); homegenie.MigService.AddInterface(iface.Domain, iface.AssemblyName); migCommand.Response = JsonHelper.GetSimpleResponse("OK"); } else { migCommand.Response = JsonHelper.GetSimpleResponse("NOT A VALID ADD-ON PACKAGE"); } break; case "System.Configure": if (migCommand.GetOption(0) == "Service.Restart") { Program.Quit(true); migCommand.Response = JsonHelper.GetSimpleResponse("OK"); } else if (migCommand.GetOption(0) == "UpdateManager.UpdatesList") { migCommand.Response = JsonConvert.SerializeObject(homegenie.UpdateChecker.RemoteUpdates); } else if (migCommand.GetOption(0) == "UpdateManager.Check") { homegenie.UpdateChecker.Check(); migCommand.Response = JsonHelper.GetSimpleResponse("OK"); } else if (migCommand.GetOption(0) == "UpdateManager.DownloadUpdate") { var resultMessage = "ERROR"; bool success = homegenie.UpdateChecker.DownloadUpdateFiles(); if (success) { if (homegenie.UpdateChecker.IsRestartRequired) { resultMessage = "RESTART"; } else { resultMessage = "OK"; } } migCommand.Response = JsonHelper.GetSimpleResponse(resultMessage); } else if (migCommand.GetOption(0) == "UpdateManager.InstallUpdate") //UpdateManager.InstallProgramsCommit") { string resultMessage = "OK"; if (!homegenie.UpdateChecker.InstallFiles()) { resultMessage = "ERROR"; } else { if (homegenie.UpdateChecker.IsRestartRequired) { resultMessage = "RESTART"; Utility.RunAsyncTask(() => { Thread.Sleep(2000); Program.Quit(true); }); } else { homegenie.LoadConfiguration(); homegenie.MigService.ClearWebCache(); homegenie.UpdateChecker.Check(); } } migCommand.Response = JsonHelper.GetSimpleResponse(resultMessage); } else if (migCommand.GetOption(0) == "HttpService.SetWebCacheEnabled") { if (migCommand.GetOption(1) == "1") { homegenie.MigService.IsWebCacheEnabled = true; homegenie.SystemConfiguration.MIGService.EnableWebCache = "true"; } else { homegenie.MigService.IsWebCacheEnabled = false; homegenie.SystemConfiguration.MIGService.EnableWebCache = "false"; } homegenie.SystemConfiguration.Update(); migCommand.Response = JsonHelper.GetSimpleResponse("OK"); } else if (migCommand.GetOption(0) == "HttpService.GetWebCacheEnabled") { migCommand.Response = JsonHelper.GetSimpleResponse(homegenie.MigService.IsWebCacheEnabled ? "1" : "0"); } else if (migCommand.GetOption(0) == "HttpService.GetPort") { migCommand.Response = JsonHelper.GetSimpleResponse(homegenie.SystemConfiguration.HomeGenie.ServicePort.ToString()); } else if (migCommand.GetOption(0) == "HttpService.SetPort") { try { homegenie.SystemConfiguration.HomeGenie.ServicePort = int.Parse(migCommand.GetOption(1)); homegenie.SystemConfiguration.Update(); } catch { } } else if (migCommand.GetOption(0) == "HttpService.GetHostHeader") { migCommand.Response = JsonHelper.GetSimpleResponse(homegenie.SystemConfiguration.HomeGenie.ServiceHost.ToString()); } else if (migCommand.GetOption(0) == "HttpService.SetHostHeader") { try { homegenie.SystemConfiguration.HomeGenie.ServiceHost = migCommand.GetOption(1); homegenie.SystemConfiguration.Update(); } catch { } } else if (migCommand.GetOption(0) == "Statistics.GetStatisticsDatabaseMaximumSize") { migCommand.Response = JsonHelper.GetSimpleResponse(homegenie.SystemConfiguration.HomeGenie.Statistics.MaxDatabaseSizeMBytes.ToString()); } else if (migCommand.GetOption(0) == "Statistics.SetStatisticsDatabaseMaximumSize") { try { homegenie.SystemConfiguration.HomeGenie.Statistics.MaxDatabaseSizeMBytes = int.Parse(migCommand.GetOption(1)); homegenie.SystemConfiguration.Update(); } catch { } } else if (migCommand.GetOption(0) == "SystemLogging.DownloadCsv") { string csvlog = ""; string logpath = Path.Combine("log", "homegenie.log"); if (migCommand.GetOption(1) == "1") { logpath = Path.Combine("log", "homegenie.log.bak"); } if (File.Exists(logpath)) { using (var fs = new FileStream(logpath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using (var sr = new StreamReader(fs, Encoding.Default)) { csvlog = sr.ReadToEnd(); } } (request.Context as HttpListenerContext).Response.AddHeader("Content-Disposition", "attachment;filename=homegenie_log_" + migCommand.GetOption(1) + ".csv"); migCommand.Response = csvlog; } else if (migCommand.GetOption(0) == "SystemLogging.Enable") { SystemLogger.Instance.OpenLog(); homegenie.SystemConfiguration.HomeGenie.EnableLogFile = "true"; homegenie.SystemConfiguration.Update(); } else if (migCommand.GetOption(0) == "SystemLogging.Disable") { SystemLogger.Instance.CloseLog(); homegenie.SystemConfiguration.HomeGenie.EnableLogFile = "false"; homegenie.SystemConfiguration.Update(); } else if (migCommand.GetOption(0) == "SystemLogging.IsEnabled") { migCommand.Response = JsonHelper.GetSimpleResponse((homegenie.SystemConfiguration.HomeGenie.EnableLogFile.ToLower().Equals("true") ? "1" : "0")); } else if (migCommand.GetOption(0) == "Security.SetPassword") { // password only for now, with fixed user login 'admin' string pass = migCommand.GetOption(1) == "" ? "" : MIG.Utility.Encryption.SHA1.GenerateHashString(migCommand.GetOption(1)); homegenie.MigService.SetWebServicePassword(pass); homegenie.SystemConfiguration.HomeGenie.UserPassword = pass; // regenerate encrypted files homegenie.SystemConfiguration.Update(); homegenie.UpdateModulesDatabase(); } else if (migCommand.GetOption(0) == "Security.ClearPassword") { homegenie.MigService.SetWebServicePassword(""); homegenie.SystemConfiguration.HomeGenie.UserPassword = ""; // regenerate encrypted files homegenie.SystemConfiguration.Update(); homegenie.UpdateModulesDatabase(); } else if (migCommand.GetOption(0) == "Security.HasPassword") { migCommand.Response = JsonHelper.GetSimpleResponse((homegenie.SystemConfiguration.HomeGenie.UserPassword != "" ? "1" : "0")); } else if (migCommand.GetOption(0) == "System.ConfigurationRestore") { // file uploaded by user string archivename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Utility.GetTmpFolder(), "homegenie_restore_config.zip"); Utility.FolderCleanUp(Utility.GetTmpFolder()); try { MIG.Gateways.WebServiceUtility.SaveFile(request.InputStream, archivename); Utility.UncompressZip(archivename, Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Utility.GetTmpFolder())); File.Delete(archivename); } catch { } } else if (migCommand.GetOption(0) == "System.ConfigurationRestoreS1") { var serializer = new XmlSerializer(typeof(List<ProgramBlock>)); var reader = new StreamReader(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Utility.GetTmpFolder(), "programs.xml")); var newProgramsData = (List<ProgramBlock>)serializer.Deserialize(reader); reader.Close(); var newProgramList = new List<ProgramBlock>(); foreach (ProgramBlock program in newProgramsData) { if (program.Address >= ProgramEngine.USER_SPACE_PROGRAMS_START) { ProgramBlock p = new ProgramBlock(); p.Address = program.Address; p.Name = program.Name; p.Description = program.Description; newProgramList.Add(p); } } newProgramList.Sort(delegate(ProgramBlock p1, ProgramBlock p2) { string c1 = p1.Address.ToString(); string c2 = p2.Address.ToString(); return c1.CompareTo(c2); }); migCommand.Response = JsonConvert.SerializeObject(newProgramList); } else if (migCommand.GetOption(0) == "System.ConfigurationRestoreS2") { var serializer = new XmlSerializer(typeof(List<Group>)); var reader = new StreamReader(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Utility.GetTmpFolder(), "automationgroups.xml")); var automationGroups = (List<Group>)serializer.Deserialize(reader); reader.Close(); // foreach (var automationGroup in automationGroups) { if (homegenie.AutomationGroups.Find(g => g.Name == automationGroup.Name) == null) { homegenie.AutomationGroups.Add(automationGroup); } } // homegenie.UpdateGroupsDatabase("Automation"); // //File.Copy(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "tmp", "automationgroups.xml"), "./automationgroups.xml", true); File.Copy(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Utility.GetTmpFolder(), "groups.xml"), Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "groups.xml"), true); File.Copy(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Utility.GetTmpFolder(), "lircconfig.xml"), Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "lircconfig.xml"), true); File.Copy(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Utility.GetTmpFolder(), "modules.xml"), Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "modules.xml"), true); File.Copy(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Utility.GetTmpFolder(), "scheduler.xml"), Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "scheduler.xml"), true); File.Copy(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Utility.GetTmpFolder(), "systemconfig.xml"), Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "systemconfig.xml"), true); // homegenie.LoadConfiguration(); // // Restore automation programs string selectedPrograms = migCommand.GetOption(1); serializer = new XmlSerializer(typeof(List<ProgramBlock>)); reader = new StreamReader(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Utility.GetTmpFolder(), "programs.xml")); var newProgramsData = (List<ProgramBlock>)serializer.Deserialize(reader); reader.Close(); foreach (var program in newProgramsData) { var currentProgram = homegenie.ProgramEngine.Programs.Find(p => p.Address == program.Address); program.IsRunning = false; // Only restore user space programs if (selectedPrograms.Contains("," + program.Address.ToString() + ",") && program.Address >= ProgramEngine.USER_SPACE_PROGRAMS_START) { int oldPid = program.Address; if (currentProgram == null) { var newPid = ((currentProgram != null && currentProgram.Address == program.Address) ? homegenie.ProgramEngine.GeneratePid() : program.Address); try { File.Copy(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Utility.GetTmpFolder(), "programs", program.Address + ".dll"), Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "programs", newPid + ".dll"), true); } catch { } program.Address = newPid; homegenie.ProgramEngine.ProgramAdd(program); } else if (currentProgram != null) { homegenie.ProgramEngine.ProgramRemove(currentProgram); try { File.Copy(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Utility.GetTmpFolder(), "programs", program.Address + ".dll"), Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "programs", program.Address + ".dll"), true); } catch { } homegenie.ProgramEngine.ProgramAdd(program); } // Restore Arduino program folder ... // TODO: this is untested yet... if (program.Type.ToLower() == "arduino") { string sourceFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Utility.GetTmpFolder(), "programs", "arduino", oldPid.ToString()); string arduinoFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "programs", "arduino", program.Address.ToString()); if (Directory.Exists(arduinoFolder)) Directory.Delete(arduinoFolder, true); Directory.CreateDirectory(arduinoFolder); foreach (string newPath in Directory.GetFiles(sourceFolder)) { File.Copy(newPath, newPath.Replace(sourceFolder, arduinoFolder), true); } } } else if (currentProgram != null && program.Address < ProgramEngine.USER_SPACE_PROGRAMS_START) { // Only restore Enabled/Disabled status of system programs currentProgram.IsEnabled = program.IsEnabled; } } // homegenie.UpdateProgramsDatabase(); // // regenerate encrypted files homegenie.UpdateModulesDatabase(); homegenie.SystemConfiguration.Update(); } else if (migCommand.GetOption(0) == "System.ConfigurationReset") { homegenie.RestoreFactorySettings(); } else if (migCommand.GetOption(0) == "System.ConfigurationBackup") { homegenie.BackupCurrentSettings(); (request.Context as HttpListenerContext).Response.Redirect("/hg/html/homegenie_backup_config.zip"); } else if (migCommand.GetOption(0) == "System.ConfigurationLoad") { homegenie.LoadConfiguration(); } break; case "Modules.Get": try { var module = homegenie.Modules.Find(m => m.Domain == migCommand.GetOption(0) && m.Address == migCommand.GetOption(1)); migCommand.Response = Utility.Module2Json(module, false); } catch (Exception ex) { migCommand.Response = JsonHelper.GetSimpleResponse("ERROR: \n" + ex.Message + "\n\n" + ex.StackTrace); } break; case "Modules.List": try { homegenie.modules_Sort(); migCommand.Response = homegenie.GetJsonSerializedModules(migCommand.GetOption(0).ToLower() == "short"); } catch (Exception ex) { migCommand.Response = JsonHelper.GetSimpleResponse("ERROR: \n" + ex.Message + "\n\n" + ex.StackTrace); } break; case "Modules.RoutingReset": try { for (int m = 0; m < homegenie.Modules.Count; m++) { homegenie.Modules[m].RoutingNode = ""; } migCommand.Response = JsonHelper.GetSimpleResponse("OK"); } catch (Exception ex) { migCommand.Response = JsonHelper.GetSimpleResponse("ERROR: \n" + ex.Message + "\n\n" + ex.StackTrace); } break; case "Modules.Save": string body = new StreamReader(request.InputStream).ReadToEnd(); var newModules = JsonConvert.DeserializeObject(body) as JArray; for (int i = 0; i < newModules.Count; i++) { try { var module = homegenie.Modules.Find(m => m.Address == newModules[i]["Address"].ToString() && m.Domain == newModules[i]["Domain"].ToString()); module.Name = newModules[i]["Name"].ToString(); // try { module.DeviceType = (MIG.ModuleTypes)Enum.Parse(typeof(MIG.ModuleTypes), newModules[i]["DeviceType"].ToString(), true); } catch { // TODO: check what's wrong here... } // var moduleProperties = newModules[i]["Properties"] as JArray; for (int p = 0; p < moduleProperties.Count; p++) { string propertyName = moduleProperties[p]["Name"].ToString(); string propertyValue = moduleProperties[p]["Value"].ToString(); ModuleParameter parameter = null; parameter = module.Properties.Find(delegate(ModuleParameter mp) { return mp.Name == propertyName; }); // if (propertyName == ModuleParameters.MODPAR_VIRTUALMETER_WATTS) { try { propertyValue = double.Parse(propertyValue.Replace(",", "."), System.Globalization.CultureInfo.InvariantCulture).ToString(); } catch { propertyValue = "0"; } } // if (parameter == null) { module.Properties.Add(new ModuleParameter() { Name = propertyName, Value = propertyValue }); } else { if (moduleProperties[p]["NeedsUpdate"] != null && moduleProperties[p]["NeedsUpdate"].ToString() == "true") { parameter.Value = propertyValue; } } } } catch (Exception) { //TODO: notify exception? } } homegenie.UpdateModulesDatabase();//write modules break; case "Modules.Update": string streamContent = new StreamReader(request.InputStream).ReadToEnd(); var newModule = JsonConvert.DeserializeObject<Module>(streamContent); var currentModule = homegenie.Modules.Find(p => p.Domain == newModule.Domain && p.Address == newModule.Address); // if (currentModule == null) { homegenie.Modules.Add(newModule); } else { currentModule.Name = newModule.Name; currentModule.Description = newModule.Description; currentModule.DeviceType = newModule.DeviceType; foreach (var newParameter in newModule.Properties) { var currentParameter = currentModule.Properties.Find(mp => mp.Name == newParameter.Name); if (currentParameter == null) { currentModule.Properties.Add(newParameter); } else if (newParameter.NeedsUpdate) { // reset current reporting Watts if VMWatts field is set to 0 if (newParameter.Name == ModuleParameters.MODPAR_VIRTUALMETER_WATTS && newParameter.DecimalValue == 0 && currentParameter.DecimalValue != 0) { homegenie.migService_InterfacePropertyChanged(new InterfacePropertyChangedAction() { Domain = currentModule.Domain, SourceId = currentModule.Address, SourceType = currentModule.Description, Path = ModuleParameters.MODPAR_METER_WATTS, Value = "0.0" }); } currentParameter.Value = newParameter.Value; } } // look for deleted properties var deletedParameters = new List<ModuleParameter>(); foreach (var parameter in currentModule.Properties) { var currentParameter = newModule.Properties.Find(mp => mp.Name == parameter.Name); if (currentParameter == null) { deletedParameters.Add(parameter); } } foreach (var parameter in deletedParameters) { currentModule.Properties.Remove(parameter); } deletedParameters.Clear(); } // homegenie.UpdateModulesDatabase(); break; case "Modules.Delete": var deletedModule = homegenie.Modules.Find(m => m.Domain == migCommand.GetOption(0) && m.Address == migCommand.GetOption(1)); if (deletedModule != null) { homegenie.Modules.Remove(deletedModule); } migCommand.Response = JsonHelper.GetSimpleResponse("OK"); // homegenie.UpdateModulesDatabase(); break; case "Stores.List": { var module = homegenie.Modules.Find(m => m.Domain == migCommand.GetOption(0) && m.Address == migCommand.GetOption(1)); if (module != null) { //module.Stores migCommand.Response = "["; for (int s = 0; s < module.Stores.Count; s++) { migCommand.Response += "{ \"Name\": \"" + Utility.XmlEncode(module.Stores[s].Name) + "\", \"Description\": \"" + Utility.XmlEncode(module.Stores[s].Description) + "\" },"; } migCommand.Response = migCommand.Response.TrimEnd(',') + "]"; } } break; case "Stores.Delete": break; case "Stores.ItemList": { var module = homegenie.Modules.Find(m => m.Domain == migCommand.GetOption(0) && m.Address == migCommand.GetOption(1)); if (module != null) { migCommand.Response = "["; var store = new StoreHelper(module.Stores, migCommand.GetOption(2)); for (int p = 0; p < store.List.Count; p++) { migCommand.Response += "{ \"Name\": \"" + Utility.XmlEncode(store.List[p].Name) + "\", \"Description\": \"" + Utility.XmlEncode(store.List[p].Description) + "\" },"; } migCommand.Response = migCommand.Response.TrimEnd(',') + "]"; } } break; case "Stores.ItemDelete": { var module = homegenie.Modules.Find(m => m.Domain == migCommand.GetOption(0) && m.Address == migCommand.GetOption(1)); if (module != null) { var name = migCommand.GetOption(3); var store = new StoreHelper(module.Stores, migCommand.GetOption(2)); store.List.RemoveAll(i => i.Name == name); } } break; case "Stores.ItemGet": { var module = homegenie.Modules.Find(m => m.Domain == migCommand.GetOption(0) && m.Address == migCommand.GetOption(1)); if (module != null) { var store = new StoreHelper(module.Stores, migCommand.GetOption(2)); migCommand.Response += JsonConvert.SerializeObject(store.Get(migCommand.GetOption(3))); } } break; case "Stores.ItemSet": { // value is the POST body string itemData = new StreamReader(request.InputStream).ReadToEnd(); var module = homegenie.Modules.Find(m => m.Domain == migCommand.GetOption(0) && m.Address == migCommand.GetOption(1)); if (module != null) { var store = new StoreHelper(module.Stores, migCommand.GetOption(2)); store.Get(migCommand.GetOption(3)).Value = itemData; } } break; case "Groups.ModulesList": var theGroup = homegenie.Groups.Find(z => z.Name.ToLower() == migCommand.GetOption(0).Trim().ToLower()); if (theGroup != null) { string jsonmodules = "["; for (int m = 0; m < theGroup.Modules.Count; m++) { var groupModule = homegenie.Modules.Find(mm => mm.Domain == theGroup.Modules[m].Domain && mm.Address == theGroup.Modules[m].Address); if (groupModule != null) { jsonmodules += Utility.Module2Json(groupModule, false) + ",\n"; } } jsonmodules = jsonmodules.TrimEnd(',', '\n'); jsonmodules += "]"; migCommand.Response = jsonmodules; } break; case "Groups.List": try { migCommand.Response = JsonConvert.SerializeObject(homegenie.GetGroups(migCommand.GetOption(0))); } catch (Exception ex) { migCommand.Response = JsonHelper.GetSimpleResponse("ERROR: \n" + ex.Message + "\n\n" + ex.StackTrace); } break; case "Groups.Rename": string oldName = migCommand.GetOption(1); string newName = new StreamReader(request.InputStream).ReadToEnd(); var currentGroup = homegenie.GetGroups(migCommand.GetOption(0)).Find(g => g.Name == oldName); var newGroup = homegenie.GetGroups(migCommand.GetOption(0)).Find(g => g.Name == newName); // ensure that the new group name is not already defined if (newGroup == null && currentGroup != null) { currentGroup.Name = newName; homegenie.UpdateGroupsDatabase(migCommand.GetOption(0)); //cmd.response = JsonHelper.GetSimpleResponse("OK"); } else { migCommand.Response = JsonHelper.GetSimpleResponse("New name already in use."); } break; case "Groups.Sort": using (var reader = new StreamReader(request.InputStream)) { var newGroupList = new List<Group>(); string[] newPositionOrder = reader.ReadToEnd().Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < newPositionOrder.Length; i++) { newGroupList.Add(homegenie.GetGroups(migCommand.GetOption(0))[int.Parse(newPositionOrder[i])]); } homegenie.GetGroups(migCommand.GetOption(0)).Clear(); homegenie.GetGroups(migCommand.GetOption(0)).RemoveAll(g => true); homegenie.GetGroups(migCommand.GetOption(0)).AddRange(newGroupList); homegenie.UpdateGroupsDatabase(migCommand.GetOption(0)); } // try { migCommand.Response = JsonConvert.SerializeObject(homegenie.GetGroups(migCommand.GetOption(0))); } catch (Exception ex) { migCommand.Response = JsonHelper.GetSimpleResponse("ERROR: \n" + ex.Message + "\n\n" + ex.StackTrace); } break; case "Groups.SortModules": using (var reader = new StreamReader(request.InputStream)) { string groupName = migCommand.GetOption(1); Group sortGroup = null; try { sortGroup = homegenie.GetGroups(migCommand.GetOption(0)).Find(zn => zn.Name == groupName); } catch { } // if (sortGroup != null) { var newModulesReference = new List<ModuleReference>(); string[] newPositionOrder = reader.ReadToEnd().Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < newPositionOrder.Length; i++) { newModulesReference.Add(sortGroup.Modules[int.Parse(newPositionOrder[i])]); } sortGroup.Modules.Clear(); sortGroup.Modules = newModulesReference; homegenie.UpdateGroupsDatabase(migCommand.GetOption(0)); } } try { migCommand.Response = JsonConvert.SerializeObject(homegenie.GetGroups(migCommand.GetOption(0))); } catch (Exception ex) { migCommand.Response = JsonHelper.GetSimpleResponse("ERROR: \n" + ex.Message + "\n\n" + ex.StackTrace); } break; case "Groups.Add": string newGroupName = new StreamReader(request.InputStream).ReadToEnd(); homegenie.GetGroups(migCommand.GetOption(0)).Add(new Group() { Name = newGroupName }); homegenie.UpdateGroupsDatabase(migCommand.GetOption(0));//write groups break; case "Groups.Delete": string deletedGroupName = new StreamReader(request.InputStream).ReadToEnd(); Group deletedGroup = null; try { deletedGroup = homegenie.GetGroups(migCommand.GetOption(0)).Find(zn => zn.Name == deletedGroupName); } catch { } // if (deletedGroup != null) { homegenie.GetGroups(migCommand.GetOption(0)).Remove(deletedGroup); homegenie.UpdateGroupsDatabase(migCommand.GetOption(0));//write groups if (migCommand.GetOption(0).ToLower() == "automation") { var groupPrograms = homegenie.ProgramEngine.Programs.FindAll(p => p.Group.ToLower() == deletedGroup.Name.ToLower()); if (groupPrograms != null) { // delete group association from programs foreach (ProgramBlock program in groupPrograms) { program.Group = ""; } } } } break; case "Groups.Save": string jsonGroups = new StreamReader(request.InputStream).ReadToEnd(); var newGroups = JsonConvert.DeserializeObject<List<Group>>(jsonGroups); for (int i = 0; i < newGroups.Count; i++) { try { var group = homegenie.Groups.Find(z => z.Name == newGroups[i].Name); group.Modules.Clear(); group.Modules = newGroups[i].Modules; } catch { } } homegenie.UpdateGroupsDatabase(migCommand.GetOption(0));//write groups break; case "Groups.WallpaperList": List<string> wallpaperList = new List<string>(); var images = Directory.GetFiles(groupWallpapersPath); for (int i = 0; i < images.Length; i++) { wallpaperList.Add(Path.GetFileName(images[i])); } migCommand.Response = JsonConvert.SerializeObject(wallpaperList); break; case "Groups.WallpaperAdd": { string wallpaperFile = ""; try { wallpaperFile = MIG.Gateways.WebServiceUtility.SaveFile(request.InputStream, groupWallpapersPath); } catch { } migCommand.Response = JsonHelper.GetSimpleResponse(Path.GetFileName(wallpaperFile)); } break; case "Groups.WallpaperSet": { string wpGroupName = migCommand.GetOption(0); var wpGroup = homegenie.GetGroups(migCommand.GetOption(0)).Find(g => g.Name == wpGroupName); if (wpGroup != null) { wpGroup.Wallpaper = migCommand.GetOption(1); homegenie.UpdateGroupsDatabase("Control"); } } break; case "Groups.WallpaperDelete": { string wallpaperFile = migCommand.GetOption(0); wallpaperFile = Path.Combine(groupWallpapersPath, Path.GetFileName(wallpaperFile)); if (File.Exists(wallpaperFile)) { File.Delete(wallpaperFile); } migCommand.Response = JsonHelper.GetSimpleResponse("OK"); } break; case "Widgets.List": List<string> widgetsList = new List<string>(); var groups = Directory.GetDirectories(widgetBasePath); for (int d = 0; d < groups.Length; d++) { var categories = Directory.GetDirectories(groups[d]); for (int c = 0; c < categories.Length; c++) { var widgets = Directory.GetFiles(categories[c], "*.js"); var group = groups[d].Replace(widgetBasePath, "").Substring(1); var category = categories[c].Replace(groups[d], "").Substring(1); for (int w = 0; w < widgets.Length; w++) { widgetsList.Add(group + "/" + category + "/" + Path.GetFileNameWithoutExtension(widgets[w])); } } } migCommand.Response = JsonConvert.SerializeObject(widgetsList); break; case "Widgets.Add": { response = "ERROR"; string widgetPath = migCommand.GetOption(0); // eg. homegenie/generic/dimmer string[] widgetParts = widgetPath.Split('/'); widgetParts[0] = new String(widgetParts[0].Where(Char.IsLetter).ToArray()).ToLower(); widgetParts[1] = new String(widgetParts[1].Where(Char.IsLetter).ToArray()).ToLower(); widgetParts[2] = new String(widgetParts[2].Where(Char.IsLetter).ToArray()).ToLower(); if (!String.IsNullOrWhiteSpace(widgetParts[0]) && !String.IsNullOrWhiteSpace(widgetParts[1]) && !String.IsNullOrWhiteSpace(widgetParts[2])) { string filePath = Path.Combine(widgetBasePath, widgetParts[0], widgetParts[1]); if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } // copy widget template into the new widget var htmlFile = Path.Combine(filePath, widgetParts[2] + ".html"); var jsFile = Path.Combine(filePath, widgetParts[2] + ".js"); if (!File.Exists(htmlFile) && !File.Exists(jsFile)) { File.Copy(Path.Combine(widgetBasePath, "template.html"), htmlFile); File.Copy(Path.Combine(widgetBasePath, "template.js"), jsFile); response = "OK"; } } migCommand.Response = JsonHelper.GetSimpleResponse(response); } break; case "Widgets.Save": { response = "ERROR"; string widgetData = new StreamReader(request.InputStream).ReadToEnd(); string fileType = migCommand.GetOption(0); string widgetPath = migCommand.GetOption(1); // eg. homegenie/generic/dimmer string[] widgetParts = widgetPath.Split('/'); string filePath = Path.Combine(widgetBasePath, widgetParts[0], widgetParts[1]); if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } switch (fileType) { // html/javascript source case "html": case "js": using (TextWriter widgetWriter = new StreamWriter(Path.Combine(filePath, widgetParts[2] + "." + fileType))) { widgetWriter.Write(widgetData); } response = "OK"; break; // style sheet file case "css": break; // locale file case "json": break; // image file case "jpg": case "png": case "gif": break; } migCommand.Response = JsonHelper.GetSimpleResponse(response); } break; case "Widgets.Delete": { response = "ERROR"; string widgetPath = migCommand.GetOption(0); // eg. homegenie/generic/dimmer string[] widgetParts = widgetPath.Split('/'); string filePath = Path.Combine(widgetBasePath, widgetParts[0], widgetParts[1], widgetParts[2] + "."); if (File.Exists(filePath + "html")) { File.Delete(filePath + "html"); response = "OK"; } if (File.Exists(filePath + "js")) { File.Delete(filePath + "js"); response = "OK"; } migCommand.Response = JsonHelper.GetSimpleResponse(response); } break; case "Widgets.Export": { string widgetPath = migCommand.GetOption(0); // eg. homegenie/generic/dimmer string[] widgetParts = widgetPath.Split('/'); string widgetBundle = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Utility.GetTmpFolder(), "export", widgetPath.Replace('/', '_') + ".zip"); if (File.Exists(widgetBundle)) { File.Delete(widgetBundle); } else if (!Directory.Exists(Path.GetDirectoryName(widgetBundle))) { Directory.CreateDirectory(Path.GetDirectoryName(widgetBundle)); } string inputPath = Path.Combine(widgetBasePath, widgetParts[0], widgetParts[1]); string outputPath = Path.Combine(widgetParts[0], widgetParts[1]); string infoFilePath = Path.Combine(inputPath, "widget.info"); File.WriteAllText(infoFilePath, "HomeGenie exported widget."); Utility.AddFileToZip(widgetBundle, infoFilePath, "widget.info"); Utility.AddFileToZip(widgetBundle, Path.Combine(inputPath, widgetParts[2] + ".html"), Path.Combine(outputPath, widgetParts[2] + ".html")); Utility.AddFileToZip(widgetBundle, Path.Combine(inputPath, widgetParts[2] + ".js"), Path.Combine(outputPath, widgetParts[2] + ".js")); // byte[] bundleData = File.ReadAllBytes(widgetBundle); (request.Context as HttpListenerContext).Response.AddHeader("Content-Disposition", "attachment; filename=\"" + widgetPath.Replace('/', '_') + ".zip\""); (request.Context as HttpListenerContext).Response.OutputStream.Write(bundleData, 0, bundleData.Length); } break; case "Widgets.Import": { string archiveFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Utility.GetTmpFolder(), "import_widget.zip"); string importPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Utility.GetTmpFolder(), "import"); if (Directory.Exists(importPath)) Directory.Delete(importPath, true); MIG.Gateways.WebServiceUtility.SaveFile(request.InputStream, archiveFile); if (WidgetImport(archiveFile, importPath)) { migCommand.Response = JsonHelper.GetSimpleResponse("OK"); } else { migCommand.Response = JsonHelper.GetSimpleResponse("ERROR"); } } break; case "Widgets.Parse": { string widgetData = new StreamReader(request.InputStream).ReadToEnd(); var parser = new JavaScriptParser(); try { migCommand.Response = JsonHelper.GetSimpleResponse("OK"); parser.Parse(widgetData); } catch (Jint.Parser.ParserException e) { migCommand.Response = JsonHelper.GetSimpleResponse("ERROR (" + e.LineNumber + "," + e.Column + "): " + e.Description); } } break; } }
public void ProcessRequest(MIGClientRequest request, MIGInterfaceCommand migCommand) { var logData = new List<LogEntry>(); try { switch (migCommand.Command) { case "Recent.From": logData = homegenie.RecentEventsLog.ToList().FindAll(le => le != null && le.Domain.StartsWith("MIG.") == false && (le.UnixTimestamp >= double.Parse(migCommand.GetOption(0)))); migCommand.Response = JsonConvert.SerializeObject(logData); //, Formatting.Indented); break; case "Recent.Last": logData = homegenie.RecentEventsLog.ToList().FindAll(le => le != null && le.Domain.StartsWith("MIG.") == false && le.Timestamp > DateTime.UtcNow.AddMilliseconds(-int.Parse(migCommand.GetOption(0)))); migCommand.Response = JsonConvert.SerializeObject(logData); //, Formatting.Indented); break; case "RealTime.EventStream": HttpListenerContext context = (HttpListenerContext)request.Context; //context.Response.KeepAlive = true; context.Response.ContentEncoding = Encoding.UTF8; context.Response.ContentType = "text/event-stream"; context.Response.AddHeader("Cache-Control", "no-cache"); context.Response.AddHeader("Access-Control-Allow-Origin", "*"); // // 2K padding for IE var padding = ":" + new String(' ', 2048) + "\n"; byte[] paddingData = System.Text.Encoding.UTF8.GetBytes(padding); context.Response.OutputStream.Write(paddingData, 0, paddingData.Length); byte[] retryData = System.Text.Encoding.UTF8.GetBytes("retry: 1000\n"); context.Response.OutputStream.Write(retryData, 0, retryData.Length); context.Response.OutputStream.Flush(); // double lastTimeStamp = 0; var lastId = context.Request.Headers.Get("Last-Event-ID"); if (lastId == null || lastId == "") { var queryValues = HttpUtility.ParseQueryString(context.Request.Url.Query); lastId = queryValues.Get("lastEventId"); } if (lastId != null && lastId != "") { double.TryParse(lastId, NumberStyles.Float | NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out lastTimeStamp); } if (lastTimeStamp == 0) { lastTimeStamp = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)).TotalMilliseconds; } int looped = 0; while (looped < 10) { logData = homegenie.RecentEventsLog.ToList().FindAll(le => le != null && le.Domain.StartsWith("MIG.") == false && le.UnixTimestamp > lastTimeStamp); if (logData.Count > 0) { foreach (LogEntry entry in logData) { byte[] data = System.Text.Encoding.UTF8.GetBytes("id: " + entry.UnixTimestamp.ToString("R", CultureInfo.InvariantCulture) + "\ndata: " + JsonConvert.SerializeObject(entry) + "\n\n"); context.Response.OutputStream.Write(data, 0, data.Length); context.Response.OutputStream.Flush(); lastTimeStamp = entry.UnixTimestamp; } } System.Threading.Thread.Sleep(1000); looped++; } break; } } catch { } }
// Check if command was Control.*, update the ModuleParameter. This should happen in a HWInt->HomeGenie pathway private void migService_ServiceRequestPostProcess(MIGClientRequest request, MIGInterfaceCommand command) { if (command.Domain == Domains.HomeAutomation_X10 || command.Domain == Domains.HomeAutomation_ZWave) { Module module = null; try { module = Modules.Find(o => o.Domain == command.Domain && o.Address == command.NodeId); } catch { } // if (module != null) { // wait for ZWaveLib asynchronous response from node and raise the proper "parameter changed" event if (command.Domain == Domains.HomeAutomation_ZWave) // && (context != null && !context.Request.IsLocal) { if (command.Command == ZWave.Command.BASIC_GET) { command.Response = Utility.WaitModuleParameterChange(module, Properties.ZWAVENODE_BASIC); command.Response = JsonHelper.GetSimpleResponse(command.Response); } else if (command.Command == ZWave.Command.WAKEUP_GET) { command.Response = Utility.WaitModuleParameterChange( module, Properties.ZWAVENODE_WAKEUPINTERVAL ); command.Response = JsonHelper.GetSimpleResponse(command.Response); } else if (command.Command == ZWave.Command.BATTERY_GET) { command.Response = Utility.WaitModuleParameterChange(module, Properties.ZWAVENODE_BATTERY); command.Response = JsonHelper.GetSimpleResponse(command.Response); } else if (command.Command == ZWave.Command.MULTIINSTANCE_GET) { command.Response = Utility.WaitModuleParameterChange( module, Properties.ZWAVENODE_MULTIINSTANCE + "." + command.GetOption(0).Replace( ".", "" ) + "." + command.GetOption(1) ); command.Response = JsonHelper.GetSimpleResponse(command.Response); } else if (command.Command == ZWave.Command.ASSOCIATION_GET) { command.Response = Utility.WaitModuleParameterChange( module, Properties.ZWAVENODE_ASSOCIATIONS + "." + command.GetOption(0) ); command.Response = JsonHelper.GetSimpleResponse(command.Response); } else if (command.Command == ZWave.Command.CONFIG_PARAMETERGET) { command.Response = Utility.WaitModuleParameterChange( module, Properties.ZWAVENODE_CONFIGVARIABLES + "." + command.GetOption(0) ); command.Response = JsonHelper.GetSimpleResponse(command.Response); } else if (command.Command == ZWave.Command.NODEINFO_GET) { command.Response = Utility.WaitModuleParameterChange(module, Properties.ZWAVENODE_NODEINFO); command.Response = JsonHelper.GetSimpleResponse(command.Response); } else if (command.Command == ZWave.Command.MANUFACTURERSPECIFIC_GET) { command.Response = Utility.WaitModuleParameterChange( module, Properties.ZWAVENODE_MANUFACTURERSPECIFIC ); command.Response = JsonHelper.GetSimpleResponse(command.Response); } } } } // // 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 webGateway_ProcessRequest(object gwRequest) { var request = (WebServiceGatewayRequest)gwRequest; var context = request.Context; string requestedUrl = request.UrlRequest; bool wroteBytes = false; var migRequest = new MIGClientRequest() { Context = context, RequestOrigin = context.Request.RemoteEndPoint.Address.ToString(), RequestMessage = requestedUrl, SubjectName = "HTTP", SubjectValue = context.Request.HttpMethod, InputStream = context.Request.InputStream, OutputStream = context.Response.OutputStream }; // we are expecting url in the forms http://<hgserver>/<hgservicekey>/<servicedomain>/<servicegroup>/<command>/<opt1>/.../<optn> // arguments up to <command> are mandatory. string migCommand = requestedUrl.Substring(requestedUrl.IndexOf('/', 1) + 1); //string section = requestedurl.Substring (0, requestedurl.IndexOf ('/', 1) - 1); TODO: "api" section keyword, ignored for now //TODO: implement "api" keyword in MIGInterfaceCommand? var command = new MIGInterfaceCommand(migCommand); //PREPROCESS request: if domain != html, execute command if (ServiceRequestPreProcess != null) { ServiceRequestPreProcess(migRequest, command); // request was handled by preprocess listener if (!string.IsNullOrEmpty(command.Response)) { // simple automatic json response type detection if (command.Response.StartsWith("[") && command.Response.EndsWith("]") || (command.Response.StartsWith("{") && command.Response.EndsWith("}"))) { // TODO: check the reason why this cause ajax/json error on some browser context.Response.ContentType = "application/json"; context.Response.ContentEncoding = defaultWebFileEncoding; } WebServiceUtility.WriteStringToContext(context, command.Response); return; } } if (command.Domain == "MIGService.Interfaces") { // This is a MIGService namespace Web API context.Response.ContentType = "application/json"; switch (command.Command) { case "IsEnabled.Set": if (command.GetOption(0) == "1") { configuration.GetInterface(command.NodeId).IsEnabled = true; EnableInterface(command.NodeId); } else { configuration.GetInterface(command.NodeId).IsEnabled = false; DisableInterface(command.NodeId); } WebServiceUtility.WriteStringToContext(context, "[{ \"ResponseValue\" : \"OK\" }]"); // if (InterfacePropertyChanged != null) { InterfacePropertyChanged(new InterfacePropertyChangedAction() { Domain = "MIGService.Interfaces", SourceId = command.NodeId, SourceType = "MIG Interface", Path = "Status.IsEnabled", Value = command.GetOption(0) }); } break; case "IsEnabled.Get": WebServiceUtility.WriteStringToContext( context, "[{ \"ResponseValue\" : \"" + (configuration.GetInterface(command.NodeId).IsEnabled ? "1" : "0") + "\" }]" ); break; case "Options.Set": Interfaces[command.NodeId].SetOption(command.GetOption(0), command.GetOption(1)); WebServiceUtility.WriteStringToContext(context, "[{ \"ResponseValue\" : \"OK\" }]"); // if (InterfacePropertyChanged != null) { InterfacePropertyChanged(new InterfacePropertyChangedAction() { Domain = "MIGService.Interfaces", SourceId = command.NodeId, SourceType = "MIG Interface", Path = "Options." + command.GetOption(0), Value = command.GetOption(1) }); } break; case "Options.Get": string optionValue = Interfaces[command.NodeId].GetOption(command.GetOption(0)).Value; WebServiceUtility.WriteStringToContext( context, "[{ \"ResponseValue\" : \"" + Uri.EscapeDataString(optionValue) + "\" }]" ); break; default: break; } } else if (requestedUrl.StartsWith(webServiceConfig.BaseUrl)) { // If request begins /hg/html, process as standard Web request string requestedFile = GetWebFilePath(requestedUrl); if (!System.IO.File.Exists(requestedFile)) { context.Response.StatusCode = 404; //context.Response.OutputStream.WriteByte(); } else { bool isText = false; if (requestedUrl.EndsWith(".js")) // || requestedurl.EndsWith(".json")) { context.Response.ContentType = "text/javascript"; isText = true; } else if (requestedUrl.EndsWith(".css")) { context.Response.ContentType = "text/css"; isText = true; } else if (requestedUrl.EndsWith(".zip")) { context.Response.ContentType = "application/zip"; } else if (requestedUrl.EndsWith(".png")) { context.Response.ContentType = "image/png"; } else if (requestedUrl.EndsWith(".jpg")) { context.Response.ContentType = "image/jpeg"; } else if (requestedUrl.EndsWith(".gif")) { context.Response.ContentType = "image/gif"; } else if (requestedUrl.EndsWith(".mp3")) { context.Response.ContentType = "audio/mp3"; } else if (requestedUrl.EndsWith(".appcache")) { context.Response.ContentType = "text/cache-manifest"; } else { context.Response.ContentType = "text/html"; isText = true; } var file = new System.IO.FileInfo(requestedFile); context.Response.AddHeader("Last-Modified", file.LastWriteTimeUtc.ToString("r")); context.Response.Headers.Set(HttpResponseHeader.LastModified, file.LastWriteTimeUtc.ToString("r")); // PRE PROCESS text output //TODO: add callback for handling caching (eg. function that returns true or false with requestdfile as input and that will return false for widget path) if (isText) { try { WebFileCache cachedItem = GetWebFileCache(requestedFile); context.Response.ContentEncoding = cachedItem.Encoding; context.Response.ContentType += "; charset=" + cachedItem.Encoding.BodyName; string body = cachedItem.Content; // // expand preprocessor tags body = body.Replace("{hostos}", Environment.OSVersion.Platform.ToString()); // bool tagFound; do { tagFound = false; int ts = body.IndexOf("{include "); if (ts > 0) { int te = body.IndexOf("}", ts); if (te > ts) { string rs = body.Substring(ts + (te - ts) + 1); string cs = body.Substring(ts, te - ts + 1); string ls = body.Substring(0, ts); // try { if (cs.StartsWith("{include ")) { string fileName = cs.Substring(9).TrimEnd('}').Trim(); fileName = GetWebFilePath(fileName); // Encoding fileEncoding = DetectWebFileEncoding(fileName); if (fileEncoding == null) fileEncoding = defaultWebFileEncoding; body = ls + System.IO.File.ReadAllText(fileName, fileEncoding) + rs; } } catch { body = ls + "<h5 style=\"color:red\">Error processing '" + cs.Replace( "{", "[" ).Replace( "}", "]" ) + "'</h5>" + rs; } tagFound = true; } } } while (tagFound); // pre processor tag found // if (webServiceConfig.CacheEnable) { PutWebFileCache(requestedFile, body, context.Response.ContentEncoding); } // WebServiceUtility.WriteStringToContext(context, body); } catch (Exception ex) { // TODO: report internal mig interface error context.Response.StatusCode = 500; WebServiceUtility.WriteStringToContext(context, ex.Message + "\n" + ex.StackTrace); Console.WriteLine("\nMIGService ERROR: " + ex.Message + "\n" + ex.StackTrace + "\n"); } } else { WebServiceUtility.WriteBytesToContext(context, System.IO.File.ReadAllBytes(requestedFile)); } } } else { // Try processing as MigInterface Api or Web Service Dynamic Api object responseObject = null; //domain == HomeAutomation._Interface_ call InterfaceControl var result = (from miginterface in Interfaces.Values 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 (result != null) { try { responseObject = result.InterfaceControl(command); } catch (Exception ex) { // TODO: report internal mig interface error context.Response.StatusCode = 500; responseObject = ex.Message + "\n" + ex.StackTrace; } } // if (responseObject == null || responseObject.Equals(String.Empty)) { var postData = new StreamReader(context.Request.InputStream).ReadToEnd(); if (!String.IsNullOrEmpty(postData)) { command.OriginalRequest += "/" + postData; } responseObject = WebServiceDynamicApiCall(command); } // if (responseObject != null && responseObject.GetType().Equals(typeof(string))) { command.Response = (string)responseObject; // // simple automatic json response type detection if (command.Response.StartsWith("[") && command.Response.EndsWith("]") || (command.Response.StartsWith("{") && command.Response.EndsWith("}"))) { context.Response.ContentType = "application/json"; context.Response.ContentEncoding = defaultWebFileEncoding; } } else { WebServiceUtility.WriteBytesToContext(context, (Byte[])responseObject); wroteBytes = true; } } // //POSTPROCESS if (ServiceRequestPostProcess != null) { ServiceRequestPostProcess(migRequest, command); if (!string.IsNullOrEmpty(command.Response) && !wroteBytes) { // request was handled by postprocess listener WebServiceUtility.WriteStringToContext(context, command.Response); return; } } }
public object WebServiceDynamicApiCall(MIGInterfaceCommand command) { object response = ""; // Dynamic Interface API var handler = MIG.Interfaces.DynamicInterfaceAPI.Find(command.Domain + "/" + command.NodeId + "/" + command.Command); if (handler != null) { response = handler(command.GetOption(0) + (command.GetOption(1) != "" ? "/" + command.GetOption(1) : "")); } else { handler = MIG.Interfaces.DynamicInterfaceAPI.FindMatching(command.OriginalRequest.Trim('/')); if (handler != null) { response = handler(command.OriginalRequest.Trim('/')); } } return response; }
public void ProcessRequest(MIGClientRequest request, MIGInterfaceCommand migCommand) { string domain = ""; string address = ""; string[] filterList; switch (migCommand.Command) { case "Global.CounterTotal": migCommand.Response = JsonHelper.GetSimpleResponse(homegenie.Statistics.GetTotalCounter(migCommand.GetOption(0), 3600).ToString("0.000", System.Globalization.CultureInfo.InvariantCulture)); break; case "Global.TimeRange": var totalRange = homegenie.Statistics.GetStartDate(); migCommand.Response = "[{ StartTime : '" + ((totalRange.TimeStart.Ticks - 621355968000000000) / 10000D).ToString("0.000", System.Globalization.CultureInfo.InvariantCulture) + "', EndTime : '" + ((totalRange.TimeEnd.Ticks - 621355968000000000) / 10000D).ToString("0.000", System.Globalization.CultureInfo.InvariantCulture) + "' }]"; break; case "Database.Reset": homegenie.Statistics.DatabaseReset(); break; case "Parameter.List": filterList = migCommand.GetOption(0).Split(':'); if (filterList.Length == 2) { domain = filterList[0]; address = filterList[1]; } migCommand.Response = "["; foreach (string statParameter in homegenie.Statistics.GetParametersList(domain, address)) { migCommand.Response += " '" + statParameter + "',\n"; } migCommand.Response = migCommand.Response.TrimEnd(',', '\n'); migCommand.Response += "\n]"; break; case "Parameter.Counter": filterList = migCommand.GetOption(1).Split(':'); if (filterList.Length == 2) { domain = filterList[0]; address = filterList[1]; } // migCommand.Response = "["; migCommand.Response += "[ "; // var hoursAverage = new List<StatisticsEntry>(); hoursAverage = homegenie.Statistics.GetHourlyCounter(domain, address, migCommand.GetOption(0), 3600); ; // for (int h = 0; h < 24; h++) { StatisticsEntry firstEntry = null; if (hoursAverage != null && hoursAverage.Count > 0) { firstEntry = hoursAverage.Find(se => se.TimeStart.ToLocalTime().Hour == h); } // if (firstEntry != null) { double sum = 0; sum = (double)(hoursAverage.FindAll(se => se.TimeStart.ToLocalTime().Hour == h).Sum(se => se.Value)); // date is normalized to the current date, time info is preserved from original data entry var date = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd") + " " + h.ToString("00") + ":00:00"); migCommand.Response += "[" + ((date.Ticks - 621355968000000000) / 10000D).ToString("0.000", System.Globalization.CultureInfo.InvariantCulture) + "," + sum.ToString("0.000", System.Globalization.CultureInfo.InvariantCulture) + "],"; } else { var date = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd") + " " + h.ToString("00") + ":00:00"); migCommand.Response += "[" + ((date.Ticks - 621355968000000000) / 10000D).ToString("0.000", System.Globalization.CultureInfo.InvariantCulture) + ",0.000],"; } } migCommand.Response = migCommand.Response.TrimEnd(','); migCommand.Response += " ]"; migCommand.Response += "]"; break; case "Parameter.StatsHour": filterList = migCommand.GetOption(1).Split(':'); if (filterList.Length == 2) { domain = filterList[0]; address = filterList[1]; } // migCommand.Response = "["; // var hoursAverages = new List<StatisticsEntry>[5]; hoursAverages[0] = homegenie.Statistics.GetHourlyStats(domain, address, migCommand.GetOption(0), "Min"); hoursAverages[1] = homegenie.Statistics.GetHourlyStats(domain, address, migCommand.GetOption(0), "Max"); hoursAverages[2] = homegenie.Statistics.GetHourlyStats(domain, address, migCommand.GetOption(0), "Avg"); hoursAverages[3] = homegenie.Statistics.GetHourlyStats24(domain, address, migCommand.GetOption(0), "Avg"); if (migCommand.GetOption(0).StartsWith(Properties.METER_ANY)) { hoursAverages[4] = homegenie.Statistics.GetTodayDetail(domain, address, migCommand.GetOption(0), "Sum"); } else { hoursAverages[4] = homegenie.Statistics.GetTodayDetail(domain, address, migCommand.GetOption(0), "Avg"); } // for (int x = 0; x < 4; x++) { migCommand.Response += "[ "; for (int h = 0; h < 24; h++) { StatisticsEntry firstEntry = null; if (hoursAverages[x] != null && hoursAverages[x].Count > 0) { if (migCommand.GetOption(0).StartsWith(Properties.METER_ANY)) { firstEntry = hoursAverages[x].Find(se => se.TimeStart.ToLocalTime().Hour == h && se.Value > 0); } else { firstEntry = hoursAverages[x].Find(se => se.TimeStart.ToLocalTime().Hour == h); } } // if (firstEntry != null) { double sum = 0; switch (x) { case 0: if (migCommand.GetOption(0).StartsWith(Properties.METER_ANY)) { sum = (double)(hoursAverages[x].FindAll(se => se.TimeStart.ToLocalTime().Hour == h && se.Value > 0).Min(se => se.Value)); } else { sum = (double)(hoursAverages[x].FindAll(se => se.TimeStart.ToLocalTime().Hour == h).Min(se => se.Value)); } break; case 1: sum = (double)(hoursAverages[x].FindAll(se => se.TimeStart.ToLocalTime().Hour == h).Max(se => se.Value)); break; case 2: if (migCommand.GetOption(0).StartsWith(Properties.METER_ANY)) { sum = (double)(hoursAverages[x].FindAll(se => se.TimeStart.ToLocalTime().Hour == h && se.Value > 0).Average(se => se.Value)); } else { sum = (double)(hoursAverages[x].FindAll(se => se.TimeStart.ToLocalTime().Hour == h).Average(se => se.Value)); } break; case 3: if (migCommand.GetOption(0).StartsWith(Properties.METER_ANY)) { sum = (double)(hoursAverages[x].FindAll(se => se.TimeStart.ToLocalTime().Hour == h && se.Value > 0).Average(se => se.Value)); } else { sum = (double)(hoursAverages[x].FindAll(se => se.TimeStart.ToLocalTime().Hour == h).Average(se => se.Value)); } break; } // date is normalized to the current date, time info is preserved from original data entry var date = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd") + " " + h.ToString("00") + ":00:00"); migCommand.Response += "[" + ((date.Ticks - 621355968000000000) / 10000D).ToString("0.000", System.Globalization.CultureInfo.InvariantCulture) + "," + sum.ToString("0.000", System.Globalization.CultureInfo.InvariantCulture) + "],"; } else { var date = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd") + " " + h.ToString("00") + ":00:00"); migCommand.Response += "[" + ((date.Ticks - 621355968000000000) / 10000D).ToString("0.000", System.Globalization.CultureInfo.InvariantCulture) + ",0.000],"; } } migCommand.Response = migCommand.Response.TrimEnd(','); migCommand.Response += " ],"; } // migCommand.Response += "[ "; foreach (var entry in hoursAverages[4]) { var date = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd") + " " + entry.TimeStart.ToLocalTime().ToString("HH:mm:ss.ffffff")); migCommand.Response += "[" + ((date.Ticks - 621355968000000000) / 10000D).ToString("0.000", System.Globalization.CultureInfo.InvariantCulture) + "," + entry.Value.ToString("0.000", System.Globalization.CultureInfo.InvariantCulture) + "],"; } migCommand.Response = migCommand.Response.TrimEnd(','); migCommand.Response += " ]"; // migCommand.Response += "]"; break; } }