public override void execute(Tag tag, String devCode, CellTag cellTag, SystemDateTag systemDateTag, CasicSession session) { WakeUpTag wakeUpTag = tag as WakeUpTag; if (session.devMaps.ContainsKey(devCode)) { CasicSession.DeviceDTO dev = session.devMaps[devCode]; byte flag = byte.Parse(wakeUpTag.DataValue, System.Globalization.NumberStyles.HexNumber); dev.IsWakeUp = flag == 0?true:false; //上传0代表已经唤醒 session.devMaps[devCode] = dev; session.Logger.Info("设备唤醒" + devCode); session.Logger.Info("唤醒TAG:oid:" + wakeUpTag.Oid + "唤醒状态:" + wakeUpTag.IsWAkeUp); } }
//TODO LIST:获取所有在线的设备列表 private List <DeviceDTO> getOnlineCasicDev() { List <DeviceDTO> devices = new List <DeviceDTO>(); var server = bootstrap.GetServerByName(CASIC_SERVER_NAME); if (server == null) { return(devices); } CasicServer casicServer = server as CasicServer; // CasicSession session = casicServer.GetAllSessions() as CasicSession; foreach (CasicSession session in casicServer.GetAllSessions()) { String sessionId = session.SessionID; //casic的设备一个session中,可能存在集中器,噪声、液位传感器都多种数据信息 foreach (KeyValuePair <string, CasicSession.DeviceDTO> kvp in session.devMaps) { String devInfo = kvp.Key; CasicSession.DeviceDTO dto = kvp.Value; String devType = dto.DevType; String devID = dto.DevCode; DeviceDTO devDto = new DeviceDTO(); devDto.SessionId = sessionId; devDto.Company = "203所"; devDto.TypeName = devType; devDto.Name = devID; devDto.Tag = dto.Detail; devDto.Status = "在线"; devDto.ServerName = "CasicServer"; devices.Add(devDto); } } return(devices); }
private void updateSessionInfo(CasicSession session, StringRequestInfo requestInfo) { String deviceId = requestInfo.Parameters[3]; String pduType = requestInfo.Parameters[6]; String devType = getDeviceTypeByPdu(pduType); String seq = requestInfo.Parameters[7]; String sessionKey = deviceId; byte btSeq = byte.Parse(seq, System.Globalization.NumberStyles.HexNumber); if (!session.devMaps.ContainsKey(sessionKey)) { //session.devMaps.Add(sessionKey, requestInfo.Body); CasicSession.DeviceDTO dev = new CasicSession.DeviceDTO(); dev.DevCode = deviceId; dev.DevType = devType; dev.Detail = requestInfo.Body; dev.Company = "203"; dev.Status = "在线"; dev.IsWakeUp = false; //dev.Seq = 0x01; dev.Seq = (UInt16)(0x01 << (btSeq - 1)); session.devMaps.Add(sessionKey, dev); } else { //更新序列状态 CasicSession.DeviceDTO dto = session.devMaps[sessionKey]; if (dto.Seq == 0) { //dto.Seq = 0x01; dto.Seq = (UInt16)(0x01 << (btSeq - 1)); } else { dto.Seq = (UInt16)(dto.Seq | 0x01 << (btSeq - 1)); } session.devMaps[sessionKey] = dto; } }
public void wakeUpCasicDev(DeviceDTO dto) { //判断当前是够已经唤醒,如果已经唤醒则不再下发配置信息 //判断设备是否已经唤醒 //TODO LIST:获取会话信息,发送数据 var server0 = bootstrap.GetServerByName(CASIC_SERVER_NAME); CasicServer casicServer0 = server0 as CasicServer; CasicSession session0 = casicServer0.GetSessionByID(dto.SessionId) as CasicSession; CasicSession.DeviceDTO dev = session0.devMaps[dto.Name]; if (dev.IsWakeUp || dev.DevType == "集中器") { return; } String[] parameters = dto.Tag.Split(','); //TODO: construct the receving casic data String preamble = parameters[0]; byte btpreamble = byte.Parse(preamble, System.Globalization.NumberStyles.HexNumber); String version = parameters[1]; byte btVersion = byte.Parse(version, System.Globalization.NumberStyles.HexNumber); String deviceId = parameters[3];//6个字节 byte[] btDevId = new byte[6]; btDevId[0] = byte.Parse(deviceId.Substring(0, 2), System.Globalization.NumberStyles.HexNumber); btDevId[1] = byte.Parse(deviceId.Substring(2, 2), System.Globalization.NumberStyles.HexNumber); btDevId[2] = byte.Parse(deviceId.Substring(4, 2), System.Globalization.NumberStyles.HexNumber); btDevId[3] = byte.Parse(deviceId.Substring(6, 2), System.Globalization.NumberStyles.HexNumber); btDevId[4] = byte.Parse(deviceId.Substring(8, 2), System.Globalization.NumberStyles.HexNumber); btDevId[5] = byte.Parse(deviceId.Substring(10, 2), System.Globalization.NumberStyles.HexNumber); String routeFlag = parameters[4]; byte btRouteFlag = byte.Parse(routeFlag, System.Globalization.NumberStyles.HexNumber); String dstNodeAddr = parameters[5];//2个字节 byte[] btDstNode = new byte[2]; btDstNode[0] = byte.Parse(dstNodeAddr.Substring(0, 2), System.Globalization.NumberStyles.HexNumber); btDstNode[1] = byte.Parse(dstNodeAddr.Substring(2, 2), System.Globalization.NumberStyles.HexNumber); String sPtuType = parameters[6];//2个字节 Int16 btpduType = Int16.Parse(sPtuType, System.Globalization.NumberStyles.HexNumber); Int16 opearType = (Int16)(btpduType & 0x7F); Int16 pdu = (Int16)(2688 + opearType); //0X0A80 byte[] btPdu0 = BitConverter.GetBytes(pdu); byte[] btPdu = { btPdu0[1], btPdu0[0] }; // btDstNode[0] = byte.Parse(dstNodeAddr.Substring(0, 2), System.Globalization.NumberStyles.HexNumber); // btDstNode[1] = byte.Parse(dstNodeAddr.Substring(2, 2), System.Globalization.NumberStyles.HexNumber); String seq = parameters[7]; byte btSeq = byte.Parse(seq, System.Globalization.NumberStyles.HexNumber); byte[] tag = { 0x60, 0x00, 0x02, 0x00, 0x00, 0x01, 0x00 }; byte[] totoalLen = { 0x00, 0x13 }; //wrap the whole data byte[] result = new byte[1 + 1 + 2 + 6 + 1 + 2 + 2 + 1 + 7]; result[0] = btpreamble; result[1] = btVersion; totoalLen.CopyTo(result, 2); btDevId.CopyTo(result, 4); result[10] = btRouteFlag; btDstNode.CopyTo(result, 11); btPdu.CopyTo(result, 13); result[15] = btSeq; tag.CopyTo(result, 16); //增加CRC校验 String strCrc = StringUtil.To16HexString(String.Format("{0:X}", (int)CodeUtils.CRC16_AD(result))); byte[] btcrc = { CodeUtils.String2Byte(strCrc.Substring(0, 2)), CodeUtils.String2Byte(strCrc.Substring(2, 2)) }; byte[] afcrc = new byte[result.Length + 2]; result.CopyTo(afcrc, 0); btcrc.CopyTo(afcrc, result.Length); //TODO LIST:获取会话信息,发送数据 var server = bootstrap.GetServerByName(CASIC_SERVER_NAME); CasicServer casicServer = server as CasicServer; CasicSession session = casicServer.GetSessionByID(dto.SessionId) as CasicSession; session.Logger.Info("下发唤醒信息:" + "设备编号:" + deviceId + "" + BitConverter.ToString(afcrc)); session.Send(afcrc, 0, afcrc.Length); }
public override void ExecuteCommand(CasicSession session, StringRequestInfo requestInfo) { //casicSession = session; //TODO: construct the receving casic data String preamble = requestInfo.Parameters[0]; String version = requestInfo.Parameters[1]; String leng = requestInfo.Parameters[2]; String deviceId = requestInfo.Parameters[3]; String routeFlag = requestInfo.Parameters[4]; String dstNodeAddr = requestInfo.Parameters[5]; String pduType = requestInfo.Parameters[6]; String seq = requestInfo.Parameters[7]; String settings = requestInfo.Parameters[8]; String crcs = requestInfo.Parameters[9]; //print the receving data String devType = getDeviceTypeByPdu(pduType); String operType = getOpeTypeByPdu(pduType); session.Logger.Info("AD接收数据:" + requestInfo.Body); session.Logger.Info("当前版本:" + version); session.Logger.Info("设备编号:" + deviceId); session.Logger.Info("路由标志:" + routeFlag); session.Logger.Info("目标节点地址:" + dstNodeAddr); session.Logger.Info("设备类型:" + devType); session.Logger.Info("操作类型" + operType); session.Logger.Info("序列seq:" + seq); //update the device code //devCode = deviceId; //如果是集中器更新session,如果不是把设备切换成在线 if (devType == "集中器") { session.HubAddr = deviceId; new BLL.AlarmConcentrator().setHubOnLine(deviceId); } else { new BLL.DevHub().setOnLineByDevcode(deviceId); } //判断是返回的设置确认数据帧,更新DeviceConfig表 if (operType == "SetResponse") { new BLL.DeviceConfig().clearDeviceConfigByDevCode(deviceId); return; } //TODO LIST:更新会话中session信息 this.updateSessionInfo(session, requestInfo); //upload periodically or config query according to oid //获取电量信息,系统时间,传递给对应的handler List <Tag> tags = this.getTags(settings, session); CellTag cellTag = this.getCellTag(tags); SystemDateTag systemDateTag = this.getSystemDateTag(tags); try { //TODO LIST:处理主动上报的数据、参数查询、参数设置 TagHandler systemDateHandler = new SystemDateTagHandler(); TagHandler noiseHandler = new NoiseTagHandler(); TagHandler liquidHandler = new LiquidTagHandler(); TagHandler wellHandler = new WellTagHandler(); TagHandler sysTimeHandler = new SystemTimeTagHandler(); TagHandler wakeupHandler = new WakeUpTagHandler(); TagHandler cellTagHandler = new CellTagHandler(); TagHandler waterMeterHadler = new WaterMeterTagHandler(); TagHandler pressHandler = new PressTagHandler(); TagHandler tempHandler = new TempTagHandler(); TagHandler fsslHandler = new FSSLTagHandler(); TagHandler fshjHandler = new FSHJTagHandler(); TagHandler rqHandler = new RQTagHandler(); TagHandler sensorExp0 = new SensorException0TagHandler(); TagHandler sensorExp1 = new SensorException1TagHandler(); systemDateHandler.NextHandler = noiseHandler; noiseHandler.NextHandler = liquidHandler; liquidHandler.NextHandler = waterMeterHadler; waterMeterHadler.NextHandler = wellHandler; wellHandler.NextHandler = tempHandler; tempHandler.NextHandler = pressHandler; pressHandler.NextHandler = rqHandler; rqHandler.NextHandler = sysTimeHandler; sysTimeHandler.NextHandler = wakeupHandler; wakeupHandler.NextHandler = cellTagHandler; cellTagHandler.NextHandler = fsslHandler; fsslHandler.NextHandler = fshjHandler; fshjHandler.NextHandler = sensorExp0; sensorExp0.NextHandler = sensorExp1; foreach (Tag tag in tags) { //采用责任链的方式来处理各个tag: //目前已经处理噪声、液位、系统时间、系统日期tag systemDateHandler.handleTag(tag, deviceId, cellTag, systemDateTag, session); } if (routeFlag == "03") //GPRS { senderGPRSConfig(devType, deviceId, version, session); } else //433 { //下发返回的信息 if ((operType != "GetResponse") && (devType != "井盖传感器") && isfinishe(pduType) && (operType == "TrapRequest" || operType == "OnlineRequest") ) { HeadConfig headConfig = new HeadConfig(version, deviceId, pduType, seq, routeFlag, dstNodeAddr); TrapRespConfig trapResp = new TrapRespConfig(headConfig); SystemTimeConfig sysTimeConfig = new SystemTimeConfig(trapResp); if (session.devMaps.ContainsKey(deviceId)) { CasicSession.DeviceDTO dto = session.devMaps[deviceId]; trapResp.NoSeq = dto.Seq; } byte[] btConfig = sysTimeConfig.getConfig(new byte[0]); session.Logger.Info(devType + ":" + deviceId + " CRC校验前数据:" + BitConverter.ToString(btConfig)); String strCrc = StringUtil.To16HexString((String.Format("{0:X}", (int)CodeUtils.CRC16_AD(btConfig)))); session.Logger.Info(devType + ":" + deviceId + " 生成的CRC校验:" + strCrc); byte[] btcrc = { CodeUtils.String2Byte(strCrc.Substring(0, 2)), CodeUtils.String2Byte(strCrc.Substring(2, 2)) }; byte[] afcrc = new byte[btConfig.Length + 2]; btConfig.CopyTo(afcrc, 0); btcrc.CopyTo(afcrc, btConfig.Length); session.Logger.Info(devType + ":" + deviceId + " CRC校验后数据:" + BitConverter.ToString(afcrc)); session.Send(afcrc, 0, afcrc.Length); CasicSession.DeviceDTO dto0 = session.devMaps[deviceId]; dto0.Seq = 0x00; session.devMaps[deviceId] = dto0; } } } catch (Exception e) { session.Logger.Error(e.ToString()); } }