|
| 1 | +import { FDBTypes } from '@fitbit/fdb-protocol'; |
| 2 | +import { isRight } from 'fp-ts/lib/Either'; |
| 3 | +import vorpal from 'vorpal'; |
| 4 | + |
| 5 | +import HostConnections from '../models/HostConnections'; |
| 6 | + |
| 7 | +function isSupportedButton(supportedButtons: FDBTypes.Button[], button: string): button is FDBTypes.Button { |
| 8 | + return supportedButtons.includes(button as FDBTypes.Button); |
| 9 | +} |
| 10 | + |
| 11 | +function isValidTouchState(state: string): state is FDBTypes.TouchState { |
| 12 | + return isRight(FDBTypes.TouchState.decode(state)); |
| 13 | +} |
| 14 | + |
| 15 | +const wait = (durationMs: number) => new Promise(resolve => setTimeout(resolve, durationMs)); |
| 16 | + |
| 17 | +export default function input( |
| 18 | + stores: { |
| 19 | + hostConnections: HostConnections, |
| 20 | + }, |
| 21 | +) { |
| 22 | + return (cli: vorpal) => { |
| 23 | + cli.command('input button <button>', 'Simulate a button press on device') |
| 24 | + .hidden() |
| 25 | + .action(async (args: vorpal.Args & { button?: string }) => { |
| 26 | + const { appHost } = stores.hostConnections; |
| 27 | + if (!appHost) { |
| 28 | + cli.activeCommand.log('Not connected to a device'); |
| 29 | + return false; |
| 30 | + } |
| 31 | + |
| 32 | + if (!appHost.host.hasButtonInputSupport()) { |
| 33 | + cli.activeCommand.log('Connected device does not support simulated button presses'); |
| 34 | + return false; |
| 35 | + } |
| 36 | + |
| 37 | + cli.activeCommand.log(args.button); |
| 38 | + if (!isSupportedButton(appHost.host.buttons(), args.button!)) { |
| 39 | + cli.activeCommand.log(`Connected device does not support requested button type. Supported buttons: ${appHost.host.buttons().join(', ')}`); |
| 40 | + return false; |
| 41 | + } |
| 42 | + |
| 43 | + return appHost.host.simulateButtonPress(args.button); |
| 44 | + }); |
| 45 | + |
| 46 | + cli.command('input touch <state> <x> <y>', 'Simualate a touch event on device') |
| 47 | + .hidden() |
| 48 | + .action(async (args: vorpal.Args & { state?: string, x?: number, y?: number }) => { |
| 49 | + const { appHost } = stores.hostConnections; |
| 50 | + if (!appHost) { |
| 51 | + cli.activeCommand.log('Not connected to a device'); |
| 52 | + return false; |
| 53 | + } |
| 54 | + |
| 55 | + if (!appHost.host.hasTouchInputSupport()) { |
| 56 | + cli.activeCommand.log('Connected device does not support simulated touch events'); |
| 57 | + return false; |
| 58 | + } |
| 59 | + |
| 60 | + if (args.state === 'tap') { |
| 61 | + await appHost.host.simulateTouch({ x: args.x!, y: args.y! }, 'down'); |
| 62 | + await wait(250); |
| 63 | + await appHost.host.simulateTouch({ x: args.x!, y: args.y! }, 'up'); |
| 64 | + } else { |
| 65 | + if (!isValidTouchState(args.state!)) { |
| 66 | + cli.activeCommand.log('Touch state provided was not valid'); |
| 67 | + return false; |
| 68 | + } |
| 69 | + |
| 70 | + return appHost.host.simulateTouch({ x: args.x!, y: args.y! }, args.state) |
| 71 | + } |
| 72 | + }); |
| 73 | + }; |
| 74 | +} |
0 commit comments