server-ansys.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. from abc import ABC
  2. import tornado.ioloop
  3. import tornado.web
  4. import tornado.escape
  5. import collections
  6. import json
  7. import pyansys
  8. import os
  9. import sqlite3 as lite
  10. import sys
  11. import gfk_plate
  12. import time
  13. import logging
  14. from ini_logger import *
  15. # log_dir = r'/var/log/server'
  16. # ini_logger(log_dir)
  17. inputs = [
  18. {'type': "e11", 'value': [42.5]},
  19. {'type': "e22", 'value': [11]},
  20. {'type': "e33", 'value': [11]},
  21. {'type': "pr12", 'value': [0.28]},
  22. {'type': "pr13", 'value': [0.28]},
  23. {'type': "pr23", 'value': [0.28]},
  24. {'type': "g12", 'value': [4.2]},
  25. {'type': "g13", 'value': [4.2]},
  26. {'type': "g23", 'value': [2.56]},
  27. {'type': "dens", 'value': [1950E-6]},
  28. {'type': "alp11", 'value': [5.7E-6]},
  29. {'type': "alp22", 'value': [45E-6]},
  30. {'type': "alp33", 'value': [45E-6]},
  31. {'type': "k11", 'value': [0.72E3]},
  32. {'type': "k22", 'value': [0.5E3]},
  33. {'type': "k33", 'value': [0.5E3]},
  34. {'type': "bet11", 'value': [0E-3]},
  35. {'type': "bet22", 'value': [4E-3]},
  36. {'type': "bet33", 'value': [4E-3]},
  37. {'type': "d11", 'value': [4.4E3]},
  38. {'type': "d22", 'value': [3.1E3]},
  39. {'type': "d33", 'value': [3.1E3]},
  40. {'type': "angles", 'value': [90, 0, 0, 0, 0, 0]}
  41. ]
  42. outputs = [
  43. {'type': "Srmax", 'value': [0]},
  44. {'type': "Stmax", 'value': [2]},
  45. {'type': "S1max", 'value': [4]},
  46. {'type': "S2max", 'value': [4]}
  47. ]
  48. status = {
  49. "status": "ready"
  50. }
  51. def update():
  52. materials = []
  53. for i in range(0, len(inputs[0]['value'])):
  54. material = {}
  55. for j in range(0, len(inputs) - 1):
  56. material[inputs[j]['type']] = inputs[j]['value'][i]
  57. materials.append(material)
  58. success = False
  59. while not success:
  60. try:
  61. outputs[0]['value'][0], outputs[1]['value'][0], outputs[2]['value'][0], outputs[3]['value'][
  62. 0] = gfk_plate.sim_rotor(materials, inputs[len(inputs) - 1]['value'])
  63. success = True
  64. except PermissionError:
  65. print("Catch Error")
  66. # time.sleep(3)
  67. success = False
  68. class MainHandler(tornado.web.RequestHandler, ABC):
  69. def prepare(self):
  70. self.set_header("Content-Type", "application/json")
  71. if self.request.headers.get('Content-Type') == 'application/json':
  72. print(self.request.body)
  73. self.json_args = json.loads(self.request.body.decode('utf-8'))
  74. else:
  75. self.json_args = None
  76. def get(self, param):
  77. print("INFO: GET /" + param)
  78. if param == "inputs":
  79. self.write(json.dumps(inputs))
  80. elif param == "outputs":
  81. self.write(json.dumps(outputs))
  82. elif param == "status":
  83. self.write(json.dumps(status))
  84. elif param == "stop":
  85. instance = tornado.ioloop.IOLoop.instance()
  86. instance.add_callback(instance.stop)
  87. else:
  88. self.write("Error: GET /" + param)
  89. print("Error: GET /" + param)
  90. def post(self, param):
  91. if param == "update":
  92. status["status"] = "updating"
  93. if self.json_args is not None:
  94. # print("INFO: GET /" + param + "with body: " + self.json_args.dump())
  95. counter = 0
  96. for entity in self.json_args:
  97. inputs[counter]['value'] = entity['value']
  98. # print("INFO: update " + str(entity['value'][0]))
  99. counter = counter + 1
  100. update()
  101. self.write(json.dumps(outputs))
  102. status["status"] = "ready"
  103. else:
  104. self.write("Error: POST /" + param)
  105. print("Error: POST /" + param)
  106. def make_app():
  107. return tornado.web.Application([
  108. (r"/(.*)", MainHandler),
  109. ])
  110. if __name__ == "__main__":
  111. app = make_app()
  112. if len(sys.argv) == 1:
  113. app.listen(int(8888))
  114. else:
  115. app.listen(int(sys.argv[1]))
  116. tornado.ioloop.IOLoop.current().start()