using BPA.PahoMQ; using Newtonsoft.Json; using System.Collections.Specialized; using System.Reflection; using System.Text; using System.Text.RegularExpressions; using uPLibrary.Networking.M2Mqtt.Messages; namespace 阿里云IOT平台测试 { /// /// 以输送线设备作为测试对象 /// internal class Program { public static bool PropertyReport = false; //属性上报主题订阅标志 //public static bool PropertySet = false; //属性设置主题订阅标志 public static bool EventReport = false; //事件上报主题订阅标志 //public static bool ServiceCall = false; //服务调用主题订阅标志 public static string productKey = "j090GlJPJNv"; //public static string deviceName = "Conveyer"; //public static string deviceSecret = "b3df8aaa82de59f478dffb172acf78cd"; public static string deviceName = "Stock1"; public static string deviceSecret = "b0928d8f6e36aabd94f0fce3f9b02536"; private static MessageHandler _messageHandler; static void Main(string[] args) { //string s= JsonConvert.SerializeObject(new Test()); //string s2 = JsonConvert.SerializeXmlNode(); //string s1 = JsonConvert.SerializeXNode(new Test()); if (_messageHandler == null) _messageHandler = new MessageHandler(new PahoMQOption() { Broker = "iot-06z00carjduqaue.mqtt.iothub.aliyuncs.com", ProductKey = productKey, DeviceName = deviceName, DeviceSecret = deviceSecret, Port = 1883 }); //云端属性设置主题订阅及回调 _messageHandler.Subscribe((op) => { op.TopicReply = $"/sys/{productKey}/{deviceName}/thing/service/property/set"; op.ThresholdCallback += Op_ThresholdCallback; }); //服务订阅 var serviceIdentifier = "RecipeSendService"; var serviceTopicReply = $"/sys/{productKey}/{deviceName}/thing/service/{serviceIdentifier}"; _messageHandler.Subscribe((op) => { op.TopicReply = serviceTopicReply; op.ThresholdCallback += Op_ThresholdCallback1; ; // MqttPostProperty_MqttMsgPublishReceived; }); Console.WriteLine("云端属性设置主题订阅成功"); Console.WriteLine("云端服务调用主题订阅成功"); ep: string result = Console.ReadLine(); if (result.Equals("属性上报")) { //属性上报与订阅 var mqdeviceTestProperty = new PostPropertyParams(); mqdeviceTestProperty.sys = new Sys { ack = 1 }; mqdeviceTestProperty.method = "thing.event.property.post"; mqdeviceTestProperty.version = "1.0"; //默认模板 // mqdeviceTestProperty.Model = new StockModel(); //自定义模板 mqdeviceTestProperty.ModelName = "StockModel"; mqdeviceTestProperty.Model = new StockModel(); mqdeviceTestProperty.Init(); var message = JsonConvert.SerializeObject(mqdeviceTestProperty); var topic = $"/sys/{productKey}/{deviceName}/thing/event/property/post";//发布主题 var topicReply = $"/sys/{productKey}/{deviceName}/thing/event/property/post_reply";//订阅主题 _messageHandler.Publish((op) => { op.Message = message; op.Topic = topic; op.TopicReply = topicReply; if (!PropertyReport) { op.ThresholdCallback += MqttPostProperty_MqttMsgPublishReceived; // MqttPostProperty_MqttMsgPublishReceived; PropertyReport = true; } }); } if (result.Equals("事件上报")) { string eventIdentifier = "StockModel:StockJerk1StatusEvent"; var mqdeviceTestEvent = new PostEventParams(); mqdeviceTestEvent.id = Guid.NewGuid().ToString(); mqdeviceTestEvent.sys = new Sys { ack = 1 }; mqdeviceTestEvent.method = $"thing.event.{eventIdentifier}.post"; mqdeviceTestEvent.version = "1.0"; mqdeviceTestEvent.@params = new EventParam { value = new EventModel (), time = new DateTimeOffset(DateTime.Now).ToUnixTimeMilliseconds() }; var eventMessage = JsonConvert.SerializeObject(mqdeviceTestEvent); //事件上报 var eventTopic = $"/sys/{productKey}/{deviceName}/thing/event/{eventIdentifier}/post"; //事件订阅主题 var eventTopicReply = $"/sys/{productKey}/{deviceName}/thing/event/{eventIdentifier}/post_reply"; _messageHandler.Publish((op) => { op.Message = eventMessage; op.Topic = eventTopic; op.TopicReply = eventTopicReply; if (!EventReport) { op.ThresholdCallback += MqttPostEvent_MqttMsgPublishReceived; // MqttPostProperty_MqttMsgPublishReceived; EventReport= true; } }); } //if (result.Equals("事件上报2")) //{ // string eventIdentifier = "StockServoFaultEvent"; // var mqdeviceTestEvent = new PostEventParams(); // mqdeviceTestEvent.id = Guid.NewGuid().ToString(); // mqdeviceTestEvent.sys = new Sys { ack = 1 }; // mqdeviceTestEvent.method = $"thing.event.{eventIdentifier}.post"; // mqdeviceTestEvent.version = "1.0"; // mqdeviceTestEvent.@params = new EventParam { value = new EventModel { StockNum = 1 }, time = new DateTimeOffset(DateTime.Now).ToUnixTimeMilliseconds() }; // var eventMessage = JsonConvert.SerializeObject(mqdeviceTestEvent); // //事件上报 // var eventTopic = $"/sys/{productKey}/{deviceName}/thing/event/{eventIdentifier}/post"; // //事件订阅主题 // var eventTopicReply = $"/sys/{productKey}/{deviceName}/thing/event/{eventIdentifier}/post_reply"; // _messageHandler.Publish((op) => // { // op.Message = eventMessage; // op.Topic = eventTopic; // op.TopicReply = eventTopicReply; // if (!EventReport) // { // op.ThresholdCallback += MqttPostEvent_MqttMsgPublishReceived; // MqttPostProperty_MqttMsgPublishReceived; // EventReport = true; // } // }); // } goto ep; } /// /// 云端服务调用回调 /// /// /// private static void Op_ThresholdCallback1(object sender, MqttMsgPublishEventArgs e) { if (Regex.IsMatch(e.Topic, "[0-9a-zA-Z/]*thing/service/[0-9a-zA-Z:]*$")) { Console.WriteLine("---------------------------云端设备服务调用Start---------------------"); Console.WriteLine("reply topic :" + e.Topic); var rtn = JsonConvert.DeserializeObject>(Encoding.UTF8.GetString(e.Message, 0, e.Message.Length)); PropertyInfo property = rtn.@params.GetType().GetProperty("Root"); string result = property.GetValue(rtn.@params).ToString(); var array = result.Replace("\r\n", "").Replace(" ", "").Replace("{", "").Replace("}", "").Replace("\"", "").Split(','); Console.WriteLine("reply payload:" + Encoding.UTF8.GetString(e.Message, 0, e.Message.Length)); Console.WriteLine("---------------------------云端设备服务调用End---------------------"); } } /// /// 事件上报 /// /// /// /// private static void MqttPostEvent_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e) { if (Regex.IsMatch(e.Topic, "[0-9a-zA-Z/]*thing/event/(?!property).*/(post_reply)$")) { Console.WriteLine("---------------------------设备事件上报Start---------------------"); Console.WriteLine("reply topic :" + e.Topic); var rtn = JsonConvert.DeserializeObject>(Encoding.UTF8.GetString(e.Message, 0, e.Message.Length)); Console.WriteLine("reply payload:" + Encoding.UTF8.GetString(e.Message, 0, e.Message.Length)); Console.WriteLine("---------------------------设备事件上报End---------------------"); } } /// /// 云端属性设置回调 /// /// /// /// private static void Op_ThresholdCallback(object sender, MqttMsgPublishEventArgs e) { if (Regex.IsMatch(e.Topic, "[0-9a-zA-Z/]*(thing/service/property/set)$")) { Console.WriteLine("---------------------------云端设置属性Start---------------------"); Console.WriteLine("reply topic :" + e.Topic); var rtn = JsonConvert.DeserializeObject>(Encoding.UTF8.GetString(e.Message, 0, e.Message.Length)); PropertyInfo property= rtn.@params.GetType().GetProperty("Root"); string result=property.GetValue(rtn.@params).ToString(); var array= result.Replace("\r\n", "").Replace(" ","").Replace("{","").Replace("}","").Replace("\"", "").Split(','); foreach (var item in array) { var str=item.Split(":"); //数据格式: switch (str[1]) { } } Console.WriteLine("reply payload:" + Encoding.UTF8.GetString(e.Message, 0, e.Message.Length)); Console.WriteLine("---------------------------云端设置属性End---------------------"); } } private static void MqttPostProperty_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e) { //考虑使用正则表达判断是属性,事件,服务其中那个回调 if (Regex.IsMatch(e.Topic, "[0-9a-zA-Z/]*thing/event/property/post_reply")) { Console.WriteLine("---------------------------设备端属性上报回调Start---------------------"); var rtn = JsonConvert.DeserializeObject>(Encoding.UTF8.GetString(e.Message, 0, e.Message.Length)); Console.WriteLine($"code:{rtn.code}"); Console.WriteLine("reply topic :" + e.Topic); Console.WriteLine("messageID :" + rtn.id); Console.WriteLine("reply payload:" + Encoding.UTF8.GetString(e.Message, 0, e.Message.Length)); Console.WriteLine("---------------------------设备端属性上报回调End---------------------"); } } } /// /// 属性测试类 /// public class StockModel { //public string Test { get; set; } = "测试2"; public int StockRealWeight { get; set; } = 250000; public float BucketRealWeight { get; set; } = 16000; public string StockBatchStatus { get; set; } = "配料完成"; public int StockWorkModel { get; set; } = 1; } /// /// 事件测试类 /// public class EventModel { // public string Result { get; set; } } }