/// <summary> /// Get the last Response saved on DB cache for a given method and payload /// </summary> /// <param name="device"></param> /// <param name="method"></param> /// <param name="payload"></param> /// <param name="db"></param> /// <returns></returns> public string GetResponseFromCache(IoTDevice device, IoTDeviceMethod method, string payload, Database db = null) { if (method == null) { return(string.Empty); } db = GetDatabase(db); if (db == null) { return(string.Empty); } var key = GetMethodKey(device, method, payload); if (string.IsNullOrEmpty(key)) { return(string.Empty); } // Retrieve and return saved value var savedValue = db.PropertyStore.GetStringValue(key); return(savedValue); }
/// <summary> /// Deserialize and Parse a string using Deserializer from the giving method /// </summary> /// <param name="method"></param> /// <param name="response"></param> /// <returns></returns> public static DynamicMessage DeserializeAndParse(IoTDeviceMethod method, string response) { // Get returnType and deserializer var returnType = method.GetMessageType(); if (returnType == null) { Log.Error($"Method {method.ID} has an invalid Return Type", typeof(IoTDeviceMethod)); return(null); } var deserializer = returnType.GetDeserializer(); if (deserializer == null) { Log.Error($"Message Type {returnType.ID} has an invalid Deserializer", typeof(IoTDeviceMethod)); return(null); } // Instantiate Deserializer object var deserializerObject = deserializer.GetDeserializerObject(); if (deserializerObject == null) { return(null); } // Parse results var parsedDictionary = (DynamicMessage)deserializerObject.Deserialize(response); parsedDictionary.RawMessage = response; return(parsedDictionary); }
/// <summary> /// Invoke a given Method passing optional payload /// </summary> /// <param name="device"></param> /// <param name="method"></param> /// <param name="payload"></param> /// <returns></returns> public static DynamicMessage InvokeMethod(IoTDevice device, IoTDeviceMethod method, string payload = "") { var hub = device.GetHub(); var connectionStringsServer = hub.ConnectionString; var parsedDictionary = InvokeMethodInternal(device, method, connectionStringsServer, payload).GetAwaiter().GetResult(); return(parsedDictionary); }
private static string GetMethodKey(IoTDevice device, IoTDeviceMethod method, string payload) { var sb = new StringBuilder(); sb.Append(device.ID); sb.Append("_"); sb.Append(method.ID); sb.Append("_"); sb.Append(GetMd5(payload)); return(sb.ToString()); }
public IoTDevice GetDeviceAndMethodByName(string deviceMethodName, out IoTDeviceMethod method, Database database = null) { var device = GetDeviceByName(deviceMethodName, database); if (device == null) { method = null; return(null); } method = GetMethodByName(deviceMethodName, database); return(device); }
/// <summary> /// Save response to DB cache for a given method and payload /// </summary> /// <param name="device"></param> /// <param name="method"></param> /// <param name="payload"></param> /// <param name="response"></param> /// <param name="db"></param> public void SaveResponseToCache(IoTDevice device, IoTDeviceMethod method, string payload, string response, Database db = null) { if (method == null) { return; } db = GetDatabase(db); if (db == null) { return; } var key = GetMethodKey(device, method, payload); if (string.IsNullOrEmpty(key)) { return; } // Save value to DB db.PropertyStore.SetStringValue(key, response); }
public SmartDisplayController(IIoTHubRepository ioTHubRepository) { _method = ioTHubRepository.GetMethodByName(MethodPath); _device = ioTHubRepository.GetDeviceByName(MethodPath); }
/// <summary> /// Internal Invoke method call /// </summary> /// <param name="device"></param> /// <param name="method"></param> /// <param name="connectionStringsServer"></param> /// <param name="payload"></param> /// <returns></returns> private static async Task <DynamicMessage> InvokeMethodInternal(IoTDevice device, IoTDeviceMethod method, string connectionStringsServer, string payload = "") { var methodInvocation = new CloudToDeviceMethod(method.MethodName) { ResponseTimeout = TimeSpan.FromSeconds(30) }; // Payload to send if (!string.IsNullOrEmpty(payload)) { methodInvocation.SetPayloadJson(JsonConvert.SerializeObject(payload)); } // Invoke the direct method asynchronously and get the response from the device. var serviceClient = ServiceClient.CreateFromConnectionString(connectionStringsServer); CloudToDeviceMethodResult response; try { response = serviceClient.InvokeDeviceMethodAsync(device.DeviceName, methodInvocation).GetAwaiter().GetResult(); } catch (Exception e) { Log.Warn($"Error calling method '{method.ID}' on device '{device.ID}' - Error: {e.Message}", typeof(IoTDeviceMethod)); return(new DynamicMessage()); } // Resulting Payload var receivedPayload = response.GetPayloadAsJson(); // Log results Log.Info($"Response status: {response.Status}, payload:", typeof(IoTDeviceMethod)); Log.Info(receivedPayload, typeof(IoTDeviceMethod)); // Deserialize var result = DeserializeAndParse(method, receivedPayload); return(result); }