def end_user_sessions(user_id=None, machine_id=None): """ End all sessions for a user or a machine (Either can be given)""" timestamp = datetime.now().timestamp() sessions = [] if user_id: sessions.extend( UserSession.query.filter_by(user_id=user_id, active=True).all()) elif machine_id: sessions.extend( UserSession.query.filter_by(machine_id=machine_id, active=True).all()) else: sessions.extend(UserSession.query.filter_by(active=True).all()) for us in sessions: current_app.logger.info(f"Ending user session {us}") us.timestamp_logout = timestamp us.active = False # End all jobs assigned to the session for job in us.jobs: job.end_time = timestamp job.active = None db.session.commit( ) # Not committing here would sometimes cause sqlite to have too many operations # Set the activity to "no user" complete_last_activity(machine_id=us.machine.id, timestamp_end=timestamp) new_activity = Activity(machine_id=us.machine.id, timestamp_start=timestamp, activity_code_id=Config.NO_USER_CODE_ID, machine_state=Config.MACHINE_STATE_OFF) current_app.logger.debug( f"Starting {new_activity} on logout of {us.user}") db.session.add(new_activity) db.session.commit()
def pneumatrol_1_resume_job(): timestamp = datetime.now().timestamp() # Get the reason for the pause. The app will return the short_description of the activity code if "downtime_reason" not in request.json: return json.dumps({"success": False}) notes = request.json.get("notes", "") downtime_reason = request.json["downtime_reason"] # Get the activity code corresponding to the description activity_code = ActivityCode.query.filter_by(short_description=downtime_reason).first() user_session = UserSession.query.filter_by(device_ip=request.remote_addr, active=True).first() if user_session is None: return json.dumps({"success": False, "reason": "User is logged out"}) current_job = Job.query.filter_by(user_session_id=user_session.id, active=True).first() time = datetime.now().strftime("%H:%M") if not current_job.notes: current_job.notes = "" # Initialise the string if null if notes != "": current_job.notes += f"{time} - {downtime_reason} - {notes} \n" # Mark the most recent activity in the database as complete complete_last_activity(machine_id=user_session.machine_id, activity_code_id=activity_code.id, timestamp_end=timestamp) # Start a new activity new_activity = Activity(machine_id=user_session.machine_id, machine_state=Config.MACHINE_STATE_RUNNING, activity_code_id=Config.UPTIME_CODE_ID, user_id=user_session.user_id, job_id=current_job.id, timestamp_start=timestamp) db.session.add(new_activity) db.session.commit() return json.dumps({"success": True})
def pneumatrol_1_start_job_or_setting(setting): if not request.is_json: return 404 timestamp = datetime.now().timestamp() user_session = UserSession.query.filter_by(device_ip=request.remote_addr, active=True).first() if user_session is None: return json.dumps({"success": False, "reason": "User is logged out"}) try: machine = user_session.machine except: return json.dumps({"success": False, "reason": "Server did not receive correct data"}) # Create the job if setting: try: machine = user_session.machine except: return json.dumps({"success": False, "reason": "Server did not receive correct data"}) job = Job(start_time=timestamp, user_id=user_session.user_id, user_session_id=user_session.id, wo_number=request.json["wo_number"], planned_set_time=request.json["planned_set_time"], machine_id=machine.id, active=True) else: job = Job(start_time=timestamp, user_id=user_session.user_id, user_session_id=user_session.id, wo_number=request.json["wo_number"], planned_run_time=request.json["planned_run_time"], planned_quantity=request.json["planned_quantity"], planned_cycle_time=request.json["planned_cycle_time"], machine_id=machine.id, active=True) db.session.add(job) db.session.commit() # End the current activity complete_last_activity(machine_id=machine.id, timestamp_end=timestamp) # Set the first activity depending on whether the machine is being set if setting: starting_activity_code = Config.SETTING_CODE_ID else: starting_activity_code = Config.UPTIME_CODE_ID # Start a new activity new_activity = Activity(machine_id=machine.id, machine_state=Config.MACHINE_STATE_RUNNING, activity_code_id=starting_activity_code, user_id=user_session.user_id, job_id=job.id, timestamp_start=timestamp) db.session.add(new_activity) db.session.commit() current_app.logger.info(f"{user_session.user} started {job}") return json.dumps({"success": True})
def pneumatrol_end_job_or_setting(setting): timestamp = datetime.now().timestamp() user_session = UserSession.query.filter_by(device_ip=request.remote_addr, active=True).first() if user_session is None: return json.dumps({"success": False, "reason": "User is logged out"}) # End the current job current_job = Job.query.filter_by(user_session_id=user_session.id, active=True).first() current_job.end_time = timestamp current_job.active = None if setting: try: scrap_quantity = int(request.json["scrap_quantity"]) except KeyError: return json.dumps({"success": False, "reason": "Did not receive correct data from server"}) current_job.setup_scrap = scrap_quantity else: try: actual_quantity = int(request.json["actual_quantity"]) scrap_quantity = int(request.json["scrap_quantity"]) except KeyError: return json.dumps({"success": False, "reason": "Did not receive correct data from server"}) current_job.production_scrap = scrap_quantity current_job.actual_quantity = actual_quantity db.session.commit() # Mark the most recent activity in the database as complete complete_last_activity(machine_id=user_session.machine_id, timestamp_end=timestamp) # Start a new activity new_activity = Activity(machine_id=user_session.machine_id, machine_state=Config.MACHINE_STATE_OFF, activity_code_id=Config.UNEXPLAINED_DOWNTIME_CODE_ID, user_id=user_session.user_id, timestamp_start=timestamp) db.session.add(new_activity) db.session.commit() current_app.logger.debug(f"User {user_session.user} ended {current_job}") return json.dumps({"success": True})
def pneumatrol_1_pause_job(): timestamp = datetime.now().timestamp() user_session = UserSession.query.filter_by(device_ip=request.remote_addr, active=True).first() if user_session is None: return json.dumps({"success": False, "reason": "User is logged out"}) current_job = Job.query.filter_by(user_session_id=user_session.id, active=True).first() # Mark the most recent activity in the database as complete complete_last_activity(machine_id=user_session.machine_id, timestamp_end=timestamp) # Start a new activity new_activity = Activity(machine_id=user_session.machine_id, machine_state=Config.MACHINE_STATE_OFF, activity_code_id=Config.UNEXPLAINED_DOWNTIME_CODE_ID, user_id=user_session.user_id, job_id=current_job.id, timestamp_start=timestamp) db.session.add(new_activity) db.session.commit() return json.dumps({"success": True})
def android_start_job(): if not request.is_json: return 404 timestamp = datetime.now().timestamp() user_session = UserSession.query.filter_by(device_ip=request.remote_addr, active=True).first() machine = user_session.machine if user_session.user.has_job(): return 400 # Create the job job = Job(start_time=timestamp, user_id=user_session.user_id, user_session_id=user_session.id, wo_number=request.json["wo_number"], planned_run_time=request.json["planned_run_time"], planned_quantity=request.json["planned_quantity"], machine_id=machine.id, active=True) db.session.add(job) db.session.commit() # End the current activity complete_last_activity(machine_id=machine.id, timestamp_end=timestamp) # Set the first activity starting_activity_code = Config.UPTIME_CODE_ID # Start a new activity new_activity = Activity(machine_id=machine.id, machine_state=Config.MACHINE_STATE_RUNNING, activity_code_id=starting_activity_code, user_id=user_session.user_id, job_id=job.id, timestamp_start=timestamp) db.session.add(new_activity) db.session.commit() current_app.logger.info(f"{user_session.user} started {job}") return json.dumps({"success": True})
def android_end_job(): timestamp = datetime.now().timestamp() user_session = UserSession.query.filter_by(device_ip=request.remote_addr, active=True).first() try: actual_quantity = int(request.json["actual_quantity"]) except KeyError: current_app.logger.error( f"Received incorrect data from {user_session} while ending job") return json.dumps({ "success": False, "reason": "Server error parsing data" }) # End the current job current_job = Job.query.filter_by(user_session_id=user_session.id, active=True).first() current_job.end_time = timestamp current_job.active = None current_job.actual_quantity = actual_quantity db.session.commit() # Mark the most recent activity in the database as complete complete_last_activity(machine_id=user_session.machine_id, timestamp_end=timestamp) # Start a new activity new_activity = Activity( machine_id=user_session.machine_id, machine_state=Config.MACHINE_STATE_OFF, activity_code_id=Config.UNEXPLAINED_DOWNTIME_CODE_ID, user_id=user_session.user_id, timestamp_start=timestamp) db.session.add(new_activity) db.session.commit() current_app.logger.debug(f"User {user_session.user} ended {current_job}") return json.dumps({"success": True})
def change_activity(machine, job, user): current_app.logger.debug(f"changing activity") complete_last_activity(machine_id=machine.id) chance_the_activity_is_uptime = 0.8 if random.random() < chance_the_activity_is_uptime: new_activity = Activity(machine_id=machine.id, timestamp_start=datetime.now().timestamp(), machine_state=1, activity_code_id=Config.UPTIME_CODE_ID, job_id=job.id, user_id=user.id) else: # otherwise the activity is downtime new_activity = Activity(machine_id=machine.id, timestamp_start=datetime.now().timestamp(), machine_state=0, activity_code_id=randrange( 2, len(ActivityCode.query.all())), job_id=job.id, user_id=user.id) db.session.add(new_activity) db.session.commit()
def start_user_session(user_id, device_ip): """ Start a new session. Usually called when a user logs in. Fails if no machine is assigned to the device ip""" timestamp = datetime.now().timestamp() user_session = UserSession.query.filter_by(user_id=user_id, device_ip=device_ip, active=True).first() # Close any user sessions that the current user has if user_session is not None: current_app.logger.warning( f"Tried to start a user session for user {user_id} while one is already open. Closing..." ) end_user_sessions(user_id) machine = Machine.query.filter_by(device_ip=device_ip).first() if machine is None: current_app.logger.info(f"No machine assigned to {device_ip}") return False # Close any sessions that exist on the current machine end_user_sessions(machine_id=machine.id) # Create the new user session new_us = UserSession(user_id=user_id, machine_id=machine.id, device_ip=device_ip, timestamp_login=timestamp, active=True) db.session.add(new_us) # Change the machine activity now that the user is logged in complete_last_activity(machine_id=machine.id, timestamp_end=timestamp) new_activity = Activity( machine_id=machine.id, timestamp_start=timestamp, activity_code_id=Config.UNEXPLAINED_DOWNTIME_CODE_ID, machine_state=Config.MACHINE_STATE_OFF) db.session.add(new_activity) db.session.commit() current_app.logger.info(f"Started user session {new_us}") return True
def android_update_activity(): timestamp = datetime.now().timestamp() user_session = UserSession.query.filter_by(device_ip=request.remote_addr, active=True).first() try: selected_activity_description = request.json["selected_activity_code"] except KeyError: return json.dumps({"success": False}) # Mark the most recent activity in the database as complete complete_last_activity(machine_id=user_session.machine_id, timestamp_end=timestamp) # Start a new activity # The current job is the only active job belonging to the user session current_job = Job.query.filter_by(user_session_id=user_session.id, active=True).first() # The activity code is obtained from the request activity_code = ActivityCode.query.filter_by( short_description=selected_activity_description).first() # The machine state is calculated from the activity code if activity_code.id == Config.UPTIME_CODE_ID: machine_state = Config.MACHINE_STATE_RUNNING else: machine_state = Config.MACHINE_STATE_OFF # Create the new activity new_activity = Activity(machine_id=user_session.machine_id, machine_state=machine_state, activity_code_id=activity_code.id, user_id=user_session.user_id, job_id=current_job.id, timestamp_start=timestamp) db.session.add(new_activity) db.session.commit() current_app.logger.debug(f"Started {new_activity} for {current_job}") return json.dumps({"success": True, "colour": activity_code.graph_colour})
def end_job(job, machine): current_app.logger.debug(f"ending job") job.end_time = datetime.now().timestamp() job.active = None complete_last_activity(machine_id=machine.id) db.session.commit()