protected override bool Execute(ExecuteIoTMethodData data, FormSubmitContext formSubmitContext) { Assert.ArgumentNotNull(data, nameof(data)); Assert.ArgumentNotNull(data.DeviceId, nameof(data.DeviceId)); Assert.ArgumentNotNull(data.MethodId, nameof(data.MethodId)); Assert.ArgumentNotNull(formSubmitContext, nameof(formSubmitContext)); // Method not selected if (data.MethodId == Guid.Empty) { Log.Error($"[ExecuteIoTMethod] Form Submit Action failed - IoTMethod is not selected", this); return(false); } // Payload var payload = string.Empty; if (data.PayloadFieldId.HasValue && data.PayloadFieldId.Value != Guid.Empty) { payload = GetPayloadFromFieldId(formSubmitContext, data.PayloadFieldId.Value); } else if (!string.IsNullOrEmpty(data.PayloadString)) { payload = GetPayloadFromString(formSubmitContext, data.PayloadString); } // Call IoTMethod try { var device = _hubRepository.GetDevice(new ID(data.DeviceId)); if (device == null) { Log.Error($"[ExecuteIoTMethod] Form Submit Action failed - IoTDevice '{data.DeviceId}' does not exists", this); return(false); } var method = _hubRepository.GetMethod(new ID(data.MethodId)); if (method == null) { Log.Error($"[ExecuteIoTMethod] Form Submit Action failed - IoTMethod '{data.MethodId}' does not exists", this); return(false); } var response = method.Invoke(device, payload); Log.Info( $"[ExecuteIoTMethod] Called IoTMethod '{data.MethodId}' at the IoTDevice '{data.DeviceId}' with payload '{payload}' - Return: '{response.RawMessage}'", this); } catch (Exception e) { Log.Error( $"[ExecuteIoTMethod] Form Submit Action failed - Error calling IoTMethod '{data.MethodId}' at the IoTDevice '{data.DeviceId}'", e, this); return(false); } return(true); }
// GET api/<controller> public ActionResult GetMethods(string deviceId) { var device = _hubRepository.GetDevice(deviceId); var methods = device.GetMethods(); var result = new List <object>(); foreach (var method in methods) { result.Add(new { Key = method.ID.ToString(), Value = method.MethodName }); } var json = Json(result); json.JsonRequestBehavior = JsonRequestBehavior.AllowGet; return(json); }