awlsim-client 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. #
  4. # AWL simulator - Client interface
  5. #
  6. # Copyright 2013-2016 Michael Buesch <m@bues.ch>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License along
  19. # with this program; if not, write to the Free Software Foundation, Inc.,
  20. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. #
  22. from __future__ import division, absolute_import, print_function, unicode_literals
  23. import sys
  24. import getopt
  25. from awlsim_loader.common import *
  26. from awlsim_loader.coreclient import *
  27. class TextInterfaceAwlSimClient(AwlSimClient):
  28. pass
  29. def usage():
  30. print("awlsim-client version %s" % VERSION_STRING)
  31. print("")
  32. print("Usage: awlsim-client [OPTIONS] <ACTIONS>")
  33. print("")
  34. print("Options:")
  35. print(" -C|--connect-to HOST[:PORT] Connect to the server at HOST:PORT")
  36. print(" Defaults to %s:%d" %\
  37. (AwlSimServer.DEFAULT_HOST, AwlSimServer.DEFAULT_PORT))
  38. print(" -c|--connect Connect to default %s:%d" %\
  39. (AwlSimServer.DEFAULT_HOST, AwlSimServer.DEFAULT_PORT))
  40. print(" -t|--timeout 10.0 Set the connection timeout (default 10 s)")
  41. print(" -L|--loglevel LVL Set the client log level:")
  42. print(" 0: Log nothing")
  43. print(" 1: Log errors")
  44. print(" 2: Log errors and warnings (default)")
  45. print(" 3: Log errors, warnings and info messages")
  46. print(" 4: Verbose logging")
  47. print(" 5: Extremely verbose logging")
  48. print("")
  49. print("Actions to be performed on the server:")
  50. print(" -r|--runstate RUN/STOP Set the run state of the CPU.")
  51. def main():
  52. opt_connect = (AwlSimServer.DEFAULT_HOST, AwlSimServer.DEFAULT_PORT)
  53. opt_timeout = 10.0
  54. opt_loglevel = Logging.LOG_WARNING
  55. actions = []
  56. try:
  57. (opts, args) = getopt.getopt(sys.argv[1:],
  58. "hcC:t:L:r:",
  59. [ "help", "connect", "connect-to=", "timeout=", "loglevel=",
  60. "runstate=", ])
  61. except getopt.GetoptError as e:
  62. printError(str(e))
  63. usage()
  64. return ExitCodes.EXIT_ERR_CMDLINE
  65. for (o, v) in opts:
  66. if o in ("-h", "--help"):
  67. usage()
  68. return ExitCodes.EXIT_OK
  69. if o in ("-c", "--connect"):
  70. opt_connect = (AwlSimServer.DEFAULT_HOST,
  71. AwlSimServer.DEFAULT_PORT)
  72. if o in ("-C", "--connect-to"):
  73. try:
  74. host, port = parseNetAddress(v)
  75. if port is None:
  76. port = AwlSimServer.DEFAULT_PORT
  77. opt_connect = (host, port)
  78. except AwlSimError as e:
  79. printError("-c|--connect: %s" % e.message)
  80. sys.exit(1)
  81. if o in ("-t", "--timeout"):
  82. try:
  83. opt_timeout = float(v)
  84. except ValueError:
  85. printError("-t|--timeout: Invalid timeout value")
  86. sys.exit(1)
  87. if o in ("-L", "--loglevel"):
  88. try:
  89. opt_loglevel = int(v)
  90. except ValueError:
  91. printError("-L|--loglevel: Invalid log level")
  92. sys.exit(1)
  93. if o in ("-r", "--runstate"):
  94. if v.upper().strip() in ("RUN", "1", "START"):
  95. actions.append(("runstate", True))
  96. elif v.upper().strip() in ("STOP", "0"):
  97. actions.append(("runstate", False))
  98. else:
  99. printError("-r|--runstate: Invalid run state")
  100. sys.exit(1)
  101. if args:
  102. usage()
  103. return ExitCodes.EXIT_ERR_CMDLINE
  104. if not actions:
  105. usage()
  106. return ExitCodes.EXIT_ERR_CMDLINE
  107. client = None
  108. try:
  109. Logging.setLoglevel(opt_loglevel)
  110. client = TextInterfaceAwlSimClient()
  111. client.connectToServer(host = opt_connect[0],
  112. port = opt_connect[1],
  113. timeout = opt_timeout)
  114. for action, actionValue in actions:
  115. if action == "runstate":
  116. client.setRunState(actionValue)
  117. else:
  118. assert(0)
  119. except AwlSimError as e:
  120. printError(e.getReport())
  121. return ExitCodes.EXIT_ERR_SIM
  122. finally:
  123. if client:
  124. client.shutdown()
  125. return ExitCodes.EXIT_OK
  126. if __name__ == "__main__":
  127. sys.exit(main())