Exemple #1
0
 private void LoadData()
 {
     if (!File.Exists("data.json"))
     {
         return;
     }
     using (StreamReader r = new StreamReader("data.json"))
     {
         string s     = r.ReadToEnd();
         JArray array = JArray.Parse(s);
         foreach (JObject obj in array)
         {
             string      name = obj["Name"].Value <string>();
             SensolusCfg cfg  = new SensolusCfg();
             cfg.Interval    = obj["Interval"].Value <int>();
             cfg.Host        = obj["Host"].Value <string>();
             cfg.ApiKey      = obj["ApiKey"].Value <string>();
             cfg.Port        = obj["Port"].Value <int>();
             cfg.Database    = obj["Database"].Value <string>();
             cfg.User        = obj["User"].Value <string>();
             cfg.Password    = obj["Password"].Value <string>();
             cfg.Pool        = obj["Pool"].Value <bool>();
             cfg.Clean       = obj["Clean"].Value <bool>();
             cfg.LastExecute = obj["LastExecute"].Value <DateTime>();
             Task.Run(() => {
                 SensolusCfg copyCfg = cfg;
                 copyCfg.Clean       = false;
                 copyCfg.Interval    = (int)(DateTime.Now - copyCfg.LastExecute).TotalMinutes;
                 DataProcessor dp    = new DataProcessor(copyCfg);
                 dp.Run(NpgsqlFactory.Instance);
                 cfg.LastExecute = DateTime.Now;
                 lock (connCfgs) {
                     if (!connCfgs.ContainsKey(name.ToLower()))
                     {
                         connCfgs.Add(name.ToLower(), cfg);
                     }
                 }
                 Responder.SuperRoot.CreateChild(name, "connNode").BuildNode();
             });
         }
     }
 }
Exemple #2
0
        private void _createConnection(InvokeRequest request)
        {
            SensolusCfg cfg  = new SensolusCfg();
            string      name = request.Parameters["Name"].Value <String>();

            if (connCfgs.ContainsKey(name.ToLower()))
            {
                // ERROR
                return;
            }
            cfg.ApiKey   = request.Parameters["apiKey"].Value <String>();
            cfg.Host     = request.Parameters["DB Address"].Value <String>();
            cfg.Port     = request.Parameters["DB Port"].Value <int>();
            cfg.Database = request.Parameters["DB Name"].Value <String>();
            cfg.User     = request.Parameters["DB User"].Value <String>();
            cfg.Password = request.Parameters["DB Password"].Value <String>();
            cfg.Interval = request.Parameters["Interval (minutes)"].Value <int>();
            cfg.Pool     = request.Parameters["Pool"].Value <bool>();
            Task.Run(() =>
            {
                var copyCfg = cfg;
                if (request.Parameters["Old Data"].Value <bool>())
                {
                    copyCfg.Interval = (int)(DateTime.Now - new DateTime(1990, 1, 1)).TotalMinutes / 2;
                    copyCfg.Clean    = true;
                }
                DataProcessor dp = new DataProcessor(copyCfg);
                dp.Run(NpgsqlFactory.Instance);
                cfg.LastExecute = DateTime.Now;
                lock (connCfgs)
                {
                    connCfgs.Add(name.ToLower(), cfg);
                }
                SaveData();
            });
            Responder.SuperRoot.CreateChild(name, "connNode").BuildNode();
        }
Exemple #3
0
        public SensolusLink(Configuration config, CommandLineArguments cmdLineOptions) : base(config)
        {
            //Perform any configuration overrides from command line options
            if (cmdLineOptions.BrokerUrl != null)
            {
                config.BrokerUrl = cmdLineOptions.BrokerUrl;
            }

            if (cmdLineOptions.Token != null)
            {
                config.Token = cmdLineOptions.Token;
            }

            if (cmdLineOptions.NodesFileName != null)
            {
                config.NodesFilename = cmdLineOptions.NodesFileName;
            }

            if (cmdLineOptions.KeysFolder != null)
            {
                config.KeysFolder = cmdLineOptions.KeysFolder;
            }

            Responder.AddNodeClass("connNode", delegate(Node node)
            {
                node.CreateChild("Remove", "removeNode").BuildNode();
            });

            Responder.AddNodeClass("removeNode", delegate(Node node)
            {
                node.SetAction(new ActionHandler(Permission.Config, (InvokeRequest request) =>
                {
                    lock (connCfgs)
                    {
                        connCfgs.Remove(node.Parent.Name.ToLower());
                    }
                    SaveData();
                }));
            });

            Responder.AddNodeClass("handleActivity", delegate(Node node) {
                node.Configs.Set(ConfigType.DisplayName, new Value("Handle Activity Notification"));
                node.AddParameter(new Parameter {
                    Name      = SensolusConstants.PATH,
                    ValueType = DSLink.Nodes.ValueType.String
                });
                node.AddParameter(new Parameter {
                    Name      = SensolusConstants.SERIAL,
                    ValueType = DSLink.Nodes.ValueType.String
                });
                node.AddParameter(new Parameter {
                    Name      = SensolusConstants.ACTIVITY_ID,
                    ValueType = DSLink.Nodes.ValueType.Number
                });
                node.AddParameter(new Parameter {
                    Name      = SensolusConstants.ACTIVITY_TIME,
                    ValueType = DSLink.Nodes.ValueType.String
                });
                node.AddParameter(new Parameter
                {
                    Name      = SensolusConstants.ACTIVITY_INSERTTIME,
                    ValueType = DSLink.Nodes.ValueType.String
                });
                node.AddParameter(new Parameter
                {
                    Name      = SensolusConstants.ACTIVITY_STATE,
                    ValueType = DSLink.Nodes.ValueType.String
                });
                node.AddParameter(new Parameter
                {
                    Name      = SensolusConstants.ACTIVITY_LAT,
                    ValueType = DSLink.Nodes.ValueType.Number
                });
                node.AddParameter(new Parameter
                {
                    Name      = SensolusConstants.ACTIVITY_LON,
                    ValueType = DSLink.Nodes.ValueType.Number
                });
                node.AddParameter(new Parameter
                {
                    Name      = SensolusConstants.ACTIVITY_SRC,
                    ValueType = DSLink.Nodes.ValueType.String
                });
                node.AddParameter(new Parameter
                {
                    Name      = SensolusConstants.ACTIVITY_ACCURACY,
                    ValueType = DSLink.Nodes.ValueType.Number
                });
                node.AddParameter(new Parameter
                {
                    Name      = SensolusConstants.ACTIVITY_ADDRESS,
                    ValueType = DSLink.Nodes.ValueType.String
                });
                node.AddParameter(new Parameter
                {
                    Name      = SensolusConstants.ACTIVITY_GEOZONE,
                    ValueType = DSLink.Nodes.ValueType.String
                });
                node.SetAction(new ActionHandler(Permission.Config, _handleActivityNotification));
            });

            Responder.AddNodeClass("handleAlert", delegate(Node node) {
                node.Configs.Set(ConfigType.DisplayName, new Value("Handle Alert Notification"));
                node.AddParameter(new Parameter
                {
                    Name      = SensolusConstants.PATH,
                    ValueType = DSLink.Nodes.ValueType.String
                });
                node.AddParameter(new Parameter
                {
                    Name      = SensolusConstants.SERIAL,
                    ValueType = DSLink.Nodes.ValueType.String
                });
                node.SetAction(new ActionHandler(Permission.Config, _handleAlertNotification));
            });

            Responder.AddNodeClass("connAdd", delegate(Node node)
            {
                node.Configs.Set(ConfigType.DisplayName, new Value("Add Connection"));
                node.AddParameter(new Parameter
                {
                    Name      = "Name",
                    ValueType = DSLink.Nodes.ValueType.String
                });
                node.AddParameter(new Parameter
                {
                    Name      = "apiKey",
                    ValueType = DSLink.Nodes.ValueType.String
                });
                node.AddParameter(new Parameter
                {
                    Name      = "DB Address",
                    ValueType = DSLink.Nodes.ValueType.String
                });
                node.AddParameter(new Parameter
                {
                    Name      = "DB Port",
                    ValueType = DSLink.Nodes.ValueType.Number
                });
                node.AddParameter(new Parameter
                {
                    Name      = "DB Name",
                    ValueType = DSLink.Nodes.ValueType.String
                });
                node.AddParameter(new Parameter
                {
                    Name      = "DB User",
                    ValueType = DSLink.Nodes.ValueType.String
                });
                node.AddParameter(new Parameter
                {
                    Name      = "DB Password",
                    ValueType = DSLink.Nodes.ValueType.String,
                    Editor    = EditorType.Password
                });
                node.AddParameter(new Parameter
                {
                    Name      = "Interval (minutes)",
                    ValueType = DSLink.Nodes.ValueType.Number
                });
                node.AddParameter(new Parameter
                {
                    Name      = "Pool",
                    ValueType = DSLink.Nodes.ValueType.Boolean
                });
                node.AddParameter(new Parameter
                {
                    Name      = "Old Data",
                    ValueType = DSLink.Nodes.ValueType.Boolean
                });
                node.AddParameter(new Parameter
                {
                    Name      = "Context",
                    ValueType = DSLink.Nodes.ValueType.String
                });
                node.SetAction(new ActionHandler(Permission.Config, _createConnection));
            });

            Task.Run(() =>
            {
                long i = 0;
                while (true)
                {
                    lock (connCfgs)
                    {
                        foreach (var key in connCfgs.Keys)
                        {
                            var cfg = connCfgs[key];
                            if (i % cfg.Interval == 0)
                            {
                                Task.Run(() =>
                                {
                                    var copyCfg      = cfg;
                                    copyCfg.Clean    = false;
                                    DataProcessor dp = new DataProcessor(copyCfg);
                                    dp.Run(NpgsqlFactory.Instance);
                                    cfg.LastExecute = DateTime.Now;
                                    connCfgs[key]   = cfg;
                                });
                            }
                        }
                    }
                    ++i;
                    Thread.Sleep(TimeSpan.FromMinutes(1));
                }
            });
        }