Example #1
0
        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("传感器修改失败")));
                }
            }
        }
Example #2
0
        /// <summary>
        /// 获取sensor实例
        /// </summary>
        /// <param name="sensor"></param>
        /// <returns></returns>
        private Entity.Config.Sensor GetSensor(T_DIM_SENSOR sensor)
        {
            using (var db = new SecureCloud_Entities())
            {
                if (sensor == null)
                {
                    return(null);
                }

                var sensinfo = (from p in db.T_DIM_SENSOR_PRODUCT
                                from s in db.T_DIM_PROTOCOL_TYPE
                                from f in db.T_DIM_SAFETY_FACTOR_TYPE
                                where p.PROTOCOL_ID == s.PROTOCOL_ID && p.PRODUCT_ID == sensor.PRODUCT_SENSOR_ID && f.SAFETY_FACTOR_TYPE_ID == sensor.SAFETY_FACTOR_TYPE_ID
                                select new { p.PRODUCT_ID, p.PRODUCT_CODE, p.FORMAULAID, s.PROTOCOL_CODE, f.THEMES_TABLE_NAME, f.THEMES_COLUMNS })
                               .FirstOrDefault();
                string dtucode = (from d in db.T_DIM_REMOTE_DTU
                                  where d.ID == sensor.DTU_ID
                                  select d.REMOTE_DTU_NUMBER).FirstOrDefault();
                if (sensinfo == null)
                {
                    return(null);
                }

                var sensorinfo = new Entity.Config.Sensor
                {
                    SensorID        = (uint)sensor.SENSOR_ID,
                    StructId        = sensor.STRUCT_ID == null ? 0 : (uint)sensor.STRUCT_ID,
                    DtuID           = sensor.DTU_ID == null ? 0 : (uint)sensor.DTU_ID,
                    DtuCode         = dtucode,
                    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,
                    ProtocolType    = Convert.ToUInt32(sensinfo.PROTOCOL_CODE),
                    FormulaID       = sensinfo.FORMAULAID == null ? 0 : (uint)sensinfo.FORMAULAID,
                    FactorType      = sensor.SAFETY_FACTOR_TYPE_ID == null ? 0 : (uint)sensor.SAFETY_FACTOR_TYPE_ID,
                    FactorTypeTable = sensinfo.THEMES_TABLE_NAME,
                    TableColums     = sensinfo.THEMES_COLUMNS,
                    ProductCode     = sensinfo.PRODUCT_CODE,
                    ProductId       = sensinfo.PRODUCT_ID,
                    SensorType      = (SensorType)sensor.Identification,
                    //TODO 3-3
                    UnEnable = sensor.Enable
                };
                if (sensinfo.FORMAULAID != null)
                {
                    var pname = (from pf in db.T_DIM_FORMULA_PARA
                                 from pn in db.T_DIM_FORMULA_PARA_NAME
                                 where pf.ParaNameID == pn.ParaNameID && pf.FormulaID == sensinfo.FORMAULAID
                                 orderby pf.Order
                                 select new { pf.FormulaID, pf.FormulaParaID, pn.ParaName, pn.ParaAlias, pf.Order }
                                 ).ToList();
                    //sensorinfo.ParamCount = (ushort)pname.Count;
                    T_DIM_FORMULAID_SET param0 = (from pf in db.T_DIM_FORMULAID_SET
                                                  where pf.SENSOR_ID == sensor.SENSOR_ID
                                                  select pf).ToList().FirstOrDefault();
                    if (param0 == null)
                    {
                        return(sensorinfo);
                    }
                    for (int i = 0; i < pname.Count; i++)
                    {
                        var f = new FormulaParam
                        {
                            FID   = (int)pname[i].FormulaID,
                            PID   = pname[i].FormulaParaID,
                            Index = (int)pname[i].Order,
                            Name  = pname[i].ParaName,
                            Alias = pname[i].ParaAlias
                        };
                        var sp = new SensorParam(f)
                        {
                            Value =
                                Convert.ToDouble(param0.GetType()
                                                 .GetProperty("Parameter" + (i + 1))
                                                 .GetValue(param0, null))
                        };
                        sensorinfo.AddParameter(sp);
                    }
                }
                //else
                //{
                //    sensorinfo.ParamCount = 0;
                //}

                return(sensorinfo);
            }
        }
Example #3
0
        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("传感器新增失败")));
                }
            }
        }