awlsim-client 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. from awlsim.common.compat import *
  24. import sys
  25. import getopt
  26. from awlsim.common import *
  27. from awlsim.coreclient import *
  28. class TextInterfaceAwlSimClient(AwlSimClient):
  29. pass
  30. def usage():
  31. print("awlsim-client version %s" % VERSION_STRING)
  32. print("")
  33. print("Usage: awlsim-client [OPTIONS] <ACTIONS>")
  34. print("")
  35. print("Options:")
  36. print(" -c|--connect HOST[:PORT] Connect to the server at HOST:PORT")
  37. print(" Defaults to %s:%d" %\
  38. (AwlSimServer.DEFAULT_HOST, AwlSimServer.DEFAULT_PORT))
  39. print(" -t|--timeout 10.0 Set the connection timeout (default 10 s)")
  40. print(" -L|--loglevel LVL Set the client log level:")
  41. print(" 0: Log nothing")
  42. print(" 1: Log errors")
  43. print(" 2: Log errors and warnings (default)")
  44. print(" 3: Log errors, warnings and info messages")
  45. print(" 4: Verbose logging")
  46. print(" 5: Extremely verbose logging")
  47. print("")
  48. print("Actions to be performed on the server:")
  49. print(" -r|--runstate RUN/STOP Set the run state of the CPU.")
  50. def main():
  51. opt_connect = (AwlSimServer.DEFAULT_HOST, AwlSimServer.DEFAULT_PORT)
  52. opt_timeout = 10.0
  53. opt_loglevel = Logging.LOG_WARNING
  54. actions = []
  55. try:
  56. (opts, args) = getopt.getopt(sys.argv[1:],
  57. "hc:t:L:r:",
  58. [ "help", "connect=", "timeout=", "loglevel=",
  59. "runstate=", ])
  60. except getopt.GetoptError as e:
  61. printError(str(e))
  62. usage()
  63. return 1
  64. for (o, v) in opts:
  65. if o in ("-h", "--help"):
  66. usage()
  67. return 0
  68. if o in ("-c", "--connect"):
  69. try:
  70. host, port = parseNetAddress(v)
  71. if port is None:
  72. port = AwlSimServer.DEFAULT_PORT
  73. opt_connect = (host, port)
  74. except AwlSimError as e:
  75. printError("-c|--connect: %s" % e.message)
  76. sys.exit(1)
  77. if o in ("-t", "--timeout"):
  78. try:
  79. opt_timeout = float(v)
  80. except ValueError:
  81. printError("-t|--timeout: Invalid timeout value")
  82. sys.exit(1)
  83. if o in ("-L", "--loglevel"):
  84. try:
  85. opt_loglevel = int(v)
  86. except ValueError:
  87. printError("-L|--loglevel: Invalid log level")
  88. sys.exit(1)
  89. if o in ("-r", "--runstate"):
  90. if v.upper().strip() in ("RUN", "1", "START"):
  91. actions.append(("runstate", True))
  92. elif v.upper().strip() in ("STOP", "0"):
  93. actions.append(("runstate", False))
  94. else:
  95. printError("-r|--runstate: Invalid run state")
  96. sys.exit(1)
  97. if args:
  98. usage()
  99. return 1
  100. if not actions:
  101. usage()
  102. return 1
  103. exitCode = 0
  104. client = None
  105. try:
  106. Logging.setLoglevel(opt_loglevel)
  107. client = TextInterfaceAwlSimClient()
  108. client.connectToServer(host = opt_connect[0],
  109. port = opt_connect[1],
  110. timeout = opt_timeout)
  111. for action, actionValue in actions:
  112. if action == "runstate":
  113. client.setRunState(actionValue)
  114. else:
  115. assert(0)
  116. except AwlSimError as e:
  117. printError(e.getReport())
  118. return 1
  119. finally:
  120. if client:
  121. client.shutdown()
  122. return exitCode
  123. if __name__ == "__main__":
  124. sys.exit(main())