|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- 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平台测试
- {
- /// <summary>
- /// 以输送线设备作为测试对象
- /// </summary>
- 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<StockModel>();
- 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<EventModel>();
- 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<EventModel> { 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<EventModel>();
- // 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<EventModel> { 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;
-
- }
- /// <summary>
- /// 云端服务调用回调
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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<ServiceBaseResponse<object>>(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---------------------");
- }
- }
-
- /// <summary>
- /// 事件上报
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- /// <exception cref="NotImplementedException"></exception>
- 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<BaseMqResponse<object>>(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---------------------");
- }
-
- }
-
- /// <summary>
- /// 云端属性设置回调
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- /// <exception cref="NotImplementedException"></exception>
- 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<ServiceBaseResponse<object>>(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<BaseMqResponse<object>>(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---------------------");
- }
- }
- }
-
-
- /// <summary>
- /// 属性测试类
- /// </summary>
- 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;
-
- }
-
- /// <summary>
- /// 事件测试类
- /// </summary>
- public class EventModel
- {
- // public string Result { get; set; }
-
- }
- }
|