mirror of
https://github.com/rickbarrette/redmine_qbo_label_agent.git
synced 2026-11-17 18:10:36 -05:00
Print agent for redmine_qbo_labels
Runs on the label printer's network, asks Redmine for labels every couple of seconds (Bearer token from Administration » Label printers), draws them like the print dialog's preview, and prints them on a Brother QL printer over TCP 9100. Finds the printer on the network over Bonjour/mDNS (or PRINTER), checks its status before each label and every minute, and reports back printed or failed with the printer's error. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
e5ca5b6980
20 files changed
+1054
No files matched your search
@@ -0,0 +1,45 @@
|
||||
"""Talks to Redmine's label print API (redmine_qbo_labels' LabelAgentController)."""
|
||||
|
||||
import logging
|
||||
|
||||
import requests
|
||||
|
||||
from . import __version__
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Unauthorized(Exception):
|
||||
"""Redmine doesn't know this agent's token (wrong, replaced, or the printer is inactive)"""
|
||||
|
||||
|
||||
class Redmine:
|
||||
def __init__(self, url, token, verify=True, timeout=15):
|
||||
self.url = url.rstrip('/')
|
||||
self.timeout = timeout
|
||||
# One kept-alive connection: the name is looked up once, not on every poll
|
||||
self.session = requests.Session()
|
||||
self.session.verify = verify
|
||||
self.session.headers.update({
|
||||
'Authorization': f'Bearer {token}',
|
||||
'Accept': 'application/json',
|
||||
'User-Agent': f'redmine-qbo-label-agent/{__version__}',
|
||||
})
|
||||
|
||||
def _post(self, path, payload):
|
||||
response = self.session.post(f'{self.url}{path}', json=payload, timeout=self.timeout)
|
||||
if response.status_code == 401:
|
||||
raise Unauthorized('Redmine rejected the agent token')
|
||||
response.raise_for_status()
|
||||
return response.json()
|
||||
|
||||
def poll(self, agent=None):
|
||||
"""The next label to print (or None) and how long to wait before asking again"""
|
||||
data = self._post('/label_agent/poll', {'agent': agent} if agent else {})
|
||||
return data.get('job'), float(data.get('poll_seconds') or 2)
|
||||
|
||||
def report(self, job_id, printed, error=None):
|
||||
payload = {'status': 'printed' if printed else 'failed'}
|
||||
if error:
|
||||
payload['error'] = str(error)[:1000]
|
||||
return self._post(f'/label_agent/jobs/{int(job_id)}/result', payload)
|
||||
Reference in new issue
Block a user