|
1 | 1 | import depthai as dai
|
2 | 2 | import zipfile
|
3 |
| -from json import dump , JSONEncoder |
4 |
| -from os.path import exists |
5 |
| -import os |
| 3 | +from json import dump, JSONEncoder |
| 4 | +import io |
6 | 5 |
|
7 | 6 | class CustomEncoder(JSONEncoder):
|
8 | 7 | def default(self, obj):
|
9 |
| - # If the object has a `__str__` method, use it |
10 | 8 | if hasattr(obj, '__str__'):
|
11 | 9 | return str(obj)
|
12 |
| - # Otherwise, use the default encoder's method |
13 | 10 | return super().default(obj)
|
14 | 11 |
|
15 | 12 | data = {}
|
16 | 13 |
|
17 | 14 | with dai.Device() as device:
|
18 |
| - |
19 | 15 | data['BootloaderVersion'] = device.getBootloaderVersion()
|
20 | 16 | data['ConnectedIMU'] = device.getConnectedIMU()
|
21 | 17 | data['imuFirmwareVersion'] = device.getIMUFirmwareVersion()
|
22 |
| - filename = 'metadata.json' |
23 |
| - with open(filename, 'w') as f: |
24 |
| - dump(data, f, indent=2, cls=CustomEncoder) |
25 | 18 |
|
| 19 | + filenames = [ |
| 20 | + "metadata.json", |
| 21 | + "userCalib.json", |
| 22 | + "factoryCalib.json" |
| 23 | + |
| 24 | + ] |
26 | 25 |
|
27 |
| - usercalibPath = 'userCalib.json' |
| 26 | + # Create in-memory file-like objects |
| 27 | + metadata_io = io.StringIO() |
| 28 | + usercalib_io = io.StringIO() |
| 29 | + factcalib_io = io.StringIO() |
| 30 | + crashDump_io = io.StringIO() |
| 31 | + # Dump data into these in-memory files |
| 32 | + dump(data, metadata_io, indent=2, cls=CustomEncoder) |
| 33 | + |
28 | 34 | try:
|
29 |
| - |
30 |
| - with open(usercalibPath,'w') as f: |
31 |
| - dump(device.readCalibration2().eepromToJson(), f, indent=2) |
32 |
| - dump(device.readCalibrationRaw(), f, indent=4) |
| 35 | + dump(device.readCalibration2().eepromToJson(), usercalib_io, indent=2) |
| 36 | + dump(device.readCalibrationRaw(), usercalib_io, indent=4) |
33 | 37 | except Exception as ex:
|
34 |
| - |
35 |
| - with open(usercalibPath,'w') as f: |
36 |
| - dump(str(ex), f, indent=2) |
| 38 | + dump(str(ex), usercalib_io, indent=2) |
37 | 39 |
|
38 |
| - factcalibPath = 'factoryCalib.json' |
39 | 40 | try:
|
40 |
| - |
41 |
| - with open(factcalibPath,'w') as f: |
42 |
| - dump(device.readFactoryCalibration().eepromToJson(), f, indent=2) |
43 |
| - dump(device.readFactoryCalibrationRaw(), f, indent=4) |
| 41 | + dump(device.readFactoryCalibration().eepromToJson(), factcalib_io, indent=2) |
| 42 | + dump(device.readFactoryCalibrationRaw(), factcalib_io, indent=4) |
44 | 43 | except Exception as ex:
|
45 |
| - |
46 |
| - with open(factcalibPath,'w') as f: |
47 |
| - dump(str(ex), f, indent=2) |
| 44 | + dump(str(ex), factcalib_io, indent=2) |
| 45 | + |
| 46 | + in_memory_files = [metadata_io, usercalib_io, factcalib_io] |
48 | 47 |
|
49 | 48 | zip_name = 'device_info.zip'
|
| 49 | + |
| 50 | + with zipfile.ZipFile(zip_name, 'w') as z: |
| 51 | + # Zip in-memory file-like objects without writing to disk |
| 52 | + for file_io, filename in zip(in_memory_files, filenames): |
| 53 | + file_io.seek(0) # Reset cursor to the beginning of the file |
| 54 | + z.writestr(filename, file_io.getvalue()) |
| 55 | + |
50 | 56 | if device.hasCrashDump():
|
51 | 57 | crashDump = device.getCrashDump()
|
52 | 58 | commitHash = crashDump.depthaiCommitHash
|
53 | 59 | deviceId = crashDump.deviceId
|
| 60 | + filenames.append("crashreport_" + commitHash + "_" + deviceId + ".json") |
| 61 | + dump(crashDump.serializeToJson(), crashDump_io, indent=2) |
| 62 | + in_memory_files.append(crashDump_io) |
54 | 63 |
|
55 |
| - json = crashDump.serializeToJson() |
56 |
| - |
57 |
| - i = -1 |
58 |
| - while True: |
59 |
| - i += 1 |
60 |
| - destPath = "crashDump_" + str(i) + "_" + deviceId + "_" + commitHash + ".json" |
61 |
| - if exists(destPath): |
62 |
| - continue |
63 |
| - |
64 |
| - with open(destPath, 'w', encoding='utf-8') as f: |
65 |
| - dump(json, f, ensure_ascii=False, indent=4) |
| 64 | + with zipfile.ZipFile(zip_name, 'w') as z: |
| 65 | + # Zip in-memory file-like objects without writing to disk |
| 66 | + for file_io, filename in zip(in_memory_files, filenames): |
| 67 | + file_io.seek(0) # Reset cursor to the beginning of the file |
| 68 | + z.writestr(filename, file_io.getvalue()) |
66 | 69 |
|
67 |
| - print("Crash dump found on your device!") |
68 |
| - print(f"Saved to {destPath}") |
69 |
| - print("Please report to developers!") |
70 |
| - with zipfile.ZipFile(zip_name, 'w') as z: |
71 |
| - z.write(filename) |
72 |
| - z.write(usercalibPath) |
73 |
| - z.write(factcalibPath) |
74 |
| - z.write(destPath) |
75 |
| - os.remove(filename) |
76 |
| - os.remove(usercalibPath) |
77 |
| - os.remove(factcalibPath) |
78 |
| - os.remove(destPath) |
79 |
| - break |
| 70 | + print("Crash dump found on your device!") |
| 71 | + print("Please report to developers!") |
80 | 72 | else:
|
81 |
| - with zipfile.ZipFile(zip_name, 'w') as z: |
82 |
| - z.write(filename) |
83 |
| - z.write(usercalibPath) |
84 |
| - z.write(factcalibPath) |
85 |
| - os.remove(filename) |
86 |
| - os.remove(usercalibPath) |
87 |
| - os.remove(factcalibPath) |
88 | 73 | print("There was no crash dump found on your device!")
|
89 |
| -# Zip the JSON file |
90 |
| - |
91 |
| - |
92 | 74 |
|
93 | 75 | print("Please report to developers via forum with the zip attached!")
|
0 commit comments