-
Notifications
You must be signed in to change notification settings - Fork 1
/
fpdf_handler.py
245 lines (212 loc) · 9.63 KB
/
fpdf_handler.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
import datetime
import shutil
from os import popen
from fpdf import FPDF
from json_dumper import JsonHandler
json_out = JsonHandler()
class sample:
name = "Sample Name"
width = 10
height = 20
age = 0
class fpdf_handler(FPDF):
date = "0"
time = "0"
date_today = "0"
date_and_time = "0"
def set_time(self):
self.date_today = datetime.datetime.today()
self.date = self.date_today.strftime("%d:%m:%Y")
self.time = self.date_today.strftime("%H:%M:%S")
self.date_and_time = self.date_today.strftime("%Y_%m_%d__%H_%M_%S")
def header(self):
# Set up a logo
self.image('/home/pi/Cof-Tabs/mini_logo.png', 10, 8, 33)
self.set_font('Arial', 'I', 15)
# Add test time
self.cell(150)
self.cell(0, 0, 'Date: ' + self.date, ln=1, align="L")
self.cell(150)
self.cell(0, 10, 'Time: ' + self.time, ln=1, align="L")
self.cell(0, 10, "COF Test Results", ln=1, align="C")
# Line break
self.ln(5)
self.set_line_width(1)
self.line(x1=10, y1=30, x2=200, y2=30)
# self.ln(10)
def footer(self):
self.set_y(-10)
self.set_font('Arial', 'I', 12)
self.set_line_width(1)
self.line(x1=10, y1=285, x2=200, y2=285)
self.cell(0, 10,
"Alarge Laboratory Test Technology - https://www.alarge.com.tr/en/ - info@alarge.com.tr")
def graph_to_pdf(self, choise):
if choise == 1:
self.image('/home/pi/Cof-Tabs/graph.png', x=30, y=160, w=150)
else:
self.image('/home/pi/Cof-Tabs/graph.png', x=30, y=190, w=150)
def print_obj(self, obj):
print(obj.name)
def create_pdf(self, max_static, mean_static, max_dynamic, mean_dynamic, sample1, sample2, test_mode, forces):
self.set_time()
self.add_page()
self.set_font('Times', '', 12)
if sample2.name == "":
self.single_table(sample1, max_static, mean_static, max_dynamic, mean_dynamic, test_mode)
self.graph_to_pdf(1)
else:
self.diff_table(sample1, sample2, max_static, mean_static, max_dynamic, mean_dynamic, test_mode)
self.graph_to_pdf(2)
filename = "/home/pi/Cof-Tabs/COF_Test_" + self.date_and_time + ".pdf"
mount_dir = "/media/usb0"
self.output(filename)
self.close()
print("pdf created")
json_out.dump_time(max_static, mean_static, max_dynamic, mean_dynamic, sample1, sample2, test_mode, forces, self.date_and_time)
filename_json = "/home/pi/Cof-Tabs/COF_Test_" + self.date_and_time + ".json"
print("1")
try:
shutil.copy(filename, mount_dir)
shutil.copy(filename_json, mount_dir)
except:
print(" ")
def create_pdf_angle(self, max_static, mean_static, max_dynamic, mean_dynamic, sample1, sample2, test_mode, forces):
self.set_time()
self.add_page()
self.set_font('Times', '', 12)
if sample2.name == "":
self.single_table_angle(sample1, max_static, mean_static, max_dynamic, mean_dynamic, test_mode)
self.graph_to_pdf(1)
else:
self.diff_table_angle(sample1, sample2, max_static, mean_static, max_dynamic, mean_dynamic, test_mode)
self.graph_to_pdf(2)
filename = "/home/pi/Cof-Tabs/COF_Test_" + self.date_and_time + ".pdf"
mount_dir = "/media/usb0/"
self.output(filename)
self.close()
print("pdf created")
json_out.dump_time(max_static, mean_static, max_dynamic, mean_dynamic, sample1, sample2, test_mode, forces,
self.date_and_time)
filename_json = "/home/pi/Cof-Tabs/COF_Test_" + self.date_and_time + ".json"
print("1")
try:
shutil.copy(filename, mount_dir)
shutil.copy(filename_json, mount_dir)
except:
print(" ")
def single_table(self, sample, max_static, mean_static, max_dynamic, mean_dynamic, test_mode):
if test_mode == 1:
test_mode = "Angle Test"
elif test_mode == 0:
test_mode = "Motorized Test"
data = [['Standard: ', "ISO 8295"],
['Company Name: ', str(sample.company_name)],
['Operator Name: ', str(sample.operator_name)],
['Testing Weight(gr): ', str(sample.testing_weight)],
['Test Mode: ', str(test_mode)],
['Sample Name: ', str(sample.name)],
['Sample Width(mm): ', str(sample.width)],
['Sample Height(mm): ', str(sample.height)],
['Sample Age(months): ', str(sample.age)],
['Testing Against: ', 'The same sample'],
['Max Static Coefficient of Friction: ', str(max_static)],
['Mean Static Coefficient of Friction: ', str(mean_static)],
['Max Dynamic Coefficient of Friction: ', str(max_dynamic)],
['Mean Dynamic Coefficient of Friction: ', str(mean_dynamic)]
]
spacing = 2
self.set_font("Arial", size=12)
col_width = self.w / 2.2
row_height = self.font_size
for row in data:
for item in row:
self.cell(col_width, row_height * spacing,
txt=item, border=0)
self.ln(row_height * spacing)
def diff_table(self, sample1, sample2, max_static, mean_static, max_dynamic, mean_dynamic, test_mode):
if test_mode == 1:
test_mode = "Angle Test"
elif test_mode == 0:
test_mode = "Motorized Test"
data = [['Standard: ', "ISO 8295"],
['Company Name: ', str(sample1.company_name)],
['Operator Name: ', str(sample1.operator_name)],
['Testing Weight(gr): ', str(sample1.testing_weight)],
['Test Mode: ', str(test_mode)],
['Sample Name: ', str(sample1.name)],
['Sample Width(mm): ', str(sample1.width)],
['Sample Height(mm): ', str(sample1.height)],
['Sample Age: ', str(sample1.age)],
['Testing Against: ', 'Different Sample'],
['Second Sample Name: ', str(sample2.name)],
['Second Sample Width(mm): ', str(sample2.width)],
['Second Sample Height(mm): ', str(sample2.height)],
['Second Sample Age(months): ', str(sample2.age)],
['Max Static Coefficient of Friction: ', str(max_static)],
['Mean Static Coefficient of Friction: ', str(mean_static)],
['Max Dynamic Coefficient of Friction: ', str(max_dynamic)],
['Mean Dynamic Coefficient of Friction: ', str(mean_dynamic)]
]
spacing = 2
self.set_font("Arial", size=12)
col_width = self.w / 2.2
row_height = self.font_size
for row in data:
for item in row:
self.cell(col_width, row_height * spacing,
txt=item, border=0)
self.ln(row_height * spacing)
def single_table_angle(self, sample, max_static, mean_static, max_dynamic, mean_dynamic, test_mode):
if test_mode == 1:
test_mode = "Angle Test"
elif test_mode == 0:
test_mode = "Motorized Test"
data = [['Standard: ', "ISO 8295"],
['Company Name: ', str(sample.company_name)],
['Operator Name: ', str(sample.operator_name)],
['Testing Weight(gr): ', str(sample.testing_weight)],
['Test Mode: ', str(test_mode)],
['Sample Name: ', str(sample.name)],
['Sample Width(mm): ', str(sample.width)],
['Sample Height(mm): ', str(sample.height)],
['Sample Age(months): ', str(sample.age)],
['Testing Against: ', 'The same sample'],
['Static Coefficient of Friction: ', str(max_static)]
]
spacing = 2
self.set_font("Arial", size=12)
col_width = self.w / 2.2
row_height = self.font_size
for row in data:
for item in row:
self.cell(col_width, row_height * spacing,
txt=item, border=0)
self.ln(row_height * spacing)
def diff_table_angle(self, sample1, sample2, max_static, mean_static, max_dynamic, mean_dynamic, test_mode):
test_mode = "Angle Test"
data = [['Standard: ', "ISO 8295"],
['Company Name: ', str(sample1.company_name)],
['Operator Name: ', str(sample1.operator_name)],
['Testing Weight(gr): ', str(sample1.testing_weight)],
['Test Mode: ', str(test_mode)],
['Sample Name: ', str(sample1.name)],
['Sample Width(mm): ', str(sample1.width)],
['Sample Height(mm): ', str(sample1.height)],
['Sample Age: ', str(sample1.age)],
['Testing Against: ', 'Different Sample'],
['Second Sample Name: ', str(sample2.name)],
['Second Sample Width(mm): ', str(sample2.width)],
['Second Sample Height(mm): ', str(sample2.height)],
['Second Sample Age(months): ', str(sample2.age)],
['Static Coefficient of Friction: ', str(max_static)]
]
spacing = 2
self.set_font("Arial", size=12)
col_width = self.w / 2.2
row_height = self.font_size
for row in data:
for item in row:
self.cell(col_width, row_height * spacing,
txt=item, border=0)
self.ln(row_height * spacing)