123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- using Edge.Core.IndustryStandardInterface.NetworkController;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Security.Cryptography;
- using System.Text;
- using System.Text.Json;
- using System.Threading;
- using System.Threading.Tasks;
- using static DeviceInfoToAliIotHubViaGateway.App;
- namespace DeviceInfoToAliIotHubViaGateway
- {
- public static class ExtMethod
- {
- public static string TopicMaker(this string topicFormatStr, string productKey, string deviceName)
- {
- return topicFormatStr.Replace("{productKey}", productKey).Replace("{deviceName}", deviceName);
- }
- public static string TopicMaker(this string topicFormatStr, string productKey, string deviceName, string eventName)
- {
- return topicFormatStr.Replace("{productKey}", productKey).Replace("{deviceName}", deviceName).Replace("{tsl.event.identifier}", eventName);
- }
- public static string SignWithHMacSHA1(this byte[] target, byte[] key)
- {
- using (HMACSHA1 hmac = new HMACSHA1(key))
- {
- var hash = hmac.ComputeHash(target);
- return hash.Select(h => h.ToString("X2")).Aggregate((acc, n) => acc + n);
- }
- }
-
-
-
-
-
-
-
- public static Task<T> WaitFor<T>(this IMqttClientNetworkController client, string topic, int timeout) where T : class
- {
- int isResponseGot = 0;
- var source = new TaskCompletionSource<T>();
- EventHandler<OnMqttMessageReceivedEventArg> callback = null;
- callback = (a, b) =>
- {
- if (b.Message.Topic == topic)
- {
- try
- {
- if (typeof(T) == typeof(System.String))
- {
- if (Interlocked.CompareExchange(ref isResponseGot, 1, 0) == 0)
- {
- var fff = Encoding.UTF8.GetString(b.Message.Message) as T;
- source.SetResult(fff);
- }
- }
- else
- {
- var response = JsonSerializer.Deserialize<T>(Encoding.UTF8.GetString(b.Message.Message));
- if (Interlocked.CompareExchange(ref isResponseGot, 1, 0) == 0)
- {
- source.SetResult(response);
- }
- }
- }
- catch (Exception exx)
- {
- if (Interlocked.CompareExchange(ref isResponseGot, 1, 0) == 0)
- {
- source.SetException(exx);
- }
- }
- finally
- {
- client.OnMessageReceived -= callback;
- }
- }
- };
- client.OnMessageReceived += callback;
- var _ = new System.Timers.Timer(timeout);
- _.Elapsed += (__, ___) =>
- {
- _.Stop();
- if (Interlocked.CompareExchange(ref isResponseGot, 1, 0) == 0)
- {
- source.SetResult(default(T));
- }
- };
- _.Start();
- return source.Task;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static async Task<T> SubscribeAndWaitForThenUnsubscribe<T>(this IMqttClientNetworkController client, string topic,
- int timeout, Mqtt_QosLevel qosLevel) where T : class
- {
- try
- {
- var subResult = await client.SubscribeAsync(0, topic, qosLevel);
- if (subResult)
- return await client.WaitFor<T>(topic, timeout);
- else
- {
- var source1 = new TaskCompletionSource<T>();
- source1.SetException(new InvalidOperationException("SubscribeAsync failed with returned with false"));
- return await source1.Task;
- }
- }
- catch (Exception exx)
- {
- var source0 = new TaskCompletionSource<T>();
- source0.SetException(exx);
- return await source0.Task;
- }
- finally
- {
- await client.UnsubscribeAsync(0, topic);
- }
- }
- public static string WrapToPropertyChangedInLocalJson(this string propertyName, string value, int requestId)
- {
- return new List<KeyValuePair<string, object>>(){
- new KeyValuePair<string, object>(propertyName,value)}.WrapToPropertiesChangedInLocalJson(requestId);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static string WrapToPropertiesChangedInLocalJson(this IEnumerable<KeyValuePair<string, object>> propertyNameAndValues, int requestId)
- {
- var request = "{ \"id\": \"" + requestId + "\", \"version\": \"1.0\", \"params\": {";
- foreach (var nv in propertyNameAndValues)
- {
- string variableValue = nv.Value.ToString();
- if (nv.Value.GetType() == typeof(System.String))
- variableValue = "\"" + variableValue + "\"";
- string property_CurState_JsonStr = "\"" + nv.Key + "\": {\"value\": " + variableValue + ",\"time\": " + DateTime.Now.Ticks + "}";
- request += property_CurState_JsonStr + ",";
- }
-
- request = request.Substring(0, request.Length - 1);
- request += "}, \"method\": \"thing.event.property.post\"}";
- return request;
- }
- }
- }
|