1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
| from flask import Flask,request from flask.ext.restful import reqparse, abort, Api, Resource from monitorDB import db,Piclist,User,Setting from datetime import datetime, timedelta from flask.ext.cors import CORS import os from camera import piCameraCapture import datetime
app = Flask(__name__)
api = Api(app) cors = CORS(app, resources={r"/monitor/api/v1.0/*": {"origins": "*"}})
parser = reqparse.RequestParser() parser.add_argument('picid', type=int) parser.add_argument('userid', type=int) parser.add_argument('status', type=int) parser.add_argument('username', type=str) parser.add_argument('password', type=str) parser.add_argument('email', type=str) parser.add_argument('setid', type=int) parser.add_argument('setvalue', type=int)
def getTime(): return datetime.datetime.now()
def getCameraSwitch() : cameraSwitch = Setting.query.filter_by(setname='cameraSwitch').first() return cameraSwitch.setvalue
def getAlarmSwitch() : alarmSwitch = Setting.query.filter_by(setname='alarmSwitch').first() return alarmSwitch.setvalue
def getMailAddres() : alarmMailto = Setting.query.filter_by(setname='alarmMailto').first() mailID = alarmMailto.setvalue mailAddress = User.query.filter_by(id=mailID).first() return mailAddress.email
def getPiclist(): lim = request.args.get('limit', 10) off = request.args.get('offset', 0) results = Piclist.query.order_by(-Piclist.id).limit(lim).offset(off).all()
json_results = [] for result in results: d = {'picid': result.id, 'picname': result.picname, 'time': result.time.strftime('%Y-%m-%d %H:%M:%S'), 'status':result.status } json_results.append(d)
return json_results
def deletePic(picid): result = Piclist.query.filter_by(id=picid).first() for i in range(1,5): try: os.remove('static/imgs/%s_%i.jpg' %(result.picname,i)) except: print (' %s image file already deleted' %result.picname) try: os.remove('static/imgs/%s_%i_thumbnail.jpg' %(result.picname,i)) except: print (' %s image file already deleted' %result.picname) db.session.delete(result) db.session.commit() print('Removed image %s' %name) conn.commit() conn.close()
def getUserlist(): lim = request.args.get('limit', 10) off = request.args.get('offset', 0) results = User.query.limit(lim).offset(off).all()
json_results = [] for result in results: d = {'id': result.id, 'username': result.username, 'password': result.password, 'email':result.email } json_results.append(d)
return json_results
def userLogin(username,password): result = User.query.filter_by(username=username).first() if (result == None): return 401 else: if(username == result.username and password == result.password): return 201 else: return 401
def getSettinglist(): results = Setting.query.all()
json_results = [] for result in results: d = {'setid': result.id, 'setname': result.setname, 'setvalue': result.setvalue, 'setvalue2': result.setexplain } json_results.append(d)
return json_results
def getWeeklyCount(): NOW = datetime.now() NOW = NOW.strftime('%Y-%m-%d') NOW = datetime.strptime(NOW, '%Y-%m-%d') last_10d_count = [] for d in range(10,0,-1): count = Piclist.query.filter(Piclist.time.between(NOW - timedelta(days=d-1), NOW - timedelta(days=d-2))).count()
last_10d_count.append(count) return last_10d_count
def abort_if_pic_doesnt_exist(picid): if (Piclist.query.filter_by(id=picid).first() == None): abort(404, message="Pic {} doesn't exist".format(picid))
def abort_if_user_id_doesnt_exist(userid): if (User.query.filter_by(id=userid).first() == None): abort(404, message="User {} doesn't exist".format(userid))
app.route('/') def hello(): return "Hello! :)"
class pic(Resource): def get(self, picid): abort_if_pic_doesnt_exist(picid) result = Piclist.query.filter_by(id=picid).first() json_result = {'picid': result.id, 'picname': result.picname, 'time': result.time.strftime('%Y-%m-%d %H:%M:%S'), 'status':result.status }
return json_result
def delete(self, picid): abort_if_pic_doesnt_exist(picid) deletePic(picid) return 204
def put(self, picid): args = parser.parse_args() status = args['status'] pic = Piclist.query.filter_by(id=picid).first() pic.status = status db.session.commit() return 201
class piclists(Resource): def get(self): return getPiclist()
class user(Resource):
def get(self, userid): abort_if_user_id_doesnt_exist(userid) result = User.query.filter_by(id=userid).first() json_result = {'id': result.id, 'username': result.username, 'password': result.password, 'email':result.email } return json_result
def delete(self, userid): abort_if_user_id_doesnt_exist(userid) result = User.query.filter_by(id=userid).first() count = User.query.count() if (count == 1): return 405 else: db.session.delete(result) db.session.commit() return 204
def put(self, userid): abort_if_user_id_doesnt_exist(userid) args = parser.parse_args() user = User.query.filter_by(id=userid).first() user.userid = userid user.username = args['username'] user.password = args['password'] user.email = args['email'] db.session.add(user) db.session.commit() return 201
class userlists(Resource): def get(self): return getUserlist()
def post(self): args = parser.parse_args() user = User(args['username'],args['password'],args['email']) db.session.add(user) db.session.commit() return 201
class userlogin(Resource): def post(self): args = parser.parse_args() username = args['username'] password = args['password'] return userLogin(username,password)
class setting(Resource): def put(self, setid): args = parser.parse_args() setting = Setting.query.filter_by(id=setid).first() setting.setvalue = args['setvalue'] db.session.commit() return 201
class settinglists(Resource): def get(self): return getSettinglist()
class weeklycount(Resource): def get(self): return getWeeklyCount()
class shoot(Resource): def get(self): currentTime = getTime() piCameraCapture(currentTime) return 201
api.add_resource(piclists, '/monitor/api/v1.0/piclist/') api.add_resource(pic, '/monitor/api/v1.0/piclist/<int:picid>') api.add_resource(userlists, '/monitor/api/v1.0/user/') api.add_resource(userlogin, '/monitor/api/v1.0/userlogin/') api.add_resource(user, '/monitor/api/v1.0/user/<int:userid>') api.add_resource(settinglists, '/monitor/api/v1.0/setting/') api.add_resource(setting, '/monitor/api/v1.0/setting/<int:setid>') api.add_resource(weeklycount, '/monitor/api/v1.0/week/') api.add_resource(shoot, '/monitor/api/v1.0/shoot/')
if __name__ == '__main__': app.run(host='0.0.0.0',debug=True)
|