class Housekeeping(db.Model): __tablename__ = 'housekeeping' id = db.Column(db.Integer, primary_key=True, autoincrement=True) satellite_mode = db.Column(db.String(32)) battery_voltage = db.Column(db.Float) current_in = db.Column(db.Float) current_out = db.Column(db.Float) no_MCU_resets = db.Column(db.Integer) last_beacon_time = db.Column(db.DateTime, nullable=False) watchdog_1 = db.Column(db.Integer) # 3 watchdogs watchdog_2 = db.Column(db.Integer) watchdog_3 = db.Column(db.Integer) panel_1_current = db.Column(db.Float) # 6 solar panel currents panel_2_current = db.Column(db.Float) panel_3_current = db.Column(db.Float) panel_4_current = db.Column(db.Float) panel_5_current = db.Column(db.Float) panel_6_current = db.Column(db.Float) temp_1 = db.Column(db.Float) # 6 temperatures at diff locations temp_2 = db.Column(db.Float) temp_3 = db.Column(db.Float) temp_4 = db.Column(db.Float) temp_5 = db.Column(db.Float) temp_6 = db.Column(db.Float) # Power channels (probably 24 exactly in a HK log) channels = db.relationship('PowerChannels', backref='housekeeping', lazy=True, cascade='all') def to_json(self): """Returns a dictionary of some selected model attributes """ return { 'id': self.id, 'satellite_mode': self.satellite_mode, 'battery_voltage': self.battery_voltage, 'current_in': self.current_in, 'current_out': self.current_out, 'no_MCU_resets': self.no_MCU_resets, 'last_beacon_time': str(self.last_beacon_time), 'watchdog_1': self.watchdog_1, 'watchdog_2': self.watchdog_2, 'watchdog_3': self.watchdog_3, 'panel_1_current': self.panel_1_current, 'panel_2_current': self.panel_2_current, 'panel_3_current': self.panel_3_current, 'panel_4_current': self.panel_4_current, 'panel_5_current': self.panel_5_current, 'panel_6_current': self.panel_6_current, 'temp_1': self.temp_1, 'temp_2': self.temp_2, 'temp_3': self.temp_3, 'temp_4': self.temp_4, 'temp_5': self.temp_5, 'temp_6': self.temp_6, 'channels': [channel.to_json() for channel in self.channels] }
class FlightScheduleCommandsArgs(db.Model): __tablename__ = 'flightschedulecommandsargs' id = db.Column(db.Integer, primary_key=True, autoincrement=True) index = db.Column(db.Integer) argument = db.Column(db.String(8)) flightschedulecommand_id = db.Column( db.Integer, db.ForeignKey('flightschedulecommands.id'), nullable=False) def to_json(self): """Returns a dictionary of some selected model attributes """ return {'index': self.index, 'argument': self.argument}
class Telecommands(db.Model): __tablename__ = 'telecommands' id = db.Column(db.Integer, primary_key=True, autoincrement=True) command_name = db.Column(db.String(64)) num_arguments = db.Column(db.Integer) flightschedulecommands = db.relationship('FlightScheduleCommands', backref='command', lazy=True) is_dangerous = db.Column(db.Boolean) def to_json(self): """Returns a dictionary of some selected model attributes """ return { 'command_id': self.id, 'command_name': self.command_name, 'num_arguments': self.num_arguments, 'is_dangerous': self.is_dangerous }
class User(db.Model): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True, autoincrement=True) username = db.Column(db.String(128), unique=True) password_hash = db.Column(db.String(128)) is_admin = db.Column(db.Boolean, default=False, nullable=False) slack_id = db.Column(db.String(128), nullable=True, unique=True) subscribed_to_slack = db.Column(db.Boolean, default=False) def __init__(self, username, password, is_admin=False, slack_id=None, subscribed_to_slack=False): self.username = username num_rounds = current_app.config.get('BCRYPT_LOG_ROUNDS') self.password_hash = bcrypt.generate_password_hash( password, num_rounds).decode() self.is_admin = is_admin self.slack_id = slack_id self.subscribed_to_slack = subscribed_to_slack def verify_password(self, password): """Returns True if passes password is valid, else False :param str password: The password to validate :returns: True if password is valid :rtype: bool """ return bcrypt.check_password_hash(self.password_hash, password) def encode_auth_token_by_id(self): """Generates the auth token""" try: payload = { 'exp': datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta( days=current_app.config.get('TOKEN_EXPIRATION_DAYS'), seconds=current_app.config.get( 'TOKEN_EXPIRATION_SECONDS')), 'iat': datetime.datetime.now(datetime.timezone.utc), 'sub': self.id } return jwt.encode(payload, current_app.config.get('SECRET_KEY'), algorithm='HS256') except Exception as e: print(e) return e @staticmethod def decode_auth_token(auth_token): """Decodes the auth token :param auth_token: The authorization token :returns: user_id (int) """ payload = jwt.decode(auth_token, current_app.config.get('SECRET_KEY')) user_id = payload['sub'] return user_id def to_json(self): """Returns a dictionary of some selected model attributes """ return { 'id': self.id, 'username': self.username, 'slack_id': self.slack_id }