public async Task RegisterHandlerAsync(DeviceMethod method, Func <TRequest?, Task <TResponse> > methodAccessor, CancellationToken cancellationToken = default) { this.internalMethodAccessor = methodAccessor ?? throw new ArgumentNullException(nameof(methodAccessor)); await this.deviceClientAdapter.OpenAsync(cancellationToken); await this.deviceClientAdapter.DeviceClient.SetMethodHandlerAsync(method.GetMethodNameFor(), this.DeviceMethodHandler, new object(), cancellationToken); }
private Mock<ITelldusDevice> CreateDevice(int id, string name, DeviceMethod supportedMethods) { var mock = new Mock<ITelldusDevice>(); mock.SetupGet(m => m.Id).Returns(id); mock.SetupGet(m => m.Name).Returns(name); mock.SetupGet(m => m.SupportedMethods).Returns(supportedMethods); return mock; }
// POST api/values public async Task Post([FromBody] string value) { if (value.Length == 0) { return; // should return a failure } //try to parse the supplied JSON string try { DeviceMethod deviceMethod = JsonConvert.DeserializeObject <DeviceMethod>(value); // ToDo: Validate device is correct and available // ToDo: Validate caller is allowed to contact the device // ToDo: Validate device has the required flavours?? Or device can meet request? // Make the call string responseJSON = await CallDeviceMethod(deviceMethod.deviceId, deviceMethod.method, deviceMethod.payload); // ToDo: Evaluate response JSON and return appropriate response to caller } catch { // ToDo: Return a failure response to the caller } }
/// <summary> /// Constructs a device control code. /// </summary> /// <param name="type">Identifies the type of device.</param> /// <param name="function">Identifies the device's function.</param> /// <param name="method">Identifies the buffering method.</param> /// <param name="access">Identifies what access to the device is required.</param> public DeviceControlCode(DeviceType type, Int32 function, DeviceMethod method, DeviceAccess access) { m_code = (((Int32)type) << 16) | (((Int32)access) << 14) | (function << 2) | (Int32)method; }
public static string GetMethodNameFor(this DeviceMethod method) { return(DeviceMethodNameByDeviceMethod.ContainsKey(method) ? DeviceMethodNameByDeviceMethod[method] : throw new ArgumentException($"No method mapped for given {nameof(DeviceMethod)} with value {method.ToString()}", nameof(method))); }
/// <summary> /// Gets the last sent command to a device /// </summary> /// <param name="deviceId">Device to query</param> /// <param name="methodsSupported">The methods supported by the client</param> /// <returns></returns> public DeviceMethod GetLastSentCommand(int deviceId, DeviceMethod methodsSupported) { return NativeMethods.tdLastSentCommand(deviceId, methodsSupported); }
/// <summary> /// Gets the supported method of a device /// </summary> /// <param name="deviceId">The deviceId for which to query</param> /// <param name="methodsSupported">The methods your application supports</param> /// <returns>The applicable methods for the device</returns> public DeviceMethod GetMethods(int deviceId, DeviceMethod methodsSupported) { return NativeMethods.tdMethods(deviceId, methodsSupported); }