public async Task SynchronizeDb() { var db = _db; subplateformres = await db.subplateformres.ToListAsync(); var tStation = await db.tStation.ToListAsync(); nodemap = await db.nodemap.ToListAsync(); foreach (var st in tStation) { var ndMap = nodemap.Find(n => n.objId == st.objId); if (ndMap == null) { continue; } NdList.Add(new SuperBStation(new TStation() { ID = st.objId, Name = st.objName, Des = st.objDesc, Type = (EnumType)st.objTypeId, ParentID = st.parentId ?? 0, Latitude = st.latitude, Longitude = st.longitude }, ndMap.nodeIsid)); } var tDevice = await db.tDevice.ToListAsync(); foreach (var dev in tDevice) { var ndMap = NdList.Find(n => n.OrgiData.ID == dev.parentId); if (ndMap == null) { continue; } DevList.Add(new SuperBDevice(new TDevice() { ID = dev.objId, Name = dev.objName, Des = dev.objDesc, Type = (EnumType)dev.objTypeId, ParentID = dev.parentId ?? 0, BeginRunTime = dev.beginRunTime.HasValue ? SuperBTagBase.Transfer(dev.beginRunTime.Value) : SuperBTagBase.Transfer(DateTime.Now), DeviceType = (EnumDeviceType)dev.devTypeId, Productor = dev.productor, Version = dev.version, }, ndMap.NodeIsid)); } tagmap = await db.tagmap.ToListAsync(); devicemap = await db.devicemap.ToListAsync(); alarmmap = await db.alarmmap.ToListAsync(); var taic = await db.taic.ToListAsync(); foreach (var aic in taic) { var devMap = DevList.Find(n => n.OrgiData.ID == aic.parentId); var tagMap = tagmap.Find(t => t.objId == aic.objId); if (devMap == null || tagMap == null) { continue; } TagList.Add(new SuperB_Taic(new TAIC() { ID = aic.objId, Name = aic.objName, Des = aic.objDesc, Type = (EnumType)aic.objTypeId, ParentID = aic.parentId ?? 0, AlarmEnable = (EnumEnable)aic.alarmEnable, AlarmLevel = (EnumAlarmLevel)aic.alarmLevel, HiLimit1 = aic.hiLimit1, HiLimit2 = aic.hiLimit2, HiLimit3 = aic.hiLimit3, LoLimit1 = aic.loLimit1, LoLimit2 = aic.loLimit2, LoLimit3 = aic.loLimit3, }, tagMap.tagIsid, alarmmap.ToArray())); } var taoc = await db.taoc.ToListAsync(); foreach (var aic in taoc) { var devMap = DevList.Find(n => n.OrgiData.ID == aic.parentId); var tagMap = tagmap.Find(t => t.objId == aic.objId); if (devMap == null || tagMap == null) { continue; } TagList.Add(new SuperB_Taoc(new TAOC() { ID = aic.objId, Name = aic.objName, Des = aic.objDesc, Type = (EnumType)aic.objTypeId, ParentID = aic.parentId ?? 0, ControlEnable = (EnumEnable)aic.controlEnable, MaxVal = aic.maxVal, MinVal = aic.minVal, Percision = aic.percision, Saved = (EnumEnable)aic.saved, Stander = aic.stander, Unit = aic.unit, }, tagMap.tagIsid, alarmmap.ToArray())); } var tdic = await db.tdic.ToListAsync(); foreach (var aic in tdic) { var devMap = DevList.Find(n => n.OrgiData.ID == aic.parentId); var tagMap = tagmap.Find(t => t.objId == aic.objId); if (devMap == null || tagMap == null) { continue; } TagList.Add(new SuperB_Tdic(new TDIC() { ID = aic.objId, Name = aic.objName, Des = aic.objDesc, Type = (EnumType)aic.objTypeId, ParentID = aic.parentId ?? 0, Saved = (EnumEnable)aic.saved, AlarmEnable = (EnumEnable)aic.alarmEnable, AlarmLevel = (EnumAlarmLevel)aic.alarmLevel, AlarmThresbhold = (EnumEnable)aic.alarmThresbhold, Desc0 = aic.desc0, Desc1 = aic.desc1, }, tagMap.tagIsid, alarmmap.ToArray())); } var tdoc = await db.tdoc.ToListAsync(); foreach (var aic in tdoc) { var devMap = DevList.Find(n => n.OrgiData.ID == aic.parentId); var tagMap = tagmap.Find(t => t.objId == aic.objId); if (devMap == null || tagMap == null) { continue; } TagList.Add(new SuperB_Tdoc(new TDOC() { ID = aic.objId, Name = aic.objName, Des = aic.objDesc, Type = (EnumType)aic.objTypeId, ParentID = aic.parentId ?? 0, ControlEnable = (EnumEnable)aic.controlEnable, Saved = (EnumEnable)aic.saved, Desc0 = aic.desc0, Desc1 = aic.desc1, }, tagMap.tagIsid, alarmmap.ToArray())); } var tdsc = await db.tdsc.ToListAsync(); foreach (var aic in tdsc) { var devMap = DevList.Find(n => n.OrgiData.ID == aic.parentId); var tagMap = tagmap.Find(t => t.objId == aic.objId); if (devMap == null || tagMap == null) { continue; } TagList.Add(new SuperB_Tdsc(new TDSC() { ID = aic.objId, Name = aic.objName, Des = aic.objDesc, Type = (EnumType)aic.objTypeId, ParentID = aic.parentId ?? 0, Saved = (EnumEnable)aic.saved, AlarmEnable = (EnumEnable)aic.alarmEnable, }, tagMap.tagIsid, alarmmap.ToArray())); } }
public async Task <bool> UpdateOrAddDevice(TDevice data) { var node = nodemap.Find(m => m.objId == data.ParentID); if (node == null) { return(false); //没有父节点,返回 } var dev = DevList.Find(n => n.OrgiData.ID == data.ID); if (dev == null) //新增设备,返回新增的设备,等web新增成功,插入数据库 { dev = new SuperBDevice(data, node.nodeIsid); var map = new SqlDeviceMap() { devIsid = dev.Device.dvIsid, objId = data.ID, groupIsid = Uuid.Create16Token() }; var req = await HttpUtil.PostAsync($"http://{cfg.AppSettings["serverUrl"]}/inIoT/devServer/deviceRes", JsonSrialize.Srialize(new DevModel() { dvIsid = dev.Device.dvIsid, dvName = dev.Device.dvName, dvTypeCode = dev.Device.dvTypeCode, managerIsid = cfg.AppSettings["managerIsid"], nodeIsid = dev.NodeIsid, addrRes = new MqttCommon.Setup.AddResource() { communicationType = 1, dvAddr1 = "1.1.1.1", dvAddr2 = "2222" } }), SuperBCenter.ServerToken); if (req == null || req.codeid != 0) { return(false); } try { await _db.tDevice.AddAsync(dev.DbData); if (!devicemap.Exists(d => d.objId == dev.OrgiData.ID && d.devIsid == dev.NodeIsid)) { await _db.devicemap.AddAsync(map); devicemap.Add(map); } await _db.SaveChangesAsync(); DevList.Add(dev); } catch (Exception ex) { LogHelper.Error(ex); return(false); } } else { if (!devicemap.Exists(d => d.objId == dev.OrgiData.ID && d.devIsid == dev.Device.dvIsid)) { return(false); } if (dev.Update(data)) { try { _db.tDevice.Update(dev.DbData); await _db.SaveChangesAsync(); } catch (Exception ex) { LogHelper.Error(ex); return(false); } } } return(true); }