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
record.py 2.68 KB
Copy Edit Web IDE Raw Blame History
Mendalas authored 2019-06-15 17:34 . modyfy sql table and 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
import json
from sqlalchemy import and_,or_
import time
import collections
db = SQLAlchemy(app)
class InsRecord(db.Model):
__tablename__ = 'ins_record'
deviceId = db.Column(db.String(30), primary_key=True)
up_time = db.Column(db.DateTime, primary_key=True)
task = db.Column(db.String(255))
result = db.Column(db.String(255))
position = db.Column(db.String(255))
belt_id = db.Column(db.Integer)
alarm_id = db.Column(db.Integer)
def to_json(self):
return {
"deviceId":self.deviceId,
"time":str(self.up_time),
"task":self.task,
"result":self.result,
"position":self.position,
"belt_id":self.belt_id,
"alarmId":self.alarm_id
}
class getRecordByTime(Resource):
def post(self):
lastQueryTime = request.form['lastQueryTime']
flag = int(request.form['flag'])
if(flag == 0):
result = db.session.query(InsRecord).filter(and_(InsRecord.up_time>lastQueryTime,InsRecord.deviceId<2000)).order_by(InsRecord.up_time.desc()).all()
else:
result = db.session.query(InsRecord).filter(InsRecord.deviceId<2000).order_by(InsRecord.up_time.desc()).limit(10)
v = []
for r in result:
v.append(r.to_json())
app.logger.info(v)
return v
class getRecordByB2E(Resource):
def post(self):
beginTs = str(request.form["beginTs"])
endTs = str(request.form["endTs"])
result = db.session.query(InsRecord).filter(and_(InsRecord.up_time>=beginTs,InsRecord.up_time<endTs)).order_by(InsRecord.up_time.desc()).all()
v = []
for r in result:
v.append(r.to_json())
app.logger.info(v)
return v
class BeltMonitorApi(Resource):
def post(self):
montype = request.form['montype']
pageSize = int(request.form["pageSize"])
pageIndex = int(request.form["pageIndex"])
result = db.session.query(InsRecord).filter(and_(montype==InsRecord.task,InsRecord.deviceId>=2000)).\
order_by(InsRecord.up_time).limit(pageSize).offset((pageIndex-1)*pageSize)
msg = {}
v = []
size = 0
for r in result:
v.append(r.to_json())
size+=1
msg["data"] = v
if(size == 0):
msg["code"] = -1
msg["message"] = "已经是最后一页了"
else:
msg["code"] = 1
msg["message"] = "ok"
app.logger.info(msg)
return msg

Comment ( 0 )

Sign in for post a comment