Create your Gitee Account
Explore and code with more than 6 million developers,Free private repositories !:)
Sign up
This repository doesn't specify license. Without author's permission, this code is only for learning and cannot be used for other purposes.
Clone or download
Alarm.py 6.37 KB
Copy Edit Web IDE Raw Blame History
Mendalas authored 2019-06-19 14:56 . add api
# coding:utf-8
from flask_restful import Resource
from auth import auth
from flask import Flask, abort, request, jsonify, g, url_for
from flask_sqlalchemy import SQLAlchemy
from api import app
from sqlalchemy import and_,or_,case,func
import datetime
from record import InsRecord
db = SQLAlchemy(app)
class InsAlarmList(db.Model):
__tablename__ = 'ins_alarm'
alarm_id = db.Column(db.Integer,primary_key = True)
status = db.Column(db.Integer)
handler = db.Column(db.String(32))
handletime = db.Column(db.DateTime)
handleresult = db.Column(db.String(255))
imgurl = db.Column(db.String(255))
finishTime = db.Column(db.DateTime)
def to_json(self):
return {
"alarmId":self.alarm_id,
"status":self.status,
"handler":self.handler,
"handletimer":str(self.handletime),
"finishTime":str(self.finishTime),
"comment":self.handleresult,
"imgurl":self.imgurl
}
class DoHaveAlarm(Resource):
def post(self):
result = {}
temp = db.session.query(InsAlarmList,InsRecord).filter(and_(InsAlarmList.status == 0\
,InsAlarmList.alarm_id == InsRecord.alarm_id)).all()
value = set()
for re in temp:
value.add(re[1].task)
for i in value:
result[i] = 1
app.logger.info(result)
return result
class HandlerAlarmApi(Resource):
def post(self):
alarmid = request.form["alarmid"]
handlerName = request.form["handlerName"]
_time = request.form["time"]
OP_result = request.form["OP_result"]
_status = int(request.form["status"])
result = db.session.query(InsAlarmList).filter(and_(InsAlarmList.alarm_id == alarmid)).first()
v = {}
if(result!=None):
result.handler = handlerName
result.status = _status
result.handleresult = OP_result
if(result.status == 1):
result.handletime = _time
elif(result.status == 2):
result.finishTime = _time
elif(result.status == 3):
result.finishTime = _time
v['code'] = 1
v["message"] = "OK"
v["data"] = {"msg":"设置成功"}
else:
v['code'] = 0
v["message"] = "alarm not exist"
app.logger.info(v)
return v
class getTodayAlarmNum(Resource):
def post(self):
bid = int(request.form["bid"])
now = datetime.datetime.now()
now = now.strftime("%Y-%m-%d")
msg = {}
if(bid == 0):
unHandle = db.session.query(InsAlarmList,InsRecord).filter(InsAlarmList.status == 0)\
.filter(and_(InsAlarmList.alarm_id == InsRecord.alarm_id,InsRecord.up_time>now)).count()
total = db.session.query(InsAlarmList,InsRecord)\
.filter(and_(InsAlarmList.alarm_id == InsRecord.alarm_id,InsRecord.up_time>now)).count()
msg["unhandle"] = unHandle
msg["total"] = total
else:
unHandle = db.session.query(InsAlarmList,InsRecord).filter(InsAlarmList.status == 0)\
.filter(and_(InsAlarmList.alarm_id == InsRecord.alarm_id,InsRecord.up_time>now,InsRecord.belt_id == bid )).count()
total = db.session.query(InsAlarmList,InsRecord)\
.filter(and_(InsAlarmList.alarm_id == InsRecord.alarm_id,InsRecord.up_time>now,InsRecord.belt_id == bid)).count()
msg["unhandle"] = unHandle
msg["total"] = total
app.logger.info(msg)
return msg
class getAlarmByTimeAndPage(Resource):
def post(self):
beginTs = str(request.form["beginTs"])
pageIndex = int(request.form["page"])
endTs = str(request.form["endTs"])
alarmType = str(request.form["alarmType"])
limitSize=10
totalPage = 0
if beginTs == "0" and endTs== "0":
if(alarmType == ""):
results = db.session.query(InsAlarmList,InsRecord).order_by(InsAlarmList.status,InsRecord.up_time)\
.filter(and_(InsAlarmList.alarm_id == InsRecord.alarm_id))\
.limit(limitSize).offset((pageIndex-1)*limitSize)
totalPage = db.session.query(InsAlarmList).count()
else:
results = db.session.query(InsAlarmList,InsRecord).order_by(InsAlarmList.status,InsRecord.up_time)\
.filter(and_(InsAlarmList.alarm_id == InsRecord.alarm_id))\
.filter(InsRecord.task == alarmType)\
.limit(limitSize).offset((pageIndex-1)*limitSize)
totalPage = db.session.query(InsAlarmList,InsRecord)\
.filter(and_(InsAlarmList.alarm_id == InsRecord.alarm_id))\
.filter(InsRecord.task == alarmType).count()
else:
if(alarmType == ""):
results = db.session.query(InsAlarmList,InsRecord)\
.filter(InsRecord.up_time>=beginTs,InsRecord.up_time<endTs)\
.filter(and_(InsAlarmList.alarm_id == InsRecord.alarm_id))\
.order_by(InsAlarmList.status,InsRecord.up_time).limit(limitSize).\
offset((pageIndex-1)*limitSize)
totalPage = db.session.query(InsAlarmList,InsRecord)\
.filter(and_(InsAlarmList.alarm_id == InsRecord.alarm_id))\
.filter(InsRecord.up_time>=beginTs,InsRecord.up_time<endTs).count()
else:
results = db.session.query(InsAlarmList,InsRecord)\
.filter(InsRecord.up_time>=beginTs,InsRecord.up_time<endTs)\
.filter(and_(InsAlarmList.alarm_id == InsRecord.alarm_id))\
.filter(InsRecord.task == alarmType)\
.order_by(InsAlarmList.status,InsRecord.up_time).limit(limitSize).\
offset((pageIndex-1)*limitSize)
totalPage = db.session.query(InsAlarmList,InsRecord)\
.filter(and_(InsAlarmList.alarm_id == InsRecord.alarm_id))\
.filter(InsRecord.task == alarmType)\
.filter(InsRecord.up_time>=beginTs,InsRecord.up_time<endTs).count()
ret = dict()
ret["totalCount"] = totalPage
v = []
for r in results:
v.append( dict((r[0].to_json()),**(r[1].to_json())))
ret["data"] = v
app.logger.info(ret)
return ret

Comment ( 0 )

Sign in for post a comment