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
statistic.py 5.05 KB
Copy Edit Web IDE Raw Blame History
Mendalas authored 2019-06-12 18:07 . new create
# coding:utf-8
from flask_restful import Resource
from flask import Flask, jsonify
from flask_sqlalchemy import SQLAlchemy
from api import app
import json
from robot import RobotInfo
db = SQLAlchemy(app)
class RobotStatistics(db.Model):
__tablename__ = 'robot_monitor'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
robotid = db.Column(db.Integer)
robot_wifi = db.Column(db.Float)
system_load = db.Column(db.Float)
updatetime = db.Column(db.DateTime)
cpu_temp = db.Column(db.Float)
gpu_temp = db.Column(db.Float)
def to_json(self):
return {
"id": self.id,
"robotid": self.robotid,
"robot_wifi": self.robot_wifi,
"system_load": self.system_load,
"cpu_temp": self.cpu_temp,
"gpu_temp": self.gpu_temp,
"updatetime": str(self.updatetime)
}
class BatteryStatistics(db.Model):
__tablename__ = 'robot_battery_statistics'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
robotid = db.Column(db.Integer)
power_percentage = db.Column(db.Float)
current_voltage = db.Column(db.Float)
charging_current = db.Column(db.Float)
discharging_current = db.Column(db.Float)
temperature = db.Column(db.Float)
updatetime = db.Column(db.DateTime)
def to_json(self):
return {
"id": self.id,
"robotid": self.robotid,
"power_percentage": self.power_percentage,
"current_voltage": self.current_voltage,
"charging_current": self.charging_current,
"discharging_current": self.discharging_current,
"temperature": self.temperature,
"updatetime": str(self.updatetime),
}
class MotorStatistics(db.Model):
__tablename__ = 'robot_motor_statistics'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
robotid = db.Column(db.Integer)
rmp = db.Column(db.Integer)
motor_voltage = db.Column(db.Float)
motor_current = db.Column(db.Float)
motor_temperature = db.Column(db.Float)
updatetime = db.Column(db.DateTime)
def to_json(self):
return {
"id": self.id,
"robotid": self.robotid,
"rmp": self.rmp,
"motor_voltage": self.motor_voltage/100.0,
"motor_current": self.motor_current/100.0,
"motor_temperature": self.motor_temperature,
"updatetime": str(self.updatetime),
}
class RealtimeStat(Resource):
def get(self, robotId):
battery_result = db.session.query(BatteryStatistics).filter(
BatteryStatistics.robotid == robotId).order_by(
BatteryStatistics.updatetime.desc()).first()
motor_result = db.session.query(MotorStatistics).filter(
MotorStatistics.robotid == robotId).order_by(
MotorStatistics.updatetime.desc()).first()
robotInfo = db.session.query(RobotInfo).filter(
RobotInfo.robid == robotId).first()
robotStatistic_result = db.session.query(RobotStatistics).filter(
RobotStatistics.robotid == robotId).order_by(
RobotStatistics.updatetime.desc()).first()
ret = dict()
print motor_result
ret["motor"] = motor_result.to_json()
ret["battery"] = battery_result.to_json()
ret["robot"] = {
"sum_route_times": robotInfo.sum_route_times,
"sum_route_distances": robotInfo.sum_route_distances,
"robot_status": ((int(robotInfo.status)) & (1 << 4) >> 4),
"robot_wifi": robotStatistic_result.robot_wifi,
"system_load": robotStatistic_result.system_load,
"cpu_temp": robotStatistic_result.cpu_temp,
"gpu_temp": robotStatistic_result.gpu_temp,
}
return jsonify(ret)
class BatteryHistoryStat(Resource):
def get(self, robotId, page):
limitSize = 10
offset = limitSize * (int(page) - 1)
print "page,robotId", page, robotId, limitSize, offset
battery_result = db.session.query(BatteryStatistics).filter(
BatteryStatistics.robotid == robotId).order_by(
BatteryStatistics.updatetime.desc()).limit(limitSize).offset(
offset).all()
v = []
battery_result.reverse()
for r in battery_result:
v.append(r.to_json())
return jsonify(v)
#gpu cpu in robotStatistic and motor statistic in motostatistic should be inserted by the same speed
class MotorHistoryStat(Resource):
def get(self, robotId, page):
limitSize = 10
offset = limitSize * (int(page) - 1)
print "motorhistorystat.. page,robotId", page, robotId, limitSize, offset
motor_statstic_result = db.session.query(MotorStatistics).filter(
MotorStatistics.robotid == robotId).order_by(
MotorStatistics.updatetime.desc()).limit(limitSize).offset(
offset).all()
motor_statstic_result.reverse()
v = []
for r in motor_statstic_result:
v.append(r.to_json())
return jsonify(v)

Comment ( 0 )

Sign in for post a comment