public ActionResult AddDevicesData(MongoDevicesListModel postData) //post { MongoDevicesListModel insertList = new MongoDevicesListModel { }; var all = GetAllDevices().Where(d => d.devicesId.Contains(postData.devicesId)); int count = all.Count(); string id = postData.devicesId + count; all = GetAllDevices().Where(c => c.devicesId == (id)); while (all.Count() != 0) { count += 1; id = postData.devicesId + count; all = GetAllDevices().Where(c => c.devicesId == (id)); // code block to be executed } string desc = ""; if (postData.desc != null) { desc = postData.desc; } var iso8601String = "00010101T00:00:00Z"; DateTime dateISO8602 = DateTime.ParseExact(iso8601String, "yyyyMMddTHH:mm:ssZ", System.Globalization.CultureInfo.InvariantCulture); //DateTime turnOnDate = Convert.ToDateTime(datestr); //new Date('0001-01-01 00:00:00'); //DateTime.UtcNow insertList.roomId = postData.roomId; insertList.devicesId = postData.devicesId + count; insertList.devices_Name = postData.devices_Name; insertList.pos_x = postData.pos_x; insertList.pos_y = postData.pos_y; insertList.desc = desc; insertList.lastest_checking_time = dateISO8602; insertList.turn_on_time = dateISO8602; insertList.power = 0; insertList.status = false; insertList.set_value = 0.0; try { new DBManger().DataBase.GetCollection <MongoDevicesListModel>("DEVICES_LIST").InsertOneAsync(insertList); } catch (Exception) { //Debug.WriteLine("line 262 error "); return(ReturnUrl()); }; return(ReturnUrl()); }
public ActionResult DropDevicesData(MongoDevicesListModel postData) //post { var DeleteResult = new DBManger().DataBase.GetCollection <MongoDevicesListModel>("DEVICES_LIST").DeleteOne(de => de.devicesId == postData.devicesId & de.roomId == postData.roomId); 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.devicesId.ToString())); } return(ReturnUrl()); }
public ActionResult UpdateDevicesData(MongoDevicesListModel postData) { //Debug.WriteLine("postData :==>>" + postData.ToJson().ToString()); var filter = Builders <MongoDevicesListModel> .Filter.Eq(d => d.devicesId, postData.devicesId); var type = postData.GetType(); var props = type.GetProperties(); foreach (var property in props) { if (!property.Name.Equals("_id")) { if (property.GetValue(postData) != null) { UpdateDefinition <MongoDevicesListModel> up; if (property.Name == "latest_checking_time") { up = Builders <MongoDevicesListModel> .Update.Set(property.Name.ToString(), DateTime.UtcNow); } else { up = Builders <MongoDevicesListModel> .Update.Set(property.Name.ToString(), property.GetValue(postData).ToString()); } //Debug.WriteLine("error: line 303 =>> " + property.Name + ":" + property.GetValue(postData)); var UpdateResult = new DBManger().DataBase.GetCollection <MongoDevicesListModel>("DEVICES_LIST").FindOneAndUpdateAsync(u => u.devicesId == postData.devicesId, up); if (UpdateResult == null) { return(ReturnUrl()); //Debug.WriteLine("error: line 306 =>> "+ property.Name); //throw new Exception(string.Format("Count [value:{0}] after Modifi is invalid")); } //this.isUpdated = Updated.IsAcknowledged; } } } return(ReturnUrl()); }