-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
265 lines (215 loc) · 8.64 KB
/
Copy pathserver.js
File metadata and controls
265 lines (215 loc) · 8.64 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// server.js
const mysql = require('mysql2/promise');
const WebSocket = require('ws');
const dotenv = require('dotenv');
const TagProcessModel = require('./models/TagProcessModel');
// Load environment variables
dotenv.config();
// Database configuration
const dbConfig = {
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
port: process.env.DB_PORT
};
// Create database connection pool
const pool = mysql.createPool(dbConfig);
// Initialize model
const tagProcessModel = new TagProcessModel(pool);
// WebSocket server initialization
const wss = new WebSocket.Server({ port: process.env.WS_PORT || 8080 });
// Variable to store interval reference
let processInterval;
// WebSocket connection handler
wss.on('connection', (ws) => {
console.log('New client connected');
ws.on('close', () => {
console.log('Client disconnected');
});
});
// Broadcast function to send data to all connected clients
const broadcastData = (data) => {
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify(data));
}
});
};
// Function to start/restart the process interval
async function initializeProcessInterval() {
// Clear existing interval if any
if (processInterval) {
clearInterval(processInterval);
}
// Get interval from database using model
const intervalTime = await tagProcessModel.getIntervalConfig();
// Start new interval
processInterval = setInterval(processTagData, intervalTime);
console.log(`Process interval set to ${intervalTime}ms`);
}
// Main processing function
async function processTagData() {
try {
const rows = await tagProcessModel.getTempTableData();
console.log(`Total Data Last Update: ${rows.length}`);
// Get system settings
const pengaturanSistem = await tagProcessModel.getPengaturanSistem();
const flag_moving_in = pengaturanSistem.flag_moving_in;
const flag_moving_out = pengaturanSistem.flag_moving_out;
for (const row of rows) {
const {
id_temp_table,
rfid_tag_number,
reader_angle,
room_id: room_id_scan,
room_name,
reader_gate,
is_legal_moving,
waktu
} = row;
try {
// Get last location data
const dataLastLocation = await tagProcessModel.getLastLocation(rfid_tag_number);
if (dataLastLocation) {
// Extract data from last location
const {
kode_aset: kode_aset,
nup: nup,
kode_tid: tagCode,
nama_aset: nama_aset,
lokasi_moving: lokasi_terakhir,
status: posisi_aset,
lokasi_terakhir: lokasi_terakhir_id,
nama_lokasi_terakhir,
borrow
} = dataLastLocation;
// let kategori_pergerakan = '';
// let keterangan_pergerakan = '';
const output = reader_angle === 'in' ? flag_moving_in : flag_moving_out;
// Check if moving in same room
if (room_id_scan == lokasi_terakhir_id) {
if (reader_angle == 'in') {
if (posisi_aset == flag_moving_in) {
kategori_pergerakan = 'normal';
keterangan_pergerakan = 'normal';
} else if (posisi_aset == flag_moving_out) {
kategori_pergerakan = 'normal';
keterangan_pergerakan = 'normal!';
}
} else { // reader_angle == 'out'
if (posisi_aset == flag_moving_in) {
kategori_pergerakan = 'normal';
keterangan_pergerakan = 'normal!';
} else {
kategori_pergerakan = 'normal';
keterangan_pergerakan = 'terbaca oleh reader bagian luar, lokasi terakhir masih keluar di ruangan yang sama';
}
}
} else { // moving different room
if (reader_angle == 'out') {
if (posisi_aset == flag_moving_in) {
kategori_pergerakan = 'anomali';
keterangan_pergerakan = 'moving beda ruangan, tapi tidak terbaca oleh reader bagian luar pada ruangan sebelumnya';
} else { // posisi aset terakhir sedang di luar
kategori_pergerakan = 'normal';
keterangan_pergerakan = 'moving beda ruangan';
}
} else if (reader_angle == 'in') {
// jika posisi aset terakhir sedang di dalam
if (posisi_aset == flag_moving_in) {
kategori_pergerakan = 'anomali';
keterangan_pergerakan = 'moving beda ruangan, tidak terbaca oleh reader bagian luar';
} else { // posisi aset terakhir sedang di luar
// berarti dia sudah checkout, di ruangan sebelumnya. tapi dia tidak checkout di ruangan saat ini
kategori_pergerakan = 'normal';
keterangan_pergerakan = 'moving beda ruangan';
}
}
}
// Update status
const updateResult = await tagProcessModel.updateStatus(
id_temp_table,
rfid_tag_number,
output,
room_id_scan,
row.reader_id,
kategori_pergerakan,
keterangan_pergerakan,
room_id_scan,
room_name,
is_legal_moving,
borrow,
waktu
);
console.log('id:', id_temp_table,
'waktu:', new Date(waktu).toISOString().slice(0, 19).replace('T', ' '),
// 'tanggal:', new Date(waktu).toISOString().slice(0, 10).replace(/-/g, '-'),
'room_id_scan', room_id_scan,
'nama_aset:', nama_aset,
'rfid_tag_number:', rfid_tag_number,
'output:', output,
'room_name:', room_name,
'reader_gate', reader_gate
);
// Prepare data for WebSocket broadcast
const broadcastPayload = {
event: 'assetUpdate',
data: {
room_name,
reader_gate,
rfid_tag_number,
nama_aset,
kode_aset,
nup,
reader_angle,
new_status: reader_angle,
kategori_pergerakan,
keterangan_pergerakan,
timestamp: new Date(waktu).toISOString().slice(0, 19).replace('T', ' ')
// affectedRows: updateResult.affectedRows
}
};
// Broadcast the update to all connected clients
broadcastData(broadcastPayload);
}
} catch (error) {
console.error(`Error processing tag ${rfid_tag_number}:`, error);
}
}
} catch (error) {
console.error('Error in processTagData:', error);
}
}
// Check for interval changes periodically
async function watchConfigChanges() {
setInterval(async () => {
const newInterval = await tagProcessModel.getIntervalConfig();
if (processInterval._idleTimeout !== newInterval) {
console.log('Interval configuration changed, updating...');
await initializeProcessInterval();
}
}, 30000); // Check every 30 seconds
}
// Initialize the service
async function initializeService() {
try {
// Start initial interval
await initializeProcessInterval();
// Start watching for config changes
watchConfigChanges();
console.log('Service started successfully');
} catch (error) {
console.error('Error initializing service:', error);
process.exit(1);
}
}
// Error handling for process
process.on('uncaughtException', (error) => {
console.error('Uncaught Exception:', error);
});
process.on('unhandledRejection', (error) => {
console.error('Unhandled Rejection:', error);
});
// Start the service
initializeService();