driver.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from base64 import b64decode
  4. import json
  5. import logging
  6. import os
  7. import subprocess
  8. import time
  9. from odoo import http, tools
  10. from odoo.modules.module import get_resource_path
  11. from odoo.addons.hw_drivers.event_manager import event_manager
  12. from odoo.addons.hw_drivers.main import iot_devices, manager
  13. from odoo.addons.hw_drivers.tools import helpers
  14. _logger = logging.getLogger(__name__)
  15. class DriverController(http.Controller):
  16. @http.route('/hw_drivers/action', type='json', auth='none', cors='*', csrf=False, save_session=False)
  17. def action(self, session_id, device_identifier, data):
  18. """
  19. This route is called when we want to make a action with device (take picture, printing,...)
  20. We specify in data from which session_id that action is called
  21. And call the action of specific device
  22. """
  23. iot_device = iot_devices.get(device_identifier)
  24. if iot_device:
  25. iot_device.data['owner'] = session_id
  26. data = json.loads(data)
  27. # Skip the request if it was already executed (duplicated action calls)
  28. iot_idempotent_id = data.get("iot_idempotent_id")
  29. if iot_idempotent_id:
  30. idempotent_session = iot_device._check_idempotency(iot_idempotent_id, session_id)
  31. if idempotent_session:
  32. _logger.info("Ignored request from %s as iot_idempotent_id %s already received from session %s",
  33. session_id, iot_idempotent_id, idempotent_session)
  34. return False
  35. iot_device.action(data)
  36. return True
  37. return False
  38. @http.route('/hw_drivers/check_certificate', type='http', auth='none', cors='*', csrf=False, save_session=False)
  39. def check_certificate(self):
  40. """
  41. This route is called when we want to check if certificate is up-to-date
  42. Used in cron.daily
  43. """
  44. helpers.get_certificate_status()
  45. @http.route('/hw_drivers/event', type='json', auth='none', cors='*', csrf=False, save_session=False)
  46. def event(self, listener):
  47. """
  48. listener is a dict in witch there are a sessions_id and a dict of device_identifier to listen
  49. """
  50. req = event_manager.add_request(listener)
  51. # Search for previous events and remove events older than 5 seconds
  52. oldest_time = time.time() - 5
  53. for event in list(event_manager.events):
  54. if event['time'] < oldest_time:
  55. del event_manager.events[0]
  56. continue
  57. if event['device_identifier'] in listener['devices'] and event['time'] > listener['last_event']:
  58. event['session_id'] = req['session_id']
  59. return event
  60. # Wait for new event
  61. if req['event'].wait(50):
  62. req['event'].clear()
  63. req['result']['session_id'] = req['session_id']
  64. return req['result']
  65. @http.route('/hw_drivers/download_logs', type='http', auth='none', cors='*', csrf=False, save_session=False)
  66. def download_logs(self):
  67. """
  68. Downloads the log file
  69. """
  70. if tools.config['logfile']:
  71. res = http.send_file(tools.config['logfile'], mimetype="text/plain", as_attachment=True)
  72. res.headers['Cache-Control'] = 'no-cache'
  73. return res