public MessageDataHandler(Agent agent) { this.agent = agent; }
// 国家中心 (Notice: 开始并不Connect, 区别于省) private Agent CreateCountryCenterAgent(string serverAddress, int serverPort) { Agent agent = new Agent(serverAddress, serverPort); agent.Type = Type.Country; agent.Wireless = false; agent.NotifyEvent += this.OnNotifyEvent; return agent; }
private void OnNotifyEvent(Agent agent, NotifyEvents notify, string msg1, string msg2) { if (this.quitPressed) return; this.SafeInvoke(() => { this.statusStrip.Items[0].Text = this.GetConnetionString(); if (NotifyEvents.Connecting == notify) { ConnetionRecord cr = new ConnetionRecord(); this.connectionHistory.Add(cr); } else if (NotifyEvents.Connected == notify) { int count = this.connectionHistory.Count; ConnetionRecord cr = this.connectionHistory[count - 1]; cr.ConnectedTime = DateTime.Now; } else if (NotifyEvents.Disconnect == notify) { int count = this.connectionHistory.Count; ConnetionRecord cr = this.connectionHistory[count - 1]; cr.DisconnectedTime = DateTime.Now; } else if (NotifyEvents.HandleEvent == notify) { string line = string.Format("{0}: {1} {2}", DateTime.Now, msg1, msg2); this.AddListItem(line); } else if (NotifyEvents.HistoryDataSent == notify) { string deviceKey = msg1.ToLower(); string line = string.Format("HD: {0}", msg2); Log.GetLogFile(deviceKey).Log(line); this.UpdateSendDataRecord(deviceKey, true); } /// 国家数据中心相关 else if (NotifyEvents.ConnectToCountryCenter == notify) { /// 国家数据中心相关 this.StartConnectCountryCenter(); this.AddListItem(msg1); } else if (NotifyEvents.DisconnectToCountryCenter == notify) { /// 国家数据中心相关 this.StopConnectCountryCenter(); this.AddListItem(msg1); } }); }
// 先连接有线的线路 private Agent CreateAgent(string serverAddress, int serverPort, bool wireless) { Agent agent = new Agent(serverAddress, serverPort); agent.Type = Type.Province; agent.Wireless = wireless; agent.NotifyEvent += this.OnNotifyEvent; return agent; }
private void InitializeAgents() { Settings s = Settings.Instance; foreach (Settings.DataCenter dc in s.DataCenters) { if (dc.CountryCenter) { // 国家中心 this.countryCenterAgent = CreateCountryCenterAgent(dc.Ip, dc.Port); this.countryCenterAgent.AddWirelessInfo(dc.WirelessIp, dc.WirelessPort); } else { // 省中心 Agent agent = CreateAgent(dc.Ip, dc.Port, false); agent.AddWirelessInfo(dc.WirelessIp, dc.WirelessPort); SynchronizationContext synchronizationContext = SynchronizationContext.Current; agent.UIThreadMashaller = new ThreadMashaller(synchronizationContext); this.agent = agent; this.agent.Connect(); this.agent.CanHandleSetTime = this.SetExceptionToolStripMenuItem.Checked; } } }
private void OnReceiveMessage(Agent agent, string msg) { if (!this.keepAliveCheckBox.Checked && ("6031" == Value.Parse(msg, "CN"))) { return; } this.SafeInvoke(() => { string line = string.Format("{0}: {1}", agent.ToString(false), msg); this.listBox1.Items.Add(line); }); }
private void OnNotifyEvent(Agent agent, NotifyEvent ne, string msg) { this.SafeInvoke(() => { if (NotifyEvent.Connected == ne) { string logger = agent.ToString() + " 已连接"; this.statusStrip1.Items[1].Text = logger; Log.GetLogFile(Program.System).Log(logger); } else if (NotifyEvent.ConnectError == ne) { this.statusStrip1.Items[1].Text = msg; Log.GetLogFile(Program.System).Log(msg); } else if (NotifyEvent.ConnectToCountryCenter == ne) { this.StartConnectCountryCenter(); this.listBox1.Items.Add(msg); Log.GetLogFile(Program.System).Log(msg); } else if (NotifyEvent.DisconnectToCountryCenter == ne) { this.StopConnectCountryCenter(); this.listBox1.Items.Add(msg); Log.GetLogFile(Program.System).Log(msg); } }); }
private void InitializeAgents() { Settings s = Settings.Instance; foreach (Settings.DataCenter dc in s.DataCenters) { if (dc.CountryCenter) { // 国家中心 this.countryCenterAgent = CreateCountryCenterAgent(dc.Ip, dc.Port); this.countryCenterAgent.AddWirelessInfo(dc.WirelessIp, dc.WirelessPort); } else { // 省中心 Agent agent = CreateAgent(dc.Ip, dc.Port, false); agent.AddWirelessInfo(dc.WirelessIp, dc.WirelessPort); this.agents.Add(agent); } } this.statusStrip1.Items[0].Text = string.Format("状态: 开始 ({0})", DateTime.Now); }