public async Task <bool> ExecuteActionAsync(string id, CancellationToken cancellationToken = default) { var deviceAction = await _context.DeviceActions.SingleOrDefaultAsync(d => d.Id == id, cancellationToken); if (deviceAction is not null) { await _smartThingsClient.ExecuteDeviceAsync(deviceAction.DeviceId, new DeviceExecuteModel { Capability = deviceAction.Capability, Command = deviceAction.Command, Component = deviceAction.Component }, cancellationToken); return(true); } var sceneAction = await _context.SceneActions.SingleOrDefaultAsync(s => s.Id == id, cancellationToken); if (sceneAction is not null) { await _smartThingsClient.ExecuteSceneAsync(sceneAction.SceneId, cancellationToken); return(true); } return(false); }
private async Task ProcessEventAsync(IEnumerable <HueEventData> events, CancellationToken cancellationToken = default) { var buttonEvent = events.SingleOrDefault(s => s.Id == DeviceConstants.buttonId); if (buttonEvent is not null) { switch (buttonEvent.Button.LastEvent) { case "short_release": await SwitchComputerHaloAsync(cancellationToken); break; case "long_release": var stateResult = await _smartThingsClient.GetCapabilityStatusAsync(DeviceConstants.pcSwapId, "main", "switch", cancellationToken); string state = stateResult["switch"]["value"].GetString(); string newState = state == "on" ? "off" : "on"; await _smartThingsClient.ExecuteDeviceAsync(DeviceConstants.pcSwapId, new Models.SmartThings.DeviceExecuteModel { Component = "main", Capability = "switch", Command = newState }, cancellationToken); break; } } }
public async Task SetFanSpeedAsync(string fanId, int speed, CancellationToken cancellationToken = default) { var fanData = await _smartThingsClient.GetCapabilityStatusAsync(fanId, "main", "fanSpeed", cancellationToken); int fanSpeed = fanData["fanSpeed"]["value"].GetInt32(); if (speed == 0) { if (fanSpeed > 0) { await SwitchFanAsync("off"); } } else { await IncreaseFanAsync(); } async Task IncreaseFanAsync() { await _smartThingsClient.ExecuteDeviceAsync(fanId, new Models.SmartThings.DeviceExecuteModel { Component = "main", Capability = "fanSpeed", Command = "setFanSpeed", Arguments = new object[] { speed } }, cancellationToken); } async Task SwitchFanAsync(string value) { await _smartThingsClient.ExecuteDeviceAsync(fanId, new Models.SmartThings.DeviceExecuteModel { Component = "main", Capability = "switch", Command = value, }, cancellationToken); } }