def save_regist_user(form): session = ORM.db() flag = True try: user = User( name=form.data['name'], pwd=generate_password_hash(form.data['pwd']), email=form.data['email'], phone=form.data['phone'], sex=None, constellation=None, face=None, info=None, createAt=get_datetime(), updateAt=get_datetime(), ) session.add(user) except Exception as e: session.rollback() flag = False else: session.commit() finally: session.close() return flag
def on_message(self, message): session = ORM.db() try: data = dict() if message == "system": kw = session.query(Kongqiwendu).order_by( Kongqiwendu.create_dt.desc()).first() ks = session.query(Kongqishidu).order_by( Kongqishidu.create_dt.desc()).first() tw = session.query(Turangwendu).order_by( Turangwendu.create_dt.desc()).first() ts = session.query(Turangshidu).order_by( Turangshidu.create_dt.desc()).first() gq = session.query(Guangzhao).order_by( Guangzhao.create_dt.desc()).first() data = dict( kongqiwendu=str(kw.percent), nt=str(kw.create_dt), kongqishidu=str(ks.percent), turangwendu=str(tw.percent), turangshidu=str(ts.percent), guangzhao=str(gq.percent), ) #把消息推送到虽有连接客户端 self.broadcast(self.waiters, json.dumps(data)) except Exception as e: print(e)
def data_mm(self, method="Day", format="%Y%m%d%H"): session = ORM.db() attr_kongqiwendu, vals_kongqiwendu_max, vals_kongqiwendu_min, vals_kongqiwendu_avg = None, None, None, None attr_kongqishidu, vals_kongqishidu_max, vals_kongqishidu_min, vals_kongqishidu_avg = None, None, None, None attr_turangwendu, vals_turangwendu_max, vals_turangwendu_min, vals_turangwendu_avg = None, None, None, None attr_turangshidu, vals_turangshidu_max, vals_turangshidu_min, vals_turangshidu_avg = None, None, None, None attr_guangzhao, vals_guangzhao_max, vals_guangzhao_min, vals_guangzhao_avg = None, None, None, None try: #空气温度 kongqiwendu = self.three_query(Kongqiwendu, session, method, format) attr_kongqiwendu = [v[0] for v in kongqiwendu] vals_kongqiwendu_max = [float(v[1]) for v in kongqiwendu] vals_kongqiwendu_min = [float(v[2]) for v in kongqiwendu] vals_kongqiwendu_avg = [round(float(v[3]), 1) for v in kongqiwendu] # 空气湿度 kongqishidu = self.three_query(Kongqishidu, session, method, format) attr_kongqishidu = [v[0] for v in kongqishidu] vals_kongqishidu_max = [float(v[1]) for v in kongqishidu] vals_kongqishidu_min = [float(v[2]) for v in kongqishidu] vals_kongqishidu_avg = [round(float(v[3]), 1) for v in kongqishidu] # 土壤温度 turangwendu = self.three_query(Turangwendu, session, method, format) attr_turangwendu = [v[0] for v in turangwendu] vals_turangwendu_max = [float(v[1]) for v in turangwendu] vals_turangwendu_min = [float(v[2]) for v in turangwendu] vals_turangwendu_avg = [round(float(v[3]), 1) for v in turangwendu] # 土壤湿度 turangshidu = self.three_query(Turangshidu, session, method, format) attr_turangshidu = [v[0] for v in turangshidu] vals_turangshidu_max = [float(v[1]) for v in turangshidu] vals_turangshidu_min = [float(v[2]) for v in turangshidu] vals_turangshidu_avg = [round(float(v[3]), 1) for v in turangshidu] # 光照强度 guangzhao = self.three_query(Guangzhao, session, method, format) attr_guangzhao = [v[0] for v in guangzhao] vals_guangzhao_max = [float(v[1]) for v in guangzhao] vals_guangzhao_min = [float(v[2]) for v in guangzhao] vals_guangzhao_avg = [round(float(v[3]), 1) for v in guangzhao] except Exception as e: session.rollback() else: session.commit() finally: session.close() return attr_kongqiwendu,vals_kongqiwendu_max,vals_kongqiwendu_min,vals_kongqiwendu_avg, \ attr_kongqishidu, vals_kongqishidu_max, vals_kongqishidu_min, vals_kongqishidu_avg, \ attr_turangwendu, vals_turangwendu_max, vals_turangwendu_min, vals_turangwendu_avg, \ attr_turangshidu, vals_turangshidu_max, vals_turangshidu_min, vals_turangshidu_avg, \ attr_guangzhao, vals_guangzhao_max, vals_guangzhao_min, vals_guangzhao_avg
def post(self, *args, **kwargs): account = self.get_argument('username') passwd = self.get_argument('password') session = ORM.db() rows = session.query(User).filter(User.name==account) if rows: name = [v.name for v in rows] passw = [v.passwd for v in rows] if account==name[0] and passwd==passw[0]: self.write('ok') else: self.write("noeq")
def get_response(self): id = self.get_argument('id') nzt = self.get_argument('zt') session = ORM.db() kw = session.query(Facility).filter(Facility.id == id).first() if nzt == "打开": if kw.stat == "打开": kw.stat = "关闭" session.commit() session.close() else: pass ret = {'states': '成功', 'message': ''} self.write(json.dumps(ret))
def post(self, *args, **kwargs): account = self.get_argument('zhanghao') passwd = self.get_argument('passwd') session = ORM.db() rows = session.query(User).filter(User.name==account) if rows: name = [v.name for v in rows] passw = [v.passwd for v in rows] if account==name[0] and passwd==passw[0]: self.redirect('/') else: self.write("<h1>账号或密码错误</h1>") else: self.write("<h1>账号或密码错误</h1>")
def save_log(): """保存日志的函数""" m = Monitor() cpu_info, swap_info, mem_info = m.cpu(), m.swap(), m.mem() _date_time, _date, _time = dt() # 1.创建会话 session = ORM.db() try: # cpu cpu = Cpu( percent=cpu_info['percent_avg'], create_date=_date, create_time=_time, create_dt=_date_time, ) # 交换分区 swap = Swap( percent=swap_info['percent'], total=swap_info['total'], used=swap_info['used'], free=swap_info['free'], create_date=_date, create_time=_time, create_dt=_date_time, ) # 内存 mem = Mem( percent=mem_info['percent'], total=mem_info['total'], used=mem_info['used'], free=mem_info['free'], create_date=_date, create_time=_time, create_dt=_date_time, ) # 提交至数据库 session.add(cpu) session.add(swap) session.add(mem) except Exception as e: session.rollback() # 出错回滚 print('出错回滚:{}'.format(e)) else: session.commit() # 正常,提交 print('正常,提交') finally: session.close() # 关闭会话 print('关闭会话')
def data_by_hour(self): now_time, next_time = self.dt_range() attr_kongqiwendu, attr_kongqishidu, attr_turangwendu, attr_turangshidu, attr_guangzhao = None, None, None, None, None vals_kongqiwendu, vals_kongqishidu, vals_turangwendu, vals_turangshidu, vals_guangzhao = None, None, None, None, None session = ORM.db() try: #空气温度 kw = self.one_hour_query(Kongqiwendu, session, now_time, next_time) if kw: attr_kongqiwendu = [ v.create_time.strftime("%H:%M:%S") for v in kw ] vals_kongqiwendu = [float(v.percent) for v in kw] # 空气湿度 ks = self.one_hour_query(Kongqishidu, session, now_time, next_time) if ks: attr_kongqishidu = [ v.create_time.strftime("%H:%M:%S") for v in ks ] vals_kongqishidu = [float(v.percent) for v in ks] # 土壤温度 tw = self.one_hour_query(Turangwendu, session, now_time, next_time) if tw: attr_turangwendu = [ v.create_time.strftime("%H:%M:%S") for v in tw ] vals_turangwendu = [float(v.percent) for v in tw] # 土壤湿度 ts = self.one_hour_query(Turangshidu, session, now_time, next_time) if ts: attr_turangshidu = [ v.create_time.strftime("%H:%M:%S") for v in ts ] vals_turangshidu = [float(v.percent) for v in ts] #光照强度 gq = self.one_hour_query(Guangzhao, session, now_time, next_time) if gq: attr_guangzhao = [ v.create_time.strftime("%H:%M:%S") for v in gq ] vals_guangzhao = [float(v.percent) for v in gq] except Exception as e: session.rollback() else: session.commit() finally: session.close() return attr_kongqiwendu, attr_kongqishidu, attr_turangshidu, attr_turangwendu, attr_guangzhao, vals_guangzhao, vals_kongqiwendu, vals_turangshidu, vals_turangwendu, vals_kongqishidu
def save_log(): m = SysInfoMonitor() cpu_info, mem_info, swap_info = m.cpuinfo(), m.meminfo(), m.swapinfo() _date, _time, _date_time = dt() # 1.create session session = ORM.db() try: # CPU cpu = Cpu( percent=cpu_info["percent_avg"], create_date=_date, create_time=_time, create_dt=_date_time ) # memory mem = Mem( percent=mem_info['percent'], total=mem_info['total'], used=mem_info['used'], free=mem_info['free'], create_date=_date, create_time=_time, create_dt=_date_time ) # swap swap = Swap( percent=swap_info['percent'], total=swap_info['total'], used=swap_info['used'], free=swap_info['free'], create_date=_date, create_time=_time, create_dt=_date_time ) # commit to db session.add(cpu) session.add(mem) session.add(swap) except Exception as e: #rollback session.rollback() else: session.commit() finally: session.close()
def save_log(): #空气温度湿度 土壤温度湿度 光照强度 _date, _time, _date_time = dt() #1创建会话 session = ORM.db() try: # 空气温度 kongqiwendu = Kongqiwendu(percent=random.uniform(0, 35), create_date=_date, create_time=_time, create_dt=_date_time) # 空气湿度 kongqishidu = Kongqishidu(percent=random.uniform(0, 100), create_date=_date, create_time=_time, create_dt=_date_time) # 土壤温度 turangwendu = Turangwendu(percent=random.uniform(15, 35), create_date=_date, create_time=_time, create_dt=_date_time) # 土壤湿度 turangshidu = Turangshidu(percent=random.uniform(-20, 55), create_date=_date, create_time=_time, create_dt=_date_time) # 光照强度 guangzhao = Guangzhao(percent=random.uniform(0, 2000), create_date=_date, create_time=_time, create_dt=_date_time) # 提交至数据块 session.add(kongqiwendu) session.add(kongqishidu) session.add(turangwendu) session.add(turangshidu) session.add(guangzhao) except Exception as e: session.rollback() print(e) else: session.commit() finally: session.close()
def save_log(): m = Monitor() cpu_info, mem_info, swap_info = m.cpu(), m.mem(), m.swap() _date, _time, _date_time = dt() # 1.创建会话 session = ORM.db() try: # CPU cpu = Cpu( percent=cpu_info["percent_avg"], create_date=_date, create_time=_time, create_dt=_date_time ) # 内存 mem = Mem( percent=mem_info['percent'], total=mem_info['total'], used=mem_info['used'], free=mem_info['free'], create_date=_date, create_time=_time, create_dt=_date_time ) # 交换分区 swap = Swap( percent=swap_info['percent'], total=swap_info['total'], used=swap_info['used'], free=swap_info['free'], create_date=_date, create_time=_time, create_dt=_date_time ) # 提交至数据块 session.add(cpu) session.add(mem) session.add(swap) except Exception as e: session.rollback() # 如果发送异常回滚 else: session.commit() # 没有异常提交 finally: session.close() # 无论是否发生异常关闭
def data_by_hour(self): """获取一小时内的数据""" now_time, last_time = self.date_range() attr_cpu, attr_swap, attr_mem = None, None, None # 属性 vals_cpu, vals_swap, vals_mem = None, None, None # 值 session = ORM.db() try: # cpu cpu = self.one_hour_query(model=Cpu, session=session, now_time=now_time, last_time=last_time) if cpu: attr_cpu = [v.create_time.strftime("%H:%M:%S") for v in cpu] vals_cpu = [float(v.percent) for v in cpu] # 交换区 swap = self.one_hour_query(model=Swap, session=session, now_time=now_time, last_time=last_time) if swap: attr_swap = [v.create_time.strftime("%H:%M:%S") for v in swap] vals_swap = [float(v.percent) for v in swap] # 内存 mem = self.one_hour_query(model=Mem, session=session, now_time=now_time, last_time=last_time) if mem: attr_mem = [v.create_time.strftime("%H:%M:%S") for v in mem] vals_mem = [float(v.percent) for v in mem] # decimal不能转化为json,需要转换为float except Exception as e: session.rollback() else: session.commit() finally: session.close() return attr_cpu, attr_swap, attr_mem, vals_cpu, vals_swap, vals_mem
def user_unique(data, flag=1): session = ORM.db() user = None try: query = session.query(User) if 1 == flag: # flag=1 检查用户名是否存在 user = query.filter_by(name=data).first() if 2 == flag: # flag=2 检查邮箱是否存在 user = query.filter_by(email=data).first() if 3 == flag: # flag=3 检查手机是否存在 user = query.filter_by(phone=data).first() except Exception as e: session.rollback() else: session.commit() finally: session.close() if user: return True else: return False
def data_by_three(self, method='day', format="%Y-%m-%d %H"): """按照小时和天查询今天和本月最大最小值""" session = ORM.db() attr_mem, vals_mem_min, vals_mem_max, vals_mem_avg = None, None, None, None attr_swap, vals_swap_min, vals_swap_max, vals_swap_avg = None, None, None, None attr_cpu, vals_cpu_min, vals_cpu_max, vals_cpu_avg = None, None, None, None try: # 内存 mem = self.three_query(Mem, session, method, format) if mem: attr_mem = [v[0] for v in mem] vals_mem_min = [float(v[1]) for v in mem] vals_mem_max = [float(v[2]) for v in mem] vals_mem_avg = [round(float(v[3]), 1) for v in mem] # 交换分区 swap = self.three_query(Swap, session, method, format) if swap: attr_swap = [v[0] for v in swap] vals_swap_min = [float(v[1]) for v in swap] vals_swap_max = [float(v[2]) for v in swap] vals_swap_avg = [round(float(v[3]), 1) for v in swap] # CPU cpu = self.three_query(Cpu, session, method, format) if cpu: attr_cpu = [v[0] for v in cpu] vals_cpu_min = [float(v[1]) for v in cpu] vals_cpu_max = [float(v[2]) for v in cpu] vals_cpu_avg = [round(float(v[3]), 1) for v in cpu] except Exception as e: print('获取失败:{}'.format(e)) session.rollback() else: session.commit() finally: session.close() return attr_mem, vals_mem_min, vals_mem_max, vals_mem_avg, \ attr_swap, vals_swap_min, vals_swap_max, vals_swap_avg, \ attr_cpu, vals_cpu_min, vals_cpu_max, vals_cpu_avg
def data_by_hour(self): now_time, next_time = self.dt_range() attr_cpu, attr_mem, attr_swap = None, None, None # 属性 vals_cpu, vals_mem, vals_swap = None, None, None # 值 session = ORM.db() # 创建会话 try: # 执行代码块 # 内存 mem = self.one_hour_query(Mem, session, now_time, next_time) if mem: attr_mem = [v.create_time.strftime("%H:%M:%S") for v in mem] vals_mem = [float(v.percent) for v in mem] # 交换分区 swap = self.one_hour_query(Swap, session, now_time, next_time) if swap: attr_swap = [v.create_time.strftime("%H:%M:%S") for v in swap] vals_swap = [float(v.percent) for v in swap] # CPU cpu = self.one_hour_query(Cpu, session, now_time, next_time) if cpu: attr_cpu = [v.create_time.strftime("%H:%M:%S") for v in cpu] vals_cpu = [float(v.percent) for v in cpu] except Exception as e: # 发生异常 session.rollback() # 回滚 else: # 没有发生异常 session.commit() # 提交会话 finally: # 无论是否发生异常都要执行 session.close() # 关闭会话 return attr_cpu, attr_mem, attr_swap, vals_cpu, vals_mem, vals_swap
def get_response(self): session = ORM.db() data = session.query(Facility).filter(Facility.kind == '光照').all() self.html("faci_gunag.html", data1=dict(faci=data))
def get_response(self): session = ORM.db() data = session.query(Facility).all() auto = session.query(AutoFaci).one() self.html("faci.html", data1=dict(faci=data, auto=auto))
from app.models.models import Kongqishidu, Kongqiwendu, Turangshidu, Turangwendu, Guangzhao from app.tools.orm import ORM session = ORM.db() kw = session.query(Kongqiwendu).order_by(Kongqiwendu.create_dt.desc()).first() print(kw.percent)