-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
144 lines (120 loc) · 3.84 KB
/
app.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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const WebSocket = require('ws');
var fs = require('fs');
const express = require('express')
const app = express()
const port = 3000
const http = require('http');
var history = require('connect-history-api-fallback');
const router = express.Router({ strict: true })
const staticFileMiddleware = express.static('kisst-app/dist/');
const server = http.createServer(app);
app.use(router)
app.use(staticFileMiddleware);
app.use(history())
app.use(staticFileMiddleware);
server.listen(process.env.PORT || 3000, () => {
console.log(`Server started on port ${server.address().port} :)`);
});
const dbFile = 'database.json';
var inventory = {
database: {
default: {
maxId: 0,
data: []
}
}
}
var inventoryChanges = false;
function initFS() {
try {
let readDB = fs.readFileSync(dbFile);
let data = JSON.parse(readDB);
inventory = Object.assign({}, data);
}
catch {
fs.writeFileSync(dbFile, JSON.stringify(inventory));
console.log('Writing', JSON.stringify(inventory));
}
finally {
periodicFileSync();
}
}
initFS();
function periodicFileSync() {
if (inventoryChanges) {
console.log('Writing to db...');
fs.writeFileSync(dbFile, JSON.stringify(inventory));
inventoryChanges = false;
console.log('Done');
}
setTimeout(periodicFileSync, 1000);
}
const wss = new WebSocket.Server({
//port: 3001
server
});
var clients = [];
wss.on('connection', function connection(ws) {
clients.push(ws);
ws.on('message', function trigger(message) {
let data = JSON.parse(message);
if (data.db == undefined) data.db = 'default';
console.log('[ws client] new data:', data);
if (inventory.database[data.db] == undefined) {
console.log('Creating new database:', data.db)
inventory.database[data.db] = {
maxId: 0,
data: []
}
}
switch (data.action) {
case 'ADD':
let d = {
id: (inventory.database[data.db].maxId++),
location: data.data.location,
barcode: data.data.barcode,
qty: data.data.qty
};
console.log('Adding:', d);
inventory.database[data.db].data.push(d);
inventoryChanges = true;
break;
case 'REMOVE':
console.log('Removing id:', data.data.id);
inventory.database[data.db].data.splice(inventory.database[data.db].data.indexOf(inventory.database[data.db].data.filter(o => o.id === data.data.id)[0]), 1);
inventoryChanges = true;
break;
case 'EDIT':
console.log('Editing row:', data.data.id, data.data);
console.log()
inventory.database[data.db].data[inventory.database[data.db].data.findIndex(o => o.id === data.data.id)] = data.data;
console.log('now', inventory.database[data.db].data.filter(o => o.id === data.data.id)[0])
inventoryChanges = true;
break;
case 'REFRESH':
break;
default:
console.log('Could not interpret:', data);
break;
}
broadcastInventory();
});
ws.on('close', (event) => {
console.log('goodbye someone');
clients.splice(clients.indexOf(ws), 1);
console.log('Clients connected:', clients.length)
})
console.log('Clients connected:', clients.length)
sendInventory(ws);
})
function broadcastInventory() {
for (client of clients) {
sendInventory(client);
}
}
function sendInventory(client) {
client.send(JSON.stringify({
type: 'inventory',
database: inventory.database
}));
}