示例#1
0
        public ActionResult AddSensorsData(SensorsListModel postData)        //post
        {
            //getdb();
            //var collection = database.GetCollection<MongoSensorsListModel>("SENSOR_LIST");

            MongoSensorsListModel insertList = new MongoSensorsListModel {
            };


            var    all   = GetSensorsData().Where(c => c.sensorId.Contains(postData.Sensortype));
            int    count = all.Count();
            string id    = postData.Sensortype + count;

            all = GetSensorsData().Where(c => c.sensorId == (id));



            while (all.Count() != 0)
            {
                count += 1;
                id     = postData.Sensortype + count;
                all    = GetSensorsData().Where(c => c.sensorId == (id));

                // code block to be executed
            }


            insertList.roomId   = postData.roomId;
            insertList.sensorId = postData.Sensortype + count;
            //insertList.location = postData.location;
            insertList.pos_x = postData.pos_x;
            insertList.pos_y = postData.pos_y;
            insertList.desc  = postData.desc;
            insertList.latest_checking_time = DateTime.UtcNow;
            insertList.total_run_time       = DateTime.UtcNow;
            try
            {
                new DBManger().DataBase.GetCollection <MongoSensorsListModel>("SENSOR_LIST").InsertOneAsync(insertList);
            }
            catch (Exception)
            {
                return(ReturnUrl());
            };

            return(ReturnUrl());
        }
示例#2
0
        public ActionResult DropSensorsData(MongoSensorsListModel postData)        //post
        {
            var DeleteResult = SensorsCollection.DeleteOne(Builders <MongoSensorsListModel> .Filter.Eq("sensorId", postData.sensorId));

            if (DeleteResult.IsAcknowledged)
            {
                if (DeleteResult.DeletedCount != 1)
                {
                    return(ReturnUrl());

                    throw new Exception(string.Format("Count [value:{0}] after delete is invalid", DeleteResult.DeletedCount));
                }
            }
            else
            {
                return(ReturnUrl());

                throw new Exception(string.Format("Delete for [_id:{0}] was not acknowledged", postData.sensorId.ToString()));
            }
            return(ReturnUrl());
        }
示例#3
0
        public ActionResult UpdateSensors(MongoSensorsListModel postData)
        {
            //getdb();

            //var collection = database.GetCollection<MongoSensorsListModel>("SENSOR_LIST");
            var filter = Builders <MongoSensorsListModel> .Filter.Eq("sensorId", postData.sensorId);

            var type  = postData.GetType();
            var props = type.GetProperties();

            foreach (var property in props)
            {
                if (!property.Name.Equals("_id"))
                {
                    if (property.GetValue(postData) != null)
                    {
                        UpdateDefinition <MongoSensorsListModel> up;
                        if (property.Name == "latest_checking_time")
                        {
                            up = Builders <MongoSensorsListModel> .Update.Set(property.Name.ToString(), DateTime.UtcNow);
                        }
                        else
                        {
                            up = Builders <MongoSensorsListModel> .Update.Set(property.Name.ToString(), property.GetValue(postData).ToString());

                            Debug.WriteLine("\n " + property + " + " + property.GetValue(postData));
                        }
                        var Updated = SensorsCollection.UpdateOne(filter, up);

                        this.isUpdated = Updated.IsAcknowledged;
                        Debug.WriteLine("\n update status" + this.isUpdated);
                    }
                }
            }

            return(ReturnUrl());
        }