-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
622 lines (427 loc) · 21.2 KB
/
main.py
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
from flask import Flask, request, render_template, url_for, redirect, send_file
import datetime
from utils import filemaker
from navixy_parser.navixy import Client, JournalRecord, TrackHistory, TrackStatus, unpack_tag_value, Tag, TrackTagBindings
from flask_apscheduler import APScheduler
import requests
import pandas as pd
import logging
import urllib3
from flask_mail import Mail, Message
app = Flask(__name__)
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
logging.basicConfig(level=logging.INFO)
# Configure Flask-Mail
app.config['MAIL_SERVER'] = 'smtp.fleetcontrolsol.com' # Replace with your mail server
app.config['MAIL_PORT'] = '465'
app.config['MAIL_USERNAME'] = '[email protected]'
app.config['MAIL_PASSWORD'] = 'G30rge@Kingsw00d48'
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = True
mail = Mail(app)
class Client2:
def __init__(self):
self.hash = None
def authenticate(self, username, password):
auth_url = 'https://api.fleetcontrolsol.com/user/auth'
payload = {'login':'[email protected]' , 'password':'P@ssw321*'}
response = requests.post(auth_url, json=payload)
if response.ok:
data = response.json()
if data['success']:
self.hash = data['hash'] # Assuming 'hash' is the session key
return True
return False
ui_navixy_client = Client()
reports_navixy_client = Client2()
reports_client = Client2()
if reports_client.authenticate('username_for_reports', 'password_for_reports'):
reports_hash = reports_client.hash
else:
reports_hash = None
# Initialize scheduler
scheduler = APScheduler()
scheduler.init_app(app)
scheduler.start()
@app.route('/test_email')
def test_email_route():
return test_email()
def test_email():
try:
with app.app_context():
# Create a test message
msg = Message("Test Email", sender=app.config['MAIL_USERNAME'], recipients=["[email protected]"])
msg.body = "This is a test email sent from Flask app to check email configuration."
# Send the email
mail.send(msg)
return "Email sent successfully!"
except Exception as e:
# If something goes wrong, print the exception
return f"Failed to send email: {e}"
@app.route('/')
def index():
session_key = request.args.get('session_key')
navixy_client = Client()
if session_key:
navixy_client.hash = session_key
tracks = navixy_client.get_all_tracks()
return render_template('index.html', tracks=tracks, session_key=session_key)
else:
return render_template('error.html')
@app.route('/header1', methods=['POST'])
def header1():
button_clicked = request.form['time_period']
track_id = request.form['track_id']
track_label = request.form['track_label']
session_key = request.form['session_key']
now = datetime.datetime.now()
if button_clicked == '24_hours':
start_date = (now - datetime.timedelta(days=1)).strftime("%Y-%m-%d 00:00:00")
end_date = now.strftime("%Y-%m-%d 23:59:59")
tracks = return_track_array(track_id, track_label, start_date, end_date, session_key)
elif button_clicked == '1_week':
start_date = (now - datetime.timedelta(weeks=1)).strftime("%Y-%m-%d 00:00:00")
end_date = now.strftime("%Y-%m-%d 23:59:59")
tracks = return_track_array(track_id, track_label, start_date, end_date, session_key)
else:
if request.form['start_date'] and request.form['end_date']:
# Custom time period selected
start_date = datetime.datetime.strptime(request.form['start_date'], '%Y-%m-%d')
end_date = datetime.datetime.strptime(request.form['end_date'], '%Y-%m-%d')
tracks = return_track_array(track_id, track_label, start_date, end_date, session_key)
else:
tracks = []
if len(tracks) > 0:
save_file = filemaker.export_data1(tracks)
return send_file(save_file, as_attachment=True)
else:
print("There is no raw data to be exported")
return redirect(url_for('index', session_key=session_key))
def generate_report_header1(time_period):
# Removed track_id and track_label as they are no longer needed
session_key = reports_hash
now = datetime.datetime.now()
if time_period == '24_hours':
start_date = (now - datetime.timedelta(days=1)).strftime("%Y-%m-%d 00:00:00")
end_date = now.strftime("%Y-%m-%d 00:59:59")
elif time_period == '1_week':
start_date = (now - datetime.timedelta(weeks=1)).strftime("%Y-%m-%d 00:00:00")
end_date = now.strftime("%Y-%m-%d 23:59:59")
# Add more time periods if needed
# Call a modified version of return_track_array that handles all tracks
tracks = return_all_tracks_array(start_date, end_date, session_key)
if len(tracks) > 0:
# Populate template and return the rendered content
rendered_content = render_template('report_template.html', tracks=tracks)
return rendered_content
else:
return None
def return_all_tracks_array(start_date, end_date, session_key):
# Initialize the client with the session key
navixy_client = Client()
navixy_client.hash = session_key
all_tracks = [] # List to store information of all tracks
# Get all track IDs and their labels
all_track_ids = navixy_client.get_all_tracks()
for track in all_track_ids:
track_id = track.id
track_label = track.label
track_data = return_track_array(track_id, track_label, start_date, end_date, session_key)
all_tracks.extend(track_data) # Add data of each track to the list
return all_tracks
@app.route('/download_report')
def download_report():
try:
session_key = reports_hash # or however you obtain the session key
navixy_client = Client()
navixy_client.hash = session_key
# Fetch data
now = datetime.datetime.now()
start_date = (now - datetime.timedelta(days=7)).strftime("%Y-%m-%d 00:00:00")
end_date = now.strftime("%Y-%m-%d 23:59:59")
track_ids = navixy_client.get_all_tracks()
tracks = return_tracks_array1(navixy_client, track_ids, start_date, end_date)
# Generate Excel report
filename = '/mnt/data/Report.xlsx' # Ensure this directory exists and is writable
generate_excel_report(tracks, filename)
return send_file(filename, as_attachment=True)
except Exception as e:
return str(e)
def send_report_email(report_type, report_content):
subject = f"Report: {report_type}"
recipients = ["[email protected]"] # Add your recipient list here
with app.app_context():
msg = Message(subject, sender=app.config['MAIL_USERNAME'], recipients=recipients)
msg.html = report_content # HTML content from the template
# Send email
mail.send(msg)
def generate_report_file(time_period):
session_key = reports_hash
now = datetime.datetime.now()
# Define start and end dates based on time_period
if time_period == '24_hours':
start_date = (now - datetime.timedelta(days=1)).strftime("%Y-%m-%d 00:00:00")
end_date = now.strftime("%Y-%m-%d 23:59:59")
# Add more time periods if needed
tracks = return_all_tracks_array(start_date, end_date, session_key)
if len(tracks) > 0:
# Use the appropriate function from filemaker to generate the file
save_file = filemaker.export_dataX(tracks) # Replace export_dataX with actual function
return save_file
else:
return None
def generate_excel_report(data, filename):
# Convert the list of dictionaries to a DataFrame
df = pd.DataFrame(data)
# List of columns to exclude
columns_to_exclude = ['record_id', 'track_id', 'Parked', 'Idle_time']
# Drop the unwanted columns
df = df.drop(columns=columns_to_exclude, errors='ignore')
# Save the remaining DataFrame to an Excel file
df.to_excel(filename, index=False)
## Example: Send header1 report for the last 24 hours
#@scheduler.task('cron', id='daily_header1_report', hour=21, minute=0)
#def send_daily_header1_report():
# report_content = generate_report_header1('24_hours')
# if report_content:
# send_report_email('Header1 Daily', report_content)
# Similar scheduling for other time periods
def send_report_with_excel():
try:
print("Scheduled task started - send_report_with_excel")
session_key = reports_hash # Ensure this is the correct session key
navixy_client = Client()
navixy_client.hash = session_key
# Fetch data for the report
now = datetime.datetime.now()
start_date = (now - datetime.timedelta(days=1)).strftime("%Y-%m-%d 00:00:00")
end_date = now.strftime("%Y-%m-%d 23:59:59")
track_ids = navixy_client.get_all_tracks()
tracks = return_tracks_array1(navixy_client, track_ids, start_date, end_date)
# Generate Excel report
filename = '/mnt/data/report.xlsx'
generate_excel_report(tracks, filename)
print("Report generated, sending email...")
# Email the report as an attachment
send_report_email_with_attachment1('Daily Report', 'Please find the attached report.', filename)
except Exception as e:
app.logger.error(f'Error in send_report_with_excel: {e}')
def send_report_email_with_attachment1(subject, body, attachment_filename):
print("Preparing to send email with attachment...")
recipients = ["[email protected]"] # Replace with actual recipients
msg = Message(subject, sender=app.config['MAIL_USERNAME'], recipients=recipients)
msg.body = body
try:
with app.open_resource(attachment_filename) as fp:
msg.attach("Report.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", fp.read())
print("Attachment added, sending email...")
mail.send(msg)
print("Email sent successfully.")
except Exception as e:
print(f"Failed to send email: {e}")
# Schedule the daily report email task
#scheduler.add_job(id='send_daily_excel_report', func=send_report_with_excel, trigger='cron', hour=21, minute=0)
###########################################################
@app.route('/email_report')
def email_report():
try:
logging.info("Email report requested")
session_key = reports_hash
navixy_client = Client()
navixy_client.hash = session_key
now = datetime.datetime.now()
start_date = (now - datetime.timedelta(days=7)).strftime("%Y-%m-%d %H:%M:%S")
end_date = now.strftime("%Y-%m-%d %H:%M:%S")
# Fetch all tracks for the time period
track_objects = navixy_client.get_all_tracks() # This returns a list of Track objects
all_tracks_data = []
# Iterate through each Track object
for track_obj in track_objects:
track_id = track_obj.id # Use dot notation to access the id attribute
track_label = getattr(track_obj, 'label', 'Unknown Label') # Use getattr to handle missing 'label' safely
# Fetch data for each track using the corrected attribute access
track_data = return_track_array(track_id, track_label, start_date, end_date, session_key)
all_tracks_data.extend(track_data)
# Generate Excel report
filename = '/mnt/data/Report.xlsx'
generate_excel_report(all_tracks_data, filename)
# Email the report as an attachment
subject = "Weekly Report"
body = "Please find the attached weekly report."
recipients = ["[email protected]"] # Ensure this uses actual recipient addresses
send_report_email_with_attachment(subject, body, filename, recipients)
logging.info("Report email sent successfully")
return "Email sent successfully"
except Exception as e:
logging.error(f"Error in email_report: {e}")
return str(e)
def send_report_email_with_attachment(subject, body, attachment_filename, recipients):
try:
recipients = ["[email protected]","[email protected]","[email protected]"] # Replace with actual recipients
msg = Message(subject, sender=app.config['MAIL_USERNAME'], recipients=recipients)
msg.body = body
with app.open_resource(attachment_filename) as fp:
msg.attach("Report.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", fp.read())
mail.send(msg)
logging.info("Email with attachment sent successfully.")
except Exception as e:
logging.error(f"Failed to send email with attachment: {e}")
def scheduled_email_report():
with app.app_context():
try:
logging.info("Scheduled email report started")
session_key = reports_hash
navixy_client = Client()
navixy_client.hash = session_key
now = datetime.datetime.now()
start_date = (now - datetime.timedelta(days=7)).strftime("%Y-%m-%d 00:00:00")
end_date = now.strftime("%Y-%m-%d 23:59:59")
track_infos = navixy_client.get_all_tracks() # Assuming this returns a list of track objects or identifiers
all_tracks_data = []
for track_info in track_infos:
track_id = track_info.id # Adjust based on the actual structure
track_label = getattr(track_info, 'label', 'Unknown') # Default label if not present
track_data = return_track_array(track_id, track_label, start_date, end_date, session_key)
all_tracks_data.extend(track_data)
filename = '/mnt/data/Report.xlsx'
generate_excel_report(all_tracks_data, filename)
# Email the report as an attachment
subject = "Scheduled Weekly Report"
body = "Please find the attached weekly report."
recipients = ["[email protected]","[email protected]"]
send_report_email_with_attachment(subject, body, filename, recipients)
logging.info("Scheduled email report sent successfully")
except Exception as e:
logging.error(f"Error in scheduled_email_report: {e}")
scheduler.add_job(id='scheduled_email_report', func=scheduled_email_report, trigger='cron', hour=20, minute=5)
########################################################################################################################
@app.route('/header2', methods=['POST'])
def header2():
session_key = request.form['session_key']
if request.form['start_date'] and request.form['end_date']:
start_date = datetime.datetime.strptime(request.form['start_date'], '%Y-%m-%d')
end_date = datetime.datetime.strptime(request.form['end_date'], '%Y-%m-%d')
track_id = request.form['track_id']
track_label = request.form['track_label']
tracks = return_track_array(track_id, track_label, start_date, end_date, session_key)
save_file = filemaker.export_data2(tracks, track_label, start_date, end_date)
return send_file(save_file, as_attachment=True)
else:
return redirect(url_for('index', session_key=session_key))
@app.route('/header3', methods=['POST'])
def header3():
session_key = request.form['session_key']
if request.form['start_date'] and request.form['end_date']:
track_id = request.form['track_id']
track_label = request.form['track_label']
start_date = datetime.datetime.strptime(request.form['start_date'], '%Y-%m-%d')
end_date = datetime.datetime.strptime(request.form['end_date'], '%Y-%m-%d')
tracks = return_track_array(track_id, track_label, start_date, end_date, session_key)
save_file = filemaker.export_data3(tracks)
return send_file(save_file, as_attachment=True)
else:
return redirect(url_for('index', session_key=session_key))
@app.route('/header4', methods=['POST'])
def header4():
session_key = request.form['session_key']
now = datetime.datetime.now()
start_date = (now - datetime.timedelta(days=4)).strftime("%Y-%m-%d 00:00:00")
end_date = now.strftime("%Y-%m-%d 23:59:59")
navixy_client = Client()
if session_key:
navixy_client.hash = session_key
track_ids = navixy_client.get_all_tracks()
tracks = return_tracks_array1(navixy_client, track_ids, start_date, end_date)
save_file = filemaker.export_data4(tracks)
return send_file(save_file, as_attachment=True)
else:
return render_template('error.html')
def history_data_to_driver_journal_data(history_data: TrackHistory, journal_record: JournalRecord):
"""
history data and journal record data comparator
:param history_data:
:param journal_record:
:return:
"""
if history_data.start_date == journal_record.start_date and \
history_data.end_date == journal_record.end_date and \
history_data.start_address == journal_record.start_location.address and \
history_data.end_address == journal_record.end_location.address:
return True
return False
def create_duration(journal_record: JournalRecord) -> str:
"""
generating timedelta for track move duration and convert into string
:param journal_record:
:return:
"""
return str(journal_record.end_date - journal_record.start_date)
def get_track_status(track_status: TrackStatus, journal_record: JournalRecord):
if track_status is None:
return None
status_result = track_status.label if journal_record.start_date <= track_status.changed else None
return status_result
def return_tracks_array1(navixy_client, track_ids, time_from, time_to):
print('Start creating client')
events = []
for track in track_ids:
track_id = track.id
track_label = track.label
track_journal_data = navixy_client.get_driver_journal(track_id, time_to, time_from)
event = {}
if len(track_journal_data) > 0:
journal_record = track_journal_data[len(track_journal_data) - 1]
event["track_id"] = track_id
event["track_label"] = track_label
event["time_start"] = journal_record.start_date.strftime("%Y-%m-%d %H:%M:%S")
event["time_end"] = journal_record.end_date.strftime("%Y-%m-%d %H:%M:%S")
event["from_address"] = journal_record.start_location.address
event["to_address"] = journal_record.end_location.address
event["duration"] = create_duration(journal_record)
events.append(event)
print(f'Data from date {journal_record.start_date} for track {track_id} recorded')
print('Iteration done, sleep 15 sec')
return events
def return_track_array(track_id, track_label, time_from, time_to, session_key):
print('Start creating client')
navixy_client = Client()
navixy_client.hash = session_key
events = []
event = {}
print('Parsing starting')
track_journal_data = navixy_client.get_driver_journal(track_id, time_to, time_from)
trip_detection_data = navixy_client.get_trip_detection_data(track_id)
track_history_data = navixy_client.get_track_history(track_id, time_to, time_from)
tags_data = navixy_client.get_all_tags()
# tag_bindings = navixy_client.get_all_tracks_with_tag_bindings()
for journal_record in track_journal_data:
for history_data in track_history_data:
event = {}
if history_data_to_driver_journal_data(history_data, journal_record):
start_zone = navixy_client.get_zone(journal_record.start_location.lat,
journal_record.start_location.lng)
end_zone = navixy_client.get_zone(journal_record.end_location.lat,
journal_record.end_location.lng)
event["record_id"] = history_data.id
event["track_id"] = track_id
event["track_label"] = track_label
event["time_start"] = journal_record.start_date.strftime("%Y-%m-%d %H:%M:%S")
event["time_end"] = journal_record.end_date.strftime("%Y-%m-%d %H:%M:%S")
event["from_address"] = journal_record.start_location.address
event["to_address"] = journal_record.end_location.address
event["distance"] = journal_record.length
event["duration"] = create_duration(journal_record)
event["max_speed"] = history_data.max_speed
event["Driver"] = journal_record.employee_id
event["TT_number_tags"] = unpack_tag_value(1, tags_data, [])
event["Registration_plate"] = unpack_tag_value(2, tags_data, [])
event["Product"] = unpack_tag_value(3, tags_data, [])
event["Parked"] = trip_detection_data.ignition_aware
event["Idle_time"] = trip_detection_data.min_idle_duration_minutes
event["zone"] = f"{start_zone} > {end_zone}"
events.append(event)
print(f'Data from date {journal_record.start_date} for track {track_id} recorded')
print('Iteration done, sleep 15 sec')
return events
if __name__ == '__main__':
app.run(debug=True)