Esempio n. 1
0
        public static string LoadUploaderConfig()
        {
            try
            {
                //if (!AuthentificationManager.AuthenticateUser(HttpUtility.HtmlEncode(key + "")))
                //    return "{\"error\":\"Unauthenticated\"}";

                MongoManager mongoManager;
                List <UploaderConfigItem> items = new List <UploaderConfigItem>();

                mongoManager = new MongoManager();
                items        = mongoManager.SelectFromCollection(new CollectionItem(Constants.Database.Settings), FilterBuilder.UploaderConfig.BuildFilter());

                return(JsonConvert.SerializeObject(items));
            }
            catch (Exception ex)
            {
                LoggingManager.LogError(System.Reflection.MethodBase.GetCurrentMethod().Name, ex);
            }
            return("");
        }
Esempio n. 2
0
        public static void SaveUploaderConfig(string data)
        {
            try
            {
                MongoManager mongoManager = new MongoManager();

                List <UploaderConfigItem> itemsSaved     = new List <UploaderConfigItem>();
                List <UploaderConfigItem> itemsExisting  = new List <UploaderConfigItem>();
                List <UploaderConfigItem> alreadyChecked = new List <UploaderConfigItem>();

                itemsSaved    = JsonConvert.DeserializeObject <List <UploaderConfigItem> >(data);
                itemsExisting = mongoManager.SelectFromCollection(new CollectionItem(Constants.Database.Settings), FilterBuilder.UploaderConfig.BuildFilter());

                foreach (UploaderConfigItem item in itemsSaved)
                {
                    if (itemsExisting.Any(x => x.UploaderId == item.UploaderId))                                    //Item schon vorhanden
                    {
                        if (itemsExisting.Any(x => x.UploaderId == item.UploaderId && x.Position != item.Position)) //Positionseigenschaft hat sich geändert
                        {
                            List <BsonElement> elements = new List <BsonElement>
                            {
                                new BsonElement(Constants.Database.Fields.UploaderConfig.Id, BsonValue.Create(item.UploaderId)),
                                new BsonElement(Constants.Database.Fields.UploaderConfig.Position, BsonValue.Create(item.Position))
                            };

                            BsonDocument document = new BsonDocument(elements);
                            mongoManager.Delete(new CollectionItem(Constants.Database.Settings), FilterBuilder.UploaderConfig.BuildFilter(item.UploaderId));
                            mongoManager.Insert(document, new CollectionItem(Constants.Database.Settings));
                        }
                    }
                    else    //Item noch nicht vorhanden
                    {
                        List <BsonElement> elements = new List <BsonElement>
                        {
                            new BsonElement(Constants.Database.Fields.UploaderConfig.Id, BsonValue.Create(item.UploaderId)),
                            new BsonElement(Constants.Database.Fields.UploaderConfig.Position, BsonValue.Create(item.Position))
                        };

                        BsonDocument document = new BsonDocument(elements);
                        mongoManager.Insert(document, new CollectionItem(Constants.Database.Settings));
                    }
                }

                //Deleting
                itemsExisting = mongoManager.SelectFromCollection(new CollectionItem(Constants.Database.Settings), FilterBuilder.UploaderConfig.BuildFilter());

                if (itemsSaved.Count != itemsExisting.Count)
                {
                    foreach (UploaderConfigItem item in itemsExisting)
                    {
                        if (!itemsSaved.Any(x => x.UploaderId == item.UploaderId))
                        {
                            mongoManager.Delete(new CollectionItem(Constants.Database.Settings), FilterBuilder.UploaderConfig.BuildFilter(item.UploaderId));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LoggingManager.LogError(System.Reflection.MethodBase.GetCurrentMethod().Name, ex);
            }
        }
Esempio n. 3
0
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(Request.Params.Get("action")))
                {
                    _action = Request.Params.Get("action");
                }

                if (!string.IsNullOrEmpty(Request.Params.Get("uploaderId")))
                {
                    _uploaderId = Request.Params.Get("uploaderId");
                }

                if (!string.IsNullOrEmpty(Request.Params.Get("temp")))
                {
                    float.TryParse(Request.Params.Get("temp"), out _temperature);
                }

                if (!string.IsNullOrEmpty(Request.Params.Get("hum")))
                {
                    float.TryParse(Request.Params.Get("hum"), out _humidity);
                }

                if (!string.IsNullOrEmpty(Request.Params.Get("apisecret")))
                {
                    _apisecret = Request.Params.Get("apisecret");
                }

                if (string.IsNullOrEmpty(_action) || string.IsNullOrEmpty(_uploaderId) || string.IsNullOrEmpty(_apisecret) || _temperature == -100 || _humidity == -100)
                {
                    return;
                }

                if (!AuthentificationManager.AuthenticateApi(_apisecret))
                {
                    return;
                }

                mongoManager = new MongoManager();

                if (_action == "AddRecord")
                {
                    List <BsonElement> elements = new List <BsonElement>
                    {
                        new BsonElement(Constants.Database.Fields.Humiture.Date, BsonValue.Create(DateTime.Now)),
                        new BsonElement(Constants.Database.Fields.Humiture.Temperature, BsonValue.Create(_temperature)),
                        new BsonElement(Constants.Database.Fields.Humiture.Humidity, BsonValue.Create(_humidity))
                    };

                    List <UploaderConfigItem> configItems = mongoManager.SelectFromCollection(new CollectionItem(Constants.Database.Settings), FilterBuilder.UploaderConfig.BuildFilter(_uploaderId));
                    if (configItems.Any())
                    {
                        mongoManager.Insert(new BsonDocument(elements), new CollectionItem(configItems.FirstOrDefault().Position, Constants.Database.CollectionType.Humiture));
                    }
                    else
                    {
                        LoggingManager.LogWaring("Cant find a Position for uploader with ID " + _uploaderId + ". Pleas add this Uploader to the Config. Value recieved is lost.", System.Reflection.MethodBase.GetCurrentMethod().Name);
                    }

                    return;
                }
            }
            catch (Exception ex)
            {
                LoggingManager.LogError(System.Reflection.MethodBase.GetCurrentMethod().Name, ex);
            }
        }