Пример #1
0
class LocationModel(SQL.Model):
    '''
    One-to-one Table representing Location for iCPE
    '''
    __tablename__ = 'location'
    id = SQL.Column(SQL.Integer, primary_key=True)
    node_id = SQL.Column(SQL.Integer, SQL.ForeignKey('node.id'))
    group_id = SQL.Column(SQL.Integer, SQL.ForeignKey('group.id'))
    street = SQL.Column(SQL.String(30))
    city = SQL.Column(SQL.String(30))
    latitude = SQL.Column(SQL.String(10))
    longitude = SQL.Column(SQL.String(10))

    def __init__(self, street, city, latitude, longitude):
        self.street = street
        self.city = city
        self.latitude = latitude
        self.longitude = longitude

    def to_json(self):
        return {
            'street': self.street,
            'city': self.city,
            'latitude': self.latitude,
            'longitude': self.longitude
        }

    def __repr__(self):
        return '<%r, %r>' % (self.street, self.city)
Пример #2
0
class CommandClassModel(SQL.Model):
    __tablename__ = 'commandclass'
    id = SQL.Column(SQL.Integer, primary_key=True)
    sensor_id = SQL.Column(SQL.Integer, SQL.ForeignKey('sensor.id'))
    number = SQL.Column(SQL.String(2))
    name = SQL.Column(SQL.String(20))
    types = SQL.relationship('CommandClassTypeModel',
                             backref="commandclass",
                             cascade="save-update, merge, delete")
    supported = SQL.Column(SQL.Boolean)
    web_field = SQL.Column(SQL.Boolean)
    events = SQL.relationship('EventModel',
                              backref="commandclass",
                              cascade="save-update, merge, delete")

    def __init__(self, number, name):
        self.number = str(number)[:2]
        self.name = name
        self.supported = False

    def commandclasstypes(self):
        return [ctype.number for ctype in self.types]

    def to_json(self):
        return {
            'name': self.name,
            'number': self.number,
            'webField': self.web_field,
            'supported': self.supported,
            'sensor': self.sensor.sensor_id,
            'icpe': self.sensor.icpe.mac_address
        }

    def columns(self):
        return ['number', 'name']
Пример #3
0
class MQTTModel(SQL.Model):
    __tablename__ = 'mqtt'
    id = SQL.Column(SQL.Integer, primary_key=True)
    host = SQL.Column(SQL.String(128))
    port = SQL.Column(SQL.Integer)
    username = SQL.Column(SQL.String(64))
    password = SQL.Column(SQL.String(64))

    icpes = SQL.relationship('iCPEModel',
                             secondary=mqtt_icpe,
                             backref=SQL.backref('mqtt', lazy='dynamic'))
    date_created = SQL.Column(SQL.DateTime)

    def __init__(self, host, port, username=None, password=None):
        self.host = host
        self.port = int(port)
        self.username = username
        self.password = password
        self.date_created = datetime.now()

    def online(self):
        return NodeDefender.db.mqtt.online(self.host, self.port)

    def to_json(self):
        return {
            'id': str(self.id),
            'host': self.host,
            'port': self.port,
            'date_created': str(self.date_created),
            'online': True,
            'username ': None,
            'password': None,
            'groups': [group.name for group in self.groups],
            'icpes': [icpe.mac_address for icpe in self.icpes]
        }
Пример #4
0
class GroupModel(SQL.Model):
    '''
    Representing one group containing iCPEs and Users
    '''
    __tablename__ = 'group'
    id = SQL.Column(SQL.Integer, primary_key=True)
    name = SQL.Column(SQL.String(50))
    email = SQL.Column(SQL.String(120))
    description = SQL.Column(SQL.String(250))
    date_created = SQL.Column(SQL.DateTime)
    users = SQL.relationship('UserModel',
                             secondary=user_list,
                             backref='groups')
    mqtts = SQL.relationship('MQTTModel',
                             secondary=mqtt_list,
                             backref='groups')
    nodes = SQL.relationship('NodeModel',
                             secondary=node_list,
                             backref='groups')
    location = SQL.relationship('LocationModel',
                                uselist=False,
                                backref='group')

    messages = SQL.relationship('MessageModel',
                                backref='group',
                                cascade='save-update, merge, delete')

    def __init__(self, name, email=None, description=None):
        self.name = name
        self.email = email
        self.description = str(description)
        self.date_created = datetime.now()

    def columns(self):
        return ['name', 'email', 'description']

    def to_json(self):
        if self.location:
            location = self.location.to_json()
        else:
            location = None
        return {
            'name':
            self.name,
            'email':
            self.email,
            'created':
            str(self.date_created),
            'description':
            self.description,
            'users': [user.email for user in self.users],
            'nodes': [node.name for node in self.nodes],
            'location':
            location,
            'url':
            url_for('admin_view.admin_group',
                    name=NodeDefender.serializer.dumps(self.name))
        }
Пример #5
0
class NodeNotesModel(SQL.Model):
    __tablename__ = 'nodenotes'

    id = SQL.Column(SQL.Integer, primary_key=True)
    node_id = SQL.Column(SQL.Integer, SQL.ForeignKey('node.id'))
    author = SQL.Column(SQL.String(80))
    note = SQL.Column(SQL.String(150))
    date_created = SQL.Column(SQL.DateTime)

    def __init__(self, author, note):
        self.author = author
        self.note = note
        self.date_created = datetime.now()
Пример #6
0
class CommandClassTypeModel(SQL.Model):
    __tablename__ = 'commandclasstype'
    id = SQL.Column(SQL.Integer, primary_key=True)
    commandclass_id = SQL.Column(SQL.Integer,
                                 SQL.ForeignKey('commandclass.id'))
    number = SQL.Column(SQL.String(2))
    name = SQL.Column(SQL.String(40))
    supported = SQL.Column(SQL.Boolean)
    web_field = SQL.Column(SQL.Boolean)
    events = SQL.relationship('EventModel',
                              backref="commandclasstype",
                              cascade="save-update, merge, delete")

    def __init__(self, number):
        self.number = str(number)[:2]
        self.supported = False
Пример #7
0
class EventModel(SQL.Model):
    __tablename__ = 'event'
    id = SQL.Column(SQL.Integer, primary_key=True)
    date = SQL.Column(SQL.DateTime)

    node_id = SQL.Column(SQL.Integer, SQL.ForeignKey('node.id'))
    icpe_id = SQL.Column(SQL.Integer, SQL.ForeignKey('icpe.id'))
    sensor_id = SQL.Column(SQL.Integer, SQL.ForeignKey('sensor.id'))
    commandclass_id = SQL.Column(SQL.Integer,
                                 SQL.ForeignKey('commandclass.id'))
    commandclasstype_id = SQL.Column(SQL.Integer,
                                     SQL.ForeignKey('commandclasstype.id'))

    state = SQL.Column(SQL.Boolean)
    value = SQL.Column(SQL.String(16))

    critical = SQL.Column(SQL.Boolean)
    normal = SQL.Column(SQL.Boolean)

    def __init__(self, state, value, date=None):
        self.state = bool(state)
        self.value = value
        self.date = date if date else datetime.now()

    def to_json(self):
        commandclass = self.commandclass.name
        if self.sensor.name:
            sensor_name = self.sensor.name
        else:
            sensor_name = self.sensor.sensor_id + ', ' + self.sensor.product_name

        if self.commandclasstype:
            commandclasstype = self.commandclasstype.name
            icon = eval('NodeDefender.icpe.zwave.commandclass.'+commandclass\
                        +'.'+commandclasstype+'.icon')(self.value)
            name = eval('NodeDefender.icpe.zwave.commandclass.'+commandclass\
                        +'.'+commandclasstype+'.fields')['name']

        elif self.commandclass:
            icon = eval('NodeDefender.icpe.zwave.commandclass.'+commandclass+\
                        '.icon')(self.value)
            name = eval('NodeDefender.icpe.zwave.commandclass.'+commandclass+\
                        '.fields')['name']

        return {'icpe' : self.icpe.mac_address, 'sensor' : sensor_name, 'node' :
                self.icpe.node.name, 'value' : self.value,\
                'date' : str(self.date), 'icon' : icon,\
                'name' : name}
Пример #8
0
class SensorModel(SQL.Model):
    '''
    ZWave Sensor, child of iCPE
    '''
    __tablename__ = 'sensor'
    id = SQL.Column(SQL.Integer, primary_key=True)
    icpe_id = SQL.Column(SQL.Integer, SQL.ForeignKey('icpe.id'))

    name = SQL.Column(SQL.String(64))
    sensor_id = SQL.Column(SQL.String(4))
    date_created = SQL.Column(SQL.DateTime)

    vendor_id = SQL.Column(SQL.String(16))
    product_type = SQL.Column(SQL.String(16))
    product_id = SQL.Column(SQL.String(16))
    vendor_name = SQL.Column(SQL.String(64))
    product_name = SQL.Column(SQL.String(64))
    device_type = SQL.Column(SQL.String(48))
    library_type = SQL.Column(SQL.String(48))

    sleepable = SQL.Column(SQL.Boolean)
    wakeup_interval = SQL.Column(SQL.Integer)

    commandclasses = SQL.relationship('CommandClassModel',
                                      backref='sensor',
                                      cascade='save-update, merge, delete')
    heat = SQL.relationship('HeatModel',
                            backref="sensor",
                            cascade="save-update, merge, delete")
    power = SQL.relationship('PowerModel',
                             backref="sensor",
                             cascade="save-update, merge, delete")
    events = SQL.relationship('EventModel',
                              backref="sensor",
                              cascade="save-update, merge, delete")

    messages = SQL.relationship('MessageModel',
                                backref='sensor',
                                cascade='save-update, merge, delete')

    def __init__(self, sensor_id, sensorinfo=None):
        self.sensor_id = str(sensor_id)
        self.date_created = datetime.now()
        if sensorinfo:
            for key, value in sensorinfo.items():
                setattr(self, key.lower(), value)

            self.name = self.product_name

    def columns(self):
        return [
            'sensor_id', 'vendor_id', 'product_type', 'product_id',
            'generic_class', 'specific_class', 'sleepable', 'wakeup_interval',
            'name'
        ]

    def to_json(self):
        return {'name' : self.name, 'sensor_id' : self.sensor_id,\
                'icpe' : self.icpe.mac_address,\
                'vendor_name' : self.vendor_name,
                'product_name' : str(self.productname)}
Пример #9
0
class iCPEModel(SQL.Model):
    '''
    iCPE attached to a Node
    '''
    __tablename__ = 'icpe'
    id = SQL.Column(SQL.Integer, primary_key=True)
    node_id = SQL.Column(SQL.Integer, SQL.ForeignKey('node.id'))
    name = SQL.Column(SQL.String(64))

    mac_address = SQL.Column(SQL.String(12), unique=True)
    serial_number = SQL.Column(SQL.String(32))

    ip_dhcp = SQL.Column(SQL.Boolean)
    ip_address = SQL.Column(SQL.String(32))
    ip_subnet = SQL.Column(SQL.String(32))
    ip_gateway = SQL.Column(SQL.String(32))

    firmware = SQL.Column(SQL.String(12))
    hardware = SQL.Column(SQL.String(8))

    telnet = SQL.Column(SQL.Boolean)
    ssh = SQL.Column(SQL.Boolean)
    http = SQL.Column(SQL.Boolean)
    snmp = SQL.Column(SQL.Boolean)

    enabled = SQL.Column(SQL.Boolean)
    date_created = SQL.Column(SQL.DateTime)
    last_online = SQL.Column(SQL.DateTime)
    sensors = SQL.relationship('SensorModel',
                               backref='icpe',
                               cascade='save-update, merge, delete')
    notesticky = SQL.Column(SQL.String(150))
    heat = SQL.relationship('HeatModel',
                            backref="icpe",
                            cascade="save-update, merge, delete")
    power = SQL.relationship('PowerModel',
                             backref="icpe",
                             cascade="save-update, merge, delete")
    events = SQL.relationship('EventModel',
                              backref="icpe",
                              cascade="save-update, merge, delete")

    messages = SQL.relationship('MessageModel',
                                backref='icpe',
                                cascade='save-update, merge, delete')

    def __init__(self, mac_address):
        self.mac_address = mac_address.upper()
        self.enabled = False
        self.date_created = datetime.now()

    def __repr__(self):
        return '<Name %r, Mac %r>' % (self.name, self.mac_address)

    def columns(self):
        return [
            'name', 'mac_address', 'serial_number', 'ip_dhcp', 'ip_address',
            'ip_subnet', 'ip_gateway', 'firmware', 'hardware', 'telnet', 'ssh',
            'http', 'snmp', 'enabled', 'last_online'
        ]

    def to_json(self):
        if self.node:
            node = self.node.name
        else:
            node = 'Not assigned'

        icpe = {
            'name':
            self.name,
            'mac_address':
            self.mac_address,
            'ip_address':
            self.ip_address,
            'date_created':
            str(self.date_created),
            'sensors':
            str(len(self.sensors)),
            'mqtt': [mqtt.to_json() for mqtt in self.mqtt],
            'node':
            node,
            'url':
            url_for('node_view.nodes_node',
                    name=NodeDefender.serializer.dumps(self.node.name))
        }
        return icpe
Пример #10
0
class MessageModel(SQL.Model):
    '''
    Representing one group containing iCPEs and Users
    '''
    __tablename__ = 'message'
    id = SQL.Column(SQL.Integer, primary_key=True)
    date = SQL.Column(SQL.DateTime)
    subject = SQL.Column(SQL.String(50))
    body = SQL.Column(SQL.String(180))

    group_id = SQL.Column(SQL.Integer, SQL.ForeignKey('group.id'))
    user_id = SQL.Column(SQL.Integer, SQL.ForeignKey('user.id'))
    node_id = SQL.Column(SQL.Integer, SQL.ForeignKey('node.id'))
    icpe_id = SQL.Column(SQL.Integer, SQL.ForeignKey('icpe.id'))
    sensor_id = SQL.Column(SQL.Integer, SQL.ForeignKey('sensor.id'))

    def __init__(self, subject, body):
        self.subject = subject
        self.body = body
        self.date = datetime.now()

    def to_json(self):
        if self.group:
            group = self.group.name
            url = url_for('admin_view.admin_group',
                          name=NodeDefender.serializer.dumps(group))
            icon = 'fa fa-users fa-3x'
        else:
            group = False

        if self.user:
            user = self.user.email
            url = url_for('admin_view.admin_user',
                          email=NodeDefender.serializer.dumps(user))
            icon = 'fa fa-user fa-3x'
        else:
            user = False

        if self.node:
            node = self.node.name
            url = url_for('node_view.nodes_node',
                          name=NodeDefender.serializer.dumps(node))
            icon = 'fa fa-map-marker fa-3x'
        else:
            node = False

        if self.icpe:
            icpe = self.icpe.name
            url = "#"
            icon = 'fa fa-bug fa-3x'
        else:
            icpe = False

        if self.sensor:
            sensor = self.sensor.name
            url = "#"
            icon = 'fa fa-bug fa-3x'
        else:
            sensor = False

        return {'group' : group, 'user' : user,\
                'node' : node, 'icpe' : icpe, 'sensor' : sensor,\
                'subject' : self.subject,
                'body' : self.body,
                'date' : str(self.date),
                'icon' : icon, 'url' : url}
Пример #11
0
class UserModel(SQL.Model):
    '''
    Table of Users

    Users is a part of a Group
    Password is encrypted
    '''
    __tablename__ = 'user'
    id = SQL.Column(SQL.Integer, primary_key=True)
    firstname = SQL.Column(SQL.String(30))
    lastname = SQL.Column(SQL.String(40))
    email = SQL.Column(SQL.String(191), unique=True)
    password = SQL.Column(SQL.String(191))

    enabled = SQL.Column(SQL.Boolean())
    date_confirmed = SQL.Column(SQL.DateTime)
    date_created = SQL.Column(SQL.DateTime)

    date_last_login = SQL.Column(SQL.DateTime)
    date_current_login = SQL.Column(SQL.DateTime)
    last_login_ip = SQL.Column(SQL.String(100))
    current_login_ip = SQL.Column(SQL.String(100))
    login_count = SQL.Column(SQL.Integer)

    technician = SQL.Column(SQL.Boolean)
    administrator = SQL.Column(SQL.Boolean)
    superuser = SQL.Column(SQL.Boolean)

    messages = SQL.relationship('MessageModel',
                                backref='user',
                                cascade='save-update, merge, delete')

    def __init__(self, email):
        self.email = email
        self.firstname = None
        self.lastname = None
        self.password = None
        self.active = False
        self.date_confirmed = None
        self.date_created = datetime.now()

        self.technician = False
        self.administrator = False
        self.superuser = False

    def columns(self):
        return ['firstname', 'lastname']

    def to_json(self):
        return {
            'firstName': self.firstname,
            'lastName': self.lastname,
            'email': self.email,
            'role': self.get_role(),
            'enabled': self.enabled,
            'dateCreated': str(self.date_created),
            'dateConfirmed': str(self.date_confirmed)
        }

    def is_active(self):
        return True

    def get_id(self):
        return str(self.id)

    def is_authenticated(self):
        return self.authenticated

    def is_anonymous(self):
        return False

    def verify_password(self, password):
        if NodeDefender.bcrypt.check_password_hash(self.password, password):
            return True
        else:
            return False

    def get_role(self):
        if self.superuser:
            return 'superuser'
        elif self.administrator:
            return 'administrator'
        elif self.technician:
            return 'technician'
        else:
            return 'observer'

    def set_role(self, role):
        if role.lower() == 'observer':
            self.technician = False
            self.administrator = False
            self.superuser = False
        elif role.lower() == 'technician':
            self.technician = True
            self.administrator = False
            self.superuser = False
        elif role.lower() == 'administrator':
            self.technician = True
            self.administrator = True
            self.observer = False
        elif role.lower() == 'superuser':
            self.technician = True
            self.administrator = True
            self.superuser = True
        else:
            raise AttributeError('Wrong kind of role')

    def has_role(self, role):
        try:
            return getattr(self, role.lower())
        except AttributeError:
            return None
Пример #12
0
class NodeModel(SQL.Model):
    '''
    Nodes represent a place that can contain one or more iCPEs

    Parent is the GroupModel that owns the Node
    Alias is the Name of the Node
    Location is stored in one-to-one Relation(LocationModel)
    Notes is to enter notebook for the Node
    NoteSticky is a sticky note for that node

    iCPES is relationship to List of iCPEs in that Node
    '''
    __tablename__ = 'node'
    id = SQL.Column(SQL.Integer, primary_key=True)
    name = SQL.Column(SQL.String(40), unique=True)
    description = SQL.Column(SQL.String(128))
    location = SQL.relationship('LocationModel', uselist=False, backref='node')
    date_created = SQL.Column(SQL.DateTime)
    notes = SQL.relationship('NodeNotesModel', backref='node')
    notesticky = SQL.Column(SQL.String(150))
    icpe = SQL.relationship('iCPEModel', backref='node', uselist=False)
    heat = SQL.relationship('HeatModel',
                            backref="node",
                            cascade="save-update, merge, delete")
    power = SQL.relationship('PowerModel',
                             backref="node",
                             cascade="save-update, merge, delete")
    events = SQL.relationship('EventModel',
                              backref="node",
                              cascade="save-update, merge, delete")
    messages = SQL.relationship('MessageModel',
                                backref='node',
                                cascade='save-update, merge, delete')

    def __init__(self, name):
        self.name = name
        self.date_created = datetime.now()

    def columns(self):
        return ['name', 'description', 'notesticky']

    def to_json(self):
        if self.location:
            location = self.location.to_json()
        else:
            location = None

        if self.icpe:
            icpe = NodeDefender.db.icpe.get(self.icpe.mac_address)
        else:
            icpe = False

        return {
            'name':
            self.name,
            'description':
            self.description,
            'location':
            location,
            'url':
            url_for('node_view.nodes_node',
                    name=NodeDefender.serializer.dumps(self.name)),
            'icpe':
            icpe
        }