public HttpResponseMessage ModifySensor([FromUri] int sensorId, [FromBody] Sensor model) { using (var db = new SecureCloud_Entities()) { try { var sensor = db.T_DIM_SENSOR.FirstOrDefault(s => s.SENSOR_ID == sensorId && !s.IsDeleted); if (sensor == null) { return(Request.CreateResponse( HttpStatusCode.BadRequest, StringHelper.GetMessageString("传感器修改失败:传感器不存在或已删除"))); } int dtuid = sensor.DTU_ID.Value; StringBuilder sb = new StringBuilder(); var sensorLoc = sensor.SENSOR_LOCATION_DESCRIPTION; sb.AppendFormat("原传感器:{0}:", sensorLoc); if (model.FactorId != default(int) && model.FactorId != sensor.SAFETY_FACTOR_TYPE_ID) { sensor.SAFETY_FACTOR_TYPE_ID = model.FactorId; var fac = db.T_DIM_SAFETY_FACTOR_TYPE.Where(f => f.SAFETY_FACTOR_TYPE_ID == model.FactorId) .Select(f => f.SAFETY_FACTOR_TYPE_NAME) .FirstOrDefault(); sb.AppendFormat("监测因素改为:{0};", fac); } if (model.DtuId != default(int) && model.DtuId != sensor.DTU_ID) { sensor.DTU_ID = model.DtuId; var dtu = db.T_DIM_REMOTE_DTU.Where(d => d.ID == model.DtuId) .Select(d => d.REMOTE_DTU_NUMBER) .FirstOrDefault(); sb.AppendFormat("dtu号改为:{0};", dtu); } if (model.ModuleNo != default(int) && model.ModuleNo != sensor.MODULE_NO) { sensor.MODULE_NO = model.ModuleNo; sb.AppendFormat("模块号改为:{0};", model.ModuleNo); } if (model.Channel != null && model.Channel != sensor.DAI_CHANNEL_NUMBER) { sensor.DAI_CHANNEL_NUMBER = (byte?)model.Channel; sb.AppendFormat("通道改为:{0};", model.Channel); } //2-26 if (model.Enable != sensor.Enable) { sensor.Enable = model.Enable; sb.AppendFormat("使能改为:{0};", model.Enable); } if (model.ProductId != default(int) && model.ProductId != sensor.PRODUCT_SENSOR_ID) { sensor.PRODUCT_SENSOR_ID = model.ProductId; var pdt = db.T_DIM_SENSOR_PRODUCT.Where(p => p.PRODUCT_ID == model.ProductId) .Select(p => new { p.PRODUCT_NAME, p.PRODUCT_CODE }) .FirstOrDefault(); sb.AppendFormat("设备改为{0}({1});", pdt.PRODUCT_NAME, pdt.PRODUCT_CODE); } if (model.Location != default(string) && model.Location != sensor.SENSOR_LOCATION_DESCRIPTION) { sensor.SENSOR_LOCATION_DESCRIPTION = model.Location; sb.AppendFormat("位置标识改为{0};", model.Location); } if (model.Params != null) { var query = (from p in db.T_DIM_SENSOR_PRODUCT join fn in db.T_DIM_FORMULA_PARA on p.FORMAULAID equals fn.FormulaID into forluma from f in forluma.DefaultIfEmpty() join fname in db.T_DIM_FORMULA_PARA_NAME on f.ParaNameID equals fname.ParaNameID into name from n in name where p.PRODUCT_ID == model.ProductId orderby f.Order select new { f.FormulaParaID, n.ParaAlias }).ToList(); var para = (from q in query from v in model.Params where q.FormulaParaID == v.Id select new { q.FormulaParaID, v.Value }).ToList(); var paramStr = (from q in query from v in model.Params where q.FormulaParaID == v.Id select new { q.ParaAlias, v.Value }).ToList(); sb.AppendFormat( "参数修改为:{0}", string.Join( "-", paramStr.Select(p => string.Format("{0}:{1}", p.ParaAlias, p.Value)).ToArray())); var old = from o in db.T_DIM_FORMULAID_SET where o.SENSOR_ID == sensor.SENSOR_ID select o; foreach (var o in old) { db.Entry(o).State = System.Data.EntityState.Deleted; } var newParam = new T_DIM_FORMULAID_SET(); newParam.SENSOR_ID = sensor.SENSOR_ID; for (int i = 0; i < para.Count(); i++) { newParam.GetType() .GetProperty("FormulaParaID" + (i + 1)) .SetValue(newParam, para[i].FormulaParaID, null); newParam.GetType() .GetProperty("Parameter" + (i + 1)) .SetValue(newParam, (decimal?)para[i].Value, null); } db.Entry(newParam).State = System.Data.EntityState.Added; } //关联传感器 if (model.CorrentId != null) { var correntTable = from cp in db.T_DIM_SENSOR_CORRENT where cp.SensorId == sensorId select cp; foreach (var o in correntTable) { db.T_DIM_SENSOR_CORRENT.Remove(o); } var array = model.CorrentId.Split(','); for (int j = 0; j < array.Length; j++) { var correntSensor = new T_DIM_SENSOR_CORRENT(); correntSensor.SensorId = sensorId; var correntId = array.GetValue(j); correntSensor.CorrentSensorId = Convert.ToInt32(correntId); db.T_DIM_SENSOR_CORRENT.Add(correntSensor); db.SaveChanges(); } } #region 日志信息 this.Request.Properties["ActionParameter"] = JsonConvert.SerializeObject(model); this.Request.Properties["ActionParameterShow"] = sb.ToString(); #endregion db.SaveChanges(); if (sensor.Identification != 2) { Entity.Config.Sensor sensorinfo = GetSensor(sensor); var senopera = new SensorOperation { Sensor = sensorinfo, OldDtuId = (uint)dtuid, OldSensorId = sensorinfo.SensorID, Action = sensor.DTU_ID == dtuid ? Operations.Update : Operations.ChangedDtu }; WebClientService.SendToET(ConfigChangedMsgHelper.GetSensorConfigChangedMsg(senopera)); } return(Request.CreateResponse(HttpStatusCode.Accepted, StringHelper.GetMessageString("传感器修改成功"))); } catch (NullReferenceException e) { return(Request.CreateResponse( HttpStatusCode.BadRequest, StringHelper.GetMessageString("传感器修改失败:参数无效"))); } catch (Exception e) { return(Request.CreateResponse(HttpStatusCode.BadRequest, StringHelper.GetMessageString("传感器修改失败"))); } } }
public HttpResponseMessage RemoveSensor([FromUri] int sensorId) { using (var db = new SecureCloud_Entities()) { var sensor = db.T_DIM_SENSOR.FirstOrDefault(s => s.SENSOR_ID == sensorId && !s.IsDeleted); if (sensor == null) { return(Request.CreateResponse(HttpStatusCode.BadRequest, StringHelper.GetMessageString("传感器不存在"))); } sensor.MODULE_NO *= -1; sensor.DAI_CHANNEL_NUMBER = (byte)(sensor.DAI_CHANNEL_NUMBER * -1); sensor.IsDeleted = true; //表T_DIM_SENSOR_CORRENT的同步变动 IQueryable <T_DIM_SENSOR_CORRENT> Corrent = from p in db.T_DIM_SENSOR_CORRENT where p.SensorId == sensorId || p.CorrentSensorId == sensorId//反向删除 select p; foreach (var CorrentConfig in Corrent) { db.T_DIM_SENSOR_CORRENT.Remove(CorrentConfig); } #region 日志信息 this.Request.Properties["ActionParameterShow"] = "传感器位置:" + sensor.SENSOR_LOCATION_DESCRIPTION; #endregion try { if (sensor.Identification != 2) { var sensorinfo = new Entity.Config.Sensor { DtuID = sensor.DTU_ID == null ? 0 : (uint)sensor.DTU_ID, SensorID = (uint)sensor.SENSOR_ID, StructId = sensor.STRUCT_ID == null ? 0 : (uint)sensor.STRUCT_ID, ModuleNo = sensor.MODULE_NO == null ? 0 : (uint)sensor.MODULE_NO, ChannelNo = sensor.DAI_CHANNEL_NUMBER == null ? 0 : (uint)sensor.DAI_CHANNEL_NUMBER, Name = sensor.SENSOR_LOCATION_DESCRIPTION, UnEnable = sensor.Enable//3-3 }; var senopera = new SensorOperation { Action = Operations.Delete, OldDtuId = sensorinfo.DtuID, OldSensorId = sensorinfo.SensorID, Sensor = sensorinfo }; WebClientService.SendToET(ConfigChangedMsgHelper.GetSensorConfigChangedMsg(senopera)); } db.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.Accepted, StringHelper.GetMessageString("传感器删除成功"))); } catch (Exception e) { return(Request.CreateResponse(HttpStatusCode.BadRequest, StringHelper.GetMessageString("传感器删除失败"))); } } }
public HttpResponseMessage AddSensor([FromBody] Sensor model) { using (var db = new SecureCloud_Entities()) { try { var sensor = new T_DIM_SENSOR(); sensor.STRUCT_ID = model.StructId; sensor.SAFETY_FACTOR_TYPE_ID = model.FactorId; sensor.DTU_ID = model.DtuId; sensor.MODULE_NO = model.ModuleNo; sensor.DAI_CHANNEL_NUMBER = (byte?)model.Channel; sensor.PRODUCT_SENSOR_ID = model.ProductId; sensor.SENSOR_LOCATION_DESCRIPTION = model.Location; sensor.IsDeleted = false; sensor.Identification = model.Identify; sensor.Enable = model.Enable; var entry = db.Entry(sensor); entry.State = System.Data.EntityState.Added; var query = (from p in db.T_DIM_SENSOR_PRODUCT join fn in db.T_DIM_FORMULA_PARA on p.FORMAULAID equals fn.FormulaID into forluma from f in forluma.DefaultIfEmpty() join fname in db.T_DIM_FORMULA_PARA_NAME on f.ParaNameID equals fname.ParaNameID into name from n in name where p.PRODUCT_ID == model.ProductId orderby f.Order select f.FormulaParaID).ToList(); var para = (from q in query from v in model.Params where q == v.Id select new { q, v.Value }).ToList(); var old = from o in db.T_DIM_FORMULAID_SET where o.SENSOR_ID == sensor.SENSOR_ID select o; foreach (var o in old) { db.Entry(o).State = System.Data.EntityState.Deleted; } var newParam = new T_DIM_FORMULAID_SET(); newParam.SENSOR_ID = sensor.SENSOR_ID; for (int i = 0; i < para.Count(); i++) { newParam.GetType().GetProperty("FormulaParaID" + (i + 1)).SetValue(newParam, para[i].q, null); newParam.GetType().GetProperty("Parameter" + (i + 1)).SetValue(newParam, (decimal?)para[i].Value, null); } db.Entry(newParam).State = System.Data.EntityState.Added; if (model.CorrentId != null) { var correntSensor = new T_DIM_SENSOR_CORRENT(); var array = model.CorrentId.Split(','); for (int j = 0; j < array.Length; j++) { correntSensor.SensorId = sensor.SENSOR_ID; var correntId = array.GetValue(j); correntSensor.CorrentSensorId = Convert.ToInt32(correntId); db.T_DIM_SENSOR_CORRENT.Add(correntSensor); db.SaveChanges(); } } #region 日志信息 var fac = db.T_DIM_SAFETY_FACTOR_TYPE.Where(f => f.SAFETY_FACTOR_TYPE_ID == model.FactorId) .Select(f => f.SAFETY_FACTOR_TYPE_NAME) .FirstOrDefault(); var dtu = db.T_DIM_REMOTE_DTU.Where(d => d.ID == model.DtuId) .Select(d => d.REMOTE_DTU_NUMBER) .FirstOrDefault(); var pdt = db.T_DIM_SENSOR_PRODUCT.Where(p => p.PRODUCT_ID == model.ProductId) .Select(p => new { p.PRODUCT_NAME, p.PRODUCT_CODE }) .FirstOrDefault(); this.Request.Properties["ActionParameter"] = JsonConvert.SerializeObject(model); this.Request.Properties["ActionParameterShow"] = string.Format( "位置:{0},监测因素:{1},dtu:{2},模块号:{3},通道号:{4},设备类型:{5},参数:{6}", string.IsNullOrEmpty(model.Location) ? string.Empty : model.Location, fac ?? string.Empty, dtu ?? string.Empty, model.ModuleNo, model.Channel, pdt == null ? string.Empty : string.Format("{0}({1})", pdt.PRODUCT_NAME, pdt.PRODUCT_CODE), model.Params == null ? string.Empty : string.Join(",", model.Params.Select(p => p.Value))); #endregion db.SaveChanges(); if (sensor.Identification != 2) { Entity.Config.Sensor sensorinfo = GetSensor(sensor); var senopera = new SensorOperation { Action = Operations.Add, Sensor = sensorinfo, OldDtuId = sensorinfo.DtuID, OldSensorId = sensorinfo.SensorID }; WebClientService.SendToET(ConfigChangedMsgHelper.GetSensorConfigChangedMsg(senopera)); } return(Request.CreateResponse(HttpStatusCode.Accepted, StringHelper.GetMessageString("传感器新增成功"))); } catch (NullReferenceException e) { return(Request.CreateResponse( HttpStatusCode.BadRequest, StringHelper.GetMessageString("传感器新增失败:参数无效"))); } catch (Exception e) { return(Request.CreateResponse(HttpStatusCode.BadRequest, StringHelper.GetMessageString("传感器新增失败"))); } } }