22
22
*/
23
23
24
24
import { platform } from 'os' ;
25
- import type { HID as nodeHID , Device } from 'node-hid' ;
25
+ import type { HID as nodeHID } from 'node-hid' ;
26
26
import { Transport } from './' ;
27
27
28
28
/**
@@ -31,54 +31,36 @@ import { Transport } from './';
31
31
export class HID implements Transport {
32
32
33
33
private os : string = platform ( ) ;
34
- private path : string ;
35
- private device ?: nodeHID ;
36
34
public readonly packetSize = 64 ;
37
35
38
36
/**
39
37
* HID constructor
40
38
* @param path Path to HID device to use
41
39
*/
42
- constructor ( deviceOrPath : Device | string ) {
43
- const isDevice = ( source : Device | string ) : source is Device => {
44
- return ( source as Device ) . path !== undefined ;
45
- } ;
46
-
47
- this . path = isDevice ( deviceOrPath ) ? deviceOrPath . path ! : deviceOrPath ;
40
+ constructor ( private device : nodeHID ) {
48
41
}
49
42
50
43
/**
51
44
* Open device
52
45
* @returns Promise
53
46
*/
54
47
public async open ( ) : Promise < void > {
55
- if ( ! this . path . length ) {
56
- throw new Error ( 'No path specified' ) ;
57
- }
58
-
59
- const hid = require ( 'node-hid' ) ;
60
- this . device = new hid . HID ( this . path ) ;
48
+ return ;
61
49
}
62
50
63
51
/**
64
52
* Close device
65
53
* @returns Promise
66
54
*/
67
55
public async close ( ) : Promise < void > {
68
- if ( this . device ) {
69
- this . device . close ( ) ;
70
- }
56
+ this . device . close ( ) ;
71
57
}
72
58
73
59
/**
74
60
* Read from device
75
61
* @returns Promise of DataView
76
62
*/
77
63
public async read ( ) : Promise < DataView > {
78
- if ( ! this . device ) {
79
- throw new Error ( 'No device opened' ) ;
80
- }
81
-
82
64
const array = await new Promise < number [ ] > ( ( resolve , reject ) => {
83
65
this . device ! . read ( ( error : string , data : number [ ] ) => {
84
66
if ( error ) {
@@ -99,10 +81,6 @@ export class HID implements Transport {
99
81
* @returns Promise
100
82
*/
101
83
public async write ( data : BufferSource ) : Promise < void > {
102
- if ( ! this . device ) {
103
- throw new Error ( 'No device opened' ) ;
104
- }
105
-
106
84
const isView = ( source : ArrayBuffer | ArrayBufferView ) : source is ArrayBufferView => {
107
85
return ( source as ArrayBufferView ) . buffer !== undefined ;
108
86
} ;
0 commit comments