193 lines
7.2 KiB
Python
193 lines
7.2 KiB
Python
"""Utility functions for the driver."""
|
|
import socket
|
|
import struct
|
|
import xlsxwriter
|
|
from datetime import datetime as dt
|
|
import email, smtplib, ssl
|
|
from email import encoders
|
|
from email.mime.base import MIMEBase
|
|
from email.mime.multipart import MIMEMultipart
|
|
from email.mime.text import MIMEText
|
|
|
|
def get_public_ip_address():
|
|
"""Find the public IP Address of the host device."""
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
sock.connect(("8.8.8.8", 80))
|
|
ip_address = sock.getsockname()[0]
|
|
sock.close()
|
|
return ip_address
|
|
|
|
|
|
def int_to_float16(int_to_convert):
|
|
"""Convert integer into float16 representation."""
|
|
bin_rep = ('0' * 16 + '{0:b}'.format(int_to_convert))[-16:]
|
|
sign = 1.0
|
|
if int(bin_rep[0]) == 1:
|
|
sign = -1.0
|
|
exponent = float(int(bin_rep[1:6], 2))
|
|
fraction = float(int(bin_rep[6:17], 2))
|
|
|
|
if exponent == float(0b00000):
|
|
return sign * 2 ** -14 * fraction / (2.0 ** 10.0)
|
|
elif exponent == float(0b11111):
|
|
if fraction == 0:
|
|
return sign * float("inf")
|
|
return float("NaN")
|
|
frac_part = 1.0 + fraction / (2.0 ** 10.0)
|
|
return sign * (2 ** (exponent - 15)) * frac_part
|
|
|
|
|
|
def ints_to_float(int1, int2):
|
|
"""Convert 2 registers into a floating point number."""
|
|
mypack = struct.pack('>HH', int1, int2)
|
|
f_unpacked = struct.unpack('>f', mypack)
|
|
print("[{}, {}] >> {}".format(int1, int2, f_unpacked[0]))
|
|
return f_unpacked[0]
|
|
|
|
|
|
def degf_to_degc(temp_f):
|
|
"""Convert deg F to deg C."""
|
|
return (temp_f - 32.0) * (5.0/9.0)
|
|
|
|
|
|
def degc_to_degf(temp_c):
|
|
"""Convert deg C to deg F."""
|
|
return temp_c * 1.8 + 32.0
|
|
|
|
def generate_report(driver_id,trans_day,trans_hour,trans_min, trans_total,overall, company_1,company_2, company_3, company_4, company_5):
|
|
workbook = xlsxwriter.Workbook('daily_report.xlsx')
|
|
worksheet = workbook.add_worksheet()
|
|
bold = workbook.add_format({'bold': True})
|
|
|
|
row = 1
|
|
col = 0
|
|
worksheet.write('A1', 'Driver', bold)
|
|
worksheet.write('B1', 'Day', bold)
|
|
worksheet.write('C1', 'Time',bold)
|
|
worksheet.write('D1', 'Total', bold)
|
|
|
|
for i in range(len(trans_day)):
|
|
worksheet.write(row,col, driver_id[i])
|
|
worksheet.write(row,col+1,str(round(trans_day[i])).rstrip('0').rstrip('.') )
|
|
worksheet.write(row,col+2, ""+ str(round(trans_hour[i])).rstrip('0').rstrip('.')+":"+str(round(trans_min[i])).rstrip('0').rstrip('.'))
|
|
worksheet.write(row,col+3, trans_total[i])
|
|
row += 1
|
|
worksheet.write(row, col+1, "Today's Total", bold)
|
|
worksheet.write(row, col+2, "Yesterday's Total", bold)
|
|
worksheet.write(row, col+3, "Month's Total", bold)
|
|
worksheet.write(row, col+4, "Previous Month's Total", bold)
|
|
worksheet.write(row, col+5, "Current Year's Total", bold)
|
|
worksheet.write(row, col+6, "Previous Year's Total", bold)
|
|
row += 1
|
|
worksheet.write(row, col, "Company 1", bold)
|
|
worksheet.write(row, col+1, company_1['today_total'])
|
|
worksheet.write(row, col+2, company_1['yesterday_total'])
|
|
worksheet.write(row, col+3, company_1['monthly_total'])
|
|
worksheet.write(row, col+4, company_1['prevmonthly_total'])
|
|
worksheet.write(row, col+5, company_1['current_year_total'])
|
|
worksheet.write(row, col+6, company_1['prev_year_total'])
|
|
row += 1
|
|
worksheet.write(row, col, "Company 2", bold)
|
|
worksheet.write(row, col+1, company_2['today_total'])
|
|
worksheet.write(row, col+2, company_2['yesterday_total'])
|
|
worksheet.write(row, col+3, company_2['monthly_total'])
|
|
worksheet.write(row, col+4, company_2['prevmonthly_total'])
|
|
worksheet.write(row, col+5, company_2['current_year_total'])
|
|
worksheet.write(row, col+6, company_2['prev_year_total'])
|
|
row += 1
|
|
worksheet.write(row, col, "Company 3", bold)
|
|
worksheet.write(row, col+1, company_3['today_total'])
|
|
worksheet.write(row, col+2, company_3['yesterday_total'])
|
|
worksheet.write(row, col+3, company_3['monthly_total'])
|
|
worksheet.write(row, col+4, company_3['prevmonthly_total'])
|
|
worksheet.write(row, col+5, company_3['current_year_total'])
|
|
worksheet.write(row, col+6, company_3['prev_year_total'])
|
|
row += 1
|
|
worksheet.write(row, col, "Company 4", bold)
|
|
worksheet.write(row, col+1, company_4['today_total'])
|
|
worksheet.write(row, col+2, company_4['yesterday_total'])
|
|
worksheet.write(row, col+3, company_4['monthly_total'])
|
|
worksheet.write(row, col+4, company_4['prevmonthly_total'])
|
|
worksheet.write(row, col+5, company_4['current_year_total'])
|
|
worksheet.write(row, col+6, company_4['prev_year_total'])
|
|
row += 1
|
|
worksheet.write(row, col, "Company 5", bold)
|
|
worksheet.write(row, col+1, company_5['today_total'])
|
|
worksheet.write(row, col+2, company_5['yesterday_total'])
|
|
worksheet.write(row, col+3, company_5['monthly_total'])
|
|
worksheet.write(row, col+4, company_5['prevmonthly_total'])
|
|
worksheet.write(row, col+5, company_1['current_year_total'])
|
|
worksheet.write(row, col+6, company_1['prev_year_total'])
|
|
row +=1
|
|
worksheet.write(row, col, "Grand Total", bold)
|
|
worksheet.write(row, col+1, overall['today_total'])
|
|
worksheet.write(row, col+2, overall['yesterday_total'])
|
|
worksheet.write(row, col+3, overall['monthly_total'])
|
|
worksheet.write(row, col+4, overall['prevmonthly_total'])
|
|
worksheet.write(row, col+5, overall['current_year_total'])
|
|
worksheet.write(row, col+6, overall['prev_year_total'])
|
|
|
|
workbook.close()
|
|
|
|
def send_email(recipients):
|
|
subject = "Daily Sales Report"
|
|
body = "This is an automate email for M&W sales report"
|
|
smtp_user = "AKIA4QSVRJTZ4GXTPTMX"
|
|
smtp_pass = "BCodXCh7VY126D3/C3lu5SCSmq6pjjF2uxMxZmd4nQFJ"
|
|
sender_email = "alerts@henry-pump.com"
|
|
receiver_email = ", ".join(recipients)
|
|
|
|
|
|
# Create a multipart message and set headers
|
|
message = MIMEMultipart()
|
|
message["From"] = sender_email
|
|
message["To"] = receiver_email
|
|
message["Subject"] = subject
|
|
message["Body"] = body
|
|
|
|
filename = "daily_report.xlsx"
|
|
|
|
# Open XLSX file in binary mode
|
|
with open(filename, "rb") as attachment:
|
|
# Add file as application/octet-stream
|
|
# Email client can usually download this automatically as attachment
|
|
part = MIMEBase("application", "octet-stream")
|
|
part.set_payload(attachment.read())
|
|
|
|
# Encode file in ASCII characters to send by email
|
|
encoders.encode_base64(part)
|
|
|
|
months = {
|
|
1: 'JAN',
|
|
2: 'FEB',
|
|
3: 'MAR',
|
|
4: 'APR',
|
|
5: 'MAY',
|
|
6: 'JUN',
|
|
7: 'JUL',
|
|
8: 'AUG',
|
|
9: 'SEP',
|
|
10: 'OCT',
|
|
11: 'NOV',
|
|
12: 'DEC'
|
|
}
|
|
# Add header as key/value pair to attachment part
|
|
part.add_header(
|
|
"Content-Disposition",
|
|
"attachment; filename= daily_report_{}_{}_{}.xlsx".format(dt.today().day, months.get(dt.today().month), dt.today().year),
|
|
)
|
|
|
|
# Add attachment to message and convert message to string
|
|
message.attach(part)
|
|
text = message.as_string()
|
|
|
|
# Log in to server using secure context and send email
|
|
server = smtplib.SMTP("email-smtp.us-east-1.amazonaws.com", 587)
|
|
server.ehlo()
|
|
server.starttls()
|
|
server.ehlo()
|
|
server.login(smtp_user,smtp_pass)
|
|
server.sendmail(sender_email, receiver_email.split(","), text)
|
|
server.quit()
|
|
|