public void Init(Dictionary <string, string> pms) { this.DtuCode = 20120049; string com = pms["PortName"]; DtuNode node = new DtuNode(); node.DtuCode = com; node.AddProperty("serial", new SerialPort { PortName = com, BaudRate = Convert.ToInt32(pms["BaudRate"]), Parity = (Parity)Convert.ToInt32(pms["Parity"]), DataBits = Convert.ToInt32(pms["DataBits"]), StopBits = (StopBits)Convert.ToInt32(pms["StopBits"]), ReadTimeout = Convert.ToInt32(pms["ReadTimeOut"]) }); this._dtuConnection = new ComDtuConnection(node); }
public void TestSnGpsTask() { if (System.IO.File.Exists("lastAcqDate.dat")) { System.IO.File.Delete("lastAcqDate.dat"); } (new Gps_SN_SensorAdapter() as GpsBaseAdapter).UpdateSensorLastDataAcqTime(1, new DateTime(2015, 1, 28, 23, 51, 35)); var adapterManager = SensorAdapterManager.InitializeManager(); var taskexcutor = new DACTaskExecutor(adapterManager); var dtusens = new List <uint>(); dtusens.Add(1); var task = new DACTask("1", 100, dtusens, TaskType.TIMED, null); var dtunode = new DtuNode { DtuId = 100, Type = DtuType.File, NetworkType = NetworkType.hclocal }; var s = new Sensor { SensorID = 1, ModuleNo = 9003, ProtocolType = 9403, ChannelNo = 1, TableColums = "SURFACE_DISPLACEMENT_X_VALUE,SURFACE_DISPLACEMENT_Y_VALUE,SURFACE_DISPLACEMENT_Z_VALUE" }; s.AddParameter(new SensorParam(null) { Value = 2 }); s.AddParameter(new SensorParam(null) { Value = 2 }); s.AddParameter(new SensorParam(null) { Value = 2 }); s.AddParameter(new SensorParam(null) { Value = 0 }); dtunode.AddSensor(s); dtunode.AddProperty("param1", filepath); var conn = new FileDtuConnection(dtunode); var contxt = new DacTaskContext() { Node = dtunode, DtuConnection = conn }; var dactaskresult = taskexcutor.Run(task, contxt); Assert.IsNotNull(dactaskresult); Assert.IsTrue(dactaskresult.Task.Status == DACTaskStatus.DONE); var sensorresults = dactaskresult.SensorResults; Assert.NotNull(sensorresults); Assert.IsNotEmpty(sensorresults); Assert.AreEqual(101, sensorresults.Count); var senres = sensorresults[0]; Assert.IsTrue(senres.IsOK); var sendata = senres.Data; Assert.IsTrue(sendata is Gps3dData); Assert.AreEqual(sendata.RawValues[0], 4435175.4523 * 1000, 0.0000001); Assert.AreEqual(sendata.RawValues[1], 524121.0006 * 1000, 0.0000001); Assert.AreEqual(sendata.RawValues[2], 82.2112 * 1000, 0.0000001); Assert.AreEqual(sendata.ThemeValues[0].Value, 4435175.4523 * 1000 - 2, 0.0000001); Assert.AreEqual(sendata.ThemeValues[1].Value, 524121.0006 * 1000 - 2, 0.0000001); Assert.AreEqual(sendata.ThemeValues[2].Value, 82.2112 * 1000 - 2, 0.0000001); }
/// <summary> /// 获取DTU列表. /// </summary> /// <param name="dtuCode">要查询的DTU编码</param> /// <param name="networkType">网络类型</param> /// <returns></returns> public IList <DtuNode> QueryDtuNodes(string dtuCode = null, NetworkType?networkType = null) { List <DtuNode> dtus = new List <DtuNode>(); DataSet ds = this._helper.Query(string.Format(@" select D.[ID] id, D.[REMOTE_DTU_NUMBER] code, D.[REMOTE_DTU_SUBSCRIBER] subscriber, D.[REMOTE_DTU_GRANULARITY] interval, D.[DESCRIPTION] as [desc] , D.[ProductDtuId], P.[NetworkType], D.[P1], D.[P2], D.[P3], D.[P4], sd.StructureId sid from T_DIM_REMOTE_DTU D left join [T_DIM_DTU_PRODUCT] P on P.ProductId = D.ProductDtuId left join [T_DIM_STRUCT_DTU] sd on sd.DtuId = D.ID where D.ProductDtuId !=3 {0} {1} order by ID", string.IsNullOrEmpty(dtuCode)? "": string.Format("and D.REMOTE_DTU_NUMBER={0}", dtuCode), networkType == null ? "": string.Format("and P.NetworkType='{0}'", networkType) )); if (ds != null) { foreach (DataRow dr in ds.Tables[0].Rows) { try { NetworkType nwType; bool success = Enum.TryParse(Convert.ToString(dr["NetworkType"]), out nwType); DtuNode di = new DtuNode { DtuId = (uint)Convert.ToInt32(dr["id"]), //StructId = dr.IsNull("sid") ? 0 : Convert.ToUInt32(dr["sid"]), DtuCode = Convert.ToString(dr["code"]), DacInterval = dr.IsNull("interval") ? DtuNode.DefaultDacInterval : (uint)(Convert.ToInt32(dr["interval"]) * 60), Name = Convert.ToString(dr["desc"]), NetworkType = success ? (NetworkType?)nwType : null }; di.AddProperty("param1", Convert.ToString(dr["P1"])); di.AddProperty("param2", Convert.ToString(dr["P2"])); di.AddProperty("param3", Convert.ToString(dr["P3"])); di.AddProperty("param4", Convert.ToString(dr["P4"])); dtus.Add(di); } catch (Exception ex) { Log.WarnFormat("DTU :{0}-{1} error:{2}", Convert.ToInt32(dr["id"]), Convert.ToString(dr["code"]), ex.Message); } } } ds = this._helper.Query(@" select s.STRUCT_ID structId, s.DTU_ID dtuId, s.SENSOR_ID sid, s.MODULE_NO mno, s.DAI_CHANNEL_NUMBER cno, s.SAFETY_FACTOR_TYPE_ID factor, s.PRODUCT_SENSOR_ID pid, s.Identification sensortype, s.Enable enable, protocol.PROTOCOL_CODE protocol_type, s.SENSOR_LOCATION_DESCRIPTION name, factorType.THEMES_TABLE_NAME factorTypeTable, factorType.THEMES_COLUMNS tableColums, product.PRODUCT_ID productId, product.PRODUCT_CODE productCode from T_DIM_SENSOR s ,dbo.T_DIM_SENSOR_PRODUCT product, dbo.T_DIM_PROTOCOL_TYPE protocol,dbo.T_DIM_SAFETY_FACTOR_TYPE factorType where s.PRODUCT_SENSOR_ID = product.PRODUCT_ID AND product.PROTOCOL_ID = protocol.PROTOCOL_ID AND s.SAFETY_FACTOR_TYPE_ID = factorType.SAFETY_FACTOR_TYPE_ID AND s.MODULE_NO is not null AND s.IsDeleted = 0 order by SENSOR_ID"); DataTable table = ds.Tables[0]; var formulaParamDict = this.GetFormulaParamDict(); // 获取参数定义 DataSet paramSet = this._helper.Query(@"select * from T_DIM_FORMULAID_SET"); //公式配置。 DataTable paramTable = paramSet.Tables[0]; foreach (DtuNode dtu in dtus) { if (dtu == null) { continue; } // filter to Node var sr = from r in table.AsEnumerable() where !r.IsNull("dtuId") && r.Field <int>("dtuId") == dtu.DtuId orderby r["sid"] select r; if (!sr.Any()) { continue; } foreach (DataRow row in sr) { try { var sensor = new Sensor { DtuID = dtu.DtuId, DtuCode = dtu.DtuCode, StructId = Convert.ToUInt32(row["structId"]), SensorID = Convert.ToUInt32(row["sid"]), ChannelNo = row.IsNull("cno") ? 1 : Convert.ToUInt32(row["cno"]), ModuleNo = row.IsNull("mno") ? 0 : Convert.ToUInt32(row["mno"]), ProtocolType = Convert.ToUInt16(row["protocol_type"]), Name = Convert.ToString(row["name"]), FactorType = Convert.ToUInt32(row["factor"]), FactorTypeTable = Convert.ToString(row["factorTypeTable"]), TableColums = Convert.ToString(row["tableColums"]), ProductId = Convert.ToInt32(row["productId"]), ProductCode = row["productCode"].ToString().Trim(), AcqInterval = dtu.DacInterval / 60, // 分钟 LastTime = DateTime.MinValue, SensorType = (SensorType)Convert.ToByte(row["sensortype"]), UnEnable = Convert.ToBoolean(row["enable"]) }; this.GetParameters(paramTable, sensor, formulaParamDict); if (sensor.Parameters.Count > 0) { sensor.FormulaID = (uint)sensor.Parameters[0].FormulaParam.FID; } dtu.AddSensor(sensor); } catch (Exception ex) { Log.WarnFormat("Sensor :{0} error:{1}", Convert.ToUInt32(row["sid"]), ex.Message); } } } return(dtus); }
public HttpResponseMessage Remove(int dtuId, int structId) { using (var entity = new SecureCloud_Entities()) { var dtuEntity = entity.T_DIM_REMOTE_DTU.FirstOrDefault(d => d.ID == dtuId); if (dtuEntity == null || dtuEntity.REMOTE_DTU_STATE == false) { return(Request.CreateResponse( HttpStatusCode.BadRequest, StringHelper.GetMessageString("DTU不存在或已禁用"))); } // 检查传感器关联 var sens = entity.T_DIM_SENSOR.Where(s => s.DTU_ID == dtuId && !s.IsDeleted); if (sens.Any()) { return(Request.CreateResponse( HttpStatusCode.Conflict, StringHelper.GetMessageString("请先删除该DTU下的传感器"))); } // 删除关联 var sd = entity.T_DIM_STRUCT_DTU.Where(d => d.DtuId == dtuId && d.StructureId == structId); foreach (var m in sd) { entity.T_DIM_STRUCT_DTU.Remove(m); } // 检查关联 if (!entity.T_DIM_STRUCT_DTU.Any(d => d.DtuId == dtuId)) { dtuEntity.REMOTE_DTU_STATE = false; } #region 日志信息 var info = (from d in entity.T_DIM_REMOTE_DTU from s in entity.T_DIM_STRUCT_DTU from st in entity.T_DIM_STRUCTURE where d.ID == s.DtuId && d.ID == dtuId && s.StructureId == st.ID select new { d.REMOTE_DTU_NUMBER, st.STRUCTURE_NAME_CN }).FirstOrDefault(); var dtu = info == null ? string.Empty : info.REMOTE_DTU_NUMBER; var stc = info == null ? string.Empty : info.STRUCTURE_NAME_CN; this.Request.Properties["ActionParameterShow"] = string.Format("dtu号:{0}, 所属结构物:{1}", dtu, stc); #endregion try { var dtnod = new DtuNode { DtuId = (uint)dtuEntity.ID, Type = DtuType.Gprs, DtuCode = dtuEntity.REMOTE_DTU_NUMBER, Name = dtuEntity.DESCRIPTION, DacInterval = dtuEntity.REMOTE_DTU_GRANULARITY == null ? DtuNode.DefaultDacInterval : (uint)(dtuEntity.REMOTE_DTU_GRANULARITY * 60), NetworkType = dtuEntity.ProductDtuId == 2 ? NetworkType.hclocal : NetworkType.gprs }; if (dtnod.NetworkType == NetworkType.hclocal) { dtnod.AddProperty("param1", dtuEntity.P1); } entity.SaveChanges(); WebClientService.SendToET(ConfigChangedMsgHelper.GetDtuConfigChangedMsg(ChangedStatus.Delete, dtnod)); return(Request.CreateResponse( HttpStatusCode.Accepted, StringHelper.GetMessageString("DTU删除成功"))); } catch (Exception ex) { return(Request.CreateResponse( HttpStatusCode.BadRequest, StringHelper.GetMessageString("DTU删除失败"))); } } }
public HttpResponseMessage Modify([FromUri] int dtuId, [FromBody] DtuModel dtu) { using (var entity = new SecureCloud_Entities()) { var dtuEntity = entity.T_DIM_REMOTE_DTU.FirstOrDefault(d => d.ID == dtuId); if (dtuEntity == null || dtuEntity.REMOTE_DTU_STATE == false) { return(Request.CreateResponse( HttpStatusCode.BadRequest, StringHelper.GetMessageString("DTU不存在或已禁用"))); } StringBuilder sb = new StringBuilder(); string dtucode = dtuEntity.REMOTE_DTU_NUMBER; if (dtu.DtuNo != default(string) && dtu.DtuNo != dtuEntity.REMOTE_DTU_NUMBER) { dtuEntity.REMOTE_DTU_NUMBER = dtu.DtuNo; sb.AppendFormat("Dtu编号改为:{0},", dtu.DtuNo); } if (dtu.Granularity != default(int) && dtu.Granularity != dtuEntity.REMOTE_DTU_GRANULARITY) { dtuEntity.REMOTE_DTU_GRANULARITY = (short)dtu.Granularity; sb.AppendFormat("采集间隔改为:{0},", dtu.Granularity); } if (dtu.Sim != dtuEntity.REMOTE_DTU_SUBSCRIBER) { dtuEntity.REMOTE_DTU_SUBSCRIBER = dtu.Sim; sb.AppendFormat("sim卡号改为:{0},", dtu.Sim); } if (dtu.Ip != dtuEntity.DTU_IP) { dtuEntity.DTU_IP = dtu.Ip; sb.AppendFormat("Ip改为:{0},", dtu.Ip); } if (dtu.Port != dtuEntity.DTU_PORT) { dtuEntity.DTU_PORT = dtu.Port; sb.AppendFormat("端口改为:{0},", dtu.Port); } if (dtu.ProductId != default(int) && dtu.ProductId != dtuEntity.ProductDtuId) { dtuEntity.ProductDtuId = dtu.ProductId; var product = entity.T_DIM_DTU_PRODUCT.FirstOrDefault(p => p.ProductId == dtu.ProductId); if (product != null) { sb.AppendFormat("产品厂商改为{0},型号改为:{1}", product.DtuFactory, product.DtuModel); } else { sb.AppendFormat("产品id改为{0}", dtu.ProductId); } } if (dtu.P1 != dtuEntity.P1) { dtuEntity.P1 = dtu.P1; sb.AppendFormat("参数1改为:{0}", dtu.P1); } if (dtu.P2 != dtuEntity.P2) { dtuEntity.P2 = dtu.P2; sb.AppendFormat("参数2改为:{0}", dtu.P1); } if (dtu.P3 != dtuEntity.P3) { dtuEntity.P3 = dtu.P3; sb.AppendFormat("参数3改为:{0}", dtu.P1); } if (dtu.P4 != dtuEntity.P4) { dtuEntity.P4 = dtu.P4; sb.AppendFormat("参数4改为:{0}", dtu.P1); } #region 日志信息 this.Request.Properties["ActionParameter"] = JsonConvert.SerializeObject(dtu); this.Request.Properties["ActionParameterShow"] = string.Format("dtu号:{0}:{1}", dtu.DtuNo ?? string.Empty, sb); #endregion try { entity.SaveChanges(); var dtnod = new DtuNode { DtuId = (uint)dtuEntity.ID, Type = DtuType.Gprs, DtuCode = dtuEntity.REMOTE_DTU_NUMBER, NetworkType = dtuEntity.ProductDtuId == 2 ? NetworkType.hclocal : NetworkType.gprs, Name = dtuEntity.DESCRIPTION, DacInterval = dtuEntity.REMOTE_DTU_GRANULARITY == null ? DtuNode.DefaultDacInterval : (uint)dtuEntity.REMOTE_DTU_GRANULARITY }; if (dtnod.NetworkType == NetworkType.hclocal) { dtnod.AddProperty("param1", dtuEntity.P1); } WebClientService.SendToET(ConfigChangedMsgHelper.GetDtuConfigChangedMsg(ChangedStatus.Modify, dtnod, dtucode)); return(Request.CreateResponse( HttpStatusCode.Accepted, StringHelper.GetMessageString("DTU信息修改成功"))); } catch (Exception ex) { return(Request.CreateResponse( HttpStatusCode.BadRequest, StringHelper.GetMessageString("DTU信息修改失败"))); } } }
public HttpResponseMessage Add([FromBody] DtuModel dtu) { using (var entity = new SecureCloud_Entities()) { // 新增dtu var dtuEntity = new T_DIM_REMOTE_DTU(); dtuEntity.REMOTE_DTU_NUMBER = dtu.DtuNo; dtuEntity.REMOTE_DTU_SUBSCRIBER = dtu.Sim; dtuEntity.REMOTE_DTU_GRANULARITY = (short)dtu.Granularity; dtuEntity.DTU_IP = dtu.Ip; dtuEntity.DTU_PORT = dtu.Port; dtuEntity.P1 = dtu.P1; dtuEntity.P2 = dtu.P2; dtuEntity.P3 = dtu.P3; dtuEntity.P4 = dtu.P4; dtuEntity.ProductDtuId = dtu.ProductId; dtuEntity.REMOTE_DTU_STATE = true; var entry = entity.Entry(dtuEntity); entry.State = System.Data.EntityState.Added; // 添加关联关系 var sdEntity = new T_DIM_STRUCT_DTU(); sdEntity.DtuId = dtuEntity.ID; sdEntity.StructureId = dtu.StructId; var entry2 = entity.Entry(sdEntity); entry2.State = System.Data.EntityState.Added; #region 日志信息 var stc = entity.T_DIM_STRUCTURE.FirstOrDefault(s => s.ID == dtu.StructId); var product = entity.T_DIM_DTU_PRODUCT.FirstOrDefault(p => p.ProductId == dtu.ProductId); var paramName1 = (product != null && product.NetworkType.ToLower().Contains("local")) ? "文件路径" : "参数1"; this.Request.Properties["ActionParameter"] = JsonConvert.SerializeObject(dtu); this.Request.Properties["ActionParameterShow"] = string.Format( "dtu号:{0},结构物:{1},采集粒度:{3},产品厂商:{6},产品型号:{7},sim卡号:{2},ip地址:{4},端口:{5},[{12}:{8},参数2:{9},参数3:{10},参数4:{11}]", dtu.DtuNo ?? string.Empty, stc == null ? string.Empty : stc.STRUCTURE_NAME_CN, dtu.Sim ?? string.Empty, dtu.Granularity, dtu.Ip, dtu.Port, product != null ? product.DtuFactory : string.Empty, product != null ? product.DtuModel : string.Empty, dtu.P1, dtu.P2, dtu.P3, dtu.P4, paramName1); #endregion try { entity.SaveChanges(); var dtnod = new DtuNode { DtuId = (uint)dtuEntity.ID, Type = DtuType.Gprs, DtuCode = dtuEntity.REMOTE_DTU_NUMBER, Name = dtuEntity.DESCRIPTION, DacInterval = dtuEntity.REMOTE_DTU_GRANULARITY == null ? DtuNode.DefaultDacInterval : (uint)(dtuEntity.REMOTE_DTU_GRANULARITY * 60), NetworkType = dtuEntity.ProductDtuId == 2 ? NetworkType.hclocal : NetworkType.gprs }; if (dtnod.NetworkType == NetworkType.hclocal) { dtnod.AddProperty("param1", dtuEntity.P1); } WebClientService.SendToET(ConfigChangedMsgHelper.GetDtuConfigChangedMsg(ChangedStatus.Add, dtnod)); return(Request.CreateResponse( HttpStatusCode.Accepted, StringHelper.GetMessageString("添加DTU成功"))); } catch (Exception ex) { return(Request.CreateResponse( HttpStatusCode.BadRequest, StringHelper.GetMessageString("添加DTU失败"))); } } }
public IList <DtuNode> QueryDtuNodes(string dtuCode = null, NetworkType?networkType = null) { List <DtuNode> dtus = new List <DtuNode>(); DataSet ds = _cfgHelper.Query(string.Format(@" select ID id, PortName code, CircleIntervalTime as 'interval', [BaudRate], [Parity], [DataBits], [StopBits], [ReadTimeOut] from PortConfig {0} order by ID", dtuCode != null ? string.Format("where PortName='{0}'", dtuCode) : "")); if (ds != null) { foreach (DataRow dr in ds.Tables[0].Rows) { DtuNode di = new DtuNode { DtuId = (uint)Convert.ToInt32(dr["id"]), DtuCode = Convert.ToString(dr["code"]), DacInterval = dr.IsNull("interval") ? DtuNode.DefaultDacInterval : (uint)(Convert.ToInt32(dr["interval"]) * 60), Name = "", // Column Not Exist. Type = DtuType.Com }; di.AddProperty("serial", new SerialPort { PortName = di.DtuCode, BaudRate = Convert.ToInt32(dr["BaudRate"]), Parity = (Parity)Convert.ToInt32(dr["Parity"]), DataBits = Convert.ToInt32(dr["DataBits"]), StopBits = (StopBits)Convert.ToInt32(dr["StopBits"]), ReadTimeout = Convert.ToInt32(dr["ReadTimeOut"]) }); dtus.Add(di); } } // All Sensors ds = _cfgHelper.Query(@" select d.IP_DTU_SERIALPORT dtuId, s.SENSOR_SET_ID sid, d.MODULE_NO mno, s.CHANNEL_ID cno, s.SafeTypeId factor, s.SENSOR_PRODUCT_ID pid, product.PROTOCOL_ID protocol_type, s.SENSORLOCATION_DESCRIPTION name, 'DONOTUSE' factorTypeTable, 'DONOTUSE' tableColums from S_SENSOR_SET s , C_DAI_PRODUCT product, SAFETY_FACTOR_TYPE factorType, S_DAI_SET d where d.[DAI_SET_ID] = s.[DAI_SET_ID] AND s.SENSOR_PRODUCT_ID = product.DAI_PRODUCT_ID AND s.SafeTypeId = factorType.SAFETY_FACTOR_TYPE_ID order by sid"); DataTable table = ds.Tables[0]; DataSet paramSet = _cfgHelper.Query(@"select * from S_FORMULAID_SET"); //公式配置。 DataTable paramTable = paramSet.Tables[0]; foreach (DtuNode dtu in dtus) { // filter to Node var sr = from r in table.AsEnumerable() where !r.IsNull("dtuId") && r.Field <string>("dtuId") == dtu.DtuCode orderby r["sid"] select r; if (!sr.Any()) { continue; } foreach (DataRow row in sr) { var sensor = new Sensor { DtuID = dtu.DtuId, SensorID = Convert.ToUInt32(row["sid"]), ChannelNo = row.IsNull("cno") ? 1 : Convert.ToUInt32(row["cno"]), ModuleNo = row.IsNull("mno") ? 0 : Convert.ToUInt32(row["mno"]), ProtocolType = Convert.ToUInt16(row["protocol_type"]), Name = Convert.ToString(row["name"]), FactorType = Convert.ToUInt32(row["factor"]), FactorTypeTable = Convert.ToString(row["factorTypeTable"]), TableColums = Convert.ToString(row["tableColums"]) }; GetParameters(paramTable, sensor); if (sensor.Parameters.Count > 0) { sensor.FormulaID = (uint)sensor.Parameters[0].FormulaParam.FID; } dtu.AddSensor(sensor); } } return(dtus); }