-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulator.js
365 lines (334 loc) · 8.64 KB
/
simulator.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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
var app = require("express")();
var server = require("http").Server(app);
var redis = require("redis");
var redisClient = redis.createClient();
const fs = require('fs');
//import QRcode
//const QRCode = require("qrcode");
/*connection to FIREBASE */
const { Storage } = require("@google-cloud/storage");
const storage = new Storage({
keyFilename:
//json file - firebase project (removed)
"<file.json>",
});
let bucketName = "<gs://projectName.appspot.com>";
const districts = [
"North",
"Haifa",
"Centeral",
"Jerusalem",
"South",
"Shomron",
];
// Data for packages
const Items = [
"phone",
"headphons",
"laptop",
"streamer",
"tablet",
"phone charger",
"laptop-backpack",
"laptop bag",
"phone-case",
"tablet-case",
"airpods",
"airpods-case",
"mouse",
"television",
"watch",
];
const sizes = ["small", "medium", "large"];
const taxes = ["free", "extra", "maximum"];
const cities = [
(North = [
"Beit Shean",
"Tibirias",
"Nahariya",
"Carmiel",
"Natzrat",
"Akko",
"Tzfat",
"Kiryat Shmona",
"Shlomi",
"Katzrin",
"Rosh Pinna",
"Kefar Tavor",
"Afula",
]),
(Haifa = [
"Haifa",
"Hadera",
"Kiryat Ata",
"Nesher",
"Tirat Carmel",
"Or Akiva",
"Binyamina",
"Zichron Yaakov",
"Harish",
"Iron",
"Pardes Hanna",
"Reachasim",
]),
(Centeral = [
"Or Yehuda",
"Bene Baraq",
"Bat Yam",
"Givataym",
"Hezeliya",
"Holon",
"Kiryat Ono",
"Ramat Gan",
"Ramat Hasharon",
"Tel Aviv",
"Azur",
"Kefar Shmaryahu",
"Elad",
"Beer Yaakov",
"Givat Shmuel",
"Hod Hasharon",
"Yahud",
"Yavne",
"Kefar Yonna",
"Kefar Sava",
"Lod",
"Modiin",
"Nes Tzyona",
"Netanya",
"Petach Tikva",
"Rosh Haayin",
"Raanana",
"Rmale",
"Rehovot",
]),
(Jerusalem = [
"Beit Shemesh",
"Jerusalem",
"Mevaseret Tzion",
"Kiryat Yearim",
"Abu Gush",
"",
]),
(South = [
"Ofakim",
"Eilat",
"Ashdod",
"Beer Sheva",
"Ashkelon",
"Dimonna",
"Netivot",
"Arad",
"Kiryat Gat",
"Kiryat Malachi",
"Sedrot",
"Yeruham",
"Mitspe Ramon",
"Omer",
"Meytar",
"Neta",
]),
(Shomron = [
"Ariel",
"Beytar Ilit",
"Oranit",
"Maale Adumim",
"Alfei Menashe",
"Elkana",
"Efrat",
"Beit El",
"Emanuel",
"Karnei Shmoron",
"Kdumim",
"Kiryat Arba",
"Givat Zeev",
]),
];
const addresses = [
"dekel 20",
"hagana 15",
"ben gurion 30",
"berl 18",
"narkis 19",
"lilach 9",
"shimshon hagibor 66",
"altreman 17",
"nili 1",
"chana senesh 28",
"ir david 2",
"yefe nof 9",
"neve shaanan 3",
"savyonim 10",
"rimon 5",
"gefen 6",
"bar lev 54",
"habanim 132",
"hanadiv 68",
"yoel salomon 74",
"ehud manor 56",
"hadarim 45",
"botnim 88",
"macabim 43",
"hashmonaim 55",
];
// Random function (return index) to round number
function randInt(range) {
return Math.floor(Math.random() * range);
}
// counter for tracking packageID
let trackCounter = 0;
// array of objects of packages
let dataPackages = [];
// generate random packages
function generatePackages() {
const MAX_PACKS_NUM = 50;
const MAX_ITMS_NUM =4;
// get number of packages that will be generated - max range and +1 to get range from 1
let packs_num = randInt(MAX_PACKS_NUM) + 1;
// loop the packets numbers
for (let pack = 0; pack < packs_num; pack++) {
let districtNum = randInt(cities.length);
let districtCitiesSize = cities[districtNum].length; //number of cities in this district
let city = cities[districtNum][randInt(districtCitiesSize)]; // choose the district and then the city
// items for bigml associtions to be at least 2
let itms = [];
let itms_num = randInt(MAX_ITMS_NUM) + 2;
let coin = randInt(2);
if(coin){
itms.push(Items[0]);
itms.push(Items[1]);
}
for (let itm = 0; itm < itms_num; itm++) {
itms.push(Items[randInt(Items.length)]);
}
// define package object
let packageRedis = {
trackingId: trackCounter++,
size: sizes[randInt(sizes.length)],
taxes: taxes[randInt(taxes.length)],
district: districts[districtNum],
items: itms,
address: `${addresses[randInt(addresses.length)]}, ${city}`,
};
// insert to data packages array the object
dataPackages.push(packageRedis);
sendMessageRedis(packageRedis); // send the package to redis and message
}
}
/*send message and insert the JSON package data to redis*/
const sendMessageRedis = function(package){
jsonPackage = JSON.stringify(package.items);
console.log("jsonpack: "+ jsonPackage)
redisClient.RPUSH("packages",jsonPackage);
// catch 404 and forward to error handler
app.use(function (req, res, next) {
var err = new Error("Not Found");
err.status = 404;
next(err);
});
messagePackage= {
district: package.district,
size: package.size,
taxes: package.taxes
}
jsonMessagePackage= JSON.stringify(messagePackage);
redisClient.publish(
"message",
jsonMessagePackage,
function () {
}
);
console.log("mess:" +jsonMessagePackage);
}
// לתקשר עם הרדיס
redisClient.on("connect", function () {
console.log("Sender connected to Redis");
});
server.listen(6062, function () {
console.log("Sender is running on port 6062");
});
setInterval(generatePackages, 10000);
/*QRcode */
const arrivedPackages = function () {
if(dataPackages.length){
let numArrivedPackages = Math.floor(Math.random() * Math.min(10,dataPackages.length)) + 1; //decide how much packages will arrive
for (let i = 0; i < numArrivedPackages; i++) {
// find random index of package in the array
let randomIndex = Math.floor(Math.random() * dataPackages.length);
const pack = dataPackages.splice(randomIndex, 1)[0]; //get the package at randomIndex and delete it
console.log(`chosen pack: ${pack.trackingId}`);
const packPath = `./QRcodes/beforeFB/package${pack.trackingId}`;
var jsonPack = {trackingId: pack.trackingId,district:pack.district,size:pack.size,taxes:pack.taxes};
const stringdata = JSON.stringify(jsonPack); // Converting the data into String format
//generateQRcode(pack.trackingId,stringdata); // generate a QRcode for this package
generateText(pack.trackingId,stringdata);
uploadFile(packPath); // upload the QRcode and text file to fireBase -> means they are arrived
}
}
};
setInterval(arrivedPackages, 25000);
/*
const generateQRcode = function (trackingId,stringdata) {
QRCode.toFile(
`./QRcodes/beforeFB/package${trackingId}.png`,
stringdata,
function (err, code) {
if (err) return console.log("error occurred");
}
);
console.log(`pck num ${trackingId} generate`);
};*/
//generate text file with package details in json
const generateText= function (trackingId,stringdata) {
fs.writeFile(`./QRcodes/beforeFB/package${trackingId}.txt`, stringdata, function(err) {
if(err) {
return console.log(err);
}
console.log("The file was saved!");
});
}
// upload file text (and qrcode) to firebase
const uploadFile = async (filename) => {
// Uploads a local file to the bucket
/*
await storage.bucket(bucketName).upload(`${filename}.png`,{
gzip: false,
// By setting the option `destination`, you can change the name of the
// object you are uploading to a bucket.
metadata: {
// Enable long-lived HTTP caching headers
// Use only if the contents of the file will never change
// (If the contents will change, use cacheControl: 'no-cache')
cacheControl: 'public, max-age=31536000',
},
});
*/
await storage.bucket(bucketName).upload(`${filename}.txt`,{
gzip: false,
// By setting the option `destination`, you can change the name of the
// object you are uploading to a bucket.
metadata: {
// Enable long-lived HTTP caching headers
// Use only if the contents of the file will never change
// (If the contents will change, use cacheControl: 'no-cache')
cacheControl: 'public, max-age=31536000',
},
});
// remove from pc the qrcode
/*fs.rm(`${filename}.png`, (err) => {
if (err) {
console.log("failed to delete local image:"+err);
} else {
console.log('successfully deleted local image');
}
});*/
// remove from pc the text
fs.rm(`${filename}.txt`, (err) => {
if (err) {
console.log("failed to delete local image:"+err);
} else {
console.log('successfully deleted local image');
}
});
console.log(`${filename} uploaded to ${bucketName}.`);
};