Skip to content

Commit

Permalink
test status
Browse files Browse the repository at this point in the history
  • Loading branch information
ostapkob committed Sep 29, 2020
1 parent f3c4a58 commit 770db83
Show file tree
Hide file tree
Showing 8 changed files with 786 additions and 41 deletions.
4 changes: 4 additions & 0 deletions .project_alt.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"app/*.py": {"alternate": "tests/{}.py"},
"tests/*.py": {"alternate": "app/{}.py"}
}
128 changes: 88 additions & 40 deletions app/functions.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from datetime import datetime, timedelta
from flask import render_template, flash
from flask import flash, redirect, url_for
from app.model import Post, Mechanism, Work_1C_1
from app import db
from pprint import pprint
from random import choice

HOURS = 10 #your timezone

HOURS = 10 # your timezone


def today_shift_date():
Expand All @@ -25,20 +24,24 @@ def today_shift_date():

def all_mechanisms_id(type=None):
'''Find all mechanisms id'''
if type == None:
if type is None:
return [m.id for m in db.session.query(Mechanism).all()]
return [m.id for m in db.session.query(Mechanism).filter(Mechanism.type == type).all()]
return [m.id for m in db.session.query(Mechanism).filter(
Mechanism.type == type).all()]


def all_mechanisms_type():
'''Find all mechanisms type'''
ls = [m.type for m in db.session.query(Mechanism).all()]
return set(ls)


def all_number(type, number):
'''Need to do then'''
return [m.id for m in Mechanism.query.all()]

def multiple_5(date): #not use

def multiple_5(date): # not use
'''Return time multiple 5 minutes and remite microseconds'''
global HOURS
# date -= timedelta(minutes=5)
Expand All @@ -49,38 +52,46 @@ def multiple_5(date): #not use


def image_mechanism(value, type_mechanism, number, last_time):
dt = datetime.now()- last_time
dt =dt.total_seconds()/60
if type_mechanism=="usm":
dt = datetime.now() - last_time
dt = dt.total_seconds() / 60
if type_mechanism == "usm":
if dt > 120.0:
return './static/numbers/'+str(type_mechanism)+'/gray/'+str(number)+'.png'
return './static/numbers/'+str(
type_mechanism)+'/gray/'+str(number)+'.png'
if dt >= 3.0:
return './static/numbers/'+str(type_mechanism)+'/red/'+str(number)+'.png'
if value<0.1:
return './static/numbers/'+str(type_mechanism)+'/yellow/'+str(number)+'.png'
return './static/numbers/'+str(
type_mechanism)+'/red/'+str(number)+'.png'
if value < 0.1:
return './static/numbers/'+str(
type_mechanism)+'/yellow/'+str(number)+'.png'
else:
return './static/numbers/'+str(type_mechanism)+'/green/'+str(number)+'.png'
return './static/numbers/'+str(
type_mechanism)+'/green/'+str(number)+'.png'

if type_mechanism=="kran":
if type_mechanism == "kran":
if dt > 120.0:
return './static/numbers/'+str(type_mechanism)+'/gray/'+str(number)+'.png'
return './static/numbers/'+str(
type_mechanism)+'/gray/'+str(number)+'.png'
if dt >= 5.0:
return './static/numbers/'+str(type_mechanism)+'/red/'+str(number)+'.png'
if value==1:
return './static/numbers/'+str(type_mechanism)+'/black/'+str(number)+'.png'
if value==2:
return './static/numbers/'+str(type_mechanism)+'/blue/'+str(number)+'.png'
return './static/numbers/'+str(
type_mechanism)+'/red/'+str(number)+'.png'
if value == 1:
return './static/numbers/'+str(
type_mechanism)+'/black/'+str(number)+'.png'
if value == 2:
return './static/numbers/'+str(
type_mechanism)+'/blue/'+str(number)+'.png'
else:
return './static/numbers/'+str(type_mechanism)+'/yellow/'+str(number)+'.png'

return './static/numbers/'+str(
type_mechanism)+'/yellow/'+str(number)+'.png'


# not use
def time_for_shift_list(date_shift, shift): #not use
def time_for_shift_list(date_shift, shift): # not use
'''get dict with all minute's values for the period'''
# get data from db
cursor = db.session.query(Post).filter(
Post.date_shift == date_shift, Post.shift == shift).order_by(Post.mechanism_id).all()
Post.date_shift == date_shift,
Post.shift == shift).order_by(Post.mechanism_id).all()

# create dict all works mechanism in shift
data_per_shift = {}
Expand Down Expand Up @@ -122,65 +133,78 @@ def time_for_shift_list(date_shift, shift): #not use
break
return time_by_minuts


def handle_date(date):
day = month = year = None
spl_date = date.split('.')
if len(spl_date) >3:
if len(spl_date) > 3:
return redirect(url_for('index'))
try:
day = int(spl_date[0])
month = int(spl_date[1])
year = int(spl_date[2])
except IndexError:
print('ERR', day, month, year)
if not year: year=datetime.now().year
if not month: month=datetime.now().month
if not day: day=datetime.now().day
if not year:
year = datetime.now().year
if not month:
month = datetime.now().month
if not day:
day = datetime.now().day
try:
return datetime(year, month, day).date()
except:
except ValueError:
flash('Enter correct shift')
return datetime.now().date()


def data_from_1c(date_shift, shift):
time_from = datetime.combine(date_shift, datetime.min.time())
if shift==1:
if shift == 1:
time_from += timedelta(hours=8)
else:
time_from += timedelta(hours=20)
time_to = time_from + timedelta(hours=12)
cursor = db.session.query(Work_1C_1).filter(Work_1C_1.data_nach>=time_from, Work_1C_1.data_nach<=time_to).all()
cursor = db.session.query(Work_1C_1).filter(
Work_1C_1.data_nach >= time_from,
Work_1C_1.data_nach <= time_to).all()
data_1C = [x.get() for x in cursor]
return data_1C


def data_from_1c_by_id(date_shift, shift, id_mech):
time_from = datetime.combine(date_shift, datetime.min.time())
if shift==1:
if shift == 1:
time_from += timedelta(hours=8)
else:
time_from += timedelta(hours=20)
time_to = time_from + timedelta(hours=12)
cursor = db.session.query(Work_1C_1).filter(Work_1C_1.data_nach>=time_from, Work_1C_1.data_nach<time_to, Work_1C_1.inv_num==id_mech).all()
cursor = db.session.query(Work_1C_1).filter(
Work_1C_1.data_nach >= time_from,
Work_1C_1.data_nach < time_to,
Work_1C_1.inv_num == id_mech).all()
data_1C = [x.get() for x in cursor]
return data_1C


def fio_to_fi(item):
fio = item[3].split()
if not fio:
return None
return f'{fio[0].capitalize()} {fio[1][0]}.'


def add_fio(data_kran_period, date_shift, shift):
''' add fio and grab if it exec'''
if not data_kran_period:
return None
for key, value in data_kran_period.items():
id_mech = data_kran_period[key]['id']
data_by_id_mech = data_from_1c_by_id(date_shift, shift, id_mech)
if len(data_by_id_mech)<1:
if len(data_by_id_mech) < 1:
data_kran_period[key]['fio'] = None
data_kran_period[key]['grab'] = None
elif len(data_by_id_mech)==1:
elif len(data_by_id_mech) == 1:
data_kran_period[key]['fio'] = fio_to_fi(data_by_id_mech[0])
if data_by_id_mech[0][2] == 0:
data_kran_period[key]['grab'] = None
Expand All @@ -191,7 +215,31 @@ def add_fio(data_kran_period, date_shift, shift):
data_kran_period[key]['fio'] = 'Two operators'
return data_kran_period


def get_state():
state = ['work', 'stay', 'no_power', 'long_not_work']
return choice(state)
return 'work'


def state_mech(args):
values = list(x.value == -1 for x in args.values())
result = all(values[1:])
print(values, result)
if result:
return 'no_power'

values = list(x.value <= .1 for x in args.values())
result = all(values[1:])
if result:
return 'stay'

return 'work'


def alarm_mech(args):
"""state = ['work', 'stay', 'no_power', 'long_not_work', 'bad_work']"""
# for x in args.values():
# print(x.value, x.timestamp)
values = list(x.value for x in args.values())
if sum(values) <= 1:
return False
return True
2 changes: 1 addition & 1 deletion app/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def __init__(self, mechanism_id, latitude=0, longitude=0, value=None, value2=Non
f.write(d)

def __repr__(self):
return f'{self.value}'
return f'{self.timestamp} {self.value} '

def add_post(self):
print(super().get_tables_for_bind())
Expand Down
Loading

0 comments on commit 770db83

Please sign in to comment.