/// <summary> /// 增加一条数据 /// </summary> public int Add(Models.location_log model) { StringBuilder strSql = new StringBuilder(); strSql.Append("insert into tb_location_log(actual_coord,location_coord,room_id,mobile_id,location_algorithm,location_time,flag,mobile_mac)"); strSql.Append(" values (@actual_coord,@location_coord,@room_id,@mobile_id,@location_algorithm,@location_time,@flag,@mobile_mac)"); SqlParameter[] parameters = { new SqlParameter("@actual_coord", SqlDbType.VarChar, 10), new SqlParameter("@location_coord", SqlDbType.VarChar, 10), new SqlParameter("@room_id", SqlDbType.Int), new SqlParameter("@mobile_id", SqlDbType.Int), new SqlParameter("@location_algorithm", SqlDbType.Int), new SqlParameter("@location_time", SqlDbType.VarChar, 50), new SqlParameter("@flag", SqlDbType.Int), new SqlParameter("@mobile_mac", SqlDbType.VarChar, 20) }; parameters[0].Value = model.actual_coord; parameters[1].Value = model.location_coord; parameters[2].Value = model.room_id; parameters[3].Value = model.mobile_id; parameters[4].Value = model.location_algorithm; parameters[5].Value = model.location_time; parameters[6].Value = model.flag; parameters[7].Value = model.mobile_mac; object obj = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters); model.id = Convert.ToInt32(obj); return(model.id); }
/// <summary> /// 得到一个对象实体 /// </summary> public Models.location_log GetModel(int id) { StringBuilder strSql = new StringBuilder(); strSql.Append("select top 1 id,model_name"); strSql.Append(" from tb_location_log"); strSql.Append(" where id=@id"); SqlParameter[] parameters = { new SqlParameter("@id", SqlDbType.Int, 4) }; parameters[0].Value = id; Models.location_log model = new Models.location_log(); DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters); if (ds.Tables[0].Rows.Count > 0) { return(DataRowToModel(ds.Tables[0].Rows[0])); } else { return(null); } }
///<summary> /// 将对象转换为实体 /// </summary> private Models.location_log DataRowToModel(DataRow row) { Models.location_log model = new Models.location_log(); if (row != null) { if (row["id"] != null && row["id"].ToString() != "") { model.id = int.Parse(row["id"].ToString()); } } return(model); }
{ // POST /api/DataTest/ public HttpResponseMessage Post([FromBody] JObject value) { var response = Request.CreateResponse(); var room_id = value["room_id"].ToString(); var mobile_id = value["mobile_id"].ToString(); var algorithm = value["algorithm"].ToString(); JToken ap = new JObject(); ap = value["ap"]; var macList = ""; var rssiDictionary = new Dictionary <string, int>(); if (ap.Count() > 0) { IEnumerable <JToken> temp = ap.Values(); foreach (var item in temp) { try { macList += "'" + item["mac"].ToString() + "',"; rssiDictionary.Add(item["mac"].ToString(), int.Parse(item["rssi"].ToString())); } catch (Exception) { response.Content = new StringContent("请求数据错误!", Encoding.UTF8); return(response); } } } macList = macList.Substring(0, macList.Length - 1); DB.rssi rssiDb = new rssi(); string where = ""; if (algorithm == "0") { where = "room_id = " + room_id + " and mobile_id = " + mobile_id + " and mac in(" + macList + ")"; } else { where = "room_id = " + room_id + " and mobile_id = " + mobile_id; } DataSet ds = rssiDb.GetRssiList(where); var allRssiList = new Dictionary <int, DataTable>(); if (ds != null) { allRssiList = convertAllRssiList(ds); } Location location = new Location(); var result = ""; var k = 4; // k的取值 if (algorithm == "0") { result = location.KNNalgorithm(k, rssiDictionary, allRssiList); } else { result = location.MHJalgorithm(k, rssiDictionary, allRssiList); } DB.location_log locationLogDb = new DB.location_log(); Models.location_log locationLogModel = new Models.location_log(); locationLogModel.location_coord = result; locationLogModel.actual_coord = value["actual_coord"].ToString(); locationLogModel.room_id = int.Parse(value["room_id"].ToString()); locationLogModel.mobile_id = int.Parse(value["mobile_id"].ToString()); locationLogModel.mobile_mac = value["mobile_mac"].ToString(); locationLogModel.flag = k; locationLogModel.location_algorithm = int.Parse(algorithm); locationLogModel.location_time = GetTimeStamp(); if (locationLogDb.Add(locationLogModel) > 0) { response.Content = new StringContent(result, Encoding.UTF8); } else { response.Content = new StringContent(result + "(定位记录添加失败)", Encoding.UTF8); } return(response); }
/// <summary> /// 更新一条数据 /// </summary> public bool Update(Models.location_log model) { return(true); }