// 广播事件 private void btnBC_Click(object sender, System.EventArgs e) { //BroadCastForm bcForm = new BroadCastForm(); //bcForm.StartPosition = FormStartPosition.CenterParent; //bcForm.ShowDialog(); if (txtBCMsg.Text != string.Empty) { CommObj commObj = new CommObj(); commObj.SrcId = 0x00000001; commObj.DestId = 0x00000000; commObj.SendTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); commObj.DataType = "String"; commObj.DataBody = txtBCMsg.Text; string json = CommObj.ToJson(commObj); Obj.BroadCastingInfo(json); ILog log = log4net.LogManager.GetLogger("server.Logging"); log.Info("Obj.BroadCastingInfo--" + json); // log.Error("error", new Exception("发生了一个异常")); } else { MessageBox.Show("请输入信息!"); } }
public void CheckCommTestButton() { lock (this) Invoke(new MethodInvoker(delegate() { PLCControlObj obj = new PLCControlObj(0, 1000, 1, 1000, 1, 1000, 1, 1000); CommObj commObj = new CommObj(); commObj.SrcId = 0x10; commObj.DestId = 0x30; commObj.SendTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); commObj.DataType = "PLCControlObj"; commObj.DataCmd = ""; commObj.DataBody = PLCControlObj.ToByteJson(obj); string json = CommObj.ToJson(commObj); int N = 20; for (int i = 0; i < N; i++) { visComm.SendToServer(json); Thread.Sleep(1000); } })); }
// 响应客户端的事件 private void DefaultUpCastEventHandler(string msg) { // 接收并提取信息 CommObj commObj = CommObj.FromJson(msg); XYZ xyz = new XYZ(0, 0, 0); if (commObj == null) { serverComm.RcvMsg = "Json解析错误"; } else { commObj.RcvTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); serverComm.RcvMsg = commObj.ToString(); if (commObj.DataType.Equals("XYZ")) { xyz = JsonConvert.DeserializeObject <XYZ>(commObj.DataBody); } BroadCastMessage(msg); } // UI线程安全,监控,日志 new Thread(Check).Start(); }
// 定时发送消息到服务端 private void timer1_Tick(object sender, EventArgs e) { if (allTimerCount % 20 == 0) { this.timer1.Enabled = false; } allTimerCount++; PLCControlObj obj = new PLCControlObj(0, 500, 0, 0, 0, 0, 0, 0); CommObj commObj = new CommObj(); commObj.SrcId = 0x10; commObj.DestId = 0x30; commObj.SendTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); commObj.DataType = "PLCControlObj"; commObj.DataCmd = ""; commObj.DataBody = PLCControlObj.ToByteJson(obj); string json = CommObj.ToJson(commObj); visComm.SendToServer(json); // visComm.SendToServer(json); visLog.DisplaySendToServerInfo(json); }
public void ProcessBroadCastingInfo(CommObj commObj) { // 视觉端发送,PLC端接收 if (commObj.SrcId == 0x10 && commObj.DestId == 0x30) { if (commObj.DataType.Equals("PLCControlObj")) { PLCControlObj plcControlObj = PLCControlObj.FromByteJson(commObj.DataBody); SendToPLC(PLCControlObj.ToBytes(plcControlObj)); allSendCount++; } } }
private void SendToServer() { TcpClient tcpClient = new TcpClient(); IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1313); tcpClient.Connect(endPoint); NetworkStream stream = tcpClient.GetStream(); BinaryFormatter bf = new BinaryFormatter(); bf.AssemblyFormat = FormatterAssemblyStyle.Simple; //User userToCheck = new User(txtUsername.Text, txtPassword.Text); CommObj co = new CommObj(message, obj1, obj2); bf.Serialize(stream, co); returnedObj = (object)bf.Deserialize(stream); }
private void SendStatusMsgbutton_Click(object sender, EventArgs e) { CommObj commObj = new CommObj(); commObj.SrcId = 0x00000002; commObj.DestId = 0x00000000; commObj.SendTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); commObj.DataType = "String"; commObj.DataBody = "vision system"; string json = CommObj.ToJson(commObj); //VisComm.upCast.SendMsg(json); VisComm.SendToServer(json); log.Info("visUpcast.SendMsg--" + json); }
protected void cmdRegister_Click(object sender, EventArgs e) { UserStore <IdentityUser> userStore = new UserStore <IdentityUser>(); UserManager <IdentityUser> manager = new UserManager <IdentityUser>(userStore); //Attempt to register new user IdentityUser user = new IdentityUser() { UserName = txtUsername.Text }; //IdentityResult result = manager.Create(user, txtPassword.Text); //Begin Server Code TcpClient tcpClient = new TcpClient(); IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1313); tcpClient.Connect(endPoint); NetworkStream stream = tcpClient.GetStream(); BinaryFormatter bf = new BinaryFormatter(); bf.AssemblyFormat = FormatterAssemblyStyle.Simple; User userToCheck = new User(txtUsername.Text, txtPassword.Text); CommObj co = new CommObj("CreateUser", (object)userToCheck); bf.Serialize(stream, co); bool result = (bool)bf.Deserialize(stream); //If success if (result) { litStatus.Text = "User " + user.UserName + " has been successfully registered!"; //login new user //var authenticationManager = HttpContext.Current.GetOwinContext().Authentication; //var userIdentity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie); //authenticationManager.SignIn(new AuthenticationProperties() { }, userIdentity); //Go to login page Response.Redirect("~/Login.aspx"); } else { litStatus.Text = "There was a problem registering this account"; } }
// 开启客户端 // 结束客户端 // 响应广播信息 public void OnBroadCastingInfo(string message) { CommObj commObj = CommObj.FromJson(message); if (commObj == null) { visComm.RcvMsg = "Json解析错误"; } else { commObj.RcvTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); visComm.RcvMsg = commObj.ToString(); } new Thread(Check).Start(); log.Info("BroadCastingMessage--" + visComm.RcvMsg); }
// 响应广播信息 public void BroadCastingMessage(string message) { CommObj commObj = CommObj.FromJson(message); if (commObj == null) { rcvMsg = "Json解析错误"; } else { commObj.RcvTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); rcvMsg = commObj.ToString(); } new Thread(Check).Start(); ILog log = log4net.LogManager.GetLogger("server.Logging"); log.Info("BroadCastingMessage--" + rcvMsg); }
public void OnBroadCastingInfo(string message) { CommObj commObj = CommObj.FromJson(message); if (commObj == null) { moverComm.RcvMsg = "Json解析错误"; } else { commObj.RcvTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); moverComm.RcvMsg = commObj.ToString(); ProcessBroadCastingInfo(commObj); } new Thread(Check).Start(); moverLog.DisplayBroadCastInfo(moverComm.RcvMsg); }
protected void cmdSignIn_Click(object sender, EventArgs e) { UserStore <IdentityUser> userStore = new UserStore <IdentityUser>(); UserManager <IdentityUser> userManager = new UserManager <IdentityUser>(userStore); //search user //IdentityUser user = userManager.Find(txtUsername.Text, txtPassword.Text); //Begin Server Code TcpClient tcpClient = new TcpClient(); IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1313); tcpClient.Connect(endPoint); NetworkStream stream = tcpClient.GetStream(); BinaryFormatter bf = new BinaryFormatter(); bf.AssemblyFormat = FormatterAssemblyStyle.Simple; User userToCheck = new User(txtUsername.Text, txtPassword.Text); CommObj co = new CommObj("AuthenticateUser", (object)userToCheck); bf.Serialize(stream, co); bool result = (bool)bf.Deserialize(stream); //If the user exists //if (user != null) if (result) { //IAuthenticationManager authenticationManager = HttpContext.Current.GetOwinContext().Authentication; //ClaimsIdentity userIdentity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie); //Sign in //authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, userIdentity); Session["Username"] = txtUsername.Text; Response.Redirect("~/Login.aspx"); } else //if the user does not exist { litStatus.Text = "Invalid login or password"; Session["Username"] = ""; phLoginStatus.Visible = true; } }
// 响应客户端的事件 public void OnUpCastEvent(string msg) { CommObj commObj = CommObj.FromJson(msg); if (commObj == null) { rcvMsg = "Json解析错误"; } else { commObj.RcvTime = DateTimeNowFmt1(); rcvMsg = commObj.ToString(); } //lbMonitor.Items.Add(rcvMsg); new Thread(Check).Start(); ILog log = log4net.LogManager.GetLogger("server.Logging"); log.Info("OnUpCastEvent--" + rcvMsg); }
private void btnSend_Click(object sender, System.EventArgs e) { if (txtInfo.Text != string.Empty) { CommObj commObj = new CommObj(0x10, 0x01, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "string", txtInfo.Text); string json = CommObj.ToJson(commObj); upCast.SendMsg(json); ILog log = log4net.LogManager.GetLogger("server.Logging"); log.Info("upCast.SendMsg--" + json); } else { MessageBox.Show("请输入信息!"); } }
// 广播事件 private void 广播ToolStripMenuItem_Click(object sender, EventArgs e) { string text = "这是广播的测试信息"; try { CommObj commObj = new CommObj(); commObj.SrcId = 0xff; commObj.DestId = 0x0; commObj.SendTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); commObj.DataType = "String"; commObj.DataBody = text; string json = CommObj.ToJson(commObj); BroadCastMessage(json); methodHandlerComposite.OnBroadCastMessage(json); } catch (Exception ex) { methodHandlerComposite.OnException("广播ToolStripMenuItem_Click", ex.Message); } }
public void Run() { NetworkStream stream = tcpClient.GetStream(); BinaryFormatter bf = new BinaryFormatter(); bf.AssemblyFormat = FormatterAssemblyStyle.Simple; object o = bf.Deserialize(stream); CommObj commobj = o as CommObj; Console.WriteLine("new " + commobj.command + " request"); if (commobj.command == "AuthenticateUser") { Console.WriteLine("Authenticating User"); try { User myUser = (User)commobj.myObject; ServerMgr sm = new ServerMgr(); //IdentityUser iu = sm.AuthenticateUser(myUser.username, myUser.password); bool iu = sm.AuthenticateUser(myUser.username, myUser.password); if (iu) { Console.WriteLine(myUser.username + " Authenticated"); } else { Console.WriteLine(myUser.username + " NOT Authenticated"); } bf.Serialize(stream, iu); //May not be correct } catch (Exception e) { Console.WriteLine("Exception: " + e); } } else if (commobj.command == "CreateUser") { Console.WriteLine("Creating New User"); try { User myUser = (User)commobj.myObject; ServerMgr sm = new ServerMgr(); //IdentityResult ir = sm.CreateUser(myUser.username, myUser.password); bool ir = sm.CreateUser(myUser.username, myUser.password); if (ir) { Console.WriteLine(myUser.username + " Created"); } else { Console.WriteLine(myUser.username + " NOT Created"); } bf.Serialize(stream, ir); } catch (Exception e) { Console.WriteLine("Exception: " + e); } } else if (commobj.command == "AddMeet") { Console.WriteLine("Add Meet"); try { //bool AddMeet(Meet meet, string user); Meet meet = (Meet)commobj.myObject; string user = commobj.myObject2.ToString(); MeetDBMgr mdbm = new MeetDBMgr(); bool result = mdbm.AddMeet(meet, user); if (result) { Console.WriteLine("Meet for " + user + " Added"); } else { Console.WriteLine("Problem adding Meet for " + user + " Added"); } bf.Serialize(stream, result); } catch (Exception e) { Console.WriteLine("Exception: " + e); } } else if (commobj.command == "FindMeetId") { Console.WriteLine("Find Meet ID"); try { //int FindMeetId(Meet meet); Meet meet = (Meet)commobj.myObject; MeetDBMgr mdbm = new MeetDBMgr(); int meetID = mdbm.FindMeetId(meet); Console.WriteLine("Meet found, ID:" + meetID); bf.Serialize(stream, meetID); } catch (Exception e) { Console.WriteLine("Exception: " + e); } } else if (commobj.command == "FindMeet") { Console.WriteLine("Find Meet"); try { //Meet FindMeet(int id); int meetID = (int)commobj.myObject; MeetDBMgr mdbm = new MeetDBMgr(); Meet myMeet = mdbm.FindMeet(meetID); Console.WriteLine("Meet found at:" + meetID); bf.Serialize(stream, myMeet); } catch (Exception e) { Console.WriteLine("Exception: " + e); } } else if (commobj.command == "DeleteMeet") { //public bool DeleteMeet(int id) Console.WriteLine("Delete Meet"); try { //Meet FindMeet(int id); int meetID = (int)commobj.myObject; MeetDBMgr mdbm = new MeetDBMgr(); bool didDelete = mdbm.DeleteMeet(meetID); Console.WriteLine("Meet deleted? " + didDelete); bf.Serialize(stream, didDelete); } catch (Exception e) { Console.WriteLine("Exception: " + e); } } else if (commobj.command == "AddBoysTeam") { //public bool AddBoysTeam(Meet meet) Console.WriteLine("Add Boys Team"); try { //Meet FindMeet(int id); Meet meet = (Meet)commobj.myObject; MeetDBMgr mdbm = new MeetDBMgr(); bool didAdd = mdbm.AddBoysTeam(meet); Console.WriteLine("Meet added? " + didAdd); bf.Serialize(stream, didAdd); } catch (Exception e) { Console.WriteLine("Exception: " + e); } } else if (commobj.command == "FindBoysTeam") { //public Dictionary<string, string> FindBoysTeam(int id) Console.WriteLine("Find Boys Team"); try { //Meet FindMeet(int id); int id = (int)commobj.myObject; MeetDBMgr mdbm = new MeetDBMgr(); Dictionary <string, string> boysTeam = mdbm.FindBoysTeam(id); Console.WriteLine("Meet Dictionary Created and being sent"); bf.Serialize(stream, boysTeam); } catch (Exception e) { Console.WriteLine("Exception: " + e); } } else if (commobj.command == "DeleteBoysTeam") { //public bool DeleteBoysTeam(int id) Console.WriteLine("Delete Boys Team"); try { //Meet FindMeet(int id); int id = (int)commobj.myObject; MeetDBMgr mdbm = new MeetDBMgr(); bool didDelete = mdbm.DeleteBoysTeam(id); Console.WriteLine("Meet Deleted at: " + id); bf.Serialize(stream, didDelete); } catch (Exception e) { Console.WriteLine("Exception: " + e); } } else if (commobj.command == "AddGirlsTeam") { //public bool AddGirlsTeam(Meet meet) Console.WriteLine("Add Girls Team"); try { //Meet FindMeet(int id); Meet meet = (Meet)commobj.myObject; MeetDBMgr mdbm = new MeetDBMgr(); bool didAdd = mdbm.AddGirlsTeam(meet); Console.WriteLine("Meet added? " + didAdd); bf.Serialize(stream, didAdd); } catch (Exception e) { Console.WriteLine("Exception: " + e); } } else if (commobj.command == "FindGirlsTeam") { //public Dictionary<string, string> FindGirlsTeam(int id) Console.WriteLine("Find Girls Team"); try { //Meet FindMeet(int id); int id = (int)commobj.myObject; MeetDBMgr mdbm = new MeetDBMgr(); Dictionary <string, string> girlsTeam = mdbm.FindGirlsTeam(id); Console.WriteLine("Meet Dictionary Created and being sent"); bf.Serialize(stream, girlsTeam); } catch (Exception e) { Console.WriteLine("Exception: " + e); } } else if (commobj.command == "DeleteGirlsTeam") { //public bool DeleteGirlsTeam(int id) Console.WriteLine("Delete Girls Team"); try { //Meet FindMeet(int id); int id = (int)commobj.myObject; MeetDBMgr mdbm = new MeetDBMgr(); bool didDelete = mdbm.DeleteGirlsTeam(id); Console.WriteLine("Meet Deleted at: " + id); bf.Serialize(stream, didDelete); } catch (Exception e) { Console.WriteLine("Exception: " + e); } } else if (commobj.command == "AddPerformances") { //public bool AddPerformances(Meet meet) Console.WriteLine("Add Performances"); try { Meet meet = (Meet)commobj.myObject; MeetDBMgr mdbm = new MeetDBMgr(); bool didAdd = mdbm.AddPerformances(meet); Console.WriteLine("Performances Added? " + didAdd); bf.Serialize(stream, didAdd); } catch (Exception e) { Console.WriteLine("Exception: " + e); } } else if (commobj.command == "AddPerformance") { //public bool AddPerformance(Meet meet, string eventName) Console.WriteLine("Add Performance"); try { Meet meet = (Meet)commobj.myObject; string eventName = commobj.myObject2.ToString(); MeetDBMgr mdbm = new MeetDBMgr(); bool didAdd = mdbm.AddPerformance(meet, eventName); Console.WriteLine("Performances Added? " + didAdd); bf.Serialize(stream, didAdd); } catch (Exception e) { Console.WriteLine("Exception: " + e); } } else if (commobj.command == "FindPerformancesDictionary") { //public Dictionary<string, List<Performance>> FindPerformances(int id) Console.WriteLine("Find Performances - List"); try { int id = (int)commobj.myObject; MeetDBMgr mdbm = new MeetDBMgr(); Dictionary <string, List <Performance> > myDictionary = mdbm.FindPerformances(id); Console.WriteLine("Dictionary of Performances added and being sent"); bf.Serialize(stream, myDictionary); } catch (Exception e) { Console.WriteLine("Exception: " + e); } } else if (commobj.command == "FindPerformancesList") { //public List<Performance> FindPerformances(int id, string eventName) Console.WriteLine("Find Performancess - Dictionary"); try { int id = (int)commobj.myObject; string eventName = commobj.myObject2.ToString(); MeetDBMgr mdbm = new MeetDBMgr(); List <Performance> myList = mdbm.FindPerformances(id, eventName); Console.WriteLine("List of Performances added and being sent"); bf.Serialize(stream, myList); } catch (Exception e) { Console.WriteLine("Exception: " + e); } } else if (commobj.command == "DeletePerformances") { //public bool DeletePerformances(int id) Console.WriteLine("Delete Performances"); try { int id = (int)commobj.myObject; MeetDBMgr mdbm = new MeetDBMgr(); bool didDelete = mdbm.DeletePerformances(id); Console.WriteLine("Performances deleted? " + didDelete); bf.Serialize(stream, didDelete); } catch (Exception e) { Console.WriteLine("Exception: " + e); } } else if (commobj.command == "DeletePerformance") { //public bool DeletePerformance(int id, string eventName) Console.WriteLine("Delete Performance"); try { int id = (int)commobj.myObject; string eventName = commobj.myObject2.ToString(); MeetDBMgr mdbm = new MeetDBMgr(); bool didDelete = mdbm.DeletePerformance(id, eventName); Console.WriteLine("Performance deleted? " + didDelete); bf.Serialize(stream, didDelete); } catch (Exception e) { Console.WriteLine("Exception: " + e); } } else if (commobj.command == "ListOfMeets") { //public Dictionary<int, Meet> ListOfMeets(string user) Console.WriteLine("List Of Meets"); try { string user = commobj.myObject.ToString(); MeetDBMgr mdbm = new MeetDBMgr(); Dictionary <int, Meet> listOfMeets = mdbm.ListOfMeets(user); Console.WriteLine("List of meets gathered and being sent"); bf.Serialize(stream, listOfMeets); } catch (Exception e) { Console.WriteLine("Exception: " + e); } } else { Console.WriteLine(commobj.command + " command not found in TCPClientServer"); } tcpClient.Close(); }