Example App: Retail store inventry
[ealt-edge.git] / example-apps / ROBO / retail_app / inventry / retail_app.py
1 #
2 # Copyright 2020 Huawei Technologies Co., Ltd.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16
17 import config
18 from flask_sslify import SSLify
19 from flask import Flask, request, Response
20 from flask_cors import CORS
21
22
23 app = Flask(__name__)
24 CORS(app)
25 sslify = SSLify(app)
26 app.config['JSON_AS_ASCII'] = False
27 app.config['UPLOAD_PATH'] = '/usr/app/images/'
28 app.config['supports_credentials'] = True
29 app.config['CORS_SUPPORTS_CREDENTIALS'] = True
30 app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
31 ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg'])
32 count = 0
33 listOfMsgs = []
34
35
36 class shelf_inventry():
37     """
38     def __init__(self, url):
39         # self.video = cv2.VideoCapture(url)
40
41     def delete(self):
42         # self.video.release()
43         return
44     """
45
46
47 def store_data():
48     """
49     store time series data in influx db
50     """
51     # TODO config, schema table, DB, fill data set
52
53
54 def obj_detect():
55     """
56     detect obj and count for self
57     """
58
59
60 @app.route('/v1/monitor/cameras', methods=['POST'])
61 def add_camera():
62     camera_info = request.json
63     app.logger.info("Received message from ClientIP [" + request.remote_addr
64                     + "] Operation [" + request.method + "]" +
65                     " Resource [" + request.url + "]")
66     camera_info = {"name": camera_info["name"],
67                    "rtspurl": camera_info["rtspurl"],
68                    "location": camera_info["location"]}
69     # listOfCameras.append(camera_info)
70     return Response("success")
71
72
73 @app.route('/v1/monitor/cameras/<name>/<rtspurl>/<location>', methods=['GET'])
74 def get_camera(name, rtspurl, location):
75     """
76     register camera with location
77     """
78     app.logger.info("Received message from ClientIP [" + request.remote_addr
79                     + "] Operation [" + request.method + "]" +
80                     " Resource [" + request.url + "]")
81     # camera_info = {"name": name, "rtspurl": rtspurl, "location": location}
82     """
83     if "mp4" in camera_info["rtspurl"]:
84         # video_file = VideoFile(camera_info["rtspurl"])
85         # video_dict = {camera_info["name"]:video_file}
86         # listOfVideos.append(video_dict)
87         # return Response(video(video_file, camera_info["name"]),
88                         # mimetype='multipart/x-mixed-replace; boundary=frame')
89     else:
90         # video_file = VideoCamera(camera_info["rtspurl"])
91         # video_dict = {camera_info["name"]: video_file}
92         # listOfVideos.append(video_dict)
93         # return Response(video(video_file, camera_info["name"]),
94                      # mimetype='multipart/x-mixed-replace; boundary=frame')
95         return Response("success")
96     """
97
98
99 @app.route('/v1/monitor/cameras/<camera_name>', methods=['DELETE'])
100 def delete_camera(camera_name):
101     app.logger.info("Received message from ClientIP [" + request.remote_addr
102                     + "] Operation [" + request.method + "]" +
103                     " Resource [" + request.url + "]")
104     """
105     for video1 in listOfVideos:
106         if camera_name in video1:
107             video_obj = video1[camera_name]
108             video_obj.delete()
109     for camera in listOfCameras:
110         if camera_name == camera["name"]:
111             listOfCameras.remove(camera)
112     for msg in listOfMsgs:
113         if camera_name in msg["msg"]:
114             listOfMsgs.remove(msg)
115     return Response("success")
116     """
117
118
119 @app.route('/v1/monitor/cameras')
120 def query_cameras():
121     app.logger.info("Received message from ClientIP [" + request.remote_addr
122                     + "] Operation [" + request.method + "]" +
123                     " Resource [" + request.url + "]")
124     # return jsonify(listOfCameras)
125     return Response("success")
126
127
128 def start_server(handler):
129     app.logger.addHandler(handler)
130     if config.ssl_enabled:
131         context = (config.ssl_certfilepath, config.ssl_keyfilepath)
132         app.run(host=config.server_address, debug=True, ssl_context=context,
133                 threaded=True, port=config.server_port)
134     else:
135         app.run(host=config.server_address, debug=True, threaded=True,
136                 port=config.server_port)