hooks.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. Modbus TestKit: Implementation of Modbus protocol in python
  5. (C)2009 - Luc Jean - luc.jean@gmail.com
  6. (C)2009 - Apidev - http://www.apidev.fr
  7. This is distributed under GNU LGPL license, see license.txt
  8. """
  9. from __future__ import with_statement
  10. import threading
  11. _LOCK = threading.RLock()
  12. _HOOKS = {}
  13. def install_hook(name, fct):
  14. """
  15. Install one of the following hook
  16. modbus_rtu.RtuMaster.before_open((master,))
  17. modbus_rtu.RtuMaster.after_close((master,)
  18. modbus_rtu.RtuMaster.before_send((master, request)) returns modified request or None
  19. modbus_rtu.RtuMaster.after_recv((master, response)) returns modified response or None
  20. modbus_rtu.RtuServer.before_close((server, ))
  21. modbus_rtu.RtuServer.after_close((server, ))
  22. modbus_rtu.RtuServer.before_open((server, ))
  23. modbus_rtu.RtuServer.after_open(((server, ))
  24. modbus_rtu.RtuServer.after_read((server, request)) returns modified request or None
  25. modbus_rtu.RtuServer.before_write((server, response)) returns modified response or None
  26. modbus_rtu.RtuServer.after_write((server, response))
  27. modbus_rtu.RtuServer.on_error((server, excpt))
  28. modbus_tcp.TcpMaster.before_connect((master, ))
  29. modbus_tcp.TcpMaster.after_connect((master, ))
  30. modbus_tcp.TcpMaster.before_close((master, ))
  31. modbus_tcp.TcpMaster.after_close((master, ))
  32. modbus_tcp.TcpMaster.before_send((master, request))
  33. modbus_tcp.TcpServer.after_send((master, request))
  34. modbus_tcp.TcpMaster.after_recv((master, response))
  35. modbus_tcp.TcpServer.on_connect((server, client, address))
  36. modbus_tcp.TcpServer.on_disconnect((server, sock))
  37. modbus_tcp.TcpServer.after_recv((server, sock, request)) returns modified request or None
  38. modbus_tcp.TcpServer.before_send((server, sock, response)) returns modified response or None
  39. modbus_tcp.TcpServer.on_error((server, sock, excpt))
  40. modbus_rtu_over_tcp.RtuOverTcpMaster.after_recv((master, response))
  41. modbus.Master.before_send((master, request)) returns modified request or None
  42. modbus.Master.after_send((master))
  43. modbus.Master.after_recv((master, response)) returns modified response or None
  44. modbus.Slave.handle_request((slave, request_pdu)) returns modified response or None
  45. modbus.Slave.handle_write_multiple_coils_request((slave, request_pdu))
  46. modbus.Slave.handle_write_multiple_registers_request((slave, request_pdu)) returns modified response or None
  47. modbus.Slave.handle_write_single_register_request((slave, request_pdu)) returns modified response or None
  48. modbus.Slave.handle_write_single_coil_request((slave, request_pdu)) returns modified response or None
  49. modbus.Slave.handle_read_input_registers_request((slave, request_pdu)) returns modified response or None
  50. modbus.Slave.handle_read_holding_registers_request((slave, request_pdu)) returns modified response or None
  51. modbus.Slave.handle_read_discrete_inputs_request((slave, request_pdu)) returns modified response or None
  52. modbus.Slave.handle_read_coils_request((slave, request_pdu)) returns modified response or None
  53. modbus.Slave.on_handle_broadcast((slave, response_pdu)) returns modified response or None
  54. modbus.Slave.on_exception((slave, function_code, excpt))
  55. modbus.Databank.on_error((db, excpt, request_pdu))
  56. modbus.ModbusBlock.setitem((self, slice, value))
  57. modbus.Server.before_handle_request((server, request)) returns modified request or None
  58. modbus.Server.after_handle_request((server, response)) returns modified response or None
  59. """
  60. with _LOCK:
  61. try:
  62. _HOOKS[name].append(fct)
  63. except KeyError:
  64. _HOOKS[name] = [fct]
  65. def uninstall_hook(name, fct=None):
  66. """remove the function from the hooks"""
  67. with _LOCK:
  68. if fct:
  69. _HOOKS[name].remove(fct)
  70. else:
  71. del _HOOKS[name][:]
  72. def call_hooks(name, args):
  73. """call the function associated with the hook and pass the given args"""
  74. with _LOCK:
  75. try:
  76. for fct in _HOOKS[name]:
  77. retval = fct(args)
  78. if retval is not None:
  79. return retval
  80. except KeyError:
  81. pass
  82. return None