/// <summary> /// 处理设备注册 /// </summary> /// <param name="registerInfo"></param> /// <param name="con"></param> /// <param name="db"></param> public void ProcessReqest(Register registerInfo, SocketConnection con, IClientRequest db) { var registerBk = new CmdHeader { CmdCode = 0xFF01, Identity = registerInfo.Identity, ServerId = SocketConnectionServerDispatcher.ServerId, //TimeToken = registerInfo.TimeToken }; SocketConnection.ShowMsg(registerInfo.ToString()); var registerDto = new DtoRegister { Iccid = registerInfo.Iccid, ImeiNo = registerInfo.Identity, ImeiType = registerInfo.ImeiType, ImeiVersion = registerInfo.ImeiVersion, RemoteIpEndPoint = con.ConnectSocket.RemoteEndPoint.ToString(), ServerIp = con.ConnectSocket.LocalEndPoint.ToString(), ServerOutIp = SocketConnectionServerDispatcher.ServerOutIpEndPort, Lng = registerInfo.Lng, Lat = registerInfo.Lat }; registerBk.ResultCode = db.DeviceRegister(registerDto); con.Send(registerBk.ToByteArray()); SocketConnection.ShowMsg(registerBk.ToString()); }
public void ProcessReqest(Waring waringInfo, SocketConnection con, IClientRequest db) { var fbdb = db as StreetLightPersistence; if (fbdb == null) { return; } var comBk = new CmdHeader { CmdCode = 0xFF0F, Identity = waringInfo.Identity, ResultCode = fbdb.AddWaringMessage(new DtoWaring { ImeiNo = waringInfo.Identity, ImeiLightNo = waringInfo.LightNo, WaringCode = waringInfo.WaringCode, WaringMessage = waringInfo.WaringDesc, ImeiWaringMsgContent = JsonConvert.SerializeObject(waringInfo) }), ServerId = SocketConnectionServerDispatcher.ServerId, //TimeToken = waringInfo.TimeToken }; con.Send(comBk.ToByteArray()); switch (waringInfo.WaringCode) { case "0001": //设置门未开报警 break; } SocketConnection.ShowMsg(waringInfo.ToString()); //if (con.StateData?.BoundList == null) return; //foreach (var usr in con.StateData.BoundList) //{ // //if (string.IsNullOrWhiteSpace(usr.ClientId) || usr.ClientId.Equals("null")) continue; // //var res = CommCode.GeTuiMsg("设备报警", $"设备{waringInfo.Identity}发生警报,{waringInfo.WaringDesc}", _gettuiappid, _gettuiappkey, _gettuimasterkey, usr.ClientId, null); // //SocketConnection.ShowMsg(res); // #region 直接发送异常信息 // //SocketConnection clientSkt; // //if (SocketConnectionServerDispatcher.DicSockectConnection.TryGetValue(usr, out clientSkt) && clientSkt.ConnectSocket.Connected) // //{ // // var waringBk = new WaringBkPb // // { // // CmdCode = "FF06", // // Identity = usr, // // OppositeId = con.Identity, // // ResultCode = dealRes.res, // // ServerId = SocketConnectionServerDispatcher.ServerId, // // TimeToken = waringInfo.TimeToken, // // WaringCode = waringInfo.WaringCode, // // WaringDesc = waringInfo.WaringDesc // // }; // // clientSkt.Send(waringBk.ToByteArray()); // //} // #endregion //} }
/// <summary> /// 用户登录 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnUserLogin_Click(object sender, EventArgs e) { var register = new CmdHeader { CmdCode = 0x0202, Identity = UserIdentity, //TimeToken = CurrentTime }; SocketConnectionClient.SocketClientConnection.Send(register.ToByteArray()); }
/// <summary> /// 中继重置 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnReset_Click(object sender, EventArgs e) { var dto = new CmdHeader { CmdCode = 0x020, Identity = UserIdentity, OppositeId = DeviceIdentity, //TimeToken = CurrentTime }; SocketConnectionClient.SocketClientConnection.Send(dto.ToByteArray()); }
//private static int currentOrderId; public Form1() { InitializeComponent(); ContextMenuStrip listboxMenu = new ContextMenuStrip(); ToolStripMenuItem rightMenu = new ToolStripMenuItem("Copy"); rightMenu.Click += Copy_Click; listboxMenu.Items.AddRange(new ToolStripItem[] { rightMenu }); lstMsg.ContextMenuStrip = listboxMenu; SocketConnectionClient.ClientSocketStarting(new SocketConnectConfig() { //ConnectCallback = con => //{ // var message = new Hellomessage // { // Identity = Guid.NewGuid().ToString("N"), // Cmdcode = "0000", // Timetoken = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") // }; // con.Identity = message.Identity; // con.Send(message.ToByteArray()); //}, ReceiveCallback = (bytes, con) => { var header = CmdHeader.Parser.ParseFrom(bytes); //var msg = Hellomessage.Parser.ParseFrom(bytes); lstMsg.Invoke(new Action(() => { lstMsg.Items.Add($"接收header[{header}]"); })); switch (header.CmdCode) { case 0xFF01: //硬件注册后,上位机返回 break; case 0xFF02: //登录后,上位机返回 break; case 0xFF03: //硬件上报状态后,上位机返回 break; case 0x0203: //下单前查询设备状态 break; case 0x0204: //用户设置上报时间间隔 var timeSpan = SettingsTimeSpan.Parser.ParseFrom(bytes); var timeSpanBk = new CmdHeader { CmdCode = 0x0104, Identity = timeSpan.OppositeId, OppositeId = timeSpan.Identity, ServerId = header.ServerId, ResultCode = 1, //TimeToken = header.TimeToken }; con.Send(timeSpanBk.ToByteArray()); break; case 0x0205: //返回添加格子、灯 var addLightBk = new CmdHeader { CmdCode = 0x0105, Identity = header.OppositeId, OppositeId = header.Identity, ServerId = header.ServerId, ResultCode = 1, //TimeToken = header.TimeToken }; con.Send(addLightBk.ToByteArray()); break; case 0x0206: //删除灯 //var removeLightBk = new CmdHeader //{ // CmdCode = "0106", // Identity = header.OppositeId, // OppositeId = header.Identity, // ServerId = header.ServerId, // ResultCode = 1, // //TimeToken = header.TimeToken //}; var removeLight = RemoveLight.Parser.ParseFrom(bytes); var removeLightBk = new ComDealBk { CmdCode = "0106", Identity = header.OppositeId, OppositeId = header.Identity, ServerId = header.ServerId, ResultCode = 1 }; foreach (var c in removeLight.CellAddrs) { removeLightBk.Res.Add(new DealRes() { CellAddr = c, ResultCode = 1 }); } con.Send(removeLightBk.ToByteArray()); break; case 0x0207: //设置中继开关 var openDeviceBk = new CmdHeader { CmdCode = 0x0107, Identity = header.OppositeId, OppositeId = header.Identity, ServerId = header.ServerId, ResultCode = 1, //TimeToken = header.TimeToken }; con.Send(openDeviceBk.ToByteArray()); break; case 0x0208: //设置路灯开关 var openLight = OpenLight.Parser.ParseFrom(bytes); //var lightBk = new OpenLightBk //{ // CmdCode = "0108", // Identity = openLight.OppositeId, // OppositeId = openLight.Identity, // ServerId = openLight.ServerId, // ResultCode = 1, // Status = openLight.Status, // LightNos = // { // openLight.LightNos // }, // //TimeToken = header.TimeToken //}; var lightBk = new ComDealBk { CmdCode = "0108", Identity = header.OppositeId, OppositeId = header.Identity, ServerId = header.ServerId, ResultCode = 1 }; foreach (var c in openLight.CellAddrs) { lightBk.Res.Add(new DealRes() { CellAddr = c, ResultCode = openLight.CellAddrs.Count > 1 ? 0 : 1 }); } con.Send(lightBk.ToByteArray()); break; case 0x0209: //设置路灯开/关功率上下限值 //var pbk = new CmdHeader //{ // CmdCode = "0109", // Identity = header.OppositeId, // OppositeId = header.Identity, // ServerId = header.ServerId, // ResultCode = 1, // //TimeToken = header.TimeToken //}; var setp = SettingsRange.Parser.ParseFrom(bytes); var cdpk = new ComDealBk { CmdCode = "0109", Identity = header.OppositeId, OppositeId = header.Identity, ServerId = header.ServerId, ResultCode = 1 }; foreach (var c in setp.CellAddrs) { cdpk.Res.Add(new DealRes() { CellAddr = c, ResultCode = setp.CellAddrs.Count > 1 ? 0 : 1 }); } con.Send(cdpk.ToByteArray()); break; case 0x020A: //设置路灯开/关电压上下限值 //var ubk = new CmdHeader //{ // CmdCode = "010A", // Identity = header.OppositeId, // OppositeId = header.Identity, // ServerId = header.ServerId, // ResultCode = 1, // //TimeToken = header.TimeToken //}; var setu = SettingsRange.Parser.ParseFrom(bytes); var cduk = new ComDealBk { CmdCode = "010A", Identity = header.OppositeId, OppositeId = header.Identity, ServerId = header.ServerId, ResultCode = 1 }; foreach (var c in setu.CellAddrs) { cduk.Res.Add(new DealRes() { CellAddr = c, ResultCode = setu.CellAddrs.Count > 1 ? 0 : 1 }); } con.Send(cduk.ToByteArray()); break; case 0x020B: //设置温度上下限值 //var tembk = new CmdHeader //{ // CmdCode = "010B", // Identity = header.OppositeId, // OppositeId = header.Identity, // ServerId = header.ServerId, // ResultCode = 1, // //TimeToken = header.TimeToken //}; var sett = SettingsRange.Parser.ParseFrom(bytes); var cdtk = new ComDealBk { CmdCode = "010B", Identity = header.OppositeId, OppositeId = header.Identity, ServerId = header.ServerId, ResultCode = 1 }; foreach (var c in sett.CellAddrs) { cdtk.Res.Add(new DealRes() { CellAddr = c, ResultCode = sett.CellAddrs.Count > 1 ? 0 : 1 }); } con.Send(cdtk.ToByteArray()); break; case 0x020C: //设置阶段点 //var stageBk = new CmdHeader //{ // CmdCode = "010C", // Identity = header.OppositeId, // OppositeId = header.Identity, // ServerId = header.ServerId, // ResultCode = 1, // //TimeToken = header.TimeToken //}; var setr = SettingsRange.Parser.ParseFrom(bytes); var cdrk = new ComDealBk { CmdCode = "010C", Identity = header.OppositeId, OppositeId = header.Identity, ServerId = header.ServerId, ResultCode = 1 }; foreach (var c in setr.CellAddrs) { cdrk.Res.Add(new DealRes() { CellAddr = c, ResultCode = setr.CellAddrs.Count > 1 ? 0 : 1 }); } con.Send(cdrk.ToByteArray()); break; case 0x020D: //中继器重置 var restBk = new CmdHeader { CmdCode = 0x010D, Identity = header.OppositeId, OppositeId = header.Identity, ServerId = header.ServerId, ResultCode = 1, //TimeToken = header.TimeToken }; con.Send(restBk.ToByteArray()); break; case 0x020E: //设置默认功率 var dpBk = new CmdHeader { CmdCode = 0x010E, Identity = header.OppositeId, OppositeId = header.Identity, ServerId = header.ServerId, ResultCode = 1, //TimeToken = header.TimeToken }; con.Send(dpBk.ToByteArray()); break; case 0xFF0F: //硬件发送报警信息返回 break; } }, ShowMsg = msg => { lstMsg.Invoke(new Action(() => { lstMsg.Items.Add(msg); })); }, SocketConnectExceptionCallback = ex => { lstMsg.Invoke(new Action(() => { lstMsg.Items.Add(ex.Message); })); }, BeforeSend = (bs, con) => { var cmdHeader = CmdHeader.Parser.ParseFrom(bs); lstMsg.Invoke(new Action(() => { lstMsg.Items.Add($"耗时:{DateTime.Now-con.LastSessionTime}发送header{cmdHeader}"); })); } }); }
/// <summary> /// 处理设备上报状态 /// </summary> /// <param name="statusInfo"></param> /// <param name="con"></param> /// <param name="db"></param> public void ProcessReqest(DeviceStatus statusInfo, SocketConnection con, IClientRequest db) { if (!string.IsNullOrWhiteSpace(statusInfo.OppositeId)) {//立即返回当前设备状态 SocketConnection client; if (SocketConnectionServerDispatcher.DicSockectConnection.TryGetValue(statusInfo.OppositeId, out client) && client.ConnectSocket.Connected) { client.Send(statusInfo.ToByteArray()); } } var statusBk = new CmdHeader { CmdCode = 0xFF03, Identity = con.Identity, ServerId = SocketConnectionServerDispatcher.ServerId, //TimeToken = statusInfo.TimeToken, }; if (statusInfo.LightStatus == null) { statusBk.ResultCode = 0; con.Send(statusBk.ToByteArray()); return; } var deviceStatus = new DtoStatusInfo { ImeiNo = statusInfo.Identity, LightStatus = new DtoLightStatus() { lightstatus = statusInfo.LightStatus.Status, CellAddr = statusInfo.LightStatus.CellAddr, lightBt = statusInfo.LightStatus.LightBt, lightBu = statusInfo.LightStatus.LightBu, lightLi = statusInfo.LightStatus.LightLi, lightLu = statusInfo.LightStatus.LightLu, lightPw = statusInfo.LightStatus.LightPw, lightUi = statusInfo.LightStatus.LightUi, lightUu = statusInfo.LightStatus.LightUu, lightBgu = JsonConvert.SerializeObject(statusInfo.LightStatus.LightBgus) } }; //foreach (var l in statusInfo.LightStatus) //{ // deviceStatus.LightStatus.Add(new DtoLightStatus // { // lightstatus = l.Status, // CellAddr = l.CellAddr, // //lightNo = l.LightNo, // lightBt = l.LightBt, // lightBu = l.LightBu, // lightLi = l.LightLi, // lightLu = l.LightLu, // lightPw = l.LightPw, // lightUi = l.LightUi, // lightUu = l.LightUu, // lightBgu = JsonConvert.SerializeObject(l.LightBgus) // }); //} statusBk.ResultCode = db.DeviceStatusInfo(deviceStatus); con.Send(statusBk.ToByteArray()); SocketConnection.ShowMsg(statusInfo.ToString()); }
/// <summary> /// 开启服务端socket监听 /// </summary> public static void ServerSocketRefListing(SocketConnectConfig sktconfig) { var pfiles = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*Process.dll"); foreach (var fileName in pfiles) { var assembly = Assembly.LoadFile(fileName); var types = assembly.GetTypes().Where(e => e.GetInterfaces().Any(e1 => e1.Namespace != null && e1.Namespace.Equals("Socket.Reflect") && e1.Name.Equals("IProcess`1")));//IProcess`1 foreach (var type in types) { type.GetCustomAttributes().ToList().ForEach(e => { var code = e as CmdCodeAttribute; if (code != null && type.GetInterfaces().Length > 0) { ReflactProcesses.Add(code.CmdCode, new ProcessRef() { ProcessType = type, RefObj = Activator.CreateInstance(type), ArgType = type.GetInterfaces()[0].GetGenericArguments()[0] }); } }); } } var sktconfig2 = new SocketConnectConfig { AcceptCallback = sktconfig.AcceptCallback, BeforeSend = sktconfig.BeforeSend, ConnectCallback = sktconfig.ConnectCallback, DisConnectCallback = (con) => { sktconfig.DisConnectCallback(con); if (con.SocketConnectType == SocketConnectType.DeviceSocket) { //var appconfig = CommCode.AppSettings("gettuiappid", "gettuiappkey", "gettuimasterkey"); //if (con.StateData.BoundList == null) return; //foreach (var usrClient in con.StateData.BoundList) //{ // CommCode.GeTuiMsg("设备通知", "设备" + con.Identity + "离线", appconfig[0], appconfig[1], appconfig[2], usrClient.ClientId, null); //} ClientRequest.DeviceOffLine(new DtoOffLine { ImeiNo = con.Identity, RemoteIpEndPoint = con.ConnectSocket.RemoteEndPoint.ToString(), ServerIp = con.ConnectSocket.LocalEndPoint.ToString() }); } }, DisposeCallback = sktconfig.DisposeCallback, KeepAliveTime = sktconfig.KeepAliveTime, SendCallback = sktconfig.SendCallback, ShowMsg = sktconfig.ShowMsg, ReceiveCallback = (bytes, con) => { sktconfig.ReceiveCallback?.Invoke(bytes, con); //先执行外面注册展示层的方法 var header = CmdHeader.Parser.ParseFrom(bytes); if (string.IsNullOrWhiteSpace(header.Identity)) { return; } #region 反射执行Process SocketConnection oppSocketConnection; if (ReflactProcesses.Keys.Contains(header.CmdCode)) { var objProcess = ReflactProcesses[header.CmdCode]; var param = objProcess.ArgType.Name.Equals("CmdHeader") ? header : objProcess?.ArgType.GetProperty("Parser")? .PropertyType.GetMethod("ParseFrom", new[] { typeof(byte[]) })? .Invoke(objProcess.ArgType.GetProperty("Parser")?.GetValue(objProcess.RefObj), new object[] { bytes }); if (param == null) { return; } try { objProcess?.ProcessType.InvokeMember("ProcessReqest", BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Instance, null, objProcess.RefObj, new[] { param, con, ClientRequest }); } catch (Exception ex) { ShowMsg($"{DateTime.Now:yyyy/MM/dd HH:mm:ss.fffffff} 调用发生异常{(ex.InnerException != null ? ex.InnerException.StackTrace : ex.StackTrace)}"); } } else if (string.IsNullOrWhiteSpace(header.OppositeId)) //没有操作对象,不需要上位机处理 { var msgBk = new CmdHeader { CmdCode = header.CmdCode, Identity = header.Identity, ResultCode = 1, ServerId = ServerId, //TimeToken = header.TimeToken }; con.Send(msgBk.ToByteArray()); } else if (DicSockectConnection.TryGetValue(header.OppositeId, out oppSocketConnection)) { ShowMsg($"{DateTime.Now:yyyy/MM/dd HH:mm:ss.fffffff} 透传控制查询:{header.OppositeId}"); oppSocketConnection.Send(bytes); } else //设备离线 { var msgBk = new CmdHeader { CmdCode = header.CmdCode, Identity = header.Identity, OppositeId = header.OppositeId, ResultCode = 3, //离线 ServerId = ServerId, //TimeToken = header.TimeToken }; con.Send(msgBk.ToByteArray()); if (con.SocketConnectType == SocketConnectType.DeviceSocket) { ClientRequest.DeviceOffLine(new DtoOffLine { ImeiNo = header.OppositeId, RemoteIpEndPoint = "", ServerIp = "" }); } } #endregion } }; ServerSocketListing(sktconfig2); }