gateway_web.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. from sanic import Sanic
  4. from sanic_cors import CORS, cross_origin
  5. from sanic import response
  6. from event_storage import EventStorage
  7. from utility import Utility
  8. from api_context import ApiContext
  9. app = Sanic(__name__)
  10. CORS(app)
  11. gateway_storage = EventStorage()
  12. @app.route('/readRealText', methods=['POST'])
  13. async def read_point_data_text(request):
  14. list = request.json['pointList']
  15. dict = gateway_storage.get_real_data(list)
  16. point_list = gateway_storage.get_point_info(list)
  17. data_list = {}
  18. for info in point_list:
  19. data_list[info['io_point_name']] = str(dict["c"+str(info['serial_number'])]) + " " +str(info['unit'])
  20. return response.json(data_list)
  21. @app.route('/readReal', methods=['POST'])
  22. async def read_point_data(request):
  23. list = request.json['pointList']
  24. dict = gateway_storage.get_real_data(list)
  25. '''
  26. data_list = gateway_storage.get_point_info(list)
  27. for info in data_list:
  28. info['value'] = dict["c"+str(info['serial_number'])]
  29. '''
  30. return response.json(dict)
  31. @app.route('/api', methods=['POST'])
  32. async def read_statistics_data(request):
  33. if len(request.json) > 0:
  34. list = []
  35. for index in range(len(request.json)):
  36. api_object = request.json[index]['apiObject']
  37. parameter = request.json[index]['parameter']
  38. api = ApiContext()
  39. api.set_api_object(api_object)
  40. result = api.operation(parameter)
  41. list.append(result)
  42. data_json = Utility.data_encoder(list)
  43. return response.text(data_json)
  44. @app.route('/write',methods=['POST'])
  45. async def write_data(request):
  46. station_name = request.json["station_name"]
  47. command = request.json["command"]
  48. try:
  49. result = Utility.available_connectors[station_name].send_command(command)
  50. return response.json({'message': result})
  51. except Exception as e:
  52. print(station_name+"write[ERROR]:" + str(e))
  53. return response.json({'message': result})
  54. @app.route('/s7/getState', methods=['POST'])
  55. async def read_point_data(request):
  56. point_list = ["c577", "c578", "c569", "c570", "c571", "c572", "c573", "c574", "c575",
  57. "c402", "c423", "c411", "c815", "c432", "c817"]
  58. dict = gateway_storage.get_real_data(point_list)
  59. return response.json(dict)
  60. if __name__ == "__main__":
  61. app.run(host="0.0.0.0", port=8080, workers=8)