/
rfid-server.js
51 lines (45 loc) · 1.76 KB
/
rfid-server.js
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
import net from 'net';
import fetch from 'node-fetch';
import gql from 'graphql-tag';
import { host, port, rfidPort } from './config';
import { ApolloClient } from 'apollo-client';
import { InMemoryCache, IntrospectionFragmentMatcher } from 'apollo-cache-inmemory';
import { HttpLink } from 'apollo-link-http';
const INVENTORIZE_CONTAINER = gql`
mutation inventorizeContainer($barcode: String!) {
inventorizeContainer(barcode: $barcode)
}
`;
const INVENTORIZE_EQUIPMENT= gql`
mutation inventorizeEquipment($barcode: String!) {
inventorizeEquipment(barcode: $barcode)
}
`;
export default introspectionQueryResultData => {
const fragmentMatcher = new IntrospectionFragmentMatcher({
introspectionQueryResultData
});
const cache = new InMemoryCache({ fragmentMatcher });
const link = new HttpLink({ uri: `http://${host}:${port}/graphql`, fetch });
const client = new ApolloClient({
link,
cache
});
const inventorizeContainer = async barcode => await client.mutate({ mutation: INVENTORIZE_CONTAINER, variables: { barcode }});
const inventorizeEquipment = async barcode => await client.mutate({ mutation: INVENTORIZE_EQUIPMENT, variables: { barcode }});
const tcpServer = net.createServer();
tcpServer.on('connection', socket => {
socket.on('data', data => {
socket.write(data);
// console.log('Received data: ' + data);
inventorizeContainer(Buffer.from(data.toString('utf8'), 'hex').toString('utf8').replace(/\0/g, ''));
inventorizeEquipment(Buffer.from(data.toString('utf8'), 'hex').toString('utf8').replace(/\0/g, ''));
});
});
// tcpServer.on('error', err => {
// console.log(err);
// });
tcpServer.listen(rfidPort, host, () => {
console.log('Server listening to ', tcpServer.address());
});
};