def get_all_last_data_ico(): '''get all data mechanism and mechanism state''' try: last_data_mech = [ db.session.query(Post).filter(Post.mechanism_id == x).order_by( Post.timestamp.desc()).first() for x in all_mechanisms_id() ] except Exception as e: logger.debug(e) last_data_mech = filter(lambda x: x is not None, last_data_mech) data = { el.mech.type + str(el.mech.number): { 'id': el.mech.id, 'name': el.mech.name, 'value': el.value, 'latitude': el.latitude, 'longitude': el.longitude, 'src': image_mechanism(el.value, el.mech.type, el.mech.number, el.timestamp + timedelta(hours=HOURS)), 'time': el.timestamp + timedelta(hours=HOURS) } for el in last_data_mech } return jsonify(data)
def add_usm(): '''add post by GET request from arduino''' mechanism_id = request.args.get('mechanism_id') password = request.args.get('password') value = request.args.get('value') value2 = request.args.get('value2') value3 = request.args.get('value3') count = request.args.get('count') latitude = request.args.get('latitude') longitude = request.args.get('longitude') try: number = dict_mechanisms_number_by_id['usm'][int(mechanism_id)] except KeyError: return 'Not this id or not usm' try: mech = Mechanism.query.get(mechanism_id) except Exception as e: logger.debug(e) # if mechanism_id == '33287' and value3 == '0': # FIX # value3 = '15' items = mechanism_id, password, latitude, longitude test_items = any([item is None for item in items]) if int(value3) < 5: # if roller not circle value = 0 if test_items: return 'Bad request' if password not in post_pass: return 'Bad password' if int(mechanism_id) not in all_mechanisms_id('usm'): return 'Not this id' if number in usm_no_move: latitude = 0 longitude = 0 if latitude == '': latitude = 0 longitude = 0 if float(latitude) == 0 or float(longitude) == 0: try: data_mech = db.session.query(Post).filter( Post.mechanism_id == mechanism_id).order_by( Post.timestamp.desc()).first() except Exception as e: logger.debug(e) latitude = data_mech.latitude longitude = data_mech.longitude terminal = which_terminal('usm', number, latitude, longitude) # exist 9, 11, 13, 15 new_post = Post(value=value, value2=value2, value3=value3, count=count, latitude=latitude, longitude=longitude, mechanism_id=mechanism_id, terminal=terminal) add_fix_post(new_post) return f'Success, {str(items)}, {str(datetime.now().strftime("%d.%m.%Y %H:%M:%S"))}'
def add_kran(): '''add post by GET request from arduino''' mechanism_id = request.args.get('mechanism_id') password = request.args.get('password') value = request.args.get('value') value3 = request.args.get('value3') latitude = request.args.get('latitude') longitude = request.args.get('longitude') try: mech = Mechanism.query.get(mechanism_id) except Exception as e: logger.debug(e) # if mechanism_id == '15510' and value == '1': # value = '2' items = mechanism_id, password, latitude, longitude, value, value3 test_items = any([item is None for item in items]) # if this id is exist if test_items: return 'Bad request' if password not in post_pass: return 'Bad password' if int(mechanism_id) not in all_mechanisms_id('kran'): return 'Not this id or not kran' if latitude == '': latitude = 0 longitude = 0 if float(latitude) == 0 or float(longitude) == 0: try: data_mech = db.session.query(Post).filter( Post.mechanism_id == mechanism_id).order_by( Post.timestamp.desc()).first() except Exception as e: logger.debug(e) latitude = data_mech.latitude longitude = data_mech.longitude if mech.number in krans_if_3_then_2 and value == '3': value = 2 if mech.number in krans_if_1_then_0 and value == '1': value = 4 k1, b1 = line_kran(mech.number) k2, b2 = perpendicular_line_equation(k1, float(latitude), float(longitude)) latitude, longitude = intersection_point_of_lines(k1, b1, k2, b2) terminal = which_terminal('kran', number, latitude, longitude) # exist 9, 11, 13, 15 new_post = Post(value=value, value3=value3, latitude=latitude, longitude=longitude, mechanism_id=mechanism_id, terminal=terminal) db.session.add(new_post) db.session.commit() return f'Success, {str(mech.number)}, {str(items)}, {str(datetime.now().strftime("%d.%m.%Y %H:%M:%S"))}'
def hash_all_last_data_state(): start = datetime.now() try: last_data_mech = [ db.session.query(Post).filter(Post.mechanism_id == x).order_by( Post.timestamp.desc()).first() for x in all_mechanisms_id() ] except Exception as e: logger.debug(e) last_data_mech = filter(lambda x: x is not None, last_data_mech) data = { el.mech.type + str(el.mech.number): { 'id': el.mech.id, 'name': el.mech.name, 'type': el.mech.type, 'number': el.mech.number, # if roller not work 'value': round(el.value, 2) if not el.value3 else 0, # !need function 'value2': el.value2, 'value3': el.value3, 'latitude': el.latitude, 'longitude': el.longitude, 'state': state_mech(el.mech.type, el.value, el.value2, el.value3, el.timestamp + timedelta(hours=HOURS)), 'alarm': get_status_alarm(el.mech.id, el.mech.type), 'terminal': el.terminal, 'time': el.timestamp + timedelta(hours=HOURS) } for el in last_data_mech } client = MongoClient('mongodb://localhost:27017') mongodb = client['HashShift'] posts = mongodb['hash'] if data is not None: data['_id'] = 'last_data' posts.delete_one({"_id": "last_data"}) posts.insert_one(data)
def get_all_last_data_state(): '''get all data mechanism and mechanism state''' start = datetime.now() try: last_data_mech = [ db.session.query(Post).filter(Post.mechanism_id == x).order_by( Post.timestamp.desc()).first() for x in all_mechanisms_id() ] except Exception as e: logger.debug(e) last_data_mech = filter(lambda x: x is not None, last_data_mech) data = { el.mech.type + str(el.mech.number): { 'id': el.mech.id, 'name': el.mech.name, 'type': el.mech.type, 'number': el.mech.number, # if roller not work 'value': round(el.value, 2) if not el.value3 else 0, 'value2': el.value2, 'value3': el.value3, 'latitude': el.latitude, 'longitude': el.longitude, 'state': state_mech(el.mech.type, el.value, el.value2, el.value3, el.timestamp + timedelta(hours=HOURS)), # 'alarm': get_status_alarm(el.mech.id, el.mech.type), # 'alarm': True, 'alarm': False, 'terminal': el.terminal, 'time': el.timestamp + timedelta(hours=HOURS) } for el in last_data_mech } print('time last:', datetime.now() - start) return jsonify(data)
def add_sennebogen(): '''add post by GET request from arduino''' type_mechanism = 'sennebogen' number = request.args.get('number') password = request.args.get('password') x = request.args.get('x') y = request.args.get('y') count = request.args.get('count') latitude = request.args.get('lat') longitude = request.args.get('lon') mechanism_id = id_by_number(type_mechanism, number) try: mech = Mechanism.query.get(mechanism_id) except Exception as e: logger.debug(e) items = mechanism_id, password, latitude, longitude, x, y test_items = any([item is None for item in items]) if test_items: return 'Bad request' if password not in post_pass: return 'Bad password' if int(mechanism_id) not in all_mechanisms_id(type_mechanism): return 'Not this id' if latitude == '': latitude = 0 longitude = 0 if float(latitude) == 0 or float(longitude) == 0: # get last find value try: data_mech = db.session.query(Post).filter( Post.mechanism_id == mechanism_id).order_by( Post.timestamp.desc()).first() except Exception as e: logger.debug(e) latitude = data_mech.latitude longitude = data_mech.longitude terminal = which_terminal('sennebogen', number, latitude, longitude) # exist 9, 11, 13, 15 new_post = Post(value=x, value2=y, count=count, latitude=latitude, longitude=longitude, mechanism_id=mechanism_id, terminal=terminal) add_fix_post(new_post) return f'Success, {str(items)}, {str(datetime.now().strftime("%d.%m.%Y %H:%M:%S"))}'
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) # last_find_item = db.session.query(Work_1C_1).filter(Work_1C_1.inv_num==id_mech, Work_1C_1.greifer_vol>0 ).first() if len(data_by_id_mech) < 1: data_kran_period[key]['fio'] = None data_kran_period[key]['grab'] = None data_kran_period[key]['contract'] = None elif len(data_by_id_mech) == 1: data_kran_period[key]['fio'] = fio_to_fi(data_by_id_mech[0]) data_kran_period[key]['contract'] = data_by_id_mech[0][8] if data_by_id_mech[0][2]: data_kran_period[key]['grab'] = float(data_by_id_mech[0][2]) else: data_kran_period[key]['grab'] = None else: for operator in data_by_id_mech: data_kran_period[key]['fio'] = 'Два оператора' data_kran_period[key]['contract'] = 1 if data_by_id_mech[0][2]: # dublicate data_kran_period[key]['grab'] = float(data_by_id_mech[0][2]) else: data_kran_period[key]['grab'] = None # if grab not write then find last item if data_kran_period[key][ 'grab'] == None and id_mech in all_mechanisms_id('kran'): try: last_find_item = db.session.query(Work_1C_1).filter( Work_1C_1.inv_num == id_mech, Work_1C_1.greifer_vol > 0).order_by( Work_1C_1.data_nach.desc()).first() data_kran_period[key]['grab'] = float( last_find_item.greifer_vol) except AttributeError: data_kran_period[key]['grab'] = None return data_kran_period
def add_post(): '''add post by POST request from arduino''' need_keys = 'password', 'value', 'latitude', 'longitude', 'mechanism_id' request_j = request.json if request.method == 'POST': if not request_j: abort(400) keys = [p for p in request_j.keys()] if not set(keys).issubset(need_keys): abort(400) if request_j['password'] not in post_pass: abort(403) # need use this password in Arduino if request_j['mechanism_id'] not in all_mechanisms_id(): abort(405) value = request_j['value'] latitude = request_j['latitude'] longitude = request_j['longitude'] mechanism_id = request_j['mechanism_id'] if float(latitude) == 0 or float(longitude) == 0: try: data_mech = db.session.query(Post).filter( Post.mechanism_id == mechanism_id).order_by( Post.timestamp.desc()).first() except Exception as e: logger.debug(e) latitude = data_mech.latitude longitude = data_mech.longitude elif request.method == 'GET': return 'Need POST methods' else: abort(400) new_post = Post(value, latitude, longitude, mechanism_id) data = request.data db.session.add(new_post) db.session.commit() return data, 201
def time_for_shift_sennebogen(date_shift, shift): '''get dict with all minute's values for the period, name and total value is lever, value3 is speed roler, ''' # get data from db shift = int(shift) all_mechs = all_mechanisms_id('sennebogen') try: cursor = db.session.query(Post).filter( Post.date_shift == date_shift, Post.shift == shift, Post.mechanism_id.in_(all_mechs)).order_by( Post.mechanism_id).all() except Exception as e: logger.debug(e) # create dict all works mechanism in shift data_per_shift = {} for el in cursor: date_t = el.timestamp.replace(second=0, microsecond=0) date_t += timedelta(hours=HOURS) # date_t = date_t.strftime("%H:%M") x = -1 if el.value is None else el.value y = -1 if el.value2 is None else el.value2 if x < 500 and y < 500: # and change state_mech in function # if y < 750: # and change state_mech in function value_minute = 0 else: value_minute = 1 if data_per_shift.get(el.mech.number): data_per_shift[ el.mech.number]['data'][date_t] = value_minute, x, y # ? x, y data_per_shift[el.mech.number]['total_time'] += 1 else: data_per_shift[el.mech.number] = {} data_per_shift[el.mech.number]['mechanism'] = el.mech data_per_shift[el.mech.number]['total_time'] = 1 data_per_shift[el.mech.number]['data'] = {} data_per_shift[el.mech.number]['data'][date_t] = value_minute, x, y data_per_shift[el.mech.number].setdefault('work_time', 0) if value_minute != 0: data_per_shift[el.mech.number]['work_time'] += 1 # get start time for this shift start = datetime.combine(date_shift, datetime.min.time()) if shift == 1: start = start.replace(hour=8, minute=0, second=0, microsecond=0) else: start = start.replace(hour=20, minute=0, second=0, microsecond=0) if data_per_shift == {}: return None # create dict with all minutes to now if value is not return (-1) because # 0 may exist #---------------------------PART2------------------------------ i don't want 2 functions time_by_minuts = {} # pprint(data_per_shift) for key in data_per_shift.keys(): flag_start = True # flag_finish = True time_by_minuts[key] = {} time_by_minuts[key]['name'] = data_per_shift[key]['mechanism'].name time_by_minuts[key]['id'] = data_per_shift[key]['mechanism'].id time_by_minuts[key]['number'] = data_per_shift[key]['mechanism'].number # translate hours into minutes and round time_by_minuts[key]['total_time'] = round( data_per_shift[key]['total_time'] / 60, 1) time_by_minuts[key]['work_time'] = round( data_per_shift[key]['work_time'] / 60, 1) time_by_minuts[key]['data'] = {} delta_minutes = start time_move = 0 for i in range(1, 60 * 12 + 1): date_t = delta_minutes.strftime("%H:%M") val_minute = data_per_shift[key]['data'].setdefault( delta_minutes, (-1, 0, 0)) if val_minute[0] != -1: time_move += val_minute[0] / 60 time_by_minuts[key]['data'][i] = { 'time': date_t, 'value': val_minute[0], 'x': val_minute[1], 'y': val_minute[2], 'time_move': round(time_move, 2), } delta_minutes += timedelta(minutes=1) today_date, today_shift = today_shift_date() if val_minute[0] > 0 and flag_start: time_by_minuts[key]['start'] = date_t flag_start = False if val_minute[0] > 0: time_by_minuts[key]['finish'] = date_t if delta_minutes >= datetime.now( ) and date_shift == today_date and today_shift == shift: break return time_by_minuts
def time_for_shift_kran(date_shift, shift): '''get dict with all minute's values for the period, name and total''' # get data from db shift = int(shift) all_mechs = all_mechanisms_id('kran') try: cursor = db.session.query(Post).filter(Post.date_shift == date_shift, Post.shift == shift, Post.mechanism_id.in_(all_mechs)).order_by(Post.mechanism_id).all() except Exception as e: logger.debug(e) # create dict all works mechanism in shift data_per_shift = {} for el in cursor: date_t = el.timestamp.replace(second=0, microsecond=0) date_t += timedelta(hours=HOURS) if data_per_shift.get(el.mech.number): # if exist if el.value == 1 or el.value == 3: # 123 data_per_shift[el.mech.number]['total_90'] += 1 if el.value == 2: data_per_shift[el.mech.number]['total_180'] += 1 try: data_per_shift[el.mech.number]['total_terminals_180'][str(el.terminal)] +=1 except KeyError: data_per_shift[el.mech.number]['total_terminals_180'][str(el.terminal)] = 1 data_per_shift[el.mech.number]['data'][date_t] = el.value, el.count, el.terminal # pre_value=el.count # if will be problem with GPRS else: data_per_shift[el.mech.number] = {} data_per_shift[el.mech.number]['mechanism'] = el.mech data_per_shift[el.mech.number]['total_90'] = 0 data_per_shift[el.mech.number]['total_180'] = 0 data_per_shift[el.mech.number]['total_terminals_180'] = {} if el.value == 1: data_per_shift[el.mech.number]['total_90'] = 1 if el.value == 2: data_per_shift[el.mech.number]['total_180'] = 1 data_per_shift[el.mech.number]['total_terminals_180'][str(el.terminal)] = 1 data_per_shift[el.mech.number]['data'] = {} data_per_shift[el.mech.number]['data'][date_t] = el.value, el.count, el.terminal # pre_value=el.count # get start time for this shift start = datetime.combine(date_shift, datetime.min.time()) if shift == 1: start = start.replace(hour=8, minute=0, second=0, microsecond=0) else: start = start.replace(hour=20, minute=0, second=0, microsecond=0) if data_per_shift == {}: return None #---------------------------PART2------------------------------ i don't want 2 functions # create dict with all minutes to now if value is not return (-1) because 0 may exist time_by_minuts = {} for key, value in data_per_shift.items(): flag_start = True time_by_minuts[key] = {} time_by_minuts[key]['name'] = data_per_shift[key]['mechanism'].name time_by_minuts[key]['number'] = data_per_shift[key]['mechanism'].number time_by_minuts[key]['id'] = data_per_shift[key]['mechanism'].id time_by_minuts[key]['total_terminals_180'] = data_per_shift[key]['total_terminals_180'] # translate hours into minutes and round time_by_minuts[key]['total_180'] = round(data_per_shift[key]['total_180'], 2) time_by_minuts[key]['total_90'] = round(data_per_shift[key]['total_90'], 2) time_by_minuts[key]['data'] = {} delta_minutes = start try: last_find_item = db.session.query(Post).filter(Post.mechanism_id==data_per_shift[key]['mechanism'].id).order_by( Post.timestamp.desc()).first() except Exception as e: logger.debug(e) tmp_terminal = last_find_item.terminal # time_by_minuts[key]['total_terminals_180'] = {str(tmp_terminal): 0} # str becouse mongo need str key for i in range(1, 60 * 12 + 1): # 720 minutes in shift date_t = delta_minutes.strftime("%H:%M") try: val_minute = data_per_shift[key]['data'][delta_minutes][0] except KeyError: val_minute = -1 try: terminal = data_per_shift[key]['data'][delta_minutes][2] tmp_terminal = terminal except KeyError: # if item not exist get last found value terminal = tmp_terminal if val_minute == 4: # show 4 how 0 look get api val_minute =0 time_by_minuts[key]['data'][i] = { 'time': date_t, 'value': val_minute, 'terminal': terminal } delta_minutes += timedelta(minutes=1) today_date, today_shift = today_shift_date() if val_minute > 0 and flag_start: time_by_minuts[key]['start'] = date_t flag_start = False if val_minute > 0: time_by_minuts[key]['finish'] = date_t if delta_minutes >= datetime.now() and date_shift == today_date and today_shift == shift: # if now moment break time_by_minuts[key]['terminal'] = terminal # replace items from -1 to 0 if kran work, show + 4 minuts pre_items = -1 work_count = 0 last_value = 0 for number_item, data in time_by_minuts[key]['data'].items(): if data['value'] == -1 and pre_items != -1 and work_count < 5: time_by_minuts[key]['data'][number_item]['value'] = last_value work_count += 1 else: last_value = 0 if data['value'] == 5: # if kran move last_value = 5 work_count = 0 pre_items = data['value'] return time_by_minuts
def time_for_shift_usm(date_shift, shift): '''get dict with all minute's values for the period, name and total value is lever, value3 is speed roler, ''' # get data from db shift = int(shift) all_mechs = all_mechanisms_id('usm') try: cursor = db.session.query(Post).filter( Post.date_shift == date_shift, Post.shift == shift, Post.mechanism_id.in_(all_mechs)).order_by( Post.mechanism_id).all() except Exception as e: logger.debug(e) # create dict all works mechanism in shift data_per_shift = {} for el in cursor: date_t = el.timestamp.replace(second=0, microsecond=0) date_t += timedelta(hours=HOURS) # date_t = date_t.strftime("%H:%M") el.value = -1 if el.value is None else el.value el.value3 = 0 if el.value3 is None else el.value3 val_min = 0 if el.value < 0.1 else el.value el.value = 0 if el.value3 < 5 else el.value # maybe more, value3 is speed rool val_min = 0 if el.value3 < 5 else el.value if data_per_shift.get(el.mech.number): # bad data_per_shift[ el.mech.number]['data'][date_t] = val_min, el.value3, el.value data_per_shift[el.mech.number]['time_coal'] += el.value data_per_shift[el.mech.number]['total_time'] += 1 else: data_per_shift[el.mech.number] = {} data_per_shift[el.mech.number]['mechanism'] = el.mech data_per_shift[el.mech.number]['time_coal'] = el.value data_per_shift[el.mech.number]['total_time'] = 1 data_per_shift[el.mech.number]['data'] = {} data_per_shift[ el.mech.number]['data'][date_t] = val_min, el.value3, el.value data_per_shift[el.mech.number].setdefault('work_time', 0) if el.value > 0: data_per_shift[el.mech.number]['work_time'] += 1 # get start time for this shift start = datetime.combine(date_shift, datetime.min.time()) if shift == 1: start = start.replace(hour=8, minute=0, second=0, microsecond=0) else: start = start.replace(hour=20, minute=0, second=0, microsecond=0) if data_per_shift == {}: return None # create dict with all minutes to now if value is not return (-1) because # 0 may exist time_by_minuts = {} # pprint(data_per_shift) for key in data_per_shift.keys(): flag_start = True mech = data_per_shift[key]['mechanism'] time_by_minuts[key] = {} time_by_minuts[key]['name'] = mech.name time_by_minuts[key]['id'] = mech.id time_by_minuts[key]['number'] = mech.number time_by_minuts[key]['tons_in_hour'] = usm_tons_in_hour[mech.number] # translate hours into minutes and round time_by_minuts[key]['time_coal'] = round( data_per_shift[key]['time_coal'] / 60, 2) time_by_minuts[key]['total_time'] = round( data_per_shift[key]['total_time'] / 60, 1) time_by_minuts[key]['work_time'] = round( data_per_shift[key]['work_time'] / 60, 1) time_by_minuts[key]['data'] = {} delta_minutes = start try: last_find_item = db.session.query(Post).filter( Post.mechanism_id == data_per_shift[key] ['mechanism'].id).order_by(Post.timestamp.desc()).first() except Exception as e: logger.debug(e) terminal = last_find_item.terminal time_coal = 0 for i in range(1, 60 * 12 + 1): date_t = delta_minutes.strftime("%H:%M") val_minute = data_per_shift[key]['data'].setdefault( delta_minutes, (-1, -1, 0)) if len(val_minute) < 3: print(val_minute) time_coal += val_minute[2] / 60 time_by_minuts[key]['data'][i] = { 'time': date_t, 'value': val_minute[0], 'speed': val_minute[1], 'time_coal': round(time_coal, 2), 'terminal': terminal, } delta_minutes += timedelta(minutes=1) today_date, today_shift = today_shift_date() if val_minute[0] > 0 and flag_start: time_by_minuts[key]['start'] = date_t flag_start = False if val_minute[0] > 0: time_by_minuts[key]['finish'] = date_t if delta_minutes >= datetime.now( ) and date_shift == today_date and today_shift == shift: break time_by_minuts[key]['terminal'] = terminal return time_by_minuts