public object UnInstall(HttpRequestParams request)
        {
            string packageId = request.GetRequiredString("packageId");

            Context.PackageManager.UnInstall(packageId);
            return null;
        }
        public object GetPackages(HttpRequestParams request)
        {
            string query = request.GetString("query");

            var list = Context.PackageManager.GetPackages(query);

            return list.Select(BuildModel).Where(x => x != null).ToArray();
        }
        private object apiSetZoneConfiguration(HttpRequestParams request)
        {
            var id = request.GetRequiredGuid("id");
            var monitorsIds = request.GetRequiredString("monitorsIds");
            var controllersIds = request.GetRequiredString("controllersIds");
            var scriptsIds = request.GetRequiredString("scriptsIds");
            //var graphsIds = request.GetRequiredString("graphsIds");

            using (var session = Context.OpenSession())
            {
                var zone = session.Load<Zone>(id);
                zone.MonitorsList = monitorsIds;
                zone.ControllersList = controllersIds;
                zone.ScriptsList = scriptsIds;
                //zone.GraphsList = graphsIds;

                session.Flush();
            }

            //NotifyForSignalR(new { MsgId = "ControllerIsVisibleChanged", Data = BuildControllerWebModel(ctrl) });

            return null;
        }
        private object apiAddZone(HttpRequestParams request)
        {
            var name = request.GetRequiredString("name");

            using (var session = Context.OpenSession())
            {
                Zone zone = new Zone()
                {
                    Id = Guid.NewGuid(),
                    Name = name,
                    MonitorsList = "[]",
                    ControllersList = "[]",
                    ScriptsList = "[]",

                    GraphsList = "[]"
                };

                session.Save(zone);
                session.Flush();
            }

            //NotifyForSignalR(new { MsgId = "SensorNameChanged", Data = new { Id = id, Name = name } });

            return null;
        }
 private object apiGetZones(HttpRequestParams request)
 {
     return Get()
         .Select(BuildZoneWebModel)
         .Where(x => x != null)
         .ToArray();
 }
        public object AddSubscription(HttpRequestParams request)
        {
            string eventAlias = request.GetRequiredString("eventAlias");
            Guid scriptId = request.GetRequiredGuid("scriptId");

            using (var session = Context.OpenSession())
            {
                var guid = Guid.NewGuid();

                var script = session.Load<UserScript>(scriptId);

                var subscription = new ScriptEventHandler
                {
                    Id = guid,
                    EventAlias = eventAlias,
                    UserScript = script
                };

                session.Save(subscription);
                session.Flush();

                return guid;
            }
        }
        public object GetSubscriptionForm(HttpRequestParams request)
        {
            using (var session = Context.OpenSession())
            {
                var events = Context.GetPlugin<ScriptsPlugin>().ScriptEvents
                    .Select(eventAlias => new { id = eventAlias, name = eventAlias })
                    .ToList();

                var scripts = session.Query<UserScript>()
                    .Select(x => new { id = x.Id, name = x.Name })
                    .ToArray();

                var selectedEventAlias = events.Any() ? events.First().id : null;
                var selectedScriptId = scripts.Any() ? (Guid?)scripts.First().id : null;

                return new
                {
                    eventList = events,
                    scriptList = scripts,
                    selectedEventAlias,
                    selectedScriptId
                };
            }
        }
        private object apiDeleteScript(HttpRequestParams request)
        {
            Guid scriptId = request.GetRequiredGuid("scriptId");

            using (var session = Context.OpenSession())
            {
                var subscription = session.Load<UserScript>(scriptId);
                session.Delete(subscription);
                session.Flush();
            }

            return null;
        }
 private object apiGetControllerTypes(HttpRequestParams request)
 {
     return Enum.GetValues(typeof(ControllerType)).Cast<ControllerType>()
         .Select(v => new
         {
             Id = v,
             Name = v.GetEnumDescription(),
         }).ToArray();
 }
        private object apiDeleteMonitor(HttpRequestParams request)
        {
            var id = request.GetRequiredGuid("Id");

            using (var session = Context.OpenSession())
            {
                var monitor = session.Load<Monitor>(id);
                session.Delete(monitor);
                session.Flush();
            }

            //NotifyForSignalR(new { MsgId = "SensorDeleted", Data = new { Id = id } });

            return null;
        }
        private object apiSetMonitorConfiguration(HttpRequestParams request)
        {
            var id = request.GetRequiredGuid("id");
            var configuration = request.GetRequiredString("config");

            using (var session = Context.OpenSession())
            {
                var monitor = session.Load<Monitor>(id);
                monitor.Configuration = configuration;
                session.Flush();
            }

            //NotifyForSignalR(new { MsgId = "ControllerIsVisibleChanged", Data = BuildControllerWebModel(ctrl) });

            return null;
        }
        private object apiSetMonitorNameForInformer(HttpRequestParams request)
        {
            var id = request.GetRequiredGuid("id");
            var name = request.GetString("name");

            using (var session = Context.OpenSession())
            {
                var monitor = session.Load<Monitor>(id);
                monitor.NameForInformer = name;
                session.Flush();
            }

            //NotifyForSignalR(new
            //{
            //    MsgId = "SensorNameChanged",
            //    Data = new
            //    {
            //        Id = id,
            //        Name = name
            //    }
            //});

            return null;
        }
        private object apiAddMonitor(HttpRequestParams request)
        {
            var name = request.GetRequiredString("name");
            var sensorId = request.GetRequiredGuid("sensorId");
            var configuration = request.GetRequiredString("config");

            using (var session = Context.OpenSession())
            {
                Monitor monitor = new Monitor()
                {
                    Id = Guid.NewGuid(),
                    Name = name,
                    SensorId = sensorId,
                    Configuration = configuration
                };

                session.Save(monitor);
                session.Flush();
            }

            //NotifyForSignalR(new { MsgId = "MonitorAdded", Data = BuildMonitorWebModel(ctrl) });

            return null;
        }
 private object apiGetMonitorBySensor(HttpRequestParams request)
 {
     var id = request.GetRequiredGuid("id");
     return BuildMonitorRichWebModel(GetBySensor(id));
 }
        private object apiSetCommandName(HttpRequestParams request)
        {
            var id = request.GetRequiredGuid("id");
            var text = request.GetRequiredString("text");

            using (var session = Context.OpenSession())
            {
                var cmd = session.Load<VoiceCommand>(id);
                cmd.CommandText = text;
                session.Flush();
            }

            CloseRecognitionEngine();
            InitRecognitionEngine();

            //NotifyForSignalR(new
            //{
            //    MsgId = "SensorNameChanged",
            //    Data = new
            //    {
            //        Id = id,
            //        Name = name
            //    }
            //});

            return null;
        }
 private object apiGetScript(HttpRequestParams request)
 {
     Guid id = request.GetRequiredGuid("id");
     return BuildScriptRichWebModel(GetScript(id));
 }
        private object apiSaveScript(HttpRequestParams request)
        {
            Guid? id = request.GetGuid("id");
            string name = request.GetRequiredString("name");
            string body = request.GetString("body");

            using (var session = Context.OpenSession())
            {
                var script = id.HasValue ? session.Get<UserScript>(id.Value) : new UserScript { Id = Guid.NewGuid() };
                script.Name = name;
                script.Body = body;

                session.SaveOrUpdate(script);
                session.Flush();
            }

            return null;
        }
 private object apiGetControllers(HttpRequestParams request)
 {
     return Get()
         .Select(BuildControllerWebModel)
         .Where(x => x != null)
         .ToArray();
 }
        private object apiRunScript(HttpRequestParams request)
        {
            Guid scriptId = request.GetRequiredGuid("scriptId");

            ExecuteScript(GetScript(scriptId), new object[0]);

            return null;
        }
        private object apiGetController(HttpRequestParams request)
        {
            var id = request.GetRequiredGuid("id");

            return BuildControllerWebModel(Get(id));
        }
 public object GetSubscriptionList(HttpRequestParams request)
 {
     using (var session = Context.OpenSession())
     {
         return session.Query<ScriptEventHandler>()
             .Select(x => new
             {
                 id = x.Id,
                 scriptId = x.UserScript.Id,
                 scriptName = x.UserScript.Name,
                 eventAlias = x.EventAlias
             })
             .ToList();
     }
 }
        private object apiAddController(HttpRequestParams request)
        {
            var name = request.GetRequiredString("name");
            var type = (ControllerType)request.GetRequiredInt32("type");

            var ctrl = new Controller()
            {
                Id = Guid.NewGuid(),
                Name = name,
                Type = type,
                IsAutoMode = false
            };

            ControllerBase controller = CastController(ctrl);
            if (controller != null)
            {
                controller.Init(Context);
                controller.SaveToDB();
                controllers.Add(controller);

                NotifyForSignalR(new { MsgId = "ControllerAdded", Data = BuildControllerWebModel(ctrl) });
            }

            return null;
        }
        public object DeleteSubscription(HttpRequestParams request)
        {
            Guid subscriptionId = request.GetRequiredGuid("subscriptionId");

            using (var session = Context.OpenSession())
            {
                var subscription = session.Load<ScriptEventHandler>(subscriptionId);
                session.Delete(subscription);
                session.Flush();
            }

            return null;
        }
        private object apiSetControllerIsAutoMode(HttpRequestParams request)
        {
            var id = request.GetRequiredGuid("id");
            var isAutoMode = request.GetRequiredBool("isAutoMode");

            foreach (ControllerBase controller in controllers)
                if (controller.ID == id)
                {
                    controller.IsAutoMode = isAutoMode;
                    break;
                }

            //NotifyForSignalR(new { MsgId = "ControllerIsAutoModeChanged", Data = BuildControllerWebModel(ctrl) });

            return null;
        }
 private object apiGetZoneRich(HttpRequestParams request)
 {
     var id = request.GetRequiredGuid("id");
     return BuildZoneRichWebModel(Get(id));
 }
        private object apiSetControllerConfiguration(HttpRequestParams request)
        {
            var id = request.GetRequiredGuid("id");
            var conf = request.GetRequiredString("config");

            foreach (ControllerBase controller in controllers)
                if (controller.ID == id)
                {
                    controller.SetConfiguration(conf);
                    break;
                }

            //NotifyForSignalR(new { MsgId = "ControllerIsVisibleChanged", Data = BuildControllerWebModel(ctrl) });

            return null;
        }
        private object apiSetZoneName(HttpRequestParams request)
        {
            var id = request.GetRequiredGuid("id");
            var name = request.GetRequiredString("name");

            using (var session = Context.OpenSession())
            {
                var zone = session.Load<Zone>(id);
                zone.Name = name;
                session.Flush();
            }

            //NotifyForSignalR(new
            //{
            //    MsgId = "SensorNameChanged",
            //    Data = new
            //    {
            //        Id = id,
            //        Name = name
            //    }
            //});

            return null;
        }
        private object apiDeleteController(HttpRequestParams request)
        {
            var id = request.GetRequiredGuid("Id");

            using (var session = Context.OpenSession())
            {
                var ctrl = session.Load<Controller>(id);
                session.Delete(ctrl);
                session.Flush();

                NotifyForSignalR(new { MsgId = "ControllerDeleted", Data = new { Id = id } });
            }

            foreach (ControllerBase controller in controllers)
                if (controller.ID == id)
                {
                    controllers.Remove(controller);
                    break;
                }

            return null;
        }
 private object apiDeleteZone(HttpRequestParams request)
 {
     var id = request.GetRequiredGuid("Id");
     Delete(id);
     //NotifyForSignalR(new { MsgId = "SensorDeleted", Data = new { Id = id } });
     return null;
 }
 private object apiGetScripts(HttpRequestParams request)
 {
     using (var session = Context.OpenSession())
     {
         return GetScripts()
             .Select(BuildScriptWebModel)
             .ToArray();
     }
 }