def mac_address(context, user_id): if user_id is None: user_id = context['user']['id'] query_result = MacAddress\ .query \ .filter(MacAddress.user_id == user_id)\ .all() curr = get_MAC_address(request.environ['HTTP_X_FORWARDED_FOR']) if 'HTTP_X_FORWARDED_FOR' in request.environ else None if curr is None: curr = '' user = User.query.filter(User.id == user_id).first() data_model = { 'current': curr, 'addresses': [{ 'id': x.id, 'address': x.mac_address } for x in query_result], 'api-key': user.api_key} page_model = PageModel('Adresy MAC', context['user'])\ .add_breadcrumb_page('Adresy MAC', '')\ .to_dict() return render_template('users.html', data_model=data_model, page_model=page_model)
def meteo_now(context): page_model = PageModel('Stacja meteo - strona główna', context['user'])\ .add_breadcrumb_page('Stacja meteo', '/meteo/now')\ .to_dict() data_model = {'MIN_DATE': MIN_DATE.strftime('%Y-%m-%d')} return render_template('meteo/now.html', page_model=page_model, data_model=data_model)
def energy_now(context): page_model = PageModel('Energia - strona główna', context['user']) \ .add_breadcrumb_page('Energia', '/energy/now') \ .to_dict() data_model = {'MIN_DATE': MIN_DATE.strftime('%Y-%m-%d')} return render_template('energy/now.html', page_model=page_model, data_model=data_model)
def blinds_index(context): page_model = PageModel('Rolety', context['user'])\ .add_breadcrumb_page('Rolety', '/blinds')\ .to_dict() data_model = {'devices_names': names.devices} return render_template('blinds/index.html', data_model=data_model, page_model=page_model)
def status(context): page_model = PageModel('Status urządzeń', context['user'])\ .add_breadcrumb_page('Status urządzeń', '/admin/status')\ .to_dict() data_model = { 'flara_status': energy_data.test_flara_connection(), 'emeter_status': energy_data.test_emeter_connection(), 'meteo_status': meteo_data.test_meteo_connection() } return render_template('admin/status.html', data_model=data_model, page_model=page_model)
def streams(context): resp = requests.get('http://gostream:8002/streams').json() strs = [x['id'] for x in resp] data_model = { 'streams': strs } page_model = PageModel('Kamery', context['user'])\ .add_breadcrumb_page('Kamery', '/streams')\ .to_dict() return render_template('streams.html', data_model=data_model, page_model=page_model)
def stream(context, stream_id): resp = requests.get('http://gostream:8002/streams').json() url = "" for x in resp: if x['id'] == stream_id: url = x['url'] data_model = { 'url': "https://" + flask.request.host + url } page_model = PageModel('Kamery', context['user'])\ .add_breadcrumb_page('Kamery', '/streams')\ .to_dict() return render_template('stream.html', data_model=data_model, page_model=page_model)
def recording(context, recording_id, date, hour): resp = requests.get('http://gostream:8002/recordings/' + recording_id + '/' + date).json() url = "" for x in resp: if x['hour'] == hour: url = x['url'] data_model = { 'url': "https://" + flask.request.host + url } page_model = PageModel('Nagrania', context['user'])\ .add_breadcrumb_page('Nagrania', '/recording')\ .to_dict() return render_template('stream.html', data_model=data_model, page_model=page_model)
def energy_history_month(context, date, page): if date is None: date_temp = datetime.datetime.now().strftime('%Y-%m') tab_url = 'month' else: date_temp = date tab_url = date try: from_date = datetime.datetime.strptime(date_temp, '%Y-%m').date() except ValueError: return redirect(url_for('energy_history_month'), code=302) if MIN_DATE.year > from_date.year or ( MIN_DATE.year == from_date.year and MIN_DATE.month > from_date.month) or from_date > datetime.date.today(): return redirect(url_for('energy_history_month'), code=302) tabs = {'energy': 1, 'charts': 2, 'records': 3} if page not in tabs: return redirect(url_for('energy_history_month', date=date), code=302) page_model = PageModel('Energia - historia miesiąca ' + date_temp, context['user']) \ .add_breadcrumb_page('Energia', '/energy/now') \ .add_breadcrumb_page('Historia miesiąca', '') \ .add_tab('Energia', tab_url) \ .add_tab('Wykresy', tab_url + '_charts') \ .add_tab('Rekordy', tab_url + '_records') \ .activate_tab(tabs[page]) \ .to_dict() data_model = aggregate_energy_data(from_date, 'month') data_model.update({'active_tab': page}) return render_template('energy/history.html', data_model=data_model, page_model=page_model)
def home(context): page_model = PageModel('Strona Główna', context['user'])\ .to_dict() _, energy_total_daily, _, _ = energy.aggregate_energy_daily_data( datetime.datetime.combine(datetime.date.today(), datetime.time())) aggregated = energy.aggregate_energy_data(energy.MIN_DATE, 'all') correction = EnergyCorrection.get_total_correction()[0] / 1000.0 data_model = { 'energy': { 'daily_prod': energy_total_daily['production'], 'daily_cons': energy_total_daily['consumption'], 'total_balance': aggregated['energy_total']['store'] + correction } } return render_template('home.html', page_model=page_model, data_model=data_model)
def energy_history_all(context, page): tabs = {'energy': 1, 'charts': 2, 'records': 3} if page not in tabs: return redirect(url_for('energy_history_all'), code=302) page_model = PageModel('Energia - cała historia ', context['user']) \ .add_breadcrumb_page('Energia', '/energy/now') \ .add_breadcrumb_page('Cała historia', '') \ .add_tab('Energia', 'all') \ .add_tab('Wykresy', 'all' + '_charts') \ .add_tab('Rekordy', 'all' + '_records') \ .activate_tab(tabs[page]) \ .to_dict() MONTHLY = True data_model = aggregate_energy_data(MIN_DATE, 'all_monthly' if MONTHLY else 'all') data_model.update({'active_tab': page}) return render_template('energy/history.html', data_model=data_model, page_model=page_model)
def register_page(context): page_model = PageModel('Tworzenie konta', context['user'])\ .add_breadcrumb_page('Panel Administracyjny', '/admin')\ .add_breadcrumb_page('Rejestracja', '/auth/register')\ .to_dict() return render_template('auth/register.html', page_model=page_model)
def locks(context): page_model = PageModel('Zamki', context['user'])\ .add_breadcrumb_page('Zamki', '/locks')\ .to_dict() return render_template('locks.html', page_model=page_model)
def energy_history_day(context, date, page): if date is None: date_temp = datetime.datetime.now().strftime('%Y-%m-%d') tab_url = "day" else: date_temp = date tab_url = date try: start_time = datetime.datetime.strptime(date_temp, '%Y-%m-%d') except ValueError: return redirect(url_for('energy_history_day'), code=302) if MIN_DATE > start_time.date() or start_time.date( ) > datetime.date.today(): return redirect(url_for('energy_history_day'), code=302) tabs = {'energy': 1, 'charts': 2} if page not in tabs: return redirect(url_for('energy_history_day', date=date), code=302) grouped_data, energy_total, energy_hours, chart_power = aggregate_energy_daily_data( start_time) power_chart_data = ChartData() \ .set_labels([x.strftime('%H:%M') for x in grouped_data['time_points'][0]]) \ .add_dataset('Produkcja', chart_power['production'], [25, 180, 25, 1], [50, 200, 50, 0.2]) \ .add_dataset('Zużycie', chart_power['consumption'], [210, 15, 15, 1], [230, 30, 30, 0.2]) \ .add_dataset('Wykorzystanie', chart_power['use'], [5, 5, 231, 1], [25, 25, 250, 0.2], True) \ .add_dataset('Pobieranie', chart_power['import'], [100, 100, 5, 1], [230, 230, 30, 0.2], True) \ .add_dataset('Oddawanie', chart_power['export'], [30, 190, 190, 1], [50, 210, 210, 0.2], True) \ .add_dataset('Magazynowanie', chart_power['store'], [140, 30, 100, 1], [180, 60, 130, 0.2], True) \ .to_json() energy_chart_data = ChartData() \ .set_labels([x.strftime('%H:%M') for x in grouped_data['time_points'][1]]) \ .add_dataset('Produkcja', energy_hours['production'], [25, 180, 25, 1], [50, 200, 50, 0.2]) \ .add_dataset('Zużycie', energy_hours['consumption'], [210, 15, 15, 1], [230, 30, 30, 0.2]) \ .add_dataset('Wykorzystanie',energy_hours['use'], [5, 5, 231, 1], [25, 25, 250, 0.2], True) \ .add_dataset('Pobieranie', energy_hours['import'], [100, 100, 5, 1], [230, 230, 30, 0.2], True) \ .add_dataset('Oddawanie', energy_hours['export'], [30, 190, 190, 1], [50, 210, 210, 0.2], True) \ .add_dataset('Magazynowanie', energy_hours['store'], [140, 30, 100, 1], [180, 60, 130, 0.2], True) \ .to_json() data_model = { 'energy_total': energy_total, 'energy_hours': energy_hours, 'power_chart_data': power_chart_data, 'energy_chart_data': energy_chart_data, 'is_day_history': True, 'active_tab': page } page_model = PageModel('Energia - historia dnia ' + date_temp, context['user']) \ .add_breadcrumb_page('Energia', '/energy/now') \ .add_breadcrumb_page('Historia dnia', '/energy/day') \ .add_tab('Energia', tab_url) \ .add_tab('Wykresy', tab_url + '_charts') \ .activate_tab(tabs[page]) \ .to_dict() return render_template('energy/history.html', data_model=data_model, page_model=page_model)
def meteo_history_day(context, date): try: date_sanitized = date if date is not None else datetime.datetime.now( ).strftime('%Y-%m-%d') start_time = datetime.datetime.strptime(date_sanitized, '%Y-%m-%d') except ValueError: return redirect(url_for('meteo_history_day'), code=302) if MIN_DATE > start_time.date() or start_time.date( ) > datetime.date.today(): return redirect(url_for('meteo_history_day'), code=302) grouped_data = group_data(start_time, Meteo, [5]) meteo_data = { 'temperature': [], 'humidity': [], 'pressure': [], 'dust': [] } if grouped_data['groups'] is not None: for x in grouped_data['groups'][0]: meteo_data['temperature'].append( round_in(average_in(map(lambda x: x.temperature / 100, x)), 1)) meteo_data['humidity'].append( round_in(average_in(map(lambda x: x.humidity / 100, x)), 1)) meteo_data['pressure'].append( round_in(average_in(map(lambda x: x.pressure / 100, x)), 1)) meteo_data['dust'].append( round_in(average_in(map(lambda x: x.dust_PM25, x)), 1)) temperature_chart_data = ChartData() \ .set_labels([x.strftime('%H:%M') for x in grouped_data['time_points'][0]]) \ .add_dataset('Temperatura', meteo_data['temperature'], [20, 20, 20, 1], [20, 20, 20, 0.0], False, 2) \ .to_json() humidity_chart_data = ChartData() \ .set_labels([x.strftime('%H:%M') for x in grouped_data['time_points'][0]]) \ .add_dataset('Wilgotność', meteo_data['humidity'], [20, 20, 20, 1], [20, 20, 20, 0.0], False, 2) \ .to_json() pressure_chart_data = ChartData() \ .set_labels([x.strftime('%H:%M') for x in grouped_data['time_points'][0]]) \ .add_dataset('Ciśnienie atmosferyczne', meteo_data['pressure'], [20, 20, 20, 1], [20, 20, 20, 0.0], False, 2) \ .to_json() dust_chart_data = ChartData() \ .set_labels([x.strftime('%H:%M') for x in grouped_data['time_points'][0]]) \ .add_dataset('Pył PM2.5', meteo_data['dust'], [20, 20, 20, 1], [20, 20, 20, 0.0], False, 2) \ .to_json() average_values = { "temperature": average_in(meteo_data['temperature']), 'humidity': average_in(meteo_data['humidity']), 'pressure': average_in(meteo_data['pressure']), 'dust': average_in(meteo_data['dust']) } data_model = { 'temperature_chart_data': temperature_chart_data, 'humidity_chart_data': humidity_chart_data, 'pressure_chart_data': pressure_chart_data, 'dust_chart_data': dust_chart_data, 'average_values': average_values } page_model = PageModel('Meteo - historia dnia ' + date_sanitized, context['user']) \ .add_breadcrumb_page('Meteo', '/meteo/now') \ .add_breadcrumb_page('Historia dnia', '/meteo/day') \ .to_dict() return render_template('meteo/history.html', data_model=data_model, page_model=page_model)
def admin(context): page_model = PageModel('Panel Administracyjny', context['user'])\ .add_breadcrumb_page('Panel Administracyjny', '/admin')\ .to_dict() return render_template('admin/admin.html', page_model=page_model)
def blinds_manage(context, blinds_id): blinds_id_list = list( map( lambda x: int(x), list( filter(lambda x: x.isdigit() and names.devices.get(int(x)), blinds_id.split(','))))) if len(blinds_id_list) == 0: return web_response.redirect_to('blinds_index') task_query_result = BlindsTask\ .query \ .filter(BlindsTask.device.in_(blinds_id_list))\ .join(User)\ .add_column(User.name)\ .all() scheduled_task_query_result = BlindsTask\ .query \ .filter(BlindsTask.device.in_(blinds_id_list))\ .filter(BlindsTask.user_id == None)\ .order_by(BlindsTask.time)\ .all() tasks = [] for x in task_query_result: tasks.append({ 'device': x.BlindsTask.device, 'action': x.BlindsTask.action, 'time': x.BlindsTask.time, 'user': x.name, 'task_id': x.BlindsTask.id }) SHOW_SCHEDULED_TASKS = True if SHOW_SCHEDULED_TASKS: for x in scheduled_task_query_result: tasks.append({ 'device': x.device, 'action': x.action, 'time': x.time, 'user': '******', 'task_id': x.id }) tasks = sorted(tasks, key=lambda k: k['time']) for x in tasks: x['time'] = unixtime_to_strftime(x['time'], '%d-%m-%Y %H:%M') schedule_query_result = BlindsSchedule\ .query\ .filter(BlindsSchedule.device.in_(blinds_id_list)) \ .join(User) \ .add_column(User.name) \ .order_by(BlindsSchedule.id) \ .all() schedule = [{ 'id': x.BlindsSchedule.id, 'device': x.BlindsSchedule.device, 'action': x.BlindsSchedule.action, 'hour_type': x.BlindsSchedule.hour_type, 'time_offset_sign': ((' ', '+ ')[x.BlindsSchedule.time_offset > 0 and not x.BlindsSchedule.hour_type == 6], '- ')[x.BlindsSchedule.time_offset < 0], 'time_offset': str(timedelta(minutes=abs(x.BlindsSchedule.time_offset)))[:-3], 'user': x.name } for x in schedule_query_result] page_name = ('Zarządzaj wieloma roletami', 'Zarządzaj roletą "' + names.devices.get(blinds_id_list[0]) + '"')[len(blinds_id_list) == 1] page_model = PageModel(page_name, context['user'])\ .add_breadcrumb_page('Rolety', '/blinds')\ .add_breadcrumb_page('Zarządzanie roletami', '')\ .to_dict() data_model = { 'user_tasks': tasks, 'schedule': schedule, 'devices': blinds_id_list, 'names': names } return render_template('blinds/manage.html', data_model=data_model, page_model=page_model)