public override void Execute(IChannel session, Message package) { var dtu = DbHelper.Connect().QueryFirstOrDefault <Dtu>("select * from s_dtu where sn=@sn", new { sn = package.Sn }); if (dtu == null) { dtu = new Dtu() { Sn = package.Sn }; DbHelper.Connect().Insert <Dtu>(dtu); } else { dtu.UpdateTime = DateTime.Now; dtu.State = DtuState.Online; DbHelper.Connect().Update(dtu); } var sensors = new List <Sensor>(); var count = package.Content[0]; var msg = new Message(); List <byte> list; try { for (int i = 0; i < count; i++) { var sensor = new Sensor(); sensor.Sn = BitConverter.ToString(package.Content.Skip(i * 16 + 1).Take(4).ToArray()).Replace("-", ""); sensor.Temperature = (sbyte)package.Content[i * 16 + 5]; sensor.Pressure = ((decimal)BitConverter.ToInt16(package.Content.Skip(i * 16 + 7).Take(2).ToArray(), 0)) / 100; sensor.Num = package.Content[i * 16 + 9].ToString("X"); sensor.State = package.Content[i * 16 + 10]; sensor.UpdateTime = CmdHelper.GetBCD_Date(package.Content.Skip(i * 16 + 11).Take(6).ToArray()); sensor.Dtu = package.Sn; sensors.Add(sensor); } } //解析异常 回复否认帧 catch (Exception e) { LogHelper.Error(e); Thread.Sleep(1 * 1000); msg.Ctrl = 0x0c + 0x80; msg.Sn = package.Sn; list = new List <byte> { 0x02 }; list.AddRange(CmdHelper.GetBCD_Date(DateTime.Now)); msg.Content = list.ToArray(); session.WriteAsync(msg); return; } foreach (var sensor in sensors) { RedisHelper.Set(package.Sn + "_" + sensor.Num, sensor); DbHelper.Connect().Insert(sensor); } Thread.Sleep(1 * 1000); msg.Ctrl = 0x0c + 0x80; msg.Sn = package.Sn; list = new List <byte> { 0x01 }; list.AddRange(CmdHelper.GetBCD_Date(DateTime.Now)); msg.Content = list.ToArray(); session.WriteAsync(msg); }
private DtuList FindDtuList(Subcenter subcenter) { DtuList dtuList = new DtuList(); List <ModemInfoStruct> stateList = new List <ModemInfoStruct>(); List <CEntityStation> stations = new List <CEntityStation>(); List <CEntitySoilStation> soilStations = new List <CEntitySoilStation>(); List <CEntitySubCenter> subCenters = new List <CEntitySubCenter>(); Dictionary <string, ModemInfoStruct> gprsDic = new Dictionary <string, ModemInfoStruct>(); stateList = Clone <ModemInfoStruct>(CProtocolEventManager.Instance.GetOnlineStatusList()); stations = CDBDataMgr.Instance.GetAllStation(); soilStations = CDBSoilDataMgr.Instance.GetAllSoilStation(); subCenters = CDBDataMgr.Instance.GetAllSubCenter(); if (stateList.Count() != 0) { for (int i = 0; i < stateList.Count(); i++) { string uid = ((uint)stateList[i].m_modemId).ToString("X").PadLeft(8, '0'); gprsDic.Add(uid, stateList[i]); } } foreach (var s in stations) { if (subcenter.SubcenterdId == s.SubCenterID.ToString() || subcenter.SubcenterdId == "0") { if (gprsDic.Count() != 0) { if (gprsDic.ContainsKey(s.GPRS)) { Dtu dtu = new Dtu(); ModemInfoStruct state = gprsDic[s.GPRS]; string phoneno = CGprsUtil.Byte11ToPhoneNO(state.m_phoneno, 0); string dynIP = CGprsUtil.Byte4ToIP(state.m_dynip, 0); string connectTime = CGprsUtil.ULongToDatetime(state.m_conn_time).ToString(); string refreshTime = CGprsUtil.ULongToDatetime(state.m_refresh_time).ToString(); dtu.SubcenterId = s.SubCenterID.ToString(); string subName = CDBDataMgr.Instance.GetSubCenterName(s.SubCenterID.ToString()); dtu.SubcenterName = subName; dtu.StationId = s.StationID; dtu.StationName = s.StationName; dtu.GprsId = s.GPRS; dtu.GsmNum = phoneno; dtu.IpAddr = dynIP; dtu.ConnTime = connectTime; dtu.RefreshTime = refreshTime; dtu.State = "1"; dtu.StationType = CEnumHelper.StationTypeToDBStr(s.StationType); dtuList.Dtu.Add(dtu); } } else { Dtu dtu = new Dtu(); dtu.SubcenterId = s.SubCenterID.ToString(); string subName = CDBDataMgr.Instance.GetSubCenterName(s.SubCenterID.ToString()); dtu.SubcenterName = subName; dtu.StationId = s.StationID; dtu.StationName = s.StationName; dtu.GprsId = s.GPRS; dtu.State = "2"; dtu.StationType = CEnumHelper.StationTypeToDBStr(s.StationType); dtuList.Dtu.Add(dtu); } } } foreach (var s in stations) { if (subcenter.SubcenterdId == s.SubCenterID.ToString() || subcenter.SubcenterdId == "0") { if (gprsDic.Count() != 0) { if (!gprsDic.ContainsKey(s.GPRS)) { Dtu dtu = new Dtu(); dtu.SubcenterId = s.SubCenterID.ToString(); string subName = CDBDataMgr.Instance.GetSubCenterName(s.SubCenterID.ToString()); dtu.SubcenterName = subName; dtu.StationId = s.StationID; dtu.StationName = s.StationName; dtu.GprsId = s.GPRS; dtu.State = "2"; dtu.StationType = CEnumHelper.StationTypeToDBStr(s.StationType); dtuList.Dtu.Add(dtu); } } } } foreach (var s in soilStations) { if (subcenter.SubcenterdId == s.SubCenterID.ToString() || subcenter.SubcenterdId == "0") { if (gprsDic.Count() != 0) { if (gprsDic.ContainsKey(s.GPRS)) { Dtu dtu = new Dtu(); ModemInfoStruct state = gprsDic[s.GPRS]; string phoneno = CGprsUtil.Byte11ToPhoneNO(state.m_phoneno, 0); string dynIP = CGprsUtil.Byte4ToIP(state.m_dynip, 0); string connectTime = CGprsUtil.ULongToDatetime(state.m_conn_time).ToString(); string refreshTime = CGprsUtil.ULongToDatetime(state.m_refresh_time).ToString(); if (subcenter.SubcenterdId == s.SubCenterID.ToString() || subcenter.SubcenterdId == "0") { dtu.SubcenterId = s.SubCenterID.ToString(); string subName = CDBDataMgr.Instance.GetSubCenterName(s.SubCenterID.ToString()); dtu.SubcenterName = subName; dtu.StationId = s.StationID; dtu.StationName = s.StationName; dtu.GprsId = s.GPRS; dtu.GsmNum = phoneno; dtu.IpAddr = dynIP; dtu.ConnTime = connectTime; dtu.RefreshTime = refreshTime; dtu.State = "1"; dtu.StationType = CEnumHelper.StationTypeToDBStr(s.StationType); dtuList.Dtu.Add(dtu); } } } else { Dtu dtu = new Dtu(); dtu.SubcenterId = s.SubCenterID.ToString(); string subName = CDBDataMgr.Instance.GetSubCenterName(s.SubCenterID.ToString()); dtu.SubcenterName = subName; dtu.StationId = s.StationID; dtu.StationName = s.StationName; dtu.GprsId = s.GPRS; dtu.State = "2"; dtu.StationType = CEnumHelper.StationTypeToDBStr(s.StationType); dtuList.Dtu.Add(dtu); } } } foreach (var s in soilStations) { if (subcenter.SubcenterdId == s.SubCenterID.ToString() || subcenter.SubcenterdId == "0") { if (gprsDic.Count() != 0) { if (!gprsDic.ContainsKey(s.GPRS)) { Dtu dtu = new Dtu(); dtu.SubcenterId = s.SubCenterID.ToString(); string subName = CDBDataMgr.Instance.GetSubCenterName(s.SubCenterID.ToString()); dtu.SubcenterName = subName; dtu.StationId = s.StationID; dtu.StationName = s.StationName; dtu.GprsId = s.GPRS; dtu.State = "2"; dtu.StationType = CEnumHelper.StationTypeToDBStr(s.StationType); dtuList.Dtu.Add(dtu); } } } } return(dtuList); }