Program.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace MqttClient
  9. {
  10. class Program
  11. {
  12. [DllImport("DataTransferDll.dll", EntryPoint = "SetLogFileDir", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  13. public static extern int SetLogFileDir(string logFileDir);
  14. [DllImport("DataTransferDll.dll", EntryPoint = "Open", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  15. public static extern int Open(string a, int b);
  16. [DllImport("DataTransferDll.dll", EntryPoint = "Write", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  17. public static extern int Write(string id, string value);
  18. // 配合使用ToUTF8,发送带中文的value
  19. [DllImport("DataTransferDll.dll", EntryPoint = "Write", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  20. public static extern int Write(string id, byte[] value);
  21. [DllImport("DataTransferDll.dll", EntryPoint = "WriteWithDataId", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  22. public static extern int WriteWithDataId(string id, string value, string dataId);
  23. // 配合使用ToUTF8,发送带中文的value
  24. [DllImport("DataTransferDll.dll", EntryPoint = "WriteWithDataId", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  25. public static extern int WriteWithDataId(string id, byte[] value, string dataId);
  26. [DllImport("DataTransferDll.dll", EntryPoint = "SetCallbackWithDataId", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  27. private static extern void SetCallbackWithDataId(CallbackWithDataIdDelegate callback);
  28. [DllImport("DataTransferDll.dll", EntryPoint = "Close", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  29. public static extern int Close();
  30. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  31. public delegate void CallbackWithDataIdDelegate(string id, string value, string dataId);
  32. [DllImport("DataTransferDll.dll", EntryPoint = "ParameterConfig", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  33. public static extern int ParameterConfig(string parameterJson);
  34. static public void CallbackWithDataId(string id, string v, string dataId)
  35. {
  36. Console.WriteLine("-------CallbackWithDataId-------");
  37. byte[] buffer1 = Encoding.Default.GetBytes(v);
  38. byte[] buffer2 = Encoding.Convert(Encoding.UTF8, Encoding.Default, buffer1, 0, buffer1.Length);
  39. string strBuffer = Encoding.Default.GetString(buffer2, 0, buffer2.Length);
  40. Console.WriteLine("{0} -> {1} {2}", id, strBuffer, dataId);
  41. }
  42. public static byte[] ToUTF8(string text)
  43. {
  44. byte[] buffer1 = Encoding.Unicode.GetBytes(text);
  45. byte[] buffer2 = Encoding.Convert(Encoding.Unicode, Encoding.UTF8, buffer1, 0, buffer1.Length);
  46. return buffer2;
  47. }
  48. static void Main(string[] args)
  49. {
  50. string ip_addr = "127.0.0.1";
  51. int ip_port = 6666;
  52. // 连接MQTT工具
  53. Open(ip_addr, ip_port);
  54. // 设置回调函数
  55. SetCallbackWithDataId(CallbackWithDataId);
  56. // 参数配置
  57. string parameterJson = "{\r\n \"function\":\"parameterConfig\",\r\n \"redId\":\"123456\",\r\n \"parameter\":{\r\n \"mqtt\":{\r\n \"address\":\"staging-cnbj2-rmq-mqtt.api.xiaomi.com\",\r\n \"port\":\"80\",\r\n \"username\":\"AKMO5BGFQUZL2SBW4X\",\r\n \"password\":\"5AChJjCOYB+No68BpyDocd7uR7cv/foE20RKIpOE\"\r\n },\r\n \"equiment\":{\r\n \"factoryCode\":\"\",\r\n \"deviceCode\":\"DIGITION-TEST\",\r\n \"stationCode\":\"\",\r\n \"project\":\"\",\r\n \"productMode\":\"debug\"\r\n },\r\n \"other\":{\r\n \"logLevel\":\"0\",\r\n \"runMode\":\"online\",\r\n \"uploadDigitalTwinData\":\"true\"\r\n }}}";
  58. ParameterConfig(parameterJson);
  59. Thread.Sleep(3000);
  60. for (int i = 0; i < 10; i++)
  61. {
  62. string msg = "{\"beat_tm\":\"2023-03-31 13:54:27.937\",\"signal_name\":\"中文\",\"signal_type\":\"DI\",\"target_status\":true}";
  63. // 写入ID、值
  64. int ret = Write("beat_log/control/device_control_signal/DT_motionbeat", ToUTF8(msg));
  65. Console.WriteLine("第{0}次,ret = {1}", i, ret);
  66. Thread.Sleep(50);
  67. }
  68. Console.WriteLine("请按任意键继续...");
  69. Console.ReadKey();
  70. // 关闭连接
  71. Close();
  72. }
  73. }
  74. }