Exemplo n.º 1
0
 private void SendBridge_Click(object sender, EventArgs e)
 {
     byte[] payload = LogHelper.Payload(textToPayload.Text.ToCharArray());
     LogHelper.Log(bridges[comboBoxBridge.SelectedIndex] + " send  [" + LogHelper.Logdata(payload, 0, payload.Length) + "]");
     // PrinterHelper.SetTextData(textToPayload.Text);
     PrinterHelper.PrintPage(textToPayload.Text);
     if (bridges[comboBoxBridge.SelectedIndex] == "SerialPort")
     {
     }
     else if (bridges[comboBoxBridge.SelectedIndex] == "TcpServer")
     {
         TcpServerHelper.Write(payload, 0, payload.Length);
     }
     else if (bridges[comboBoxBridge.SelectedIndex] == "Barcode_Printer")
     {
         TcpServerHelper.Write(payload, 0, payload.Length);
     }
     else if (bridges[comboBoxBridge.SelectedIndex] == "PLC")
     {
     }
     else if (bridges[comboBoxBridge.SelectedIndex] == "OPCUA")
     {
     }
     else if (bridges[comboBoxBridge.SelectedIndex] == "BACnet")
     {
         BACnetHelper.Write(payload, 0, payload.Length);
     }
     else if (bridges[comboBoxBridge.SelectedIndex] == "Control")
     {
     }
     else if (bridges[comboBoxBridge.SelectedIndex] == "Access")
     {
     }
     else if (bridges[comboBoxBridge.SelectedIndex] == "SqlServer")
     {
     }
     else if (bridges[comboBoxBridge.SelectedIndex] == "MqttClient")
     {
         MqttClientHelper.Write(payload, 0, payload.Length);
     }
     else if (bridges[comboBoxBridge.SelectedIndex] == "MqttServer")
     {
         MqttServerHelper.Write(payload, 0, payload.Length);
     }
     else if (bridges[comboBoxBridge.SelectedIndex] == "TcpClient")
     {
         TcpClientHelper.Write(payload, 0, payload.Length);
     }
     else if (bridges[comboBoxBridge.SelectedIndex] == "UdpClient")
     {
     }
     else
     {
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// 接收到消息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void MqttClient_ApplicationMessageReceived(object sender, MqttApplicationMessageReceivedEventArgs e)
        {
            Dictionary <string, object> json = Get_payload(e.ApplicationMessage.Payload);
            string topic = e.ApplicationMessage.Topic;
            string data  = Encoding.UTF8.GetString(e.ApplicationMessage.Payload);

            LogHelper.Log("mqtt recv:topic: " + e.ApplicationMessage.Topic.ToString() + " payload: " + data);

            Regex r_subtopic = new Regex(subtopic);                          // 定义一个Regex对象实例
            Match m_subtopic = r_subtopic.Match(e.ApplicationMessage.Topic); // 在字符串中匹配

            if (m_subtopic.Success)
            {
                SerialPortHelper.Write(e.ApplicationMessage.Payload, 0, e.ApplicationMessage.Payload.Length);
            }
            if (topic.IndexOf("$dg/device/" + username + "/" + dtuAddr) == 0)
            {
                if (json.ContainsKey("cmd"))
                {
                    if (json["cmd"].ToString() == "opc_items")
                    {
                        OPCDAHelper.Readitems(json);
                    }
                    else if (json["cmd"].ToString() == "opc_report")
                    {
                        OPCDAHelper.Publishvalues(json);
                    }
                    else if (json["cmd"].ToString() == "printer")
                    {
                        PrinterHelper.PrintPage(json["data"].ToString());
                    }
                }
            }

            AccessHelper.Do_mdb(topic, json, clientid);

            MqttServerHelper.Write(e.ApplicationMessage);
        }