reHDDPrinter/reHDDPrinter.py

78 lines
3.0 KiB
Python
Raw Normal View History

2022-11-23 18:38:27 +01:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
""" Author: Hendrik Schutter, localhorst@mosad.xyz
Date of creation: 2022/11/23
Date of last modification: 2022/11/23
"""
2022-11-23 21:56:51 +01:00
import sysv_ipc
2022-11-23 22:28:01 +01:00
import pycstruct
import os
2022-12-07 21:25:09 +01:00
import time
2022-11-23 22:28:01 +01:00
from brother_ql.brother_ql_create import create_label
from brother_ql.raster import BrotherQLRaster
2022-11-23 18:38:27 +01:00
import layouter
2022-11-23 21:56:51 +01:00
str_buffer_size = 64 #keep this synchronous to reHDD
msg_queue_key = 0x1B11193C0 #keep this synchronous to reHDD
2022-11-23 22:28:01 +01:00
file_name = "output.png"
2022-12-07 21:25:09 +01:00
printer_path = "/dev/usb/lp0"
2022-11-23 22:28:01 +01:00
2022-11-23 21:56:51 +01:00
def get_struct_format():
#keep this synchronous to struct in reHDD
driveData = pycstruct.StructDef()
driveData.add('utf-8', 'driveIndex', length=str_buffer_size)
driveData.add('utf-8', 'driveHours', length=str_buffer_size)
driveData.add('utf-8', 'driveCycles', length=str_buffer_size)
driveData.add('utf-8', 'driveErrors', length=str_buffer_size)
driveData.add('utf-8', 'driveShredTimestamp', length=str_buffer_size)
driveData.add('utf-8', 'driveShredDuration', length=str_buffer_size)
driveData.add('utf-8', 'driveCapacity', length=str_buffer_size)
driveData.add('utf-8', 'driveState', length=str_buffer_size)
driveData.add('utf-8', 'driveModelFamiliy', length=str_buffer_size)
driveData.add('utf-8', 'driveModelName', length=str_buffer_size)
driveData.add('utf-8', 'driveSerialnumber', length=str_buffer_size)
driveData.add('utf-8', 'driveReHddVersion', length=str_buffer_size)
return driveData
2022-11-23 18:38:27 +01:00
def main():
2022-11-23 21:56:51 +01:00
try:
mq = sysv_ipc.MessageQueue(msg_queue_key, sysv_ipc.IPC_CREAT)
2022-11-23 18:38:27 +01:00
2022-11-23 21:56:51 +01:00
while True:
message, mtype = mq.receive()
driveData = get_struct_format().deserialize(message)
2022-11-23 18:38:27 +01:00
2022-11-23 21:56:51 +01:00
rehdd_info = layouter.ReHddInfo("https://git.mosad.xyz/localhorst/reHDD", driveData['driveReHddVersion'])
drive = layouter.DriveData(
drive_index=int(driveData['driveIndex']),\
drive_state=str(driveData['driveState']),\
modelfamiliy=str(driveData['driveModelFamiliy']),\
modelname=str(driveData['driveModelName']),\
capacity=int(driveData['driveCapacity']),\
serialnumber=str(driveData['driveSerialnumber']),\
power_on_hours=int(driveData['driveHours']),\
power_cycle=int(driveData['driveCycles']),\
smart_error_count=int(driveData['driveErrors']),\
shred_timestamp=int(driveData['driveShredTimestamp']),\
shred_duration=int(driveData['driveShredDuration']))
2022-12-07 21:25:09 +01:00
while(not os.path.exists(printer_path)):
print("Printer not found, waiting ...")
time.sleep(30) #sleep 30
2022-11-23 22:28:01 +01:00
layouter.generate_image(drive, rehdd_info, file_name)
qlr = BrotherQLRaster("QL-570")
create_label(qlr, file_name, '62')
2022-12-07 21:25:09 +01:00
with open(printer_path, 'wb') as file:
2022-11-23 22:28:01 +01:00
file.write(qlr.data)
os.remove(file_name)
2022-11-23 21:56:51 +01:00
except sysv_ipc.ExistentialError:
print("ERROR: message queue creation failed")
2022-11-23 18:38:27 +01:00
if __name__ == "__main__":
2022-11-23 21:56:51 +01:00
main()