Skip to content

Commit a076d75

Browse files
committedApr 11, 2021
WIP
1 parent a189cc6 commit a076d75

File tree

11 files changed

+656
-7
lines changed

11 files changed

+656
-7
lines changed
 

‎.husky/pre-commit

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/bin/sh
22
. "$(dirname "$0")/_/husky.sh"
33

4-
npm run lint staged
4+
npm run lint

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@
225225
"@types/react-router-dom": "^5.1.7",
226226
"@types/react-test-renderer": "^17.0.1",
227227
"@types/rimraf": "^3.0.0",
228+
"@types/serialport": "^8.0.1",
228229
"@types/uuid": "^8.3.0",
229230
"@types/webpack-env": "^1.16.0",
230231
"@typescript-eslint/eslint-plugin": "^4.15.0",

‎src/api/index.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import './src/graphql/enum/UserDefineKey';
2222
import UserDefinesBuilder from './src/services/UserDefinesBuilder';
2323
import UpdatesService from './src/services/Updates';
2424
import UpdatesResolver from './src/graphql/resolvers/Updates.resolver';
25+
import SerialMonitorResolver from './src/graphql/resolvers/SerialMonitor.resolver';
2526

2627
export default class ApiServer {
2728
app: Express | undefined;
@@ -75,7 +76,12 @@ export default class ApiServer {
7576
);
7677

7778
const schema = await buildSchema({
78-
resolvers: [FirmwareResolver, SourcesResolver, UpdatesResolver],
79+
resolvers: [
80+
FirmwareResolver,
81+
SourcesResolver,
82+
UpdatesResolver,
83+
SerialMonitorResolver,
84+
],
7985
container: Container,
8086
pubSub,
8187
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Query, Resolver } from 'type-graphql';
2+
import { Service } from 'typedi';
3+
import SerialMonitorService from '../../services/SerialMonitor';
4+
import SerialPortInformation from '../../models/SerialPortInformation';
5+
6+
@Service()
7+
@Resolver()
8+
export default class SerialMonitorResolver {
9+
constructor(private serialMonitorService: SerialMonitorService) {}
10+
11+
@Query(() => [SerialPortInformation])
12+
async availableDevicesList() {
13+
return this.serialMonitorService.getAvailableDevices();
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Field, ObjectType } from 'type-graphql';
2+
3+
@ObjectType('SerialPortInformation')
4+
export default class SerialPortInformation {
5+
@Field()
6+
path: string;
7+
8+
@Field()
9+
manufacturer: string;
10+
11+
constructor(path: string, manufacturer: string) {
12+
this.path = path;
13+
this.manufacturer = manufacturer;
14+
}
15+
}
+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
enum PubSubTopic {
22
BuildProgressNotification = 'BUILD_PROGRESS_NOTIFICATION',
33
BuildLogsUpdate = 'BUILD_LOGS_UPDATE',
4+
SerialMonitorEvents = 'SERIAL_MONITOR_EVENTS',
5+
SerialMonitorStream = 'SERIAL_MONITOR_STREAM',
46
}
57

68
export default PubSubTopic;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { Service } from 'typedi';
2+
import SerialPort from 'serialport';
3+
import { PubSubEngine } from 'graphql-subscriptions';
4+
import SerialPortInformation from '../../models/SerialPortInformation';
5+
import PubSubTopic from '../../pubsub/enum/PubSubTopic';
6+
import { LoggerService } from '../../logger';
7+
8+
@Service()
9+
export default class SerialMonitorService {
10+
port: SerialPort | null;
11+
12+
constructor(private pubSub: PubSubEngine, private logger: LoggerService) {
13+
this.port = null;
14+
}
15+
16+
private async sendLogs(data: string): Promise<void> {
17+
this.logger?.log('serial monitor stream message', {
18+
data,
19+
});
20+
return this.pubSub!.publish(PubSubTopic.SerialMonitorStream, {
21+
data,
22+
});
23+
}
24+
25+
async getAvailableDevices(): Promise<SerialPortInformation[]> {
26+
const list = await SerialPort.list();
27+
return list.map(
28+
(item) => new SerialPortInformation(item.path, item.manufacturer || '')
29+
);
30+
}
31+
32+
async connect(device: string, baudRate: number): Promise<void> {
33+
if (this.port !== null) {
34+
throw new Error('serial port is already being used');
35+
}
36+
37+
this.port = new SerialPort(device, {
38+
baudRate,
39+
});
40+
41+
// Open errors will be emitted as an error event
42+
this.port.on('error', (err) => {
43+
this.sendLogs(`Serial port error: ${err.message}`);
44+
this.port = null;
45+
});
46+
47+
// this.port.on('');
48+
}
49+
50+
async disconnect(): Promise<void> {
51+
if (this.port === null) {
52+
throw new Error('not connected');
53+
}
54+
return new Promise((resolve, reject) => {
55+
this.port?.close((err) => {
56+
if (err) {
57+
reject(err);
58+
} else {
59+
resolve();
60+
}
61+
});
62+
});
63+
}
64+
}

‎src/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,7 @@
1414
"postinstall": "yarn electron-rebuild"
1515
},
1616
"license": "GPL-3.0-or-later",
17-
"dependencies": {}
17+
"dependencies": {
18+
"serialport": "^9.0.7"
19+
}
1820
}

0 commit comments

Comments
 (0)
Please sign in to comment.