-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathpair-device.ts
More file actions
executable file
·96 lines (82 loc) · 2.68 KB
/
pair-device.ts
File metadata and controls
executable file
·96 lines (82 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env tsx
import * as fs from 'node:fs';
import { question } from 'readline-sync';
import { setup, pair, getClient } from '../src/api';
import * as dotenv from 'dotenv';
// Load environment variables
dotenv.config();
// Storage functions for client state
const getStoredClient = async (): Promise<string> => {
try {
return fs.readFileSync('./client.temp', 'utf8');
} catch (err) {
return '';
}
};
const setStoredClient = async (data: string | null): Promise<void> => {
try {
if (data) {
fs.writeFileSync('./client.temp', data);
}
} catch (err) {
console.error('Failed to store client data:', err);
}
};
async function main() {
console.log('GridPlus SDK Device Pairing Tool\n');
try {
const baseUrl = process.env.baseUrl || 'https://signing.gridpl.us'
// Get device configuration
const deviceId = process.env.DEVICE_ID || question('Enter Device ID: ');
const password =
process.env.PASSWORD ||
question('Enter Password (default: password): ', {
defaultInput: 'password',
});
const name =
process.env.APP_NAME ||
question('Enter App Name (default: CLI Pairing Tool): ', {
defaultInput: 'CLI Pairing Tool',
});
console.log('\nAttempting to connect to device...');
// Setup the client
const isPaired = await setup({
deviceId,
password,
name,
getStoredClient,
setStoredClient,
baseUrl,
});
if (isPaired) {
console.log('✅ Device is already paired!');
const client = await getClient();
console.log(`Connected to device: ${client?.getDeviceId()}`);
} else {
console.log('⚠️ Device is not paired. Starting pairing process...');
console.log('Please check your Lattice device for the pairing secret.');
const secret = question('Enter the pairing secret from your device: ');
console.log('Pairing with device...');
const pairResult = await pair(secret.toUpperCase());
if (pairResult) {
console.log('✅ Device paired successfully!');
const client = await getClient();
console.log(`Connected to device: ${client?.getDeviceId()}`);
} else {
console.log('❌ Pairing failed. Please try again.');
process.exit(1);
}
}
console.log('\n🎉 Pairing process completed successfully!');
console.log('Client state has been saved to ./client.temp');
console.log('You can now use the SDK with this device.');
} catch (error) {
console.error('❌ Error during pairing process:', error);
process.exit(1);
}
}
// Run the main function
main().catch((error) => {
console.error('❌ Unexpected error:', error);
process.exit(1);
});