def status_list_page(page=0): if page <= 0: abort(404) total = Status.select().count() total_page = int(math.ceil(total * 1.0 / config.ITEMS_PER_PAGE)) status_list = Status.select().order_by(Status.t.desc()).paginate( page, config.ITEMS_PER_PAGE) return render_template("status_list.html", page=page, total_page=total_page, status_list=status_list)
def show_histogram(): device_type = request.args.get("device_type", "") if device_type == "": return "{}" # 按照设备类型查询该类型下所有的设备,保留设备id和设备名称 device_metas = [{ "id": d.id, "name": d.name } for d in Device.select().where(Device.type == device_type)] # 构建response response = [] for d_meta in device_metas: if device_type == "temperature": week_values = [random.randrange(190, 325, 1) / 10.0 for i in range(7)] else: week_values = [random.randrange(200, 900, 1) / 10.0 for i in range(7)] # 获取最新的设备状态,对齐设备名称与最新值 latest_status = Status.select().where(Status.device_id == d_meta["id"]).order_by(Status.time.desc()) if len(latest_status) == 0: continue response.append({ "id": d_meta["id"], "name": d_meta["name"], "value": latest_status[0].value, "week_val": week_values }) return json.dumps(response, ensure_ascii=False)
def quem(update, context): logger.info("Command /quem received.") notify_msg = [] last_status = Status.select().order_by(Status.date.desc()).first() no_connected = len(last_status.who or []) if no_connected == 0: notify_msg.append("Não tem nenhuma pessoa conhecida lá...") else: space_emoji = random.choice( ["\U0001F30C", "\U0001F6F0", "\U0001F680", "\U0001F6F8"]) notify_msg.append( f"Pessoas conhecidas no espaço {space_emoji}: {last_status.who}.") if last_status.n_unknown_macs is not None and last_status.n_unknown_macs > 0: if last_status.n_unknown_macs == 1: unknown_text = random.choice([ "Mais um gato \U0001F408, provavelmente.", "Mais uma pessoa desconhecida.", "Mais um pingüim \U0001F427, provavelmente.", "Mais um bando de maritacas \U0001F99C, provavelmente.", ]) else: unknown_text = "Mais {no_unknown_macs} pessoas desconhecidas." notify_msg.append(unknown_text) context.bot.send_message(update.message.chat_id, text=" ".join(notify_msg))
def show_line_chart(device_type): # 按照设备类型查询该类型下所有的设备,保留设备id和设备名称 device_metas = [{ "id": d.id, "name": d.name } for d in Device.select().where(Device.type == device_type)] # 构建response timestamps = [s.time for s in Status.select().order_by(Status.time)] # 横轴坐标:时间戳 response = {"time": timestamps} # 对应时间戳的纵轴值 for d_meta in device_metas: response[d_meta["name"]] = [ s.value for s in Status.select().where(Status.device_id == d_meta["id"]).order_by(Status.time) ] return json.dumps(response)
def status_list_page(uid, page=1): if page <= 0: abort(404) total_page = int(math.ceil(g.user['status'] * 1.0 / config.ITEMS_PER_PAGE)) status_list = list(Status.select().where(Status.uid == uid).order_by( Status.t.desc()).paginate(page, config.ITEMS_PER_PAGE).dicts()) return render_template("status_list.html", page=page, total_page=total_page, status_list=status_list)
def save_status(): req_data = json.loads(request.data) if len(Status.select().where(Status.time == req_data["data"]["time"])) == 0: status = Status.create( device_id=req_data["deviceId"], value=req_data["data"]["value"], time=req_data["data"]["time"] ) status.save() return "{}"
def status(update, context): logger.info("Command /status received.") last_status = Status.select().order_by(Status.date.desc()).first() if last_status is None: context.bot.send_message( update.message.chat_id, text="O LHC pode estar aberto \U0001F513 ou fechado \U0001F512. Eu não consegui descobrir.", ) else: status = "aberto \U0001F513" if last_status.is_open else "fechado \U0001F512" msg = f"O LHC está {status} desde {last_status.last_change}." context.bot.send_message(update.message.chat_id, text=msg)
def status_list_page(uid, page=1): if page <= 0: abort(404) total_page = int(math.ceil(g.user['status'] * 1.0 / config.ITEMS_PER_PAGE)) status_list = list(Status.select().where(Status.uid == uid).order_by( Status.t.desc()).paginate(page, config.ITEMS_PER_PAGE).dicts()) for status in status_list: extra = entry_comments_api(entry_id=status['id']) status.update(**extra) return render_template("status_list.html", page=page, total_page=total_page, status_list=status_list)
def status_check(context): logger.info("Checking status of LHC.") last_status = Status.select().order_by(Status.date.desc()).first() response = requests.get("https://lhc.net.br/spacenet.json") spacenet = response.json() state = spacenet.get("state", {}) is_open = state.get("open") last_change_timestamp = state.get("lastchange") last_change = datetime.fromtimestamp(last_change_timestamp) current_status = Status( is_open=is_open, last_change=last_change, date=datetime.now(), ) status = "OPEN" if is_open else "CLOSED" logger.info(f"LHC is {status} since {current_status.last_change}.") status_changed = (last_status is None or current_status.is_open != last_status.is_open) if status_changed: response = requests.get("https://lhc.net.br/spacenet.json?whois") whois = response.json() n_unknown_macs = whois.get("n_unknown_macs", 0) current_status.n_unknown_macs = n_unknown_macs if is_open: who = whois.get("who", []) current_status.who = ", ".join(who) notify_msg = f"O LHC foi aberto \U0001F513 por {current_status.who} às {current_status.last_change}." else: notify_msg = ( f"O LHC está fechado \U0001F512 desde {current_status.last_change}." ) logger.info( f"LHC status changed. Sending notification to LHC channel.") # context.bot.send_message(chat_id="@lhc_campinas", text=notify_msg) current_status.save()
def update_fetch_info(uid): from models import database, FetchedUser, User, Status, Gossip, Album, Photo, Blog with database: user = User.get_or_none(User.uid == uid) if not user: raise KeyError("no such user") fetched_info = model_to_dict(user) fetched_info.update( status=Status.select().where(Status.uid == uid).count(), gossip=Gossip.select().where(Gossip.uid == uid).count(), album=Album.select().where(Album.uid == uid).count(), photo=Photo.select().where(Photo.uid == uid).count(), blog=Blog.select().where(Blog.uid == uid).count(), ) FetchedUser.insert(**fetched_info).on_conflict('replace').execute() print('update fetched info {fetched_info}'.format( fetched_info=fetched_info)) return True
def get(self): statuses = list(Status.select().dicts()) self.set_response(dict(statuses=statuses))