private void btnStartStop_Click(object sender, EventArgs e) { if (btnStartStop.Text == "启动服务") { bootstrap = BootstrapFactory.CreateBootstrap(); if (!bootstrap.Initialize()) { ServiceStatus = false; MessageBox.Show("Failed to initialize!"); return; } else { StartResult result = bootstrap.Start(); if (result == StartResult.Failed) { ServiceStatus = false; } else if (result == StartResult.Success) { var transferServer = bootstrap.GetServerByName("TransferServer") as TransferServer; transferServer.NewSessionConnected += transferServer_NewSessionConnected; transferServer.SessionClosed += transferServer_SessionClosed; var userServer = bootstrap.GetServerByName("UserServer") as UserServer; userServer.UserJoin += userServer_UserJoin; userServer.UserLeave += userServer_UserLeave; userServer.SendToServer += userServer_UserSendToServer; userServer.UserWhisper += userServer_UserWhisper; userServer.Broadcast += userServer_UserBroadcast; ServiceStatus = true; } else { //MessageBox.Show(string.Format("Start result: {0}!", result)); } this.lblStatus.Text = result.ToString(); } } else { bootstrap.Stop(); ServiceStatus = false; this.lblStatus.Text = "The server was stopped!"; } if (ServiceStatus) { btnStartStop.Text = "停止服务"; } else { btnStartStop.Text = "启动服务"; } }
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); }