Beispiel #1
0
class Graph(Common):
    """Class for Graph"""

    # INITIALIZE
    def __init__(self):
        """The Constructor for Graph class"""
        self.postgres = PostgreSQL()
        self._couch_db = CouchDatabase()
        self.couch_query = Queries()
        super(Graph, self).__init__()

    def graph(self):

        """
        This API is for Getting Graph
        ---
        tags:
          - Graph
        produces:
          - application/json
        parameters:
          - name: token
            in: header
            description: Token
            required: true
            type: string
          - name: userid
            in: header
            description: User ID
            required: true
            type: string
          - name: vessel_id
            in: query
            description: Vessel ID
            required: true
            type: string
          - name: device_id
            in: query
            description: Device ID
            required: true
            type: string
          - name: hours
            in: query
            description: Hours
            required: false
            type: integer
          - name: keys
            in: query
            description: Keys
            required: true
            type: string
          - name: combine
            in: query
            description: combine
            required: true
            type: boolean
          - name: start_time
            in: query
            description: Epoch start
            required: false
            type: string
          - name: end_time
            in: query
            description: Epoch end
            required: false
            type: string

        responses:
          500:
            description: Error
          200:
            description: Vessel Device Info
        """

        data = {}

        # VESSEL ID
        vessel_id = request.args.get('vessel_id')
        device_id = request.args.get('device_id')
        keys = request.args.get('keys')
        combine = request.args.get('combine')
        hours = request.args.get('hours')
        start_time = request.args.get('start_time')
        end_time = request.args.get('end_time')

        # GET DATA
        token = request.headers.get('token')
        userid = request.headers.get('userid')

        # CHECK TOKEN
        token_validation = self.validate_token(token, userid)

        if not token_validation:
            data["alert"] = "Invalid Token"
            data['status'] = 'Failed'

            # RETURN ALERT
            return self.return_data(data)

        if not vessel_id:

            data["alert"] = "Please complete parameters!"
            data['status'] = 'Failed'

            # RETURN ALERT
            return self.return_data(data)

        if device_id in ['COREVALUES', 'FAILOVER', 'NTWPERF1']:

            all_devices = self.couch_query.get_all_devices(vessel_id)
            for dev in all_devices:

                if dev['doc']['device'] == device_id:
                    device = dev['doc']
                    break
        else:

            device = self.couch_query.get_by_id(device_id)


        # hours_ago_epoch_ts = int(hours_ago.timestamp())
        # current_time = int(ctime.timestamp())
        if not start_time and not end_time:
            ctime = datetime.datetime.now()
            hours_ago = ctime - datetime.timedelta(hours=int(hours))
            start_time = int(hours_ago.timestamp())
            end_time = int(ctime.timestamp())

        values = self.couch_query.get_complete_values(
            vessel_id,
            device['device'],
            str(start_time),
            str(end_time),
            'all'
        )

        # temp_available_options = []
        # str_available_options = []
        # available_options = []
        opt = []
        parameters = self.couch_query.get_complete_values(
            vessel_id,
            "PARAMETERS"
        )

        if device['device'] == "COREVALUES":

            device_type = "COREVALUES"

        elif device['device'] == "NTWPERF1":

            device_type = "NTWPERF1"

        elif device['device'] == 'FAILOVER':

            device_type = 'FAILOVER'

        else:

            device_type = parameters['PARAMETERS'][device['device']]['TYPE']

        if combine.upper() == 'TRUE':

            final_data = []

            for value in values:

                timestamp = float(value['timestamp'])

                dev_value = value['value'][device['device']]

                # if  re.findall(r'VDR\d', device['device']):
                #     print("ALP ", dev_value)
                #     pass

                if  re.findall(r'NMEA\d', device['device']):

                    nmea_data = self.get_nmea_data(dev_value)
                    opt = list(set(self.get_graph_options(nmea_data)))
                    options = self.get_device_options(nmea_data)

                    if keys:
                        final_data.append(self.set_values(options, keys, timestamp))

                else:
                    opt = list(set(self.get_graph_options(dev_value)))
                    options = self.get_device_options(dev_value)

                    if keys:
                        final_data.append(self.set_values(options, keys, timestamp))

            final_data = sorted(final_data, key=lambda i: i['name'])

            for fdata in final_data:

                fdata['name'] = time.strftime('%d/%m/%Y %H:%M:%S', time.localtime(
                    float(fdata['name'])))

            data['data'] = final_data
            data['statistics'] = self.get_stat(final_data, flag=True)
        else:

            # FINAL DATA
            fnl_data = {}

            # SET DYNAMIC VARIABLE
            if keys:

                for key in keys.split(","):

                    fnl_data[key] = []

                for value in values:

                    timestamp = float(value['timestamp'])
                    dev_value = value['value'][device['device']]

                    # if  re.findall(r'VDR\d', device['device']):
                    #     print("ALP ", dev_value)
                    #     pass

                    if  re.findall(r'NMEA\d', device['device']):
                        nmea_data = self.get_nmea_data(dev_value)
                        opt = list(set(self.get_graph_options(nmea_data)))
                        options = self.get_device_options(nmea_data)

                        for key in keys.split(","):
                            fnl_data[key].append(self.set_values(options, key, timestamp))

                    else:
                        opt = list(set(self.get_graph_options(dev_value)))
                        options = self.get_device_options(dev_value)
                        for key in keys.split(","):
                            fnl_data[key].append(self.set_values(options, key, timestamp))

                for key in keys.split(","):

                    fnl_data[key] = sorted(fnl_data[key], key=lambda i: i['name'])

                    for fdata in fnl_data[key]:

                        fdata['name'] = time.strftime('%d/%m/%Y %H:%M:%S', time.localtime(float(
                            fdata['name'])))

            data['data'] = fnl_data
            data['statistics'] = self.get_stat(fnl_data, flag=False)

        # SET SQL QUERY
        sql_str = "SELECT * FROM selected_label"
        sql_str += " WHERE device_type='{0}' and select_type != 'combine'".format(device_type)
        default_selected = self.postgres.query_fetch_all(sql_str)

        # SET SQL QUERY
        sql_str = "SELECT * FROM selected_label"
        sql_str += " WHERE device_type='{0}' and select_type = 'combine'".format(device_type)
        default_selected_combine = self.postgres.query_fetch_all(sql_str)

        data['default_selected_combine'] = self.set_select_values(default_selected_combine)
        data['default_selected'] = self.set_select_values(default_selected)
        data['available_options'] = self.format_filter(opt)
        data['status'] = 'ok'

        return self.return_data(data)

    def set_values(self, options, keys, timestamp):
        """Set Values"""

        temp_data = {}

        for key in keys.split(","):

            if key in options.keys():

                if key in ['AntennaStatus', 'TxMode']:

                    if options[key].upper() in ['TRACKING', 'ON']:
                        temp_data[key] = 1
                    else:
                        temp_data[key] = 0

                elif key == 'error':

                    if options[key] is True:
                        temp_data[key] = 1
                    else:
                        temp_data[key] = 0

                else:
                    temp_data[key] = options[key]
            else:
                temp_data[key] = None

        temp_data['name'] = timestamp

        return temp_data

    def get_available_options(self, options, temp_available_options, str_available_options):
        """Return Available Options"""
        # print("Options --------------------------- >>")
        # print(options)
        # print("Options --------------------------- >>")
        available_options = []

        for opt in options.keys():

            if opt not in temp_available_options and opt not in str_available_options:

                if not options[opt]:
                    continue

                if type(options[opt]) == 'str':
                    if options[opt].isdigit():

                        options[opt] = float(options[opt])

                if self.isfloat(options[opt]) or self.isint(options[opt]) or opt in [
                        'AntennaStatus', 'TxMode']:

                    temp_available_options.append(opt)
                    temp = {}
                    temp['value'] = opt
                    temp['label'] = opt
                    available_options.append(temp)

                # else:
                #     str_available_options.append(op)

        return available_options

    def get_nmea_data(self, datas):
        """ Return NMEA Data """
        temp_data = {}
        # keys = [data for data in datas.keys()] # No need

        for key in list(datas.keys()):
            temp_data.update(datas[key])

        return temp_data

    def get_graph_options(self, datas):
        """Return Options for Graph"""
        options = []

        # modules = [data for data in datas] # No need

        for mod in list(datas):

            options += self.get_opt_available(datas[mod])

        return options

    def get_opt_available(self, options):
        """Return Available Options"""

        available_options = []

        for opt in options.keys():

            if not options[opt]:
                continue

            if type(options[opt]) == 'str':

                if options[opt].isdigit():

                    options[opt] = float(options[opt])

            if self.isfloat(options[opt]) or self.isint(options[opt]) or opt in [
                    'AntennaStatus', 'TxMode']:

                available_options.append(opt)

        return available_options

    def get_device_options(self, datas):
        """ Return All Device Options Data """

        tmp = []
        temp = {}

        # modules = [data for data in datas] # No need

        for mod in list(datas):
            tmp.append(datas[mod])

        for dta in tmp:
            temp.update(dta)

        return temp

    def set_select_values(self, datas):
        """Set Select Values"""

        final_data = []

        for data in datas:
            temp = {}
            temp['value'] = data['label']
            temp['label'] = data['label']
            final_data.append(temp)

        return final_data

    def get_stat(self, datas, flag):
        """ Return Min and Max """

        temp = {}
        tmp = []
        if datas:
            if flag is True:
                temp_data = {}

                for data in datas[0].keys():

                    if data != "name":
                        temp_data[data] = tuple(dta[data] for dta in datas)

                # keys = [data for data in temp_data.keys()]
                # for  key in keys:

                #     if all(res is None for res in temp_data[key]):
                #         pass
                #     else:
                #         data = self.get_stat_range(temp_data, key)
                #         temp.update({
                #             key : data
                #         })

                for dta in temp_data.values():
                    tmp += dta

                if tmp:
                    tmp = [dta for dta in tmp if dta is not None]
                    tmp = sorted(tmp, key=float)
                    data = self.get_stat_range(tmp)

                    temp.update({
                        "combine" : data
                    })
            else:
                # keys = [data for data in datas.keys()]

                for key in list(datas.keys()):

                    result = [data[key] for data in datas[key] if data[key] is not None]
                    result = sorted(result, key=float)
                    # if all(res is None for res in result):
                    #     pass
                    # else:
                    if result:
                        data = self.get_stat_range(result)

                        temp.update({
                            key : data
                        })

        return temp

    def get_stat_range(self, data):
        """ Return Stat Range """
        tmp = {}

        tmp['min'] = data[0]
        tmp['max'] = data[-1]
        # tmp['min'] = min(data)
        # tmp['max'] = max(data)

        return tmp
class CalculateAlarmValue(Common):
    """Class for CalculateAlarmValue"""

    # INITIALIZE
    def __init__(self):
        """The Constructor for CalculateAlarmValue class"""

        self.postgres = PostgreSQL()
        self.couch_query = Queries()
        super(CalculateAlarmValue, self).__init__()

    def calculate_value(self,
                        alarm_value_id,
                        start,
                        end,
                        vessel_id=None,
                        device_id=None):
        """Calculate Alarm Value"""

        if alarm_value_id:

            results = []
            alarm_value = self.get_alarm_value(alarm_value_id)

            if alarm_value:

                vessels = alarm_value['vessel']
                devices = alarm_value['device']
                device_types = alarm_value['device_type']
                modules = alarm_value['module']
                options = alarm_value['option']

                #VALIDATE VESSEL IF EXISTS IN ALARM VALUE
                if vessel_id:
                    vessel = self.validate_alarm_vessel(vessels, vessel_id)

                    if vessel:
                        vessel_list = [vessel]
                    else:

                        #RETURN NO DATA TO SHOW
                        return 0
                else:
                    vessel_list = self.get_all_vessels(vessels, options)

                #GET DATA
                for vessel in vessel_list:

                    #VALIDATE DEVICE IF EXISTS IN ALARM VALUE
                    if device_id:
                        device = self.validate_alarm_device(
                            vessel, devices, device_id)

                        if device:
                            device_list = [device]
                        else:
                            return 0
                    else:
                        device_list = self.find_device_by_type(
                            vessel, devices, device_types)

                    #GET COREVALUES AND PREVIOUS COREVALUES DATA
                    corevalues = self.couch_query.get_coredata(
                        vessel, start, end)
                    prev_core = self.get_previous_coremarks(vessel, start)

                    for device in device_list:

                        modlist = self.find_alarm_data(vessel,
                                                       modules,
                                                       key="module")
                        optlist = self.find_alarm_data(vessel,
                                                       options,
                                                       key="option")

                        # GET DATA ON COUCHDB BY VESSEL AND DEVICE
                        datas = self.get_vessel_data(vessel, device, start,
                                                     end)

                        if datas:

                            mod_data = self.get_mod_data(
                                device, modlist, datas)
                            opt_data = self.get_option_data(
                                device, mod_data, optlist, datas)

                            results += self.set_opt_values(
                                opt_data, vessel, corevalues, prev_core,
                                device)

                        else:

                            mod_option = self.get_mod_opt(
                                vessel, device, corevalues, prev_core, modlist,
                                optlist)
                            results += mod_option

            return results

        return 0

    def get_previous_coremarks(self, vessel, end):
        """Get Previous Corevalues Remarks"""

        data = {}
        corevalues = self.couch_query.get_coredata(vessel,
                                                   0,
                                                   end,
                                                   flag="limit")

        if corevalues:
            data['timestamp'] = corevalues[0]
            data['remarks'] = "Unknown"

        else:
            data['timestamp'] = ""
            data['remarks'] = "No Data"

        return data

    def find_alarm_data(self, vessel, datas, key):
        """Find Alarm Values Data"""

        temp_data = []

        if datas:

            #CHECK INPUTS IF EXIST IN VESSEL
            for data in datas.split(","):

                check_data = self.check_data_by_vessel(key, vessel, data)

                if check_data:

                    for cdata in check_data:

                        temp_data.append(cdata[key])

        else:

            #GET ALL DEVICES IF NO VESSELS
            datas = self.check_data_by_vessel(key, vessel)

            for data in datas:
                temp_data.append(data[key])

        return temp_data

    # GET ALARM VALUE
    def get_alarm_value(self, alarm_value_id):
        """Get Alarm Value"""

        assert alarm_value_id, "Alarm value ID is required."

        # DATA
        sql_str = "SELECT * FROM alarm_value"
        sql_str += " WHERE alarm_value_id={0}".format(alarm_value_id)
        alarm_value = self.postgres.query_fetch_one(sql_str)

        if alarm_value:

            return alarm_value

        return 0

    def get_all_vessels(self, vessels, options):
        """Get All Vessels"""

        vessel_list = []

        if vessels:

            tmp_vessel = []
            for vessel in vessels.split(","):
                tmp_vessel.append(vessel)

            vessel_ids = ','.join(["'{}'".format(tmp) for tmp in tmp_vessel])

            sql_str = "SELECT vessel_id FROM vessel"
            sql_str += " WHERE vessel_id IN ({0})".format(vessel_ids)

            datas = self.postgres.query_fetch_all(sql_str)

            vessel_list = [data['vessel_id'] for data in datas]

        else:

            #OPTION IS REQUIRED ON ALARM VALUE
            vessel_list = self.get_vessels_by_option(options)
        return vessel_list

    def get_vessels_by_option(self, options):
        """Get Vessels By Option"""

        datas = ""

        if options:

            tmp_opt = []
            for option in options.split(","):
                tmp_opt.append(option)

            opt = ','.join(["'{}'".format(tmp) for tmp in tmp_opt])

            # OPTION DATA
            sql_str = "SELECT DISTINCT vessel_id FROM option"
            sql_str += " WHERE option IN ({0})".format(opt)

            datas = self.postgres.query_fetch_all(sql_str)

        if datas:
            vessel_list = [data['vessel_id'] for data in datas]

        else:
            vessel_list = self.vessel_list()

        return vessel_list

    def vessel_list(self):
        """Get Vessel List"""

        sql_str = "SELECT vessel_id FROM vessel"

        datas = self.postgres.query_fetch_all(sql_str)

        if datas:
            vessel_list = [data['vessel_id'] for data in datas]

        return vessel_list

    def get_vessel_data(self, vessel, device, start_time, end_time):
        """Get Vessel Data"""

        if vessel:

            values = self.couch_query.get_complete_values(
                vessel, device, str(start_time), str(end_time), 'all')

            if values:

                return values

        return 0

    # GET DATA DEVICE/MODULE/OPTION
    def check_data_by_vessel(self, key, vessel_id, value=None):
        """Check Alarm Data by Vessel"""

        if key == "device_type":
            table = "device"

        else:
            table = key

        # DATA
        sql_str = "SELECT DISTINCT {0} FROM {1}".format(key, table)
        sql_str += " WHERE vessel_id ='{0}'".format(vessel_id)

        if value:
            sql_str += " AND {0} ILIKE '{1}'".format(key, value.strip())

        datas = self.postgres.query_fetch_all(sql_str)

        if datas:

            return datas

        return ''

    def get_mod_data(self, device, modules, datas):
        """Filter modules by vessel's alarm data"""

        temp_data = []

        for module in modules:

            alarm_data = [
                module for data in datas if module in data['value'][device]
            ]
            temp_data += alarm_data

        return set(temp_data)

    def get_option_data(self, device, modules, options, datas):
        """Get option by vessel's alarm data"""

        temp_data = []
        results = []

        for module in modules:
            for option in options:

                if option in datas[0]['value'][device][module]:

                    message = "ok"
                    for data in datas:
                        temp_data.append({
                            "timestamp":
                            data['timestamp'],
                            "value":
                            data['value'][device][module][option]
                        })
                else:

                    message = "No option {0} found under module".format(option)

                results.append({
                    "device": device,
                    "module": module,
                    "option": option,
                    "datas": temp_data,
                    "message": message
                })

        return results

    def get_mod_opt(self, vessel, device, core, prev_core, modules, options):
        """Get Module Option if no data found per vessel"""

        results = []
        module = "No Module found"
        option = "No Option found"

        for module in modules:

            if options:
                # Miss Ly, May something dito.
                # for option in options:
                # option = option
                option = options[-1]

            results.append({
                "vessel_id": vessel,
                "device": device,
                "device_id": self.get_device_id(vessel, device),
                "module": module,
                "option": option,
                "datas": [],
                "message": "No Data",
                "core": core,
                "previous": prev_core
            })

        return results

    def find_device_by_type(self, vessel, devices, device_types):
        """Find Device"""

        check_device = self.find_alarm_data(vessel, devices, key="device")

        if device_types:

            device_list = []
            devicetype_list = self.find_alarm_data(vessel,
                                                   device_types,
                                                   key="device_type")

            if devices:

                for device in check_device:

                    try:
                        device_index = devicetype_list.index(device)
                        device_list.append(devicetype_list[device_index])
                    except:
                        device_list.append(device)
            else:

                device_list = devicetype_list

        else:
            device_list = check_device

        return device_list

    def get_device_by_id(self, device_id):
        """Get Device by ID"""

        # DATA
        sql_str = "SELECT DISTINCT device FROM device"
        sql_str += " WHERE device_id = '{0}'".format(device_id)

        data = self.postgres.query_fetch_one(sql_str)

        if data:

            return data['device']

        return ''

    def get_device_id(self, vessel_id, device):
        """Get Device ID"""

        # DATA
        sql_str = "SELECT DISTINCT device_id FROM device"
        sql_str += " WHERE device = '{0}'".format(device)
        sql_str += " AND vessel_id ='{0}'".format(vessel_id)

        data = self.postgres.query_fetch_one(sql_str)

        if data:

            return data['device_id']

        return ''

    def validate_alarm_device(self, vessel, devices, device_id):
        """Validate Alarm Device"""

        #IF EMPTY DEVICES IT SELECTED ALL DEVICES
        device = self.get_device_by_id(device_id)

        device_list = self.find_alarm_data(vessel, devices, key="device")

        if device in device_list:

            return device

        return 0

    def validate_alarm_vessel(self, vessels, vessel_id):
        """Validate Alarm Vessel"""

        #IF EMPTY VESSELS IT SELECTED ALL VESSELS
        if vessels:

            vessel_list = []
            for vessel in vessels.split(","):
                vessel_list.append(vessel)

            if vessel_id in vessel_list:
                return vessel_id

            return 0

        return vessel_id

    def set_opt_values(self, datas, vessel, corevalues, prev_core, device):
        """ Set Option values """

        res = []

        for option in datas:
            option['vessel_id'] = vessel
            option['core'] = corevalues
            option['previous'] = prev_core
            option['device_id'] = self.get_device_id(vessel, device)

            res.append(option)

        return res
Beispiel #3
0
class Invite(Common, ShaSecurity):
    """Class for Invite"""

    # INITIALIZE
    def __init__(self):
        """The Constructor for Invite class"""

        # INIT CONFIG
        self.config = ConfigParser()

        # CONFIG FILE
        self.config.read("config/config.cfg")

        self._couch_db = CouchDatabase()
        self.couch_query = Queries()
        self.postgres = PostgreSQL()
        self.epoch_default = 26763

        self.vpn_db_build = config_section_parser(self.config,
                                                  "VPNDB")['build']
        super(Invite, self).__init__()

        if self.vpn_db_build.upper() == 'TRUE':
            self.my_ip = config_section_parser(self.config, "IPS")['my']
            self.my_protocol = config_section_parser(self.config,
                                                     "IPS")['my_protocol']

            self.user_vpn = config_section_parser(self.config,
                                                  "IPS")['user_vpn']
            self.user_protocol = config_section_parser(self.config,
                                                       "IPS")['user_protocol']

            self.vessel_vpn = config_section_parser(self.config,
                                                    "IPS")['vessel_vpn']
            self.vessel_protocol = config_section_parser(
                self.config, "IPS")['vessel_protocol']

            self.vpn_token = '269c2c3706886d94aeefd6e7f7130ab08346590533d4c5b24ccaea9baa5211ec'

    # GET VESSEL FUNCTION
    def invitation(self):
        """
        This API is for Sending invitation
        ---
        tags:
          - User
        produces:
          - application/json
        parameters:
          - name: token
            in: header
            description: Token
            required: true
            type: string
          - name: userid
            in: header
            description: User ID
            required: true
            type: string
          - name: query
            in: body
            description: Invite
            required: true
            schema:
              id: Invite
              properties:
                first_name:
                    type: string
                last_name:
                    type: string
                middle_name:
                    type: string
                url:
                    type: string
                email:
                    type: string
                companies:
                    types: array
                    example: []
                roles:
                    types: array
                    example: []
                vessels:
                    types: array
                    example: []
        responses:
          500:
            description: Error
          200:
            description: Sending invitaion
        """
        # INIT DATA
        data = {}

        # GET DATA
        token = request.headers.get('token')
        userid = request.headers.get('userid')

        # GET JSON REQUEST
        query_json = request.get_json(force=True)

        # GET REQUEST PARAMS
        email = query_json["email"]
        url = query_json["url"]
        vessels = query_json['vessels']

        # CHECK TOKEN
        token_validation = self.validate_token(token, userid)

        if not token_validation:
            data["alert"] = "Invalid Token"
            data['status'] = 'Failed'

            # RETURN ALERT
            return self.return_data(data)

        # INIT IMPORTANT KEYS
        important_keys = {}
        important_keys['companies'] = []
        important_keys['roles'] = []
        important_keys['email'] = "string"
        important_keys['url'] = "string"

        # CHECK IMPORTANT KEYS IN REQUEST JSON
        if not self.check_request_json(query_json, important_keys):

            data["alert"] = "Invalid query, Missing parameter!"
            data['status'] = 'Failed'

            # RETURN ALERT
            return self.return_data(data)

        # CHECK INVITATION
        if self.check_invitaion(email):
            data["alert"] = "Already invited!"
            data['status'] = 'Failed'

            # RETURN ALERT
            return self.return_data(data)

        password = self.generate_password()

        # INSERT INVITATION
        account_id = self.insert_invitation(password, query_json)

        if not account_id:

            data = {}
            data['message'] = "Invalid email!"
            data['status'] = "Failed"

            return self.return_data(data)

        if self.vpn_db_build.upper() == 'TRUE':

            # FOR USER VPN
            # JOB DATAS
            callback_url = self.my_protocol + "://" + self.my_ip + "/vpn/update"
            data_url = self.my_protocol + "://" + self.my_ip + "/vpn/data"

            sql_str = "SELECT * FROM account_role where account_id='{0}' ".format(
                account_id)
            sql_str += "AND role_id in (SELECT role_id FROM role "
            sql_str += "WHERE role_name='super admin')"

            super_admin = self.postgres.query_fetch_one(sql_str)

            vpn_type = 'VCLIENT'
            cvpn_type = 'CLIENT'

            if super_admin:

                vpn_type = 'VRH'
                cvpn_type = 'RHADMIN'

            # INSERT JOB
            job_id = self.insert_job(callback_url, data_url, self.vpn_token,
                                     account_id, self.user_vpn, cvpn_type)

            # INIT PARAMS FOR CREATE VPN
            vpn_params = {}
            vpn_params['callback_url'] = callback_url
            vpn_params['data_url'] = data_url
            vpn_params['job_id'] = job_id

            # CREATE VPN
            self.create_vpn(vpn_params, self.vpn_token)

            # FOR VESSEL VPN
            if vessels or super_admin:

                allow_access = False

                for vessel in vessels:

                    if vessel['allow_access']:

                        allow_access = True
                        break

                if allow_access or super_admin:

                    # INSERT JOB
                    job_id = self.insert_job(callback_url, data_url,
                                             self.vpn_token, account_id,
                                             self.vessel_vpn, vpn_type)

                    # INIT PARAMS FOR CREATE VPN
                    vpn_params = {}
                    vpn_params['callback_url'] = callback_url
                    vpn_params['data_url'] = data_url
                    vpn_params['job_id'] = job_id

                    # CREATE VPN
                    self.create_vpn(vpn_params, self.vpn_token, True)

        # SEND INVITATION
        self.send_invitation(email, password, url)

        data = {}
        data['message'] = "Invitation successfully sent!"
        data['status'] = "ok"

        return self.return_data(data)

    def check_invitaion(self, email):
        """Check Invitation"""

        sql_str = "SELECT * FROM account WHERE "
        sql_str += " email = '" + email + "'"

        res = self.postgres.query_fetch_one(sql_str)

        if res:

            return res

        return 0

    def check_username(self, username):
        """Check Invitation"""

        sql_str = "SELECT * FROM account WHERE "
        sql_str += " username = '******'"

        res = self.postgres.query_fetch_one(sql_str)

        if res:

            return res

        return 0

    def insert_invitation(self, password, query_json):
        """Insert Invitation"""

        token = self.generate_token()

        vessels = query_json['vessels']
        companies = query_json['companies']
        roles = query_json['roles']

        data = query_json
        data = self.remove_key(data, "companies")
        data = self.remove_key(data, "roles")
        data = self.remove_key(data, "vessels")

        # username = query_json["email"].split("@")[0]

        username = "******".format(int(time.time()))
        if not self.check_username(username):
            username += self.random_str_generator(5)

        data['username'] = username
        data['token'] = token
        data['status'] = True
        data['state'] = False
        data['password'] = self.string_to_sha_plus(password)
        data['created_on'] = time.time()
        account_id = self.postgres.insert('account', data, 'id')

        if not account_id:
            return 0

        for vessel in vessels:

            ntwconf = self.couch_query.get_complete_values(
                vessel['vessel_id'], "NTWCONF")

            # ACCOUNT VESSEL
            temp = {}
            temp['vessel_vpn_ip'] = ntwconf['NTWCONF']['tun1']['IP']
            temp['account_id'] = account_id
            temp['vessel_id'] = vessel['vessel_id']
            temp['vessel_vpn_state'] = 'pending'

            temp['allow_access'] = False
            if 'allow_access' in vessel.keys():
                temp['allow_access'] = vessel['allow_access']

            self.postgres.insert('account_vessel', temp)

        for company in companies:

            # ACCOUNT COMPANY
            temp = {}
            temp['account_id'] = account_id
            temp['company_id'] = company
            self.postgres.insert('account_company', temp)

        for role_id in roles:

            # ACCOUNT COMPANY
            temp = {}
            temp['account_id'] = account_id
            temp['role_id'] = role_id
            self.postgres.insert('account_role', temp)

        return account_id

    def send_invitation(self, email, password, url):
        """Send Invitation"""

        email_temp = Invitation()
        emailer = Email()

        message = email_temp.invitation_temp(password, url)
        subject = "Invitation"
        emailer.send_email(email, message, subject)

        return 1

    def generate_password(self):
        """Generate Password"""

        char = string.ascii_uppercase
        char += string.ascii_lowercase
        char += string.digits

        return self.random_str_generator(8, char)

    def insert_job(self, callback_url, data_url, vpn_token, account_id,
                   user_vpn, vpn_type):
        """Insert Job"""

        update_on = time.time()

        # INIT NEW JOB
        temp = {}
        temp['callback_url'] = callback_url
        temp['vnp_server_ip'] = user_vpn
        temp['data_url'] = data_url
        temp['token'] = vpn_token
        temp['status'] = 'pending'
        temp['account_id'] = account_id
        temp['vpn_type'] = vpn_type
        temp['action'] = 'CREATE'
        temp['account_os'] = 'WINDOWS'  # LINUX
        temp['update_on'] = update_on
        temp['created_on'] = update_on

        # INSERT NEW JOB
        job_id = self.postgres.insert('job', temp, 'job_id')

        return job_id

    def create_vpn(self, data, vpn_token, flag=False):
        """Create VPN"""
        api_endpoint = self.user_protocol + "://" + self.user_vpn + "/ovpn"

        if flag:

            api_endpoint = self.vessel_protocol + "://" + self.vessel_vpn + "/ovpn"

        headers = {'content-type': 'application/json', 'token': vpn_token}

        req = requests.post(api_endpoint,
                            data=json.dumps(data),
                            headers=headers)
        res = req.json()

        return res
class UpdateVesselState(Common):
    """Class for UpdateVesselState"""

    # INITIALIZE
    def __init__(self):
        """The Constructor for UpdateVesselState class"""

        self._couch_db = CouchDatabase()
        self.couch_query = Queries()
        self.postgres = PostgreSQL()
        super(UpdateVesselState, self).__init__()

    def update_vessel_state(self):
        """Update Vessel State"""

        vessels = self.couch_query.get_vessels()

        # JUST TO INDEX
        # self.couch_query.get_system1()

        vessel_ids = [x['id'] for x in vessels]

        # DON'T DELETE FOR SYSTEM INDEXING
        for vssl_id in vessel_ids:

            self.couch_query.get_system(vssl_id)

        # INIT SQL QUERY
        sql_str = "SELECT * FROM vessel"

        # FETCH ALL
        rows = self.postgres.query_fetch_all(sql_str)

        db_vessel_ids = [x['vessel_id'] for x in rows]

        vessel_ids = set(vessel_ids)
        db_vessel_ids = set(db_vessel_ids)

        vessel_ids.difference_update(db_vessel_ids)

        if vessel_ids:

            for vessel_id in vessel_ids:

                doc = self.couch_query.get_by_id(vessel_id)

                sql_str = "SELECT * FROM vessel WHERE "
                sql_str += "number='{0}'".format(doc['number'])

                vssl_nmbr = self.postgres.query_fetch_one(sql_str)

                if vssl_nmbr:

                    if vssl_nmbr['vessel_id'] and vssl_nmbr['number']:

                        # INIT CONDITION
                        conditions = []

                        # CONDITION FOR QUERY
                        conditions.append({
                            "col": "vessel_id",
                            "con": "=",
                            "val": vssl_nmbr['vessel_id']
                        })

                        data = {}
                        data['vessel_id'] = vessel_id

                        self.postgres.update('vessel', data, conditions)

                    else:

                        print("SAME VESSEL NUMBER: {0}!!!".format(
                            vssl_nmbr['number']))

                else:

                    data = {}
                    data['number'] = doc['number']
                    data['vessel_name'] = self.get_vessel_name(vessel_id)
                    data['vessel_id'] = vessel_id
                    data['update_on'] = time.time()
                    data['created_on'] = time.time()

                    self.postgres.insert('vessel', data)

        sql_str = "SELECT * FROM vessel WHERE number IS NULL OR vessel_name IS NULL"
        null_vessels = self.postgres.query_fetch_all(sql_str)

        for vssl in null_vessels:

            # INIT CONDITION
            conditions = []

            # CONDITION FOR QUERY
            conditions.append({
                "col": "vessel_id",
                "con": "=",
                "val": vssl['vessel_id']
            })
            doc = self.couch_query.get_by_id(vssl['vessel_id'])

            if 'error' in doc.keys():
                number = vssl['vessel_id']

            else:
                number = doc['number']

            data = {}
            data['number'] = number
            data['vessel_name'] = self.get_vessel_name(vssl['vessel_id'])

            self.postgres.update('vessel', data, conditions)

        # RETURN
        return rows

    def get_vessel_name(self, vessel_id):
        """ Return Vessel Name """

        assert vessel_id, "Vessel ID is required."

        # GET VESSEL NAME
        values = self.couch_query.get_complete_values(vessel_id, "PARAMETERS")

        if values:
            vessel_name = values['PARAMETERS']['INFO']['VESSELNAME']
        else:
            vessel_name = ""

        return vessel_name
Beispiel #5
0
class AlarmValueData(Common):
    """Class for AlarmValueData"""

    # INITIALIZE
    def __init__(self):
        """The Constructor for AlarmValueData class"""
        self.postgres = PostgreSQL()
        self.couch_query = Queries()
        self.device_state = UpdateDeviceState()
        super(AlarmValueData, self).__init__()

    def alarm_value_data(self):
        """
        This API is for Getting Alarm Value
        ---
        tags:
          - Alarm Values
        produces:
          - application/json
        parameters:
          - name: token
            in: header
            description: Token
            required: true
            type: string
          - name: userid
            in: header
            description: User ID
            required: true
            type: string

        responses:
          500:
            description: Error
          200:
            description: Alarm Value
        """
        data = {}

        # GET DATA
        token = request.headers.get('token')
        userid = request.headers.get('userid')

        # CHECK TOKEN
        token_validation = self.validate_token(token, userid)

        if not token_validation:
            data['alert'] = "Invalid Token"
            data['status'] = 'Failed'

            # RETURN ALERT
            return self.return_data(data)

        data['data'] = []

        vessels = self.get_all_vessels()

        keys = ['device', 'device_type', 'module', 'option']

        datas = []

        for key in keys:

            alarm_data = self.get_alarm_datas(key)

            format_data = self.data_filter(alarm_data, key)

            if key == "device":
                devices = format_data

            if key == "device_type":
                device_types = format_data

            if key == "module":
                modules = format_data

            if key == "option":
                options = format_data

        if datas:
            rows = datas

        else:
            rows = []

        datas = rows
        data['vessels'] = vessels
        data['devices'] = devices
        data['modules'] = modules
        data['options'] = options
        data['device_types'] = device_types
        data['status'] = 'ok'

        return self.return_data(data)

    def get_alarm_datas(self, key):
        """Get Alarm Datas"""
        if key:

            if key == "device_type":

                table = "device"

            else:
                table = key

            # DATA
            sql_str = "SELECT DISTINCT {0} FROM {1}".format(key, table)

            datas = self.postgres.query_fetch_all(sql_str)

            data = {}

            k = "{0}s".format(key)

            temp_data = [temp_data[key] for temp_data in datas]

            data[k] = temp_data

        return data

    def get_all_vessels(self):
        """Get All Vessels"""

        vessel_list = []

        vessels = self.couch_query.get_vessels()
        if vessels:

            for item in vessels:
                vessel_name = self.couch_query.get_complete_values(
                    item['id'], "PARAMETERS")

                if vessel_name:
                    vessel_name = vessel_name['PARAMETERS']['INFO'][
                        'VESSELNAME']
                else:
                    vessel_name = "Vessel Not Found"

                vessel_list.append({"label": vessel_name, "value": item['id']})

            return vessel_list

        return ''
Beispiel #6
0
class ValidateAlarmValue(Common):
    """Class for ValidateAlarmValue"""

    # INITIALIZE
    def __init__(self):
        """The Constructor for ValidateAlarmValue class"""

        self.postgres = PostgreSQL()
        self.couch_query = Queries()
        super(ValidateAlarmValue, self).__init__()

    # VALIDATE INPUTS
    def validate_alarm_value(self, query_json):
        """Validate Alarm Value"""

        data = {}

        if not query_json['name']:

            data["alert"] = "Name cannot be empty!"

            return data

        if not query_json['option']:

            data["alert"] = "Option cannot be empty!"

            return data

        if query_json['vessel']:

            for vessel in query_json['vessel'].split(","):

                values = self.couch_query.get_complete_values(
                    vessel, "PARAMETERS")

                if values:

                    vessel_name = values['PARAMETERS']['INFO']['VESSELNAME']

                else:

                    data["alert"] = "No Vessel found."
                    return data

                if query_json['device']:

                    column = "device"

                    for device in query_json['device'].split(","):

                        validate = self.check_alarm_data(
                            column, device, vessel)

                        if not validate:

                            data[
                                "alert"] = "No Device found under {0}.".format(
                                    vessel_name)
                            return data

                if query_json['module']:

                    column = "module"

                    for module in query_json['module'].split(","):

                        validate = self.check_alarm_data(
                            column, module, vessel)

                        if not validate:

                            data[
                                "alert"] = "No Module found under {0}.".format(
                                    vessel_name)
                            return data

                if query_json['option']:

                    column = "option"

                    for option in query_json['option'].split(","):

                        validate = self.check_alarm_data(
                            column, option, vessel)

                        if not validate:

                            data[
                                "alert"] = "No Option found under {0}.".format(
                                    vessel_name)
                            return data

        if query_json['device']:

            column = "device"

            for device in query_json['device'].split(","):

                validate = self.check_alarm_data(column, device)

                if not validate:

                    data["alert"] = "No Device found."
                    return data

        if query_json['device_type']:

            column = "device_type"

            for device_type in query_json['device_type'].split(","):

                validate = self.check_alarm_data(column, device_type)

                if not validate:

                    data["alert"] = "No Device found."
                    return data

        if query_json['module']:

            column = "module"

            for module in query_json['module'].split(","):

                validate = self.check_alarm_data(column, module)

                if not validate:

                    data["alert"] = "No Module found."
                    return data

        if query_json['option']:

            column = "option"

            for option in query_json['option'].split(","):

                validate = self.check_alarm_data(column, option)

                if not validate:

                    data["alert"] = "No Option found"
                    return data

        if query_json['device'] and query_json['device_type']:

            for device in query_json['device'].split(","):

                if device in [
                        'COREVALUES', 'FAILOVER', 'PARAMETERS', 'NTWCONF'
                ]:

                    data["alert"] = "No Device Type under {0}".format(
                        query_json['device'])

                else:

                    for device_type in query_json['device_type'].split(","):
                        if not self.check_device_type(device, device_type):
                            data['alert'] = "No Device Type found on Device"

            return data

        return 0

    def check_alarm_data(self, column, value, vessel=None):
        """Validate Alarm Data"""

        if column == "device_type":

            table = "device"

        else:
            table = column

        # DATA
        sql_str = "SELECT * FROM {0}".format(table)
        sql_str += " WHERE {0} ILIKE '{1}'".format(column, value)

        if vessel:
            sql_str += " AND vessel_id ILIKE '{0}'".format(vessel)

        datas = self.postgres.query_fetch_all(sql_str)

        data = {}
        data['rows'] = datas

        if datas:
            return data

        return 0

    def check_device_type(self, device, device_type):
        """Validate Device Type"""

        # DATA
        sql_str = "SELECT * FROM device"
        sql_str += " WHERE device ILIKE '{0}'".format(device)
        sql_str += " AND device_type ILIKE '{0}'".format(device_type)

        datas = self.postgres.query_fetch_all(sql_str)

        data = {}
        data['rows'] = datas

        if datas:
            return data

        return 0
Beispiel #7
0
class GeneralInfo(Common):
    """Class for GeneralInfo"""

    # INITIALIZE
    def __init__(self):
        """The Constructor for GeneralInfo class"""
        self._couch_db = CouchDatabase()
        self.couch_query = Queries()
        self.epoch_default = 26763
        super(GeneralInfo, self).__init__()

    def general_info(self):
        """
        This API is for getting general info of device
        ---
        tags:
          - Devices
        produces:
          - application/json
        parameters:
          - name: token
            in: header
            description: Token
            required: true
            type: string
          - name: userid
            in: header
            description: User ID
            required: true
            type: string
          - name: vessel_id
            in: query
            description: Vessel ID
            required: true
            type: string
        responses:
          500:
            description: Error
          200:
            description: Vessel Device List
        """
        # INIT DATA
        data = {}

        # VESSEL ID
        vessel_id = request.args.get('vessel_id')

        # GET DATA
        token = request.headers.get('token')
        userid = request.headers.get('userid')

        # CHECK TOKEN
        token_validation = self.validate_token(token, userid)

        if not token_validation:
            data["alert"] = "Invalid Token"
            data['status'] = 'Failed'

            # RETURN ALERT
            return self.return_data(data)

        if not vessel_id:
            data["alert"] = "No Vessel ID"
            data['status'] = 'Failed'

            # RETURN ALERT
            return self.return_data(data)

        rows = []

        heading = self.get_heading(vessel_id)
        if heading:
            row = {}
            row['option_name'] = 'Heading'
            row['value'] = "{0}°".format(float(heading['heading']))
            row['data_provider'] = heading['heading_source']
            rows.append(row)

        speed = self.get_speed(vessel_id)
        if speed:
            row = {}
            row['option_name'] = 'Speed'
            row['value'] = speed['speed']
            row['data_provider'] = speed['speed_source']
            rows.append(row)

        failover = self.couch_query.get_complete_values(vessel_id, "FAILOVER")

        if failover:
            row = {}
            row['option_name'] = 'Internet provider'
            row['value'] = failover['FAILOVER']['General']['Description']
            row['data_provider'] = 'Failover'
            rows.append(row)

        parameters = self.couch_query.get_complete_values(
            vessel_id, "PARAMETERS")

        info = parameters['PARAMETERS']['INFO']
        if info:
            row = {}
            row['option_name'] = 'IMO'
            row['value'] = info['IMO']
            row['data_provider'] = 'PARAMETERS'
            rows.append(row)

        if info:
            row = {}
            row['option_name'] = 'MMSI'
            row['value'] = info['MMSI']
            row['data_provider'] = 'PARAMETERS'
            rows.append(row)

        data['status'] = 'ok'
        data['rows'] = rows
        return self.return_data(data)

    def get_heading(self, vessel_id):
        """Return Heading"""

        all_devices = self.couch_query.get_all_devices(vessel_id)

        source = 'VSAT'
        devices = self.get_device(all_devices, source)

        data = {}
        for device in devices:

            values = self.couch_query.get_complete_values(
                vessel_id, device['device'])

            if values:

                # GET DEVICE NUMBER
                number = device['device'].split(source)[1]

                # GET DEVICE COMPLETE NAME
                heading_source = self.device_complete_name(source, number)

                # SET RETURN

                data['heading'] = values[
                    device['device']]['General']['Heading']
                data['heading_source'] = heading_source

                # # RETURN
                # return data

        source = 'NMEA'
        devices = self.get_device(all_devices, source)

        for device in devices:

            values = self.couch_query.get_complete_values(
                vessel_id, device['device'])

            if values:

                # GET DEVICE NUMBER
                number = device['device'].split(source)[1]

                # GET DEVICE COMPLETE NAME
                heading_source = self.device_complete_name(source, number)
                gen = values[device['device']].keys()
                if 'GP0001' in gen:

                    gp0001 = values[device['device']]['GP0001']
                    # SET RETURN

                    if not data:

                        data['heading'] = gp0001['VTG'][
                            'courseOverGroundTrueDegrees']
                        data['heading_source'] = heading_source
        # RETURN
        return data

    def get_device(self, devices, pattern):
        """Return Device"""

        data = []
        for device in devices:

            if re.findall(r'' + pattern + r'\d', device['doc']['device']):
                data.append(device['doc'])

        data = sorted(data, key=lambda i: i['device'])

        return data

    def get_speed(self, vessel_id):
        """Return Speed"""
        all_devices = self.couch_query.get_all_devices(vessel_id)

        source = 'NMEA'
        devices = self.get_device(all_devices, source)

        data = {}
        speed = None
        speed_source = ""
        for device in devices:

            values = self.couch_query.get_complete_values(
                vessel_id, device['device'])

            if values:

                # GET DEVICE NUMBER
                number = device['device'].split(source)[1]

                # GET DEVICE COMPLETE NAME
                heading_source = self.device_complete_name(source, number)
                gen = values[device['device']].keys()
                if 'GP0001' in gen:

                    gp0001 = values[device['device']]['GP0001']
                    # SET RETURN

                    if not data:

                        data['heading'] = gp0001['VTG'][
                            'courseOverGroundTrueDegrees']
                        data['heading_source'] = heading_source

                    speed = float(gp0001['VTG']['speedOverGroundKnots'])
                    speed_source = device['device']

                    data['speed'] = str(speed) + " knot(s)"
                    data['speed_source'] = speed_source

                    return data
        return 0
Beispiel #8
0
class UserCompany(Common):
    """Class for UserCompany"""

    # INITIALIZE
    def __init__(self):
        """The Constructor for UserCompany class"""
        self.postgresql_query = PostgreSQL()
        self._couch_db = CouchDatabase()
        self.couch_query = Queries()
        super(UserCompany, self).__init__()

    def user_company(self):
        """
        This API is for Getting User Company
        ---
        tags:
          - User
        produces:
          - application/json
        parameters:
          - name: token
            in: header
            description: Token
            required: true
            type: string
          - name: userid
            in: header
            description: User ID
            required: true
            type: string
          - name: account_id
            in: query
            description: Account ID
            required: true
            type: integer
        responses:
          500:
            description: Error
          200:
            description: User Company
        """
        data = {}

        # GET DATA
        token = request.headers.get('token')
        userid = request.headers.get('userid')
        account_id = int(request.args.get('account_id'))

        # CHECK TOKEN
        token_validation = self.validate_token(token, userid)

        if not token_validation:
            data["alert"] = "Invalid Token"
            data['status'] = 'Failed'

            # RETURN ALERT
            return self.return_data(data)

        # COMPANY DATA
        datas = self.get_companies(account_id)

        datas['status'] = 'ok'

        return self.return_data(datas)

    def get_companies(self, account_id):
        """Return Companies"""

        # DATA
        sql_str = "SELECT * FROM account_company WHERE account_id = " + str(account_id)

        res = self.postgresql_query.query_fetch_all(sql_str)

        rows = []

        # COMPANY'S VESSELS-ID
        for company in res:
            company_id = company['company_id']
            vessel_res = self.get_company_vessels(company_id)
            vessels = []
            for vessel in vessel_res['rows']:

                temp = {}
                temp['vessel_id'] = vessel['vessel_id']
                temp['vessel_name'] = self.get_vessel_name(vessel['vessel_id'])
                temp['allow_access'] = self.get_allow_access(account_id, vessel['vessel_id'])
                vessels.append(temp)

            company['vessels'] = vessels
            rows.append(company)

        data = {}
        data['rows'] = rows

        return data

    # GET VESSELS OF COMPANY
    def get_company_vessels(self, company_id): #, user=None):
        """Return Company Vessels"""

        assert company_id, "CompanyID is required."

        # DATA
        vessels = []
        sql_str = "SELECT * FROM company_vessels WHERE company_id={0}".format(company_id)
        vessels = self.postgres.query_fetch_all(sql_str)

        data = {}
        data['rows'] = vessels

        return data


    # GET THE VESSEL NAME
    def get_vessel_name(self, vessel_id):
        """Return Vessel Name"""

        values = self.couch_query.get_complete_values(
            vessel_id,
            "PARAMETERS"
        )
        if values:
            return values['PARAMETERS']['INFO']['VESSELNAME']
        return ''

    def get_allow_access(self, account_id, vessel_id):
        """Return  Allow access"""

        # DATA
        sql_str = "SELECT * FROM account_vessel"
        sql_str += " WHERE account_id={0} AND vessel_id='{1}'".format(account_id, vessel_id)
        vessel = self.postgres.query_fetch_one(sql_str)

        if vessel:

            return vessel['allow_access']

        return False
Beispiel #9
0
class Blockages(Common):
    """Class for Blockages Library"""

    # INITIALIZE
    def __init__(self):
        """The Constructor for Blockage class"""
        self.postgres = PostgreSQL()
        self.couch_query = Queries()
        super(Blockages, self).__init__()

    def get_device_type(self, vessel_id, device_id):
        """Get Device Type"""

        assert device_id, "Device ID is required."

        # DATA
        sql_str = "SELECT * FROM device "
        sql_str += "WHERE vessel_id='{0}' ".format(vessel_id)
        sql_str += "AND device_id='{0}'".format(device_id)
        device = self.postgres.query_fetch_one(sql_str)

        return device

    def get_blockage_zones(self, device, device_type, data):
        """ Return Blockage Zones """

        # GET NUMBER OF ZONES
        zone_number = self.get_zone_number(device_type)
        dlist = [tmp['timestamp'] for tmp in data]
        dlist.sort()
        timestamp = dlist[-1]

        blockzone = []
        blocks = {}

        if zone_number and dlist:

            try:
                time_index = dlist.index(timestamp)
                az_start_val = None
                el_start_val = None
                az_end_val = None
                el_end_val = None
                bz_type_val = None
                bz_val = None

                for num in range(1, zone_number + 1):

                    bzstatus = "bz{0}Status".format(num)
                    az_start = "bz{0}AzStart".format(num)
                    el_start = "bz{0}ElStart".format(num)
                    az_end = "bz{0}AzEnd".format(num)
                    el_end = "bz{0}ElEnd".format(num)
                    bz_type = "bz{0}Type".format(num)

                    blockzones = "Blockzone{0}".format(num)

                    if bzstatus in data[time_index]['value'][device][
                            'General']:
                        bz_val = data[time_index]['value'][device]['General'][
                            bzstatus]

                    if az_start in data[time_index]['value'][device][
                            'General']:
                        az_start_val = data[time_index]['value'][device][
                            'General'][az_start]

                    if el_start in data[time_index]['value'][device][
                            'General']:
                        el_start_val = data[time_index]['value'][device][
                            'General'][el_start]

                    if az_end in data[time_index]['value'][device]['General']:
                        az_end_val = data[time_index]['value'][device][
                            'General'][az_end]

                    if el_end in data[time_index]['value'][device]['General']:
                        el_end_val = data[time_index]['value'][device][
                            'General'][el_end]

                    if bz_type in data[time_index]['value'][device]['General']:
                        bz_type_val = data[time_index]['value'][device][
                            'General'][bz_type]

                    if blockzones.lower() in data[time_index]['value'][device]:
                        tmp_holder = data[time_index]['value'][device][
                            blockzones.lower()]
                        if not bz_val:
                            bz_val = tmp_holder['Status']
                        if not az_start_val:
                            az_start_val = tmp_holder['AzStart']
                        if not el_start_val:
                            az_end_val = tmp_holder['AzEnd']
                        if not el_end_val:
                            el_end_val = tmp_holder['ElEnd']

                    bz_item = {
                        bzstatus: bz_val,
                        az_start: az_start_val,
                        az_end: az_end_val,
                        el_start: el_start_val,
                        el_end: el_end_val,
                        bz_type: bz_type_val
                    }

                    blocks[blockzones] = bz_item

                blockzone.append(blocks)

            except ValueError:
                print("No Timestamp found")

        return blockzone

    def get_device_data(self, vessel_id, device, start, end):
        """ Return Device Data """

        values = self.couch_query.get_complete_values(vessel_id, device, start,
                                                      end, 'all')

        if values:
            return values

        return 0

    def get_blockage_data(self, device, values):
        """ Return Blockage Data """
        blockage = []
        if values:

            for val in values:

                blockage.append({
                    "antenna_status":
                    val['value'][device]['General']['AntennaStatus'],
                    "timestamp":
                    val['timestamp'],
                    "azimuth":
                    val['value'][device]['General']['RelativeAz'],
                    "elevation":
                    val['value'][device]['General']['Elevation'],
                })

            return blockage
        return 0

    def get_current_position(self, vessel_id, device):
        """ Return Current Position """

        data = {}
        values = self.couch_query.get_complete_values(vessel_id, device)
        if values:
            azimuth = None
            elevation = None

            if 'RelativeAz' in values[device]['General']:
                azimuth = values[device]['General']['RelativeAz']

            if 'Elevation' in values[device]['General']:
                elevation = values[device]['General']['Elevation']

            data['az'] = azimuth
            data['el'] = elevation

            return data
        return 0

    def get_mainloop_time(self, vessel):
        """ Return Mainloop Time """

        values = self.couch_query.get_complete_values(vessel, "PARAMETERS")

        if values:
            return values['PARAMETERS']['MAIN_PROGRAM']['MAINLOOPTIME']
        return 0

    def get_xydata(self, vessel_data, antenna_status, start_time, end_time,
                   remarks):
        """ Return XY Data """

        data = {}
        tmp = []
        for status in antenna_status:
            if remarks == 'cron':
                data[status.capitalize()] = self.get_coordinates(
                    vessel_data, status, 1)

            elif remarks == 'merge':

                coordinates = [data['coordinates'] for data in vessel_data]
                data[status.capitalize()] = self.merge_coordinates(
                    coordinates, status, start_time, end_time)

            else:
                data[status.capitalize()] = self.get_coordinates(
                    vessel_data, status, 0)

        tmp.append(data)

        return data

    def get_coordinates(self, vessel_data, status, flag=True):
        """ Return Coordinates """

        tmp = []
        data = {}
        for vdata in vessel_data:

            if vdata['antenna_status'] == status:
                if flag == 1:
                    tmp.append({
                        "elevation": float(vdata['elevation']),
                        "azimuth": float(vdata['azimuth']),
                        "timestamp": vdata['timestamp']
                    })
                else:
                    tmp.append({
                        "x-axis": round(float(vdata['elevation'])),
                        "y-axis": round(float(vdata['azimuth']))
                    })

        data['coordinates'] = tmp
        # data['count'] = len(tmp)
        return data

    def combine_axis(self, data):
        """ Combine Axis """

        seen = set()
        tmp = []
        for dta in data:
            tup = tuple(dta.items())
            if tup not in seen:
                seen.add(tup)
                tmp.append(dta)
        return tmp

    def merge_coordinates(self, datas, status, start_time, end_time):
        """ Return Merge Coordinates from PostgreSQL data """

        tmp = []
        coords = []
        data = {}

        for dta in datas:

            if status in dta:
                coords += dta[status]['coordinates']

        coordinates = sorted(coords, key=lambda i: i["timestamp"])

        for coord in coordinates:
            if coord['timestamp'] >= float(
                    start_time) and coord['timestamp'] <= float(end_time):

                tmp.append({
                    "x-axis": round(float(coord['elevation'])),
                    "y-axis": round(float(coord['azimuth']))
                })

        data['coordinates'] = tmp
        return data

    def antenna_status(self, antennas):
        """ Antenna Status """

        assert antennas, "Antenna is required."

        # antenna_state_online = map(lambda data:data.upper(), ['tracking'])
        antenna_state_online = ['TRACKING']
        antenna_state_offline = [
            'BLOCKING ZONE', 'ACQUIRING SIGNAL', 'SKYSCAN', 'SEARCH1',
            'SEARCH2', 'SEARCH3'
        ]
        antenna_state_service = [
            'ANTENNA POST', 'READY', 'INITIALIZE', 'SETUP', 'DIAGNOSTIC'
        ]
        antenna_state_invalid = ['ucli:']

        antenna_status = []
        online_state = []
        offline_state = []
        service_state = []
        invalid_state = []

        for antenna in antennas:

            status = antenna.capitalize()

            if antenna.upper() in antenna_state_online:
                online_state.append(status)

            if antenna.upper() in antenna_state_offline:
                offline_state.append(status)

            if antenna.upper() in antenna_state_service:
                service_state.append(status)

            if antenna.upper() in antenna_state_invalid:
                invalid_state.append(status)

        antenna_status.append({
            "Online": online_state,
            "Offline": offline_state,
            "Service": service_state,
            "Invalid": invalid_state
        })

        return antenna_status

    def get_zone_number(self, device_type):
        """ Return Zone Number """

        assert device_type, "Device Type is required"

        vsat = [
            'Intellian_V100_E2S', 'Intellian_V110_E2S', 'Intellian_V80_IARM',
            'Intellian_V100_IARM', 'Intellian_V100', 'Intellian_V80_E2S'
        ]

        zone = 0  # 'Cobham_500'
        if device_type in vsat:
            zone = 5

        if device_type == "Sailor_900":
            zone = 8

        return zone

    # CHECK DEVICE TYPE IF ALLOWED
    def allowed_device_type(self, device_type):
        """ Check Device Type if map available """

        assert device_type, "Device Type is required."

        allowed_device_type = [
            'Intellian_V100_E2S', 'Intellian_V110_E2S', 'Intellian_V80_IARM',
            'Intellian_V100_IARM', 'Intellian_V100', 'Intellian_V80_E2S',
            'Sailor_900', 'Cobham_500'
        ]

        if device_type in allowed_device_type:
            return 1

        return 0
Beispiel #10
0
class DeviceList(Common):
    """Class for DeviceImages"""

    # INITIALIZE
    def __init__(self):
        """The Constructor for DeviceImages class"""
        self.postgres = PostgreSQL()
        self.couch_query = Queries()
        self.aws3 = AwsS3()
        super(DeviceList, self).__init__()

    def get_list(self):
        """
        This API is for Getting All Vessel Device List
        ---
        tags:
          - Devices
        produces:
          - application/json
        parameters:
          - name: token
            in: header
            description: Token
            required: true
            type: string
          - name: userid
            in: header
            description: User ID
            required: true
            type: string
          - name: limit
            in: query
            description: Limit
            required: true
            type: integer
          - name: page
            in: query
            description: Page
            required: true
            type: integer
        responses:
          500:
            description: Error
          200:
            description: Vessel Device List
        """
        data = {}

        # GET DATA
        token = request.headers.get('token')
        userid = request.headers.get('userid')
        limit = int(request.args.get('limit'))
        page = int(request.args.get('page'))

        # CHECK TOKEN
        token_validation = self.validate_token(token, userid)

        if not token_validation:
            data['alert'] = "Invalid Token"
            data['status'] = 'Failed'

            # RETURN ALERT
            return self.return_data(data)

        # COUNT
        sql_str = "SELECT COUNT(*) FROM vessel"

        count = self.postgres.query_fetch_one(sql_str)
        total_rows = count['count']

        offset = int((page - 1) * limit)

        # DATA
        sql_str = "SELECT * FROM vessel LIMIT {0} OFFSET {1} ".format(
            limit, offset)
        vessels = self.postgres.query_fetch_all(sql_str)

        rows = []
        if vessels:

            vessel_ids = [x['vessel_id'] for x in vessels]

            for vessel_id in vessel_ids:

                vessel_name = self.get_vessel_name(vessel_id)
                devices = self.get_vessel_devices(vessel_id)

                rows.append({
                    "vessel_id": vessel_id,
                    "vessel_name": vessel_name,
                    "devices": devices
                })

        total_page = int(math.ceil(int(total_rows - 1) / limit)) + 1

        data['data'] = rows
        data['total_page'] = total_page
        data['limit'] = int(limit)
        data['page'] = int(page)
        data['total_rows'] = total_rows
        data['status'] = 'ok'

        return self.return_data(data)

    def get_vessel_name(self, vessel_id):
        """ Return Vessel Name """

        assert vessel_id, "Vessel ID is required."
        values = self.couch_query.get_complete_values(vessel_id, "PARAMETERS")

        if values:
            vessel_name = values['PARAMETERS']['INFO']['VESSELNAME']
        else:

            sql_str = "SELECT vessel_name FROM vessel WHERE vessel_id='{0}'".format(
                vessel_id)
            vname = self.postgres.query_fetch_one(sql_str)
            vessel_name = ""
            if vname:
                vessel_name = vname['vessel_name']

        return vessel_name

    def get_vessel_devices(self, vessel_id):
        """ Return Vessel Devices """
        assert vessel_id, "Vessel ID is required."

        sql_str = "SELECT device_id, device FROM device"
        sql_str += " WHERE vessel_id = '{0}'".format(vessel_id)
        sql_str += " AND device NOT IN ('PARAMETERS', 'COREVALUES',"
        sql_str += " 'FAILOVER', 'NTWCONF', 'NTWPERF1')"

        devices = self.postgres.query_fetch_all(sql_str)

        if devices:
            for device in devices:
                device['image_url'] = self.aws3.get_device_image(
                    vessel_id, device['device_id'])
        return devices
class CalculateOperatorResult(Common):
    """Class for CalculateOperatorResult"""

    # INITIALIZE
    def __init__(self):
        """The Constructor for CalculateOperatorResult class"""
        self.postgres = PostgreSQL()
        self.couch_query = Queries()
        super(CalculateOperatorResult, self).__init__()

    def calculate_operator_result(self, comparator, params):
        """Calculate Operator Result"""

        if len(params) == 1:

            for param in params:
                result = self.compute_operator(param, comparator)

        if len(params) == 2:

            result = self.split_params(params, comparator)

        if len(params) == 3:

            params1 = [params[0], params[1]]
            params2 = [params[0], params[2]]

            if comparator == "BETWEEN":

                param_result1 = self.split_params(params1, ">")
                param_result2 = self.split_params(params2, "<")

            elif comparator == "BETWEENEQ":

                param_result1 = self.split_params(params1, ">=")
                param_result2 = self.split_params(params2, "<=")

            elif comparator == "!BETWEEN":

                param_result1 = self.split_params(params1, "<")
                param_result2 = self.split_params(params2, ">")

            elif comparator == "!BETWEENQ":

                param_result1 = self.split_params(params1, "<=")
                param_result2 = self.split_params(params2, ">=")

            param_results = [param_result1, param_result2]

            result = self.split_params(param_results, "AND")

        return result

    def check_param_content(self, params):
        """Check Parameter Content"""

        temp_data = []
        for param in params:

            if any("datas" in x for x in param) or any("result" in x
                                                       for x in param):
                indicator = "datas"
            else:
                indicator = "input"

            temp_data.append({"indicator": indicator, "data": param})

        return temp_data

    def split_params(self, params, comparator):
        """Split Parameters"""
        result = []

        param_content = self.check_param_content(params)

        if param_content[0]['indicator'] == "input" and param_content[1][
                'indicator'] == "datas":

            swap = True
            result = self.analyze_param(params[1], params[0], comparator, swap)

        elif param_content[0]['indicator'] == "input" and param_content[1][
                'indicator'] == "input":

            if not 'No Data to Show' in params:

                for param1 in params[0]:
                    for param2 in params[1]:
                        results = self.compute_operator(
                            param1, comparator, param2)

                result.append({
                    "value": results,
                    "remarks": self.get_remarks(results)
                })

        else:
            swap = False
            result = self.analyze_param(params[0], params[1], comparator, swap)

        return result

    def analyze_param(self, param1, param2, comparator, swap=None):
        """Analyze Parameter"""

        results = []

        for param in param1:

            parameter1 = param['datas']

            core = param['core']
            previous_core = param['previous']
            vessel_name = self.check_vessel_name(param['vessel_id'])
            vessel_data = self.check_vessel_number(param['vessel_id'])

            if len(core) > 0:

                if param['message'] == "ok":

                    parameter2 = ""

                    if any("datas" in x for x in param2):

                        indicator = "multiple"

                        for par2 in param2:

                            if param['vessel_id'] == par2['vessel_id']:
                                if len(par2['datas']) != 0:
                                    parameter2 = par2['datas']
                    else:

                        indicator = "none"
                        parameter2 = param2

                    if not parameter2:

                        continue

                    parameter = [parameter1, parameter2]

                    # CHECK DEVICE IF FAILOVER
                    if param['device'].upper() == "FAILOVER":

                        result = self.compute_failover_param(parameter,
                                                             comparator,
                                                             swap,
                                                             flag=indicator)
                    else:
                        result = self.compute_param(parameter,
                                                    comparator,
                                                    core,
                                                    previous_core,
                                                    swap,
                                                    flag=indicator)

                    datas = sorted(result, key=lambda i: i["timestamp"])

                else:

                    datas = [{
                        "timestamp": previous_core['timestamp'],
                        "value": previous_core['remarks'],
                        "remarks": self.get_remarks(param['message'])
                    }]

                results.append({
                    "vessel_id": param['vessel_id'],
                    "vessel_name": vessel_name,
                    "vessel_number": vessel_data,
                    "device": param['device'],
                    "device_id": param['device_id'],
                    "core": param['core'],
                    "previous": previous_core,
                    "message": param['message'],
                    "module": param['module'],
                    "option": param['option'],
                    "datas": datas
                })
            else:

                datas = [{
                    "timestamp": previous_core['timestamp'],
                    "value": previous_core['remarks'],
                    "remarks": self.get_remarks(previous_core['remarks'])
                }]

                results.append({
                    "vessel_id": param['vessel_id'],
                    "vessel_name": vessel_name,
                    "vessel_number": vessel_data,
                    "device": param['device'],
                    "device_id": param['device_id'],
                    "core": param['core'],
                    "previous": previous_core,
                    "message": previous_core['remarks'],
                    "module": param['module'],
                    "option": param['option'],
                    "datas": datas
                })

        return results

    def compute_param(self,
                      params,
                      comparator,
                      cores,
                      prevcore,
                      swap=None,
                      flag=None):
        """Compute Parameter"""

        results = []

        param1 = params[0]
        param2 = params[1]

        for core in cores:

            if not param1:

                res = ""

                if prevcore['remarks'] in ['Unknown', 'ok']:
                    remarks = "Unknown"

                else:
                    remarks = "No Data"

                results.append({
                    "timestamp": core,
                    "value": res,
                    "remarks": self.get_remarks(remarks)
                })

            else:

                parameter1 = [param['timestamp'] for param in param1]

                try:
                    check_param1 = parameter1.index(core)

                    timestamp = param1[check_param1]['timestamp']
                    val1 = param1[check_param1]['value']

                    if flag == "multiple":

                        parameter2 = [p['timestamp'] for p in param2]

                        try:
                            check_param2 = parameter2.index(core)
                            val2 = param2[check_param2]['value']

                            if swap is True:
                                res = self.compute_operator(
                                    val2, comparator, val1)
                            else:
                                res = self.compute_operator(
                                    val1, comparator, val2)

                        except ValueError:
                            res = "Unknown"

                    else:
                        if not param2:
                            res = "Unknown"

                        else:

                            for par2 in param2:

                                val2 = self.check_data_type(par2)

                                if swap is True:
                                    res = self.compute_operator(
                                        val2, comparator, val1)
                                else:
                                    res = self.compute_operator(
                                        val1, comparator, val2)

                    results.append({
                        "timestamp": timestamp,
                        "value": res,
                        "remarks": self.get_remarks(res)
                    })

                except ValueError:

                    res = "Unknown"
                    results.append({
                        "timestamp": core,
                        "value": res,
                        "remarks": "Unknown"
                    })

        return results

    def get_remarks(self, data):
        """Get Remarks"""

        if data is True:

            return "red"

        if data is False:

            return "green"

        if data == "Unknown":

            return "orange"

        if data == "No Data":

            return "blue"

        return "violet"

    def check_data_type(self, data):
        """Check Data Type"""

        try:

            if data in ['True', 'False']:
                formatted = eval(data)

            elif data.lstrip("-").isdigit():
                formatted = float(int(data))

            else:
                formatted = float(data)

            return formatted

        except ValueError:

            return data

    def check_vessel_name(self, vessel_id):
        """Check Vessel Name"""

        if vessel_id:
            vessel_name = self.couch_query.get_complete_values(
                vessel_id, "PARAMETERS")
            if vessel_name:
                return vessel_name['PARAMETERS']['INFO']['VESSELNAME']

            return "Vessel Not Found"

        return 0

    def check_vessel_number(self, vessel_id):
        """Check Vessel Number"""

        data = self.couch_query.get_by_id(vessel_id)

        if data:

            if "error" in data:

                return "No Data Found"

            return data['number']
        return "No Data Found"

    def compute_failover_param(self, params, comparator, swap=None, flag=None):
        """Compute Failover Parameter"""

        results = []

        param1 = params[0]
        param2 = params[1]

        for param in param1:
            timestamp = param['timestamp']

            if param['value'] is None:
                res = "Unknown"
            else:
                val1 = self.check_data_type(param['value'])

                if flag == "multiple":

                    parameter2 = [p['timestamp'] for p in param2]

                    try:
                        check_param2 = parameter2.index(param)
                        val2 = self.check_data_type(
                            param2[check_param2]['value'])

                        if swap is True:
                            res = self.compute_operator(val2, comparator, val1)
                        else:
                            res = self.compute_operator(val1, comparator, val2)

                    except ValueError:
                        res = "Unknown"

                else:

                    if not param2:
                        res = "Unknown"

                    else:

                        for par2 in param2:

                            val2 = self.check_data_type(par2)

                            if swap is True:
                                res = self.compute_operator(
                                    val2, comparator, val1)
                            else:
                                res = self.compute_operator(
                                    val1, comparator, val2)

            results.append({
                "timestamp": timestamp,
                "value": res,
                "remarks": self.get_remarks(res)
            })

        return results
Beispiel #12
0
class Upload(Common):
    """Class for Vessels"""

    # INITIALIZE
    def __init__(self):
        """The Constructor for Vessel Upload class"""
        self.couch_query = Queries()
        self.postgres = PostgreSQL()
        self.aws3 = AwsS3()

        super(Upload, self).__init__()

    # GET VESSEL FUNCTION
    def file_upload(self):
        """
        This API is for Uploading Vessel File
        ---
        tags:
          - Vessel
        produces:
          - application/json
        parameters:
          - name: token
            in: header
            description: Token
            required: true
            type: string
          - name: userid
            in: header
            description: User ID
            required: true
            type: string
          - name: vessel_id
            in: query
            description: Vessel ID
            required: true
            type: string
        responses:
          500:
            description: Error
          200:
            description: Vessel File Upload
        """
        # INIT DATA
        data = {}

        # VESSEL ID
        vessel_id = request.args.get('vessel_id')

        # # GET DATA
        token = request.headers.get('token')
        userid = request.headers.get('userid')

        # CHECK TOKEN
        token_validation = self.validate_token(token, userid)

        if not token_validation:
            data["alert"] = "Invalid Token"
            data['status'] = 'Failed'

            # RETURN ALERT
            return self.return_data(data)

        # RH_<VesselIMO>_<ImageID>
        parameters = self.couch_query.get_complete_values(
            vessel_id,
            "PARAMETERS"
        )

        # VESSEL IMO
        vessel_imo = parameters['PARAMETERS']['INFO']['IMO']

        file_upload = []
        filenames = request.files.getlist('upfile')
        for filename in filenames:

            try:

                file_name = filename.filename
                # ext = file_name.split(".")[-1]

                # if not self.allowed_file_type(file_name):

                #     data["alert"] = "File Type Not Allowed!"
                #     data['status'] = 'Failed'
                #     return self.return_data(data)

            except ImportError:

                data["alert"] = "No image!"
                data['status'] = 'Failed'

                # RETURN ALERT
                return self.return_data(data)

            file_name = self.rename_file(vessel_id, file_name)

            vimg_data = {}
            vimg_data['vessel_id'] = vessel_id
            vimg_data['vessel_imo'] = vessel_imo
            vimg_data['file_name'] = file_name
            vimg_data['status'] = "active"
            vimg_data['created_on'] = time.time()

            # ADD FILE TO VESSEL FILE TABLE
            self.postgres.insert('vessel_file', vimg_data, 'vessel_file_id')

            # FILE NAME
            # file_name_upload = str(vessel_file_id) + "." + ext
            # upload_file = 'VesselFiles/' + "RH_" + vessel_imo + "_" + file_name_upload
            upload_file = 'VesselFiles/' + vessel_imo +"/" + file_name
            body = request.files['upfile']

            # SAVE TO S3
            url = ""
            if self.aws3.save_file(upload_file, body):
                url = self.aws3.get_url(upload_file)

            file_upload.append({
                "filename": file_name,
                "url": url
                })

        data["status"] = "ok"
        data["data"] = file_upload

        # RETURN
        return self.return_data(data)

    def allowed_file_type(self, filename):
        """ Check Allowed File Extension """

        allowed_extensions = set(['txt', 'pdf'])

        return '.' in filename and filename.rsplit('.', 1)[1].lower() in allowed_extensions

    def rename_file(self, vessel_id, filename):
        """ Rename File """
        sql_str = "SELECT * FROM vessel_file"
        sql_str += " WHERE vessel_id='{0}'".format(vessel_id)
        sql_str += " AND file_name='{0}'".format(filename)

        vessel_file = self.postgres.query_fetch_one(sql_str)

        if vessel_file:
            new_name = self.file_replace(vessel_file['file_name'])

            return self.rename_file(vessel_id, new_name)

        return filename
Beispiel #13
0
class EmailVesselInfo(Common):
    """Class for EmailVesselInfo"""

    # INITIALIZE
    def __init__(self):
        """The Constructor for EmailVesselInfo class"""

        self.postgres = PostgreSQL()
        self.couch_query = Queries()
        self.epoch_default = 26763
        super(EmailVesselInfo, self).__init__()

    def email_vessel_info(self):
        """Email Vessel Information"""

        # DONE EMAIL ID's
        email_ids = self.get_done_email_ids()

        # GET ALL ID's TO EMAIL
        email_schedules = self.get_email_schedule_ids(email_ids)

        for email_schedule in email_schedules or []:

            # SEND EMAIL
            self.email_sender(email_schedule)

        return 1

    def get_done_email_ids(self):
        """Return Done Email IDs"""

        # GET CURRENT UTC DATE
        utc_date = datetime.utcnow().strftime("%Y%m%d")

        # INIT SQL QUERY
        sql_str = "SELECT email_schedule_id FROM email_log"
        sql_str += " WHERE date_today=" + str(utc_date)

        # FETCH ALL
        res = self.postgres.query_fetch_all(sql_str)

        # RETURN
        return res

    def get_email_schedule_ids(self, email_ids):
        """Return Email Schedule IDs"""

        # SET TIME FORMAT
        time_format = '%H:%M:%S'

        # SET GET UTC DATE
        utc_date = datetime.now(tz=pytz.utc)

        # GET UTC TIME
        new_time = utc_date.strftime(time_format)

        # SPLIT TIME
        new_time = new_time.split(":")

        # SET DELTA TIME
        tdel = timedelta(hours=int(new_time[0]),
                         minutes=int(new_time[1]),
                         seconds=int(new_time[2]))

        # GET TOTAL SECONDS
        td_seconds = tdel.total_seconds()

        # INIT SQL QUERY
        cols = 'email_vessel_id, email_schedule_id, schedule'
        sql_str = "SELECT " + cols + " FROM email_schedule"
        sql_str += " WHERE utc_time<= " + str(td_seconds)

        # CHECK IF ANY OLD SEND EMAIL ID's
        if email_ids:
            email_ids = [x['email_schedule_id'] for x in email_ids]
            # CEHCK LENTH OF OLD SEND EMAIL ID's
            if len(email_ids) == 1:

                # ADD CONDITION FOR SQL QUERY
                sql_str += " AND email_schedule_id != " + str(email_ids[0])

            else:

                # ADD CONDITION FOR SQL QUERY
                sql_str += " AND email_schedule_id NOT IN " + str(
                    tuple(email_ids))

        # FETCH ALL
        res = self.postgres.query_fetch_all(sql_str)

        # RETURN
        return res

    def email_sender(self, email_schedule):
        """Email Sender"""

        # INIT SQL QUERY
        cols = 'vessel_id, email'
        sql_str = "SELECT " + cols + " FROM email_vessel"
        sql_str += " WHERE mail_enable=true AND email_vessel_id="
        sql_str += str(email_schedule['email_vessel_id'])

        # FETCH ONE
        res = self.postgres.query_fetch_one(sql_str)

        if res:
            vessel_id = res['vessel_id']

            sql_str = "SELECT * FROM report_temp WHERE vessel_id = '" + str(
                vessel_id) + "'"

            report_temp_res = self.postgres.query_fetch_one(sql_str)

            if report_temp_res:

                report_data = report_temp_res['report_data']
                report_data = report_data.splitlines()

            else:

                report_data = "General Info: \n"
                report_data += """VESSEL: *["PARAMETERS"]["INFO"]["VESSELNAME"]*\n"""
                report_data += "TIME: *TIMESTAMP*\n"
                report_data = report_data.split("\\n")

            pattern = r"\*(.*)\*"

            last_update = self.get_last_update_with_option(vessel_id)
            epoch_time = int(time.time())

            final_data = []

            for line in report_data:
                match = None
                temp_data = {}
                str_line = line
                for match in re.finditer(pattern, line):
                    start_match = match.start()
                    end_match = match.end()

                    pat = r"[a-zA-Z0-9_ ]+"
                    device = re.findall(pat, line[start_match:end_match])

                    if device[0].upper() in ['TIME', 'TIMESTAMP']:

                        pass

                    else:

                        temp_data['label'] = str_line.split(":")[0]

                        values = self.couch_query.get_complete_values(
                            vessel_id, device[0])
                        if values:
                            try:

                                temp_data['value'] = values[device[0]][
                                    device[1]][device[2]]

                            except:

                                temp_data['value'] = ""

                            final_data.append(temp_data)

                        else:

                            temp_data['value'] = ""
                            final_data.append(temp_data)

                if str_line.split(":")[0].upper() == 'STATUS':

                    status = "Offline"
                    if self.check_time_lapse(epoch_time,
                                             last_update) == "green":
                        status = "Online"

                    temp_data['label'] = 'STATUS'
                    temp_data['value'] = status
                    final_data.append(temp_data)

                elif match is None:

                    temp_data['label'] = str_line.split(":")[0]
                    temp_data['value'] = ""
                    final_data.append(temp_data)

            temp_message = copy.deepcopy(final_data)

            utc = datetime.utcfromtimestamp(last_update).strftime(
                '%A, %d %B %Y %I:%M:%S %p')
            last_date = utc + " UTC"

            values = self.couch_query.get_complete_values(
                vessel_id, "PARAMETERS")

            email = res['email']
            email_temp = VesselReport()
            emailer = Email()

            message = email_temp.vessel_report_temp(final_data, last_date)
            subject = "Scheduled Reporting Event - " + values['PARAMETERS'][
                'INFO']['VESSELNAME']

            emailer.send_email(email, message, subject)
            date_today = datetime.utcnow().strftime("%Y%m%d")

            # INIT NEW VESSEL EMAIL
            temp = {}
            temp['email_schedule_id'] = email_schedule['email_schedule_id']
            temp['email_vessel_id'] = email_schedule['email_vessel_id']
            temp['message'] = json.dumps(temp_message)
            temp['data_date'] = last_date
            temp['date_today'] = date_today
            temp['update_on'] = time.time()
            temp['created_on'] = time.time()

            # INSERT NEW VESSEL EMAIL
            self.postgres.insert('email_log', temp)

        else:

            return 0

        return 1

    # LATE UPDATE WITH OPTION
    def get_last_update_with_option(self, vessel_id):
        """Return Last Updated with Option"""

        values = self.couch_query.get_complete_values(vessel_id,
                                                      "COREVALUES",
                                                      flag='one_doc')

        if values:
            return values['timestamp']
        return 0

    def get_device(self, devices, pattern):
        """Return Device"""

        data = []
        for device in devices:

            if re.findall(r'' + pattern + r'\d', device['doc']['device']):
                data.append(device['doc'])

        data = sorted(data, key=lambda i: i['device'])

        return data
Beispiel #14
0
class Device(Common):
    """Class for Device"""

    # INITIALIZE
    def __init__(self):
        """The Constructor for Device class"""
        # INIT CONFIG
        self.config = ConfigParser()

        # CONFIG FILE
        self.config.read("config/config.cfg")

        self._couch_db = CouchDatabase()
        self.couch_query = Queries()
        self.aws3 = AwsS3()
        self.epoch_default = 26763
        self.db_host = config_section_parser(self.config, "COUCHDB")['host']
        self.db_port = config_section_parser(self.config, "COUCHDB")['port']
        super(Device, self).__init__()

    # GET VESSEL FUNCTION
    def device_list(self):
        """
        This API is for Getting Vessel Device List
        ---
        tags:
          - Devices
        produces:
          - application/json
        parameters:
          - name: token
            in: header
            description: Token
            required: true
            type: string
          - name: userid
            in: header
            description: User ID
            required: true
            type: string
          - name: vessel_id
            in: query
            description: Vessel ID
            required: true
            type: string
          - name: device_id
            in: query
            description: Device ID
            required: false
            type: string
          - name: date
            in: query
            description: Epoch date
            required: false
            type: string
        responses:
          500:
            description: Error
          200:
            description: Vessel Device List
        """
        # VESSEL ID
        vessel_id = request.args.get('vessel_id')
        device_id = request.args.get('device_id')
        epoch_date = request.args.get('date')

        # dbs = []
        # GET DATA
        token = request.headers.get('token')
        userid = request.headers.get('userid')
        device_list = []

        # CHECK TOKEN
        token_validation = self.validate_token(token, userid)

        if not token_validation:
            data = {}
            data["alert"] = "Invalid Token"
            data['status'] = 'Failed'

            # RETURN ALERT
            return self.return_data(data)

        if not vessel_id:
            data = {}
            data["alert"] = "No Vessel ID"
            data['status'] = 'Failed'

            # RETURN ALERT
            return self.return_data(data)

        if device_id:

            value = self.couch_query.get_by_id(device_id)
            device_list.append(value)

        else:

            device_list = self.couch_query.get_device(vessel_id)

        if epoch_date in ["null", "0", "NaN"]:
            epoch_date = False

        # REMOVE DATA
        data_to_remove = ['PARAMETERS', 'COREVALUES', 'FAILOVER', 'NTWCONF']
        device_list = self.remove_data(device_list, data_to_remove)

        device_info = {}

        parameters = self.couch_query.get_complete_values(
            vessel_id, "PARAMETERS")

        final_data = []
        for device in device_list:

            current_time = time.time()
            device_type = parameters['PARAMETERS'][device['device']]['TYPE']
            row = {}
            vsat_antenna = {}
            data_id = ""

            if device_type in ['Catalyst_2960', 'Catalyst_3750']:

                device_port, general, all_data, data_id = self.get_switch(
                    vessel_id, device['device'], epoch_date)

                row['ports'] = device_port
                row['general'] = general
                row['device'] = device['device']
                row['description'] = ""

                if 'DESCRIPTION' in parameters['PARAMETERS'][
                        device['device']].keys():
                    row['description'] = parameters['PARAMETERS'][
                        device['device']]['DESCRIPTION']

            elif device_type in ['Sentry3', 'Raritan_PX2']:

                outlet_status, all_data, data_id = self.get_power_s(
                    vessel_id, device['device'], epoch_date)

                row['outlet_status'] = outlet_status
                row['description'] = ""

                if 'DESCRIPTION' in parameters['PARAMETERS'][
                        device['device']].keys():
                    row['description'] = parameters['PARAMETERS'][
                        device['device']]['DESCRIPTION']

                gen_data = all_data['value'][device['device']]['General']

                if device_type == 'Sentry3':
                    infeed_load_value = gen_data['infeedLoadValue']
                    infeed_power = gen_data['infeedPower']

                    row['infeed_load_value'] = str(infeed_load_value) + " A"
                    row['infeed_power'] = str(infeed_power) + " W"

                if device_type == 'Raritan_PX2':
                    pdu_inlet_currentval = ""
                    if gen_data['pduInletCurrent']:
                        pdu_inlet_currentval = float(
                            gen_data['pduInletCurrent']) / 10

                    pdu_inlet_current = pdu_inlet_currentval
                    pdu_inlet_voltage = gen_data['pduInletVoltage']
                    pdu_power = ['pduPower']

                    row['pdu_inlet_current'] = pdu_inlet_current
                    row['pdu_line_frequency'] = 'L1'  # pduLineFrequency
                    row['pdu_inlet_voltage'] = pdu_inlet_voltage
                    row['pdu_power'] = pdu_power

                row['device'] = device['device']

            elif device_type == 'IOP':

                iop_info, all_data, data_id = self.get_iop(
                    vessel_id, device['device'], epoch_date)
                row['info'] = iop_info

                row['description'] = ""

                if 'DESCRIPTION' in parameters['PARAMETERS'][
                        device['device']].keys():
                    row['description'] = parameters['PARAMETERS'][
                        device['device']]['DESCRIPTION']

                row['device'] = device['device']

            elif device_type in [
                    'Intellian_V80_E2S', 'Intellian_V100_E2S',
                    'Intellian_V110_E2S', 'Intellian_V80_IARM',
                    'Intellian_V100_IARM', 'Intellian_V110_IARM',
                    'Intellian_V100', 'Intellian_V110', 'Sailor_900'
            ]:

                general, all_data, data_id = self.get_vsat(
                    vessel_id, device['device'], epoch_date)

                row['description'] = ""

                if 'DESCRIPTION' in parameters['PARAMETERS'][
                        device['device']].keys():
                    row['description'] = parameters['PARAMETERS'][
                        device['device']]['DESCRIPTION']

                row['device'] = device['device']
                row['general'] = general

                vsat_antenna = self.get_vsat_azimuth(row['general'],
                                                     device_type)

            elif device_type in ['Evolution_X5', 'Evolution_X7']:

                value, all_data, data_id = self.defaul_data(
                    vessel_id, device['device'])

                row['description'] = ""

                if 'DESCRIPTION' in parameters['PARAMETERS'][
                        device['device']].keys():
                    row['description'] = parameters['PARAMETERS'][
                        device['device']]['DESCRIPTION']

                row['device'] = device['device']
                row['general'] = value[device['device']]['General']

            elif device_type in ['Cisco_SNMP']:

                device_port, general, all_data, data_id = self.get_cis_snmp(
                    vessel_id, device['device'], epoch_date)

                row['ports'] = device_port
                row['general'] = general
                row['device'] = device['device']
                row['description'] = ""

                if 'DESCRIPTION' in parameters['PARAMETERS'][
                        device['device']].keys():
                    row['description'] = parameters['PARAMETERS'][
                        device['device']]['DESCRIPTION']

            elif device_type in ['Sailor_3027C']:

                general, all_data, data_id = self.get_sailors_30xxx(
                    vessel_id, device['device'], epoch_date)

                row['general'] = general
                row['device'] = device['device']
                row['description'] = ""

                if 'DESCRIPTION' in parameters['PARAMETERS'][
                        device['device']].keys():
                    row['description'] = parameters['PARAMETERS'][
                        device['device']]['DESCRIPTION']

            elif device_type in ['Sailor_62xx']:

                general, all_data, data_id = self.get_sailor_62xx(
                    vessel_id, device['device'], epoch_date)

                row['general'] = general
                row['device'] = device['device']
                row['description'] = ""

                if 'DESCRIPTION' in parameters['PARAMETERS'][
                        device['device']].keys():
                    row['description'] = parameters['PARAMETERS'][
                        device['device']]['DESCRIPTION']

            elif device_type in ['Sailor_6103']:

                general, all_data, data_id = self.get_sailor_6103(
                    vessel_id, device['device'], epoch_date)

                row['general'] = general
                row['device'] = device['device']
                row['description'] = ""

                if 'DESCRIPTION' in parameters['PARAMETERS'][
                        device['device']].keys():
                    row['description'] = parameters['PARAMETERS'][
                        device['device']]['DESCRIPTION']

            elif device_type in ['Cobham_500']:

                value, all_data, data_id = self.defaul_data(
                    vessel_id, device['device'])

                row['description'] = ""

                if 'DESCRIPTION' in parameters['PARAMETERS'][
                        device['device']].keys():
                    row['description'] = parameters['PARAMETERS'][
                        device['device']]['DESCRIPTION']

                row['device'] = device['device']
                row['general'] = value[device['device']]['General']

            _, all_data, data_id = self.defaul_data(vessel_id,
                                                    device['device'])

            row['description'] = ""

            if 'DESCRIPTION' in parameters['PARAMETERS'][
                    device['device']].keys():
                row['description'] = parameters['PARAMETERS'][
                    device['device']]['DESCRIPTION']

            row['device'] = device['device']

            if row:

                data_url = str(self.db_host) + ":" + str(self.db_port)
                data_url += "/_utils/#database/vessels_db/" + str(data_id)

                row['status'] = self.check_time_lapse(current_time,
                                                      all_data['timestamp'])
                row['device_type'] = device_type
                row['device_id'] = device['_id']
                row['vessel_id'] = vessel_id
                row['vsat_antenna'] = vsat_antenna
                row['data_url'] = data_url
                row['image_url'] = self.aws3.get_device_image(
                    vessel_id, device['_id'])
                final_data.append(row)

        device_info['status'] = 'ok'
        device_info['data'] = final_data

        return self.return_data(device_info)

    def get_iop(self, vessel_id, device, epoch_date):
        """Return IOP"""

        if epoch_date:

            data = self.couch_query.get_complete_values(
                vessel_id,
                device,
                str(self.epoch_default),
                str(epoch_date),
                flag='one_doc')

        else:

            data = self.couch_query.get_complete_values(vessel_id,
                                                        device,
                                                        flag='one_doc')

        value = data['value']
        data_id = data['_id']

        iop_info = {}
        iop_info['status'] = value[device]['General']['Status']
        iop_info['signal'] = value[device]['General']['Signal']
        iop_info['gps'] = value[device]['General']['GPS']
        iop_info['data'] = value[device]['General']['Data']
        iop_info['handset1'] = value[device]['General']['Handset1']
        iop_info['handset2'] = value[device]['General']['Handset2']
        iop_info['handset3'] = value[device]['General']['Handset3']

        return [iop_info, data, data_id]

    def get_power_s(self, vessel_id, device, epoch_date):
        """Return Power Switch"""

        if epoch_date:

            data = self.couch_query.get_complete_values(
                vessel_id,
                device,
                str(self.epoch_default),
                str(epoch_date),
                flag='one_doc')

        else:

            data = self.couch_query.get_complete_values(vessel_id,
                                                        device,
                                                        flag='one_doc')

        value = data['value']
        data_id = data['_id']
        device_outlets = []

        for i in range(1, 9):
            temp_device_outlets = {}
            temp_device_outlets['name'] = 'outlet' + str(i)

            if ('outlet' + str(i)) in value[device].keys():
                temp_device_outlets['value'] = value[device]['outlet' +
                                                             str(i)]['status']
            else:
                temp_device_outlets['value'] = None

            device_outlets.append(temp_device_outlets)

        return [device_outlets, data, data_id]

    def get_sailor_6103(self, vessel_id, device, epoch_date):
        """Return Sailor 6103"""

        if epoch_date:

            data = self.couch_query.get_complete_values(
                vessel_id,
                device,
                str(self.epoch_default),
                str(epoch_date),
                flag='one_doc')
        else:

            data = self.couch_query.get_complete_values(vessel_id,
                                                        device,
                                                        flag='one_doc')

        value = data['value'][device]
        data_id = data['_id']
        del value['General']

        gen = {}

        for key in value.keys():

            gen[key] = ""
            if not value[key]['IP'] == "0.0.0.0":

                gen[key] = value[key]['IP']

        return [gen, data, data_id]

    def get_sailors_30xxx(self, vessel_id, device, epoch_date):
        """Return Sailor 3027C"""

        if epoch_date:

            data = self.couch_query.get_complete_values(
                vessel_id,
                device,
                str(self.epoch_default),
                str(epoch_date),
                flag='one_doc')
        else:

            data = self.couch_query.get_complete_values(vessel_id,
                                                        device,
                                                        flag='one_doc')

        value = data['value']
        data_id = data['_id']

        lat_dir = value[device]['General']['latitudeDirection']
        lon_dir = value[device]['General']['longitudeDirection']
        lat = value[device]['General']['latitude']
        lon = value[device]['General']['longitude']

        lon_val = ''.join(lon.rsplit('.', 1))

        if lon_dir == 'W':

            lon_val = "-" + lon_val

        lat_val = ''.join(lat.rsplit('.', 1))

        if lat_dir == 'S':

            lat_val = "-" + lat_val

        gen = {}
        gen['currentProtocol'] = value[device]['General']['currentProtocol']
        gen['currentOceanRegion'] = value[device]['General'][
            'currentOceanRegion']
        gen['latitude'] = self.position_converter(lat_val)
        gen['longitude'] = self.position_converter(lon_val)
        gen['latitudeDirection'] = lat_dir
        gen['longitudeDirection'] = lon_dir

        return [gen, data, data_id]

    def position_converter(self, val):
        """ POSITION CONVERTER """

        absolute = abs(float(val))
        degrees = math.floor(absolute)
        minutes_not_truncated = (absolute - degrees) * 60
        minutes = math.floor(minutes_not_truncated)
        seconds = math.floor((minutes_not_truncated - minutes) * 60)

        return str(degrees) + "." + str(minutes) + "." + str(seconds)

    def get_sailor_62xx(self, vessel_id, device, epoch_date):
        """Return Sailor 62xx"""

        if epoch_date:

            data = self.couch_query.get_complete_values(
                vessel_id,
                device,
                str(self.epoch_default),
                str(epoch_date),
                flag='one_doc')
        else:

            data = self.couch_query.get_complete_values(vessel_id,
                                                        device,
                                                        flag='one_doc')

        value = data['value']
        data_id = data['_id']

        gen = {}
        gen['currentChDesc'] = value[device]['General']['currentChDesc']
        gen['currentCh'] = value[device]['General']['currentCh']
        gen['transmitMode'] = value[device]['General']['transmitMode']

        return [gen, data, data_id]

    def get_switch(self, vessel_id, device, epoch_date):
        """Return Switch"""

        if epoch_date:

            data = self.couch_query.get_complete_values(
                vessel_id,
                device,
                str(self.epoch_default),
                str(epoch_date),
                flag='one_doc')
        else:

            data = self.couch_query.get_complete_values(vessel_id,
                                                        device,
                                                        flag='one_doc')

        value = data['value']
        data_id = data['_id']
        device_port = []
        keys = value[device].keys()
        for i in range(1, (len(keys))):
            temp_device_port = {}
            port = 'Fa0/' + str(i)
            if port in keys:

                temp_device_port['name'] = port
                temp_device_port['value'] = value[device][port]['status']
                device_port.append(temp_device_port)

        for i in range(1, 3):
            temp_device_port = {}
            port = 'Gi0/' + str(i)
            if port in keys:

                temp_device_port['name'] = port
                temp_device_port['value'] = value[device][port]['status']
                device_port.append(temp_device_port)

        gen = {}
        gen['port'] = value[device]['General']['port']
        gen['serial'] = value[device]['General']['SerialNumber']
        gen['mac'] = value[device]['General']['MacAddress']
        gen['model'] = value[device]['General']['ModelNumber']

        return [device_port, gen, data, data_id]

    def get_cis_snmp(self, vessel_id, device, epoch_date):
        """Return Switch"""

        if epoch_date:

            data = self.couch_query.get_complete_values(
                vessel_id,
                device,
                str(self.epoch_default),
                str(epoch_date),
                flag='one_doc')
        else:

            data = self.couch_query.get_complete_values(vessel_id,
                                                        device,
                                                        flag='one_doc')

        value = data['value']
        data_id = data['_id']
        device_port = []
        keys = value[device].keys()

        for i in range(1, (len(keys))):
            temp_device_port = {}
            port = 'fa' + str(i)
            if port in keys:

                temp_device_port['name'] = port
                temp_device_port['value'] = value[device][port]['ifOperStatus']
                device_port.append(temp_device_port)

        for i in range(1, (len(keys))):
            temp_device_port = {}
            port = 'gi' + str(i)
            if port in keys:

                temp_device_port['name'] = port
                temp_device_port['value'] = value[device][port]['ifOperStatus']
                device_port.append(temp_device_port)

        gen = {}
        for gen_key in value[device]['General'].keys():

            gen[gen_key] = value[device]['General'][gen_key]

        return [device_port, gen, data, data_id]

    def get_vsat(self, vessel_id, device, epoch_date):
        """Return VSAT"""

        if epoch_date:

            data = self.couch_query.get_complete_values(
                vessel_id,
                device,
                str(self.epoch_default),
                str(epoch_date),
                flag='one_doc')

        else:

            data = self.couch_query.get_complete_values(vessel_id,
                                                        device,
                                                        flag='one_doc')

        value = data['value']
        data_id = data['_id']

        return [value[device]['General'], data, data_id]

    def defaul_data(self, vessel_id, device):
        """Return Default Data"""

        data = self.couch_query.get_complete_values(vessel_id,
                                                    device,
                                                    flag='one_doc')
        value = data['value']
        data_id = data['_id']

        return [value, data, data_id]

    def get_vsat_azimuth(self, general, device_type):
        """Return VSAT Azimuth and Elevation Value"""

        data = {}
        if device_type in [
                'Intellian_V80_E2S', 'Intellian_V100_E2S',
                'Intellian_V110_E2S', 'Intellian_V80_IARM',
                'Intellian_V100_IARM', 'Intellian_V110_IARM', 'Intellian_V100',
                'Intellian_V110', 'Sailor_900'
        ]:

            azimuth = []
            elev = []

            heading = None
            target_azimuth = None
            relative_az = None
            abosulte_az = None

            if 'Heading' in general:
                heading = general['Heading']

            if 'TargetAzimuth' in general:
                target_azimuth = general['TargetAzimuth']

            if 'RelativeAz' in general:
                relative_az = general['RelativeAz']

            if 'AbsoluteAz' in general:
                abosulte_az = general['AbsoluteAz']

            azimuth.append({
                "Heading": heading,
                "TargetAzimuth": target_azimuth,
                "RelativeAz": relative_az,
                "AbsoluteAz": abosulte_az
            })

            elevation = None
            target_elevation = None

            if 'Elevation' in general:
                elevation = general['Elevation']

            if 'TargetElevation' in general:
                target_elevation = general['TargetElevation']

            elev.append({
                "Elevation": elevation,
                "TargetElevation": target_elevation
            })

            data['azimuth'] = azimuth
            data['elevation'] = elev

        return data
Beispiel #15
0
class Upload(Common):
    """Class for Device Upload"""

    # INITIALIZE
    def __init__(self):
        """The Constructor for Device Upload class"""
        self.couch_query = Queries()
        self.postgres = PostgreSQL()
        self.aws3 = AwsS3()

        super(Upload, self).__init__()

    # GET VESSEL FUNCTION
    def device_upload(self):
        """
        This API is for Device Image Upload
        ---
        tags:
          - Devices
        produces:
          - application/json
        parameters:
          - name: token
            in: header
            description: Token
            required: true
            type: string
          - name: userid
            in: header
            description: User ID
            required: true
            type: string
          - name: vessel_id
            in: query
            description: Vessel ID
            required: true
          - name: device_id
            in: query
            description: Device ID
            required: true
            type: string
          - name: upfile
            in: formData
            description: Device image
            required: true
            type: file
        consumes:
          - multipart/form-data
        responses:
          500:
            description: Error
          200:
            description: Device Image Upload
        """
        # INIT DATA
        data = {}

        # VESSEL ID
        vessel_id = request.args.get('vessel_id')
        device_id = request.args.get('device_id')

        # # GET DATA
        token = request.headers.get('token')
        userid = request.headers.get('userid')

        # CHECK TOKEN
        token_validation = self.validate_token(token, userid)

        if not token_validation:
            data["alert"] = "Invalid Token"
            data['status'] = 'Failed'

            # RETURN ALERT
            return self.return_data(data)

        # RH_<VesselIMO>_<ImageID>

        parameters = self.couch_query.get_complete_values(
            vessel_id, "PARAMETERS")

        # VESSEL IMO
        vessel_imo = vessel_id
        if parameters:
            vessel_imo = parameters['PARAMETERS']['INFO']['IMO']

        try:

            filename = request.files['upfile'].filename
            ext = filename.split(".")[-1]

            if not self.allowed_image_type(filename):

                data["alert"] = "File Type Not Allowed!"
                data['status'] = 'Failed'
                return self.return_data(data)

        except ImportError:
            data["alert"] = "No image!"
            data['status'] = 'Failed'

            # RETURN ALERT
            return self.return_data(data)

        filename = self.rename_device_image(vessel_id, device_id, filename)

        dimg_data = {}
        dimg_data['vessel_id'] = vessel_id
        dimg_data['device_id'] = device_id
        dimg_data['vessel_imo'] = vessel_imo
        dimg_data['image_name'] = filename
        dimg_data['status'] = "active"
        dimg_data['created_on'] = time.time()

        # VERIFY IF IMAGE EXISTS FOR DEVICE
        sql_str = "SELECT * FROM device_image"
        sql_str += " WHERE vessel_id='{0}'".format(vessel_id)
        sql_str += " AND device_id='{0}'".format(device_id)
        sql_str += " AND status = 'active'"

        device = self.postgres.query_fetch_one(sql_str)

        if device:

            # INIT CONDITION
            conditions = []

            # CONDITION FOR QUERY
            conditions.append({
                "col": "device_image_id",
                "con": "=",
                "val": device['device_image_id']
            })

            update_column = {}
            update_column['status'] = "inactive"

            self.postgres.update('device_image', update_column, conditions)

        device_image_id = self.postgres.insert('device_image', dimg_data,
                                               'device_image_id')

        # IMAGE FILE NAME
        file_name = vessel_imo + "_" + str(device_id) + str(
            device_image_id) + "." + ext
        img_file = 'Device/' + "RH_" + str(file_name)
        body = request.files['upfile']

        # SAVE TO S3
        image_url = ""
        if self.aws3.save_file(img_file, body):
            image_url = self.aws3.get_url(img_file)

        data["status"] = "ok"
        data["image_url"] = image_url

        # RETURN
        return self.return_data(data)

    def allowed_image_type(self, filename):
        """ Check Allowed File Extension """

        allowed_extensions = set(['png', 'jpg', 'jpeg', 'gif'])

        return '.' in filename and filename.rsplit(
            '.', 1)[1].lower() in allowed_extensions

    def rename_device_image(self, vessel_id, device_id, filename):
        """Rename Image"""

        sql_str = "SELECT * FROM device_image"
        sql_str += " WHERE vessel_id='{0}'".format(vessel_id)
        sql_str += " AND device_id='{0}'".format(device_id)
        sql_str += " AND image_name='{0}'".format(filename)

        device_image = self.postgres.query_fetch_one(sql_str)

        if device_image:
            new_name = self.file_replace(device_image['image_name'])

            return self.rename_device_image(vessel_id, device_id, new_name)

        return filename
Beispiel #16
0
class UpdateDeviceState(Common):
    """Class for UpdateDeviceState"""

    # INITIALIZE
    def __init__(self):
        """The Constructor for UpdateDeviceState class"""

        self._couch_db = CouchDatabase()
        self.couch_query = Queries()
        self.postgres = PostgreSQL()
        super(UpdateDeviceState, self).__init__()

    def update_device_state(self):
        """Update Device State"""

        devices = self.couch_query.get_devices()
        device_ids = [x['_id'] for x in devices]

        # INIT SQL QUERY
        sql_str = "SELECT * FROM device"

        # FETCH ALL
        rows = self.postgres.query_fetch_all(sql_str)

        db_device_ids = [x['device_id'] for x in rows]

        device_ids = set(device_ids)
        db_device_ids = set(db_device_ids)

        device_ids.difference_update(db_device_ids)

        if device_ids:

            d_ids = list(device_ids)

            for device in devices:

                if device['_id'] in d_ids:

                    parameters = self.couch_query.get_complete_values(
                        device['vessel_id'],
                        "PARAMETERS"
                    )

                    if device['device'] in ['COREVALUES', 'FAILOVER', 'PARAMETERS', 'NTWCONF']:
                        device_type = device['device']
                    else:
                        device_type = parameters['PARAMETERS'][device['device']]['TYPE']

                    data = {}
                    data['device_id'] = device['_id']
                    data['device'] = device['device']
                    data['device_type'] = device_type
                    data['vessel_id'] = device['vessel_id']
                    data['update_on'] = time.time()
                    data['created_on'] = time.time()

                    self.postgres.insert('device', data)

        # RETURN
        return rows
Beispiel #17
0
class RemoteCommand(Common):
    """Class for RemoteCommand"""

    # INITIALIZE
    def __init__(self):
        """The Constructor for RemoteCommand class"""
        # INIT CONFIG
        self.config = ConfigParser()

        # CONFIG FILE
        self.config.read("config/config.cfg")

        self._couch_db = CouchDatabase()
        self.couch_query = Queries()
        self.postgres = PostgreSQL()
        self.epoch_default = 26763
        super(RemoteCommand, self).__init__()

    # GET VESSEL FUNCTION
    def remote_command(self):
        """
        This API is for Remote Command
        ---
        tags:
          - Remote Command
        produces:
          - application/json
        parameters:
          - name: token
            in: header
            description: Token
            required: true
            type: string
          - name: userid
            in: header
            description: User ID
            required: true
            type: string
          - name: vessel_id
            in: query
            description: Vessel ID
            required: true
            type: string
          - name: device_id
            in: query
            description: Device ID
            required: false
            type: string

        responses:
          500:
            description: Error
          200:
            description: Remote Command
        """
        # GET DATA
        token = request.headers.get('token')
        userid = request.headers.get('userid')

        # CHECK TOKEN
        token_validation = self.validate_token(token, userid)

        if not token_validation:
            data = {}
            data["alert"] = "Invalid Token"
            data['status'] = 'Failed'

            # RETURN ALERT
            return self.return_data(data)

        vessel_id = request.args.get('vessel_id')
        device_id = request.args.get('device_id')
        device_info = {}
        all_devices = self.couch_query.get_all_devices(vessel_id)

        if device_id:

            for dev in all_devices:

                if dev['id'] == device_id:

                    device_info = dev

                    break

        parameters = self.couch_query.get_complete_values(vessel_id,
                                                          "PARAMETERS",
                                                          flag='one_doc')

        ntwconf = self.couch_query.get_complete_values(vessel_id,
                                                       "NTWCONF",
                                                       flag='one_doc')
        # COREVALUES, FAILOVER, NTWCONF
        final_data = []

        if device_id and not device_info:
            pass

        elif device_id and device_info:

            if device_info['doc']['device'] == "COREVALUES":
                system = self.get_system()
                final_data.append(system)

            if device_info['doc']['device'] == "FAILOVER":
                failover = self.get_failover(parameters)
                final_data.append(failover)

            if device_info['doc']['device'] == "NTWCONF":
                netconf = self.get_nc(parameters, ntwconf)
                final_data.append(netconf)

            devices = self.get_item([device_info], 'MODEM')
            for device in devices:

                modem = self.get_modem(vessel_id, device)
                final_data.append(modem)

            devices = self.get_item([device_info], 'POWERSWITCH')
            for device in devices:

                power = self.get_power_switch(vessel_id, device)
                final_data.append(power)

        else:

            system = self.get_system()
            final_data.append(system)

            failover = self.get_failover(parameters)
            final_data.append(failover)

            netconf = self.get_nc(parameters, ntwconf)
            final_data.append(netconf)

            devices = self.get_item(all_devices, 'MODEM')
            for device in devices:

                modem = self.get_modem(vessel_id, device)
                final_data.append(modem)

            devices = self.get_item(all_devices, 'POWERSWITCH')
            for device in devices:

                power = self.get_power_switch(vessel_id, device)
                final_data.append(power)

        if not device_id:
            tun1 = ''
            if 'tun1' in ntwconf['value']['NTWCONF'].keys():

                tun1 = ntwconf['value']['NTWCONF']['tun1']['IP']

            ssh_opt = []
            opt = {}
            opt['description'] = "SSH to Vessel"
            opt['option'] = "ssh://" + tun1

            ssh_opt.append(opt)

            ssh = {}
            ssh['icon'] = "CodeIcon"
            ssh['label'] = "SSH"
            ssh['options'] = ssh_opt
            final_data.append(ssh)

        ret = {}
        ret['data'] = final_data
        ret['status'] = "ok"

        return self.return_data(ret)

    def get_system(self):
        """ SYSTEM / CORE VALUES """

        system = {}
        system["label"] = "System"
        system["icon"] = "Apps"
        system["options"] = []

        option = {}
        option["option"] = "Load-average"
        option[
            "description"] = 'Gets the number of processor cores and load-average.'
        system["options"].append(option)

        option = {}
        option["option"] = "Uptime"
        option[
            "description"] = 'Get the uptime of the system, aswell as the start-up timestamp.'
        system["options"].append(option)

        option = {}
        option["option"] = "Free RAM and Swap"
        option["description"] = 'Show the available/used RAM and swap size'
        system["options"].append(option)

        option = {}
        option["option"] = "Hardware info"
        option[
            "description"] = 'Show hardware info (like serial-number, product name, ...)'
        system["options"].append(option)

        option = {}
        option["option"] = "Memory info"
        option["description"] = 'Show memory info'
        system["options"].append(option)

        option = {}
        option["option"] = "Disk space"
        option["description"] = 'Check used/available disk space'
        system["options"].append(option)

        option = {}
        option["option"] = "Debian version"
        option["description"] = 'Get debian and kernel version'
        system["options"].append(option)

        option = {}
        option["option"] = "PWD"
        option["description"] = 'Get working directory'
        system["options"].append(option)

        option = {}
        option["option"] = "Initiate program cycle"
        option["description"] = 'Initiate a new program cycle now.'
        system["options"].append(option)

        option = {}
        option["option"] = "Date/Time"
        option["description"] = 'Show Date & Time'
        system["options"].append(option)

        option = {}
        option["option"] = "Temporary file server"
        option["others"] = []

        # OTHERS
        other = {}
        other["name"] = "File-server shuts down after..."
        other["selected"] = 60
        other["type"] = "dropdown"
        other["values"] = []

        value = {}
        value["1 Minute"] = 60
        other["values"].append(value)

        value = {}
        value["2 Minute"] = 120
        other["values"].append(value)

        value = {}
        value["5 Minute"] = 300
        other["values"].append(value)

        option["others"].append(other)

        # OTHERS
        other = {}
        other["name"] = "Directory"
        other["selected"] = "/home/rh/backendv1/log"
        other["type"] = "dropdown"
        other["values"] = []

        value = {}
        value["Log Files"] = "/home/rh/backendv1/log"
        other["values"].append(value)

        value = {}
        value["Ini File"] = "/home/rh/backendv1/ini"
        other["values"].append(value)

        option["others"].append(other)

        option["description"] = 'Opens a temporary file-server'
        system["options"].append(option)

        return system

    def get_failover(self, parameters):
        """ FAILOVER """

        failover = {}
        failover["label"] = "Failover"
        failover["icon"] = "ErrorIcon"
        failover["options"] = []

        option = {}
        option["option"] = "Reset all gateways"
        option["description"] = 'Reset all gateways'
        failover["options"].append(option)

        option = {}
        option["option"] = "Failover rule"
        option["description"] = 'Change failover rule.'
        option["others"] = []

        # OTHERS
        other = {}
        other["name"] = "Rule"
        other["type"] = "dropdown"
        other["selected"] = "allow"
        other["values"] = []

        value = {}
        value["Allow: Can be used as gateway/internet provider"] = "allow"
        other["values"].append(value)

        value = {}
        value[
            "Force: Will be set as highest priority above anything else."] = "force"
        other["values"].append(value)

        value = {}
        value["Forbid: Will be ignored as internet provider"] = "forbid"
        other["values"].append(value)

        value = {}
        value[
            "Reset: Reset to default (allow) or configured setting in the ini-files."] = "reset"
        other["values"].append(value)

        option["others"].append(other)

        # OTHERS
        other = {}
        other["name"] = "Failover"
        other["hint"] = "If current rule is not shown for the list items, "
        other[
            "hint"] += "the interface should be currently be defaulted to allowed."
        other[
            "hint"] += "<br><small>Keep in mind that the current rule state only gets "
        other["hint"] += "updated every 10 minutes (default value).</small>"
        other["type"] = "dropdown"
        other["values"] = []

        parameter_rows = parameters['value']['PARAMETERS'].copy()

        failover_array = self.get_parameters_value(parameter_rows,
                                                   r'FAILOVER\d')
        failover_array = sorted(failover_array, key=lambda i: i[0])
        for f_arr in failover_array:

            value = {}
            value["{0} - {1} - {2}".format(f_arr[1]['INTERFACE'],
                                           f_arr[1]['DESCRIPTION'],
                                           f_arr[1]['GATEWAY'])] = f_arr[0]
            other["values"].append(value)

        if failover_array:
            f_arr = failover_array[0]
            other["selected"] = "{0}".format(f_arr[0])

        option["others"].append(other)

        failover["options"].append(option)

        return failover

    def get_nc(self, parameters, ntwconf):
        """ NETWORK CONFIGURATION """

        parameter_rows = parameters['value']['PARAMETERS'].copy()

        netconf = {}
        netconf["label"] = "Network Configuration"
        netconf["icon"] = "NetworkCheck"
        netconf["options"] = []

        option = {}
        option["option"] = "Ping"
        option["description"] = "Ping an IP"
        option["others"] = []

        # OTHERS
        other = {}
        other["name"] = "Number of pings"
        other["type"] = "dropdown"
        other["selected"] = 1
        other["values"] = []

        value = {}
        value['1'] = 1
        other["values"].append(value)

        value = {}
        value['2'] = 2
        other["values"].append(value)

        value = {}
        value['3'] = 3
        other["values"].append(value)

        value = {}
        value['5'] = 5
        other["values"].append(value)

        value = {}
        value['10'] = 10
        other["values"].append(value)

        option["others"].append(other)

        # OTHERS
        other = {}
        other["name"] = "Destination IP"
        other["type"] = "input"

        option["others"].append(other)

        # OTHERS
        other = {}
        other["name"] = "Interface"
        other["type"] = "groupdropdown"
        other["selected"] = ""
        other["values"] = []

        value = {}
        value['Not defined'] = [\
            {"value": "",
             "label": "No specific interface (default)"}
                               ]
        other["values"].append(value)

        ntwconf_rows = ntwconf['value']['NTWCONF'].copy()

        ntwconf_array = self.get_parameters_value(ntwconf_rows, r'enp\w*')
        ntwconf_array = sorted(ntwconf_array, key=lambda i: i[0])

        value = {}
        value["Untagged"] = []
        for ntconf in ntwconf_array:
            value["Untagged"].append({
                "value":
                '-I ' + ntconf[0],
                "label":
                "{0}, {1}, {2}".format(ntconf[0], ntconf[1]['IP'],
                                       ntconf[1]['Netmask'])
            })

        other["values"].append(value)

        ntwconf_array = self.get_parameters_value(ntwconf_rows, r'tun\d')
        ntwconf_array = sorted(ntwconf_array, key=lambda i: i[0])

        value = {}
        value["Tunnels (VPN)"] = []
        for ntconf in ntwconf_array:
            value["Tunnels (VPN)"].append({
                "value":
                '-I ' + ntconf[0],
                "label":
                "{0}, {1}, {2}".format(ntconf[0], ntconf[1]['IP'],
                                       ntconf[1]['Netmask'])
            })

        other["values"].append(value)

        ntwconf_array = self.get_parameters_value(parameter_rows, r'VLAN\d')
        ntwconf_array = sorted(ntwconf_array, key=lambda i: i[0])

        value = {}
        value["VLAN's"] = []
        for ntconf in ntwconf_array:

            description = ""
            if 'DESCRIPTION' in ntconf[1].keys():
                description = ntconf[1]['DESCRIPTION']

            value["VLAN's"].append({
                "value":
                '-I' + ntconf[0],
                "label":
                "{0}, {1}, {2}".format(ntconf[0], description, ntconf[1]['IP'])
            })

        other["values"].append(value)

        option["others"].append(other)

        netconf["options"].append(option)

        option = {}
        option["option"] = "Interfaces"
        option[
            "description"] = "Check network-interfaces of OBU, including IP-addresses."
        option["others"] = []

        # OTHERS
        other = {}
        other["name"] = "Method"
        other["type"] = "dropdown"
        other["selected"] = 'nmap --iflist'
        other["values"] = []

        value = {}
        value["Nmap: Including routes"] = 'nmap --iflist'
        other["values"].append(value)

        value = {}
        value["Ip: New debian method"] = 'ip a'
        other["values"].append(value)

        value = {}
        value["Ifconfig: Old debian method"] = '/sbin/ifconfig'
        other["values"].append(value)

        option["others"].append(other)

        netconf["options"].append(option)

        option = {}
        option["option"] = "Routes"
        option["description"] = "Show routes, including gateways and metrics"
        option["others"] = []

        # OTHERS
        other = {}
        other["name"] = "Method"
        other["type"] = "dropdown"
        other["selected"] = 'ip route'
        other["values"] = []

        value = {}
        value["IP Route"] = 'ip route'
        other["values"].append(value)

        value = {}
        value["Route"] = '/sbin/route'
        other["values"].append(value)

        value = {}
        value["Nmap: Including interfaces"] = 'nmap --iflist'
        other["values"].append(value)

        value = {}
        value["Netstat: Kernel IP Routing table"] = 'netstat -r'
        other["values"].append(value)

        option["others"].append(other)

        netconf["options"].append(option)

        option = {}
        option["option"] = "Connections"
        option["description"] = "Show internet connections"
        option["others"] = []

        # OTHERS
        other = {}
        other["name"] = "Method"
        other["type"] = "dropdown"
        other["selected"] = 'netstat -tp'
        other["values"] = []

        value = {}
        value["Active internet connections"] = 'netstat -tp'
        other["values"].append(value)

        option["others"].append(other)

        netconf["options"].append(option)

        option = {}
        option["option"] = "Network statistics"
        option["description"] = "Show network statistics"
        option["others"] = []

        # OTHERS
        other = {}
        other["name"] = "Method"
        other["type"] = "dropdown"
        other["selected"] = 'netstat -i'
        other["values"] = []

        value = {}
        value["Netstat: Network statistics"] = 'netstat -i'
        other["values"].append(value)

        option["others"].append(other)

        netconf["options"].append(option)

        option = {}
        option["option"] = "Scan host"
        option["description"] = "Scan a host."
        option["others"] = []

        # OTHERS
        other = {}
        other["name"] = "Host to scan"
        other["type"] = "input"
        other[
            "hint"] = "Type IP-address (e.g. 192.168.0.1), can also have a subnet to"
        other["hint"] = " scan whole subnet (e.g. 192.168.0.1/24)"
        option["others"].append(other)

        # OTHERS
        other = {}
        other["name"] = "Please select an option:"
        other["type"] = "dropdown"
        other["selected"] = '-F'
        other["values"] = []

        value = {}
        value["Fast scan"] = '-F'
        other["values"].append(value)

        value = {}
        value['Normal'] = ""
        other["values"].append(value)

        value = {}
        value["Ping scan"] = '-sP'
        other["values"].append(value)

        value = {}
        value["More output"] = '-v'
        other["values"].append(value)

        value = {}
        value["Even more output"] = '-vv'
        other["values"].append(value)

        option["others"].append(other)

        netconf["options"].append(option)

        return netconf

    def get_modem(self, vessel_id, device):
        """ MODEM """

        modem = {}
        modem["label"] = device['device']
        modem["icon"] = "Router"
        modem["options"] = []

        option = {}
        option["option"] = "Reboot"
        option["description"] = "Reboot device"
        modem["options"].append(option)

        option = {}
        option["option"] = "Beam Lock"
        option["description"] = "Lock modem satellite"
        modem["options"].append(option)

        option = {}
        option["option"] = "Beam Switch"
        option["description"] = "Switch to another VSAT-Beam."
        option["others"] = []

        # OTHERS
        other = {}
        other["name"] = "Please select the beam:"
        other["type"] = "dropdown"
        other["values"] = []

        values = self.couch_query.get_complete_values(vessel_id,
                                                      device['device'],
                                                      flag='one_doc')

        general = values['value'][device['device']]['General']

        selected_flag = False

        for gen in general.keys():

            if re.findall(r'Beam\[\d*\]', str(gen)):

                num = gen[(gen.index('[') + 1):gen.index(']')]

                opt = str(general[gen]) + "(" + num + ")"

                value = {}
                value[opt] = num
                other["values"].append(value)

                if not selected_flag:

                    other["selected"] = num

                    selected_flag = True

        option["others"].append(other)

        modem["options"].append(option)

        option = {}
        option["option"] = "New Map"
        option["description"] = "Load new map for modem"
        modem["options"].append(option)

        option = {}
        option["option"] = "Remove Map"
        option["description"] = "Remove map for modem'"
        modem["options"].append(option)

        return modem

    def get_power_switch(self, vessel_id, device):
        """ POWERSWITCH """

        power = {}
        power["label"] = device['device']
        power["icon"] = "PowerInput"
        power["options"] = []

        option = {}
        option["option"] = "Reboot Outlet"
        option["description"] = "Reboot outlet"
        option["others"] = []

        # OTHERS
        other = {}
        other["name"] = "Outlet number"
        other["type"] = "dropdown"
        other["values"] = []

        values = self.couch_query.get_complete_values(vessel_id,
                                                      device['device'],
                                                      flag='one_doc')

        outlets = list(values['value'][device['device']].keys())

        outlets.remove('General')
        outlets.sort()

        selected_flag = False

        if outlets:

            for outlet in outlets:

                if re.findall(r'outlet\d', outlet):

                    num = int(re.search(r'\d+', outlet).group())
                    val = {}
                    # value[str(num)] = values['value'][device['device']][outlet]['name']
                    val[str(num) + ' - ' + values['value'][device['device']]
                        [outlet]['name']] = num
                    other["values"].append(val)

                    if not selected_flag:

                        other["selected"] = num

                        selected_flag = True

        option["others"].append(other)

        power["options"].append(option)

        return power

    def get_item(self, devices, pattern):
        """Return Item"""

        data = []
        for device in devices:

            if re.findall(r'' + pattern + r'\d', device['doc']['device']):
                data.append(device['doc'])

        data = sorted(data, key=lambda i: i['device'])

        return data

    def get_parameters_value(self, parameters, pattern):
        """Return Parameters Value"""

        values = []

        for key in parameters.keys():

            if re.findall(r'' + pattern, key):

                values.append([key, parameters[key]])

        return values
Beispiel #18
0
class AlarmValues(Common):
    """Class for AlarmValues"""
    def __str__(self):
        """ test """

    # INITIALIZE
    def __init__(self):
        """The Constructor for AlarmValues class"""

        self.postgres = PostgreSQL()
        self.couch_query = Queries()
        super(AlarmValues, self).__init__()

    def alarm_values(self):
        """
        This API is for Getting Alarm Value
        ---
        tags:
          - Alarm Values
        produces:
          - application/json
        parameters:
          - name: token
            in: header
            description: Token
            required: true
            type: string
          - name: userid
            in: header
            description: User ID
            required: true
            type: string
          - name: limit
            in: query
            description: Limit
            required: true
            type: integer
          - name: page
            in: query
            description: Page
            required: true
            type: integer
          - name: sort_type
            in: query
            description: Sort Type
            required: false
            type: string
          - name: sort_column
            in: query
            description: Sort Column
            required: false
            type: string
          - name: filter_column
            in: query
            description: Filter Column
            required: false
            type: string
          - name: filter_value
            in: query
            description: Filter Value
            required: false
            type: string
        responses:
          500:
            description: Error
          200:
            description: Alarm Value
        """
        data = {}

        # GET DATA
        token = request.headers.get('token')
        userid = request.headers.get('userid')
        page = int(request.args.get('page'))
        limit = int(request.args.get('limit'))
        sort_type = request.args.get('sort_type')
        sort_column = request.args.get('sort_column')
        filter_column = request.args.get('filter_column')
        filter_value = request.args.get('filter_value')

        # CHECK TOKEN
        token_validation = self.validate_token(token, userid)

        if not token_validation:
            data["alert"] = "Invalid Token"
            data['status'] = 'Failed'

            # RETURN ALERT
            return self.return_data(data)

        if filter_column and filter_value:
            filter_list = []
            for col in filter_column.split(","):
                filter_list.append(col)

            filter_val = []
            for val in filter_value.split(","):
                filter_val.append(val)

            filter_value = tuple(filter_val)

        else:
            filter_list = filter_column

        data['data'] = []
        total_page = 0

        #GET DATA
        datas = self.get_alarm_values()

        for dta in datas['rows']:
            vessels = dta['vessel']
            vessel_number = self.get_alarm_vessel_name(vessels)

            if not vessel_number:
                dta['vessel_name'] = ''
            else:
                dta['vessel_name'] = ", ".join(vessel_number)
        if datas:

            rows = self.param_filter(datas['rows'],
                                     filter_value,
                                     checklist=filter_list)

            if sort_type and sort_column:
                sort_column = sort_column.lower()
                if sort_type.lower() == "desc" and sort_column in [
                        'name', 'vessel', 'device', 'device_type', 'module',
                        'option', 'value'
                ]:
                    rows = sorted(rows,
                                  key=lambda i: i[sort_column],
                                  reverse=True)

                if sort_type.lower() == "asc":
                    rows = sorted(rows, key=lambda i: i[sort_column])
        else:
            rows = []

        datas = self.limits(rows, limit, page)

        #datas = self.get_alarm_vessel_name(datas)

        total_count = len(rows)
        total_page = int(math.ceil(int(total_count - 1) / limit))
        data = self.common_return(datas, total_page, limit, page, total_count)
        data['status'] = 'ok'

        return self.return_data(data)

    def get_alarm_values(self):
        """Return Alarm Values"""

        # DATA
        sql_str = "SELECT * FROM alarm_value"

        datas = self.postgres.query_fetch_all(sql_str)

        data = {}
        data['rows'] = datas

        return data

    # GET ALARM VALUE
    def get_alarm_value(self, alarm_value_id):
        """Return Alarm Value"""

        assert alarm_value_id, "Alarm Value ID is required."

        # DATA
        alarm_value = []
        sql_str = "SELECT * FROM alarm_value WHERE alarm_value_id={0}".format(
            alarm_value_id)
        alarm_value = self.postgres.query_fetch_one(sql_str)

        data = {}
        data['rows'] = alarm_value

        return data

    def get_alarm_vessel_name(self, vessels):
        """Return Alarm Vessel Name"""

        vessel_number = []

        if vessels:
            for vessel in vessels.split(","):

                values = self.couch_query.get_complete_values(
                    vessel, "PARAMETERS")

                if values:
                    vessel_name = values['PARAMETERS']['INFO']['VESSELNAME']

                else:
                    vessel_name = "Vessel Not Found"

                vessel_number.append(vessel_name)
            return vessel_number

        return 0
Beispiel #19
0
class Vessels(Common):
    """Class for Vessels"""

    # INITIALIZE
    def __init__(self):
        """The Constructor for Vessels class"""
        self._couch_db = CouchDatabase()
        self.couch_query = Queries()
        self.postgres = PostgreSQL()
        self.epoch_default = 26763
        self.vessel_name = ""
        self.aws3 = AwsS3()

        # INIT CONFIG
        self.config = ConfigParser()

        # CONFIG FILE
        self.config.read("config/config.cfg")

        self.vpn_db_build = config_section_parser(self.config,
                                                  "VPNDB")['build']

        super(Vessels, self).__init__()

    # GET VESSEL FUNCTION
    def all_vessels(self):
        """
        This API is for Getting Vessels
        ---
        tags:
          - Vessel
        produces:
          - application/json
        parameters:
          - name: token
            in: header
            description: Token
            required: true
            type: string
          - name: userid
            in: header
            description: User ID
            required: true
            type: string
          - name: vessel_number
            in: query
            description: Vessel Number
            required: false
            type: string
          - name: limit
            in: query
            description: Limit
            required: true
            type: integer
          - name: page
            in: query
            description: Page
            required: true
            type: integer
          - name: sort_type
            in: query
            description: Sort Type
            required: false
            type: string
          - name: sort_column
            in: query
            description: Sort Column
            required: false
            type: string
          - name: filter_column
            in: query
            description: Filter Column
            required: false
            type: string
          - name: filter_value
            in: query
            description: Filter Value
            required: false
            type: string
        responses:
          500:
            description: Error
          200:
            description: Vessel Information
        """
        # INIT DATA
        data = {}

        # VESSEL ID
        vessel_number = request.args.get('vessel_number')
        limit = int(request.args.get('limit'))
        page = int(request.args.get('page'))
        sort_type = request.args.get('sort_type')
        sort_column = request.args.get('sort_column')
        filter_column = request.args.get('filter_column')
        filter_value = request.args.get('filter_value')
        vessels = []
        # GET DATA
        token = request.headers.get('token')
        userid = request.headers.get('userid')

        # CHECK TOKEN
        token_validation = self.validate_token(token, userid)

        if not token_validation:
            data["alert"] = "Invalid Token"
            data['status'] = 'Failed'

            # RETURN ALERT
            return self.return_data(data)

        sql_str = "SELECT * FROM vessel "
        if not vessel_number:
            # COUCH QUERY - GET VESSELS
            offset = int((page - 1) * limit)

            # LIMIT TO ONE COLUMN
            if filter_column and filter_value:
                sql_str += " WHERE {0}='{1}'".format(filter_column,
                                                     filter_value)

            if sort_column and sort_type:
                if sort_column in ['vessel_name']:
                    sql_str += " ORDER BY  {0} {1}".format(
                        sort_column, sort_type.upper())
            else:
                sql_str += " ORDER BY vessel_name ASC"
            sql_str += " LIMIT {0} OFFSET {1}".format(limit, offset)

            vessels = self.postgres.query_fetch_all(sql_str)

        else:
            # COUCH QUERY - GET VESSEL
            sql_str += "WHERE number='{0}'".format(vessel_number)

            vessels = self.postgres.query_fetch_all(sql_str)

        # CHECK DATABASES
        rows = []
        if vessels:

            vessel_ids = [x['vessel_id'] for x in vessels]

            # INIT SQL QUERY
            if len(vessel_ids) == 1:
                sql_str = "SELECT * FROM vessel WHERE state ='1'"
                sql_str += " AND vessel_id IN ('{0}')".format(vessel_ids[0])
            else:
                sql_str = "SELECT * FROM vessel WHERE state ='1'"
                sql_str += " AND vessel_id IN {0}".format(tuple(vessel_ids))

            # FETCH ALL
            vessels_state = self.postgres.query_fetch_all(sql_str)

            to_be_install = [x['vessel_id'] for x in vessels_state]
            # INIT VARIABLES

            # LOOP DATABASES
            for item in vessels:
                row = {}

                # CONNECT TO DATABASE
                # GET VESSEL NAME

                # VESSEL POSIBLE STATE
                # To be installed = 1
                # Installed and etc. = 2

                vessel_name = self.get_vessel_name(item['vessel_id'],
                                                   item['number'])

                self.vessel_name = vessel_name

                mail_enable = self.get_email_schedule(item['vessel_id'])
                last_update = self.get_last_update_with_option(
                    item['vessel_id'])

                epoch_time = int(time.time())

                update_state = 'red'
                if not item['vessel_id'] == item['number']:

                    update_state = self.check_time_lapse(
                        epoch_time, last_update)

                vpn_url = self.get_vpn_url(item['number'])

                version_name, version_id = self.get_version(item['vessel_id'])

                # SET THE RETURN VALUE
                row['vessel_id'] = item['vessel_id']
                row['vessel_rev'] = self.get_rev_id(item['vessel_id'],
                                                    item['number'])
                row['vessel_number'] = item['number']
                row['vessel_name'] = vessel_name
                row['mail_enable'] = mail_enable['mail_enable']
                row['schedules'] = mail_enable['schedules']
                row['emails'] = mail_enable['emails']
                row['update_state'] = update_state
                row['version_name'] = version_name
                row['version_id'] = version_id
                row['vpn_url'] = vpn_url

                # GET IMAGE URL
                row['image_url'] = self.aws3.get_vessel_image(
                    item['vessel_id'])

                if item['vessel_id'] in to_be_install:
                    row['update_state'] = 'white'

                rows.append(row)

        sql_str = "SELECT COUNT(*) FROM vessel"
        count = self.postgres.query_fetch_one(sql_str)
        total_rows = count['count']
        total_page = int(math.ceil(int(total_rows - 1) / limit)) + 1

        # SET RETURN
        data['rows'] = rows
        data['status'] = 'ok'
        data['limit'] = int(limit)
        data['page'] = int(page)
        data['total_rows'] = total_rows
        data['total_page'] = total_page
        data['vessel_names'] = self.get_vessel_names()

        # RETURN
        return self.return_data(data)

    # GET THE VESSEL NAME
    def get_vessel_name(self, vessel_id, number):
        """Return Vessel Name"""

        if vessel_id == number:
            vessel_name = ""
            if self.vpn_db_build.upper() == 'TRUE':

                sql_str = "SELECT vessel_name FROM vessel_vpn_job WHERE imo='{0}' ".format(
                    number)
                sql_str += "AND vpn_type='VESSEL' ORDER BY created_on DESC LIMIT 1"
                vname = self.postgres.query_fetch_one(sql_str)
                vessel_name = vname['vessel_name']

            else:

                sql_str = "SELECT vessel_name FROM vessel WHERE number='{0}'".format(
                    number)
                vname = self.postgres.query_fetch_one(sql_str)
                vessel_name = vname['vessel_name']

            return vessel_name

        values = self.couch_query.get_complete_values(vessel_id, "PARAMETERS")
        if values:
            return values['PARAMETERS']['INFO']['VESSELNAME']
        return ''

    def get_limits(self, option_ids, limit, page):
        """Return Limits"""

        skip = int((page - 1) * limit)

        limit = skip + limit

        return option_ids[skip:limit]

    def get_email_schedule(self, vessel_id):
        """Return Email Schedule"""

        # SQL QUERY
        cols = 'email_vessel_id, mail_enable, email'
        sql_str = "SELECT " + cols + " FROM email_vessel"
        sql_str += " WHERE vessel_id='{}'".format(vessel_id)

        # FETCH ONE
        email_vessels = self.postgres.query_fetch_all(sql_str)

        data = {}
        data['emails'] = []
        data['schedules'] = []
        data['mail_enable'] = False

        if email_vessels:
            mail_enable = email_vessels[0]['mail_enable']
            email_vessel_id = email_vessels[0]['email_vessel_id']

            # SQL QUERY
            cols = 'schedule'
            sql_str = "SELECT " + cols + " FROM email_schedule"
            sql_str += " WHERE email_vessel_id='{}'".format(email_vessel_id)
            schedules = self.postgres.query_fetch_all(sql_str)

            data['emails'] = (list(map(lambda x: x['email'], email_vessels)))
            data['schedules'] = (list(map(lambda x: x['schedule'], schedules)))
            data['mail_enable'] = mail_enable

        return data

    def get_last_update_with_option(self, vessel_id):
        """Return Last Update with Option"""
        values = self.couch_query.get_complete_values(vessel_id,
                                                      "COREVALUES",
                                                      flag='one_doc')

        if values:
            return values['timestamp']
        return ''

    def get_vpn_url(self, vessel_number):
        """ GET VPN URL """

        vpn_url = ""

        sql_str = "SELECT * FROM vessel_vpn_job WHERE"
        sql_str += " imo='{0}' AND status='active'".format(vessel_number)
        job = self.postgres.query_fetch_one(sql_str)

        if job:

            filename = job['directory'].split("/")[-1]
            created_on = job['created_on']
            # vpn_url = "download/vpn/{0}".format(filename)
            vpn_url = "download/vessel/vpn/{0}_{1}".format(
                created_on, filename)

        return vpn_url

    def get_rev_id(self, vessel_id, vessel_number):
        """ GET REV ID """

        if vessel_id == vessel_number:

            return vessel_id

        vessel = self.couch_query.get_vessels(vessel_number=vessel_number)
        if vessel:

            return vessel[0]['doc']['_rev']

        return 0

    def get_vessel_names(self):
        """ RETURN VESSEL NAMES """
        # DATA
        sql_str = "SELECT array_to_json(array_agg(vessel_name)) as vessel FROM vessel"

        data = self.postgres.query_fetch_one(sql_str)
        vessels = []
        if data:
            for vessel in data['vessel']:
                vessels.append({"label": vessel, "value": vessel})

        return vessels

    def get_version(self, vessel_id):
        """ GET VESSEL """

        sql_str = "SELECT version.version_name, version.version_id FROM version INNER JOIN"
        sql_str += " vessel_version ON vessel_version.version_id=version.version_id"
        sql_str += " WHERE vessel_version.vessel_id='{0}'".format(vessel_id)
        version = self.postgres.query_fetch_one(sql_str)

        if version:

            return [version['version_name'], version['version_id']]

        return ["", ""]
Beispiel #20
0
class Vessel(Common):
    """Class for Vessel"""

    # INITIALIZE
    def __init__(self):
        """The Constructor for Vessel class"""
        # self._my_db = MySQLDatabase()
        self._couch_db = CouchDatabase()
        self.couch_query = Queries()
        self.postgres = PostgreSQL()
        self.epoch_default = 26763
        self.aws3 = AwsS3()
        super(Vessel, self).__init__()

        # # INIT CONFIG
        # self.config = ConfigParser.ConfigParser()
        # # CONFIG FILE
        # self.config.read("config/config.cfg")

    # GET VESSEL FUNCTION
    def get_vessels_data(self):
        """
        This API is for Getting Vessel Information
        ---
        tags:
          - Vessel
        produces:
          - application/json
        parameters:
          - name: token
            in: header
            description: Token
            required: true
            type: string
          - name: userid
            in: header
            description: User ID
            required: true
            type: string
          - name: vessel_ids
            in: query
            description: Vessels IDs
            required: false
            type: string

        responses:
          500:
            description: Error
          200:
            description: Vessel Information
        """

        # INIT DATA
        data = {}

        # GET DATA
        token = request.headers.get('token')
        userid = request.headers.get('userid')

        # CHECK TOKEN
        token_validation = self.validate_token(token, userid)

        if not token_validation:
            data["alert"] = "Invalid Token"
            data['status'] = 'Failed'
            # RETURN ALERT
            return self.return_data(data)

        # GET ADMIN ROLE AND SUPER ADMIN ROLE
        sql_str = "SELECT role_id FROM role WHERE role_name in ('admin', 'super admin')"
        l_ids = self.postgres.query_fetch_all(sql_str)
        role_ids = [x['role_id'] for x in l_ids]

        str_ids = ""
        if len(role_ids) == 1:

            str_ids = "(" + str(role_ids[0]) + ")"

        else:

            str_ids = str(tuple(role_ids))

        # CREATE SQL QUERY
        # GET ADMIN ACCOUNT
        sql_str = "SELECT * FROM account_role where account_id='" + str(userid)
        sql_str += "' AND role_id in " + str_ids

        res = self.postgres.query_fetch_one(sql_str)

        # EXTRACT VESSELS ID
        extracted_vessel_ids = request.args.get('vessel_ids')
        vessels = []

        if res:
            if extracted_vessel_ids:

                for vessel_id in extracted_vessel_ids.split(","):
                    res = self.couch_query.get_by_id(vessel_id)
                    if res:
                        ves = {}
                        ves['id'] = res['_id']
                        ves['key'] = res['number']
                        vessels.append(ves)

            else:
                vessels = self.couch_query.get_vessels()
        else:
            vessel_ids = []
            try:
                if not extracted_vessel_ids:  # EMPTY
                    vessel_ids = []
                else:
                    vessel_ids = extracted_vessel_ids.split(",")
            except:
                data["alert"] = "Invalid Vessel IDs"
                data['status'] = 'Failed'
                # RETURN ALERT
                return self.return_data(data)

            company_ids = self.get_company_id(userid)
            if not company_ids:
                data["message"] = "User Doesn't belong to any company."
                data['rows'] = []
                data['status'] = 'ok'
                # RETURN ALERT
                return self.return_data(data)

            # ACCOUNT'S AVAILABLE VESSELS
            company_vessel_ids = self.get_company_vessels(company_ids)['rows']

            #FILTER VESSELS TO GET
            vessels_to_get = []
            if not vessel_ids:
                vessels_to_get = company_vessel_ids
            else:
                for vessel_id in vessel_ids:
                    # found_vessel = None
                    for company_vessel in company_vessel_ids:
                        if vessel_id == company_vessel['vessel_id']:
                            vessels_to_get.append(company_vessel)
                            break
                if len(vessels_to_get) != len(vessel_ids):
                    data["alert"] = "Some Vessel are not belong to this user."
                    data['status'] = 'Failed'
                    # RETURN ALERT
                    return self.return_data(data)

            temp_ids = []

            for vessel_id in vessels_to_get:

                if vessel_id['vessel_id'] not in temp_ids:

                    temp_ids.append(vessel_id['vessel_id'])
                    res = self.couch_query.get_by_id(vessel_id['vessel_id'])

                    if res:
                        ves = {}
                        ves['id'] = res['_id']
                        ves['key'] = res['number']
                        vessels.append(ves)

        # CHECK DATABASES
        rows = []
        if vessels:

            vessel_ids = [x['id'] for x in vessels]

            # INIT SQL QUERY
            if len(vessel_ids) == 1:
                sql_str = "SELECT * FROM vessel WHERE state ='1'"
                sql_str += " AND vessel_id IN ('{0}')".format(vessel_ids[0])
            else:
                sql_str = "SELECT * FROM vessel WHERE state ='1'"
                sql_str += " AND vessel_id IN {0}".format(tuple(vessel_ids))

            # FETCH ALL
            vessels_state = self.postgres.query_fetch_all(sql_str)

            to_be_install = [x['vessel_id'] for x in vessels_state]

            # LOOP DATABASES
            for item in vessels:
                row = {}

                # GET VESSEL NAME
                vessel_name = self.get_vessel_name(item['id'])

                # self.vessel_name = vessel_name

                # GET LONGITUDE AND LATITUDE
                long_lat = self.get_long_lat(item['id'])

                if long_lat:
                    # get nearest land from ocean to land google map
                    # GET the nearest landmass
                    # https://stackoverflow.com/questions/41539432/google-maps-reverse-
                    # geocoding-api-returns-nearest-land-address-given-a-latlng-in

                    row['long'] = long_lat['long']
                    row['lat'] = long_lat['lat']
                    row['position_source'] = long_lat['position_source']
                else:
                    row['long'] = 0
                    row['lat'] = 0
                    row['position_source'] = ""

                # GET HEADING
                heading, speed = self.get_heading(item['id'])
                if heading:
                    row['heading'] = float(heading['heading'])
                    row['heading_source'] = heading['heading_source']
                else:
                    row['heading_source'] = ""
                    row['heading'] = 0

                if speed:
                    row['speed'] = float(speed)

                # GET UPDATE STATE

                last_update = self.get_last_update_with_option(item['id'])
                epoch_time = int(time.time())

                update_state = self.check_time_lapse(epoch_time, last_update)

                # SET THE RETURN VALUE
                row['isOpen'] = False
                row['vessel_id'] = item['id']
                row['vessel_number'] = item['key']
                row['vessel_name'] = vessel_name
                row['company'] = self.get_vessel_company(item['id'])
                row['update_state'] = update_state

                # GET IMAGE URL
                row['image_url'] = self.aws3.get_vessel_image(item['id'])

                if item['id'] in to_be_install:
                    row['update_state'] = 'white'

                rows.append(row)

            # SORT VESSELS ALPHABETICALLY
            rows = sorted(rows, key=lambda i: i['vessel_name'].upper())

        # SET RETURN
        data['rows'] = rows
        data['companies'] = self.get_vessels_company()
        data['status'] = 'ok'

        # RETURN
        return self.return_data(data)

    # GET THE VESSEL NAME
    def get_vessel_name(self, vessel_id):
        """Return Vessel Name"""
        values = self.couch_query.get_complete_values(vessel_id, "PARAMETERS")
        if values:
            return values['PARAMETERS']['INFO']['VESSELNAME']
        return ''

    def get_long_lat(self, vessel_id):
        """Return Longitude Latitude"""

        all_devices = self.couch_query.get_all_devices(vessel_id)
        devices = []
        source = 'MODEM'
        if all_devices:

            devices = self.get_device(all_devices, source)
            devices_info = 0

            if devices:

                devices_info = self.device_data(source, devices, vessel_id)

            if not devices_info:
                source = 'VSAT'
                devices = self.get_device(all_devices, source)

                if devices:

                    devices_info = self.device_data(source, devices, vessel_id)

            if not devices_info:
                source = 'IOP'
                devices = self.get_device(all_devices, source)

                if devices:

                    devices_info = self.device_data(source, devices, vessel_id)

            if not devices_info:
                source = 'NMEA'
                devices = self.get_device(all_devices, source)
                if devices:

                    devices_info = self.device_data(source, devices, vessel_id)

            if not devices_info:
                source = 'SATC'
                devices = self.get_device(all_devices, source)
                if devices:

                    devices_info = self.device_data(source, devices, vessel_id)

            if not devices_info:
                source = 'VHF'
                devices = self.get_device(all_devices, source)
                if devices:

                    devices_info = self.device_data(source, devices, vessel_id)

            return devices_info

        return 0

    def get_device(self, devices, pattern):
        """Return Device"""

        data = []
        for device in devices:

            if re.findall(r'' + pattern + r'\d', device['doc']['device']):
                data.append(device['doc'])

        data = sorted(data, key=lambda i: i['device'])

        return data

    def get_heading(self, vessel_id):
        """Return Heading"""

        all_devices = self.couch_query.get_all_devices(vessel_id)

        source = 'VSAT'
        devices = self.get_device(all_devices, source)

        data = {}
        speed = 0

        for device in devices:

            values = self.couch_query.get_complete_values(
                vessel_id, device['device'])

            if values:

                # GET DEVICE NUMBER
                number = device['device'].split(source)[1]

                # GET DEVICE COMPLETE NAME
                heading_source = self.device_complete_name(source, number)

                # SET RETURN
                data['heading'] = values[
                    device['device']]['General']['Heading']
                data['heading_source'] = heading_source

                # RETURN
                # return [data, 0]

        source = 'NMEA'
        devices = self.get_device(all_devices, source)

        for device in devices:

            values = self.couch_query.get_complete_values(
                vessel_id, device['device'])

            if values:

                # GET DEVICE NUMBER
                number = device['device'].split(source)[1]

                # GET DEVICE COMPLETE NAME
                heading_source = self.device_complete_name(source, number)
                gen = values[device['device']].keys()
                if 'GP0001' in gen:

                    gp0001 = values[device['device']]['GP0001']
                    # SET RETURN

                    if not data:

                        data['heading'] = gp0001['VTG'][
                            'courseOverGroundTrueDegrees']
                        data['heading_source'] = heading_source

                    speed = gp0001['VTG']['speedOverGroundKnots']
                    # RETURN
        return [data, speed]

    def get_last_update_with_option(self, vessel_id):
        """Return Last Update with option"""

        values = self.couch_query.get_complete_values(vessel_id,
                                                      "COREVALUES",
                                                      flag='one_doc')

        if values:
            return values['timestamp']
        return ''

    def get_company_id(self, userid):
        """Return Company ID"""

        sql_str = "select company_id from account_company where account_id = {}".format(
            userid)

        res = self.postgres.query_fetch_all(sql_str)

        return res if res else 0

    # GET VESSELS OF COMPANY
    def get_company_vessels(self, company_ids):
        """Return Company Vessels"""

        assert company_ids, "CompanyID is required."

        # DATA
        vessels = []
        for company_id in company_ids:
            sql_str = "SELECT * FROM company_vessels"
            sql_str += " WHERE company_id={0}".format(company_id['company_id'])
            res = self.postgres.query_fetch_all(sql_str)
            if res:
                vessels = vessels + res

        data = {}
        data['rows'] = vessels

        return data

    def device_data(self, source, devices, vessel_id):
        """Return Device Data"""

        for device in devices:

            values = self.couch_query.get_complete_values(
                vessel_id, device['device'])

            if values:
                try:
                    # GET DEVICE NUMBER
                    number = device['device'].split(source)[1]

                    # GET DEVICE COMPLETE NAME
                    position_source = self.device_complete_name(source, number)

                    # SET RETURN
                    data = {}

                    if source == 'NMEA':
                        lnp = values[
                            device['device']]['GP0001']['GGA']['eastWest']
                        ltp = values[
                            device['device']]['GP0001']['GGA']['northSouth']

                        ln1 = values[
                            device['device']]['GP0001']['GGA']['longitude']
                        lt1 = values[
                            device['device']]['GP0001']['GGA']['latitude']
                        longitude = float(ln1)
                        lat = float(lt1)

                        lat /= 100.
                        dec_lat, degrees_lat = math.modf(lat)
                        dec_lat *= 100.
                        dev_lat = degrees_lat + dec_lat / 60

                        longitude /= 100.
                        dec_long, degrees_long = math.modf(longitude)
                        dec_long *= 100.
                        dev_long = degrees_long + dec_long / 60

                        data['lat'] = "%.4f" % float(dev_lat)
                        data['long'] = "%.4f" % float(dev_long)

                        if ltp == 'S':

                            data['lat'] = "-" + str(data['lat'])

                        if lnp == 'W':

                            data['long'] = "-" + str(data['long'])

                    elif source == 'SATC':

                        lat_dir = values[
                            device['device']]['General']['latitudeDirection']
                        lon_dir = values[
                            device['device']]['General']['longitudeDirection']

                        lat = values[device['device']]['General']['latitude']
                        lon = values[device['device']]['General']['longitude']

                        lon_val = ''.join(lon.rsplit('.', 1))

                        if lon_dir == 'W':

                            lon_val = "-" + lon_val

                        lat_val = ''.join(lat.rsplit('.', 1))

                        if lat_dir == 'S':

                            lat_val = "-" + lat_val

                        data['long'] = lon_val
                        data['lat'] = lat_val

                    elif source == 'VHF':

                        lat = values[device['device']]['General']['latitude']
                        lon = values[device['device']]['General']['longitude']

                        lon_val = str(lon[1:])
                        lon_val = lon_val.replace('.', '')
                        lon_val = lon_val.replace(',', '.')

                        if lon[0] == 'W':

                            lon_val = "-" + lon_val

                        lat_val = str(lat[1:])
                        lat_val = lat_val.replace('.', '')
                        lat_val = lat_val.replace(',', '.')

                        if lat[0] == 'S':

                            lat_val = "-" + lat_val

                        data['long'] = lon_val
                        data['lat'] = lat_val

                    else:

                        data['long'] = values[
                            device['device']]['General']['Longitude']
                        data['lat'] = values[
                            device['device']]['General']['Latitude']

                    data['position_source'] = position_source

                    # RETURN
                    return data

                except:

                    continue

        return 0

    def get_vessel_company(self, vessel_id):
        """Return Company by Vessel ID"""

        assert vessel_id, "Vessel ID is required."

        data = []
        sql_str = "SELECT * FROM company_vessels cv"
        sql_str += " LEFT JOIN company c ON cv.company_id = c.company_id"
        sql_str += " WHERE vessel_id='{0}'".format(vessel_id)
        result = self.postgres.query_fetch_all(sql_str)

        if result:
            for res in result:
                data.append({
                    'company_id': res['company_id'],
                    'company_name': res['company_name']
                })

        return data

    def get_vessels_company(self):
        """ Return Vessel List by Company """

        sql_str = "SELECT v.vessel_id, v.vessel_name, cv.company_id, c.company_name FROM vessel v"
        sql_str += " LEFT JOIN company_vessels cv ON v.vessel_id=cv.vessel_id"
        sql_str += " LEFT JOIN company c ON cv.company_id = c.company_id"
        sql_str += " GROUP BY v.vessel_id, cv.company_id,c.company_id"
        sql_str += " ORDER BY c.company_name"
        results = self.postgres.query_fetch_all(sql_str)

        data = {}
        if results:
            for result in results:
                if result['company_name'] is None:
                    company = "Others"
                else:
                    company = result['company_name']

                formatted = [{
                    'vessel_id': result['vessel_id'],
                    'vessel_name': result['vessel_name']
                }]

                if company in data:
                    data[company].append(formatted)
                else:
                    data[company] = formatted

        return data
Beispiel #21
0
class UpdateUser(Common):
    """Class for UpdateUser"""

    # INITIALIZE
    def __init__(self):
        """The Constructor for UpdateUser class"""

        # INIT CONFIG
        self.config = ConfigParser()

        # CONFIG FILE
        self.config.read("config/config.cfg")

        self._couch_db = CouchDatabase()
        self.couch_query = Queries()
        self.postgres = PostgreSQL()

        self.vpn_db_build = config_section_parser(self.config,
                                                  "VPNDB")['build']
        super(UpdateUser, self).__init__()

        if self.vpn_db_build.upper() == 'TRUE':
            self.my_ip = config_section_parser(self.config, "IPS")['my']
            self.my_protocol = config_section_parser(self.config,
                                                     "IPS")['my_protocol']

            self.user_vpn = config_section_parser(self.config,
                                                  "IPS")['user_vpn']
            self.user_protocol = config_section_parser(self.config,
                                                       "IPS")['user_protocol']

            self.vessel_vpn = config_section_parser(self.config,
                                                    "IPS")['vessel_vpn']
            self.vessel_protocol = config_section_parser(
                self.config, "IPS")['vessel_protocol']

            self.vpn_token = '269c2c3706886d94aeefd6e7f7130ab08346590533d4c5b24ccaea9baa5211ec'

    def update_user(self):
        """
        This API is for Updating User
        ---
        tags:
          - User
        produces:
          - application/json
        parameters:
          - name: token
            in: header
            description: Token
            required: true
            type: string
          - name: userid
            in: header
            description: User ID
            required: true
            type: string
          - name: query
            in: body
            description: Updating User
            required: true
            schema:
              id: Updating User
              properties:
                account_id:
                    type: string
                username:
                    type: string
                password:
                    type: string
                status:
                    type: boolean
                first_name:
                    type: string
                last_name:
                    type: string
                middle_name:
                    type: string
                url:
                    type: string
                email:
                    type: string
                companies:
                    types: array
                    example: []
                roles:
                    types: array
                    example: []
                vessels:
                    types: array
                    example: []
        responses:
          500:
            description: Error
          200:
            description: Updating User
        """
        data = {}

        # GET JSON REQUEST
        query_json = request.get_json(force=True)

        # GET HEADER
        token = request.headers.get('token')
        userid = request.headers.get('userid')

        # CHECK TOKEN
        token_validation = self.validate_token(token, userid)

        if not token_validation:
            data["alert"] = "Invalid Token"
            data['status'] = 'Failed'

            # RETURN ALERT
            return self.return_data(data)

        if not self.update_users(query_json):

            data["alert"] = "Please check your query!"
            data['status'] = 'Failed'

            # RETURN ALERT
            return self.return_data(data)

        data['message'] = "User successfully updated!"
        data['status'] = "ok"
        return self.return_data(data)

    def update_users(self, query_json):
        """Update Users"""

        # GET CURRENT TIME
        query_json['update_on'] = time.time()

        account_id = query_json['account_id']

        # GET SUPER ADMIN ACCOUNT
        sa_role = self.get_sa_role(account_id)

        superadmin = self.get_super_admin()

        roles = []
        companies = []
        vessels = []

        if 'roles' in query_json.keys():
            roles = query_json['roles']

        if 'companies' in query_json.keys():
            companies = query_json['companies']

        if 'vessels' in query_json.keys():
            vessels = query_json['vessels']

        status = self.get_user_status(account_id)

        if status and self.vpn_db_build.upper() == 'TRUE':

            # Client -> Super admin
            if superadmin['role_id'] in roles and not sa_role:
                self.delete_vpn_allow(account_id)
                self.create_vpn(account_id, 'super admin')

            # Super admin -> Client
            elif not superadmin['role_id'] in roles and sa_role:
                self.delete_vpn_allow(account_id, 'super admin')
                self.create_vpn(account_id)

        # UPDATE
        # INIT CONDITION
        conditions = []

        # CONDITION FOR QUERY
        conditions.append({"col": "id", "con": "=", "val": account_id})

        # REMOVE KEYS
        query_json = self.remove_key(query_json, "account_id")
        query_json = self.remove_key(query_json, "roles")
        query_json = self.remove_key(query_json, "companies")
        query_json = self.remove_key(query_json, "vessels")

        # UPDATE ROLE
        if self.postgres.update('account', query_json, conditions):

            # INIT CONDITION
            conditions = []

            # CONDITION FOR QUERY
            conditions.append({
                "col": "account_id",
                "con": "=",
                "val": account_id
            })

            # ROLES
            if roles:

                # DELETE OLD PERMISSION
                self.postgres.delete('account_role', conditions)

                # LOOP NEW PERMISSIONS
                for role_id in roles:

                    # INIT NEW PERMISSION
                    temp = {}
                    temp['account_id'] = account_id
                    temp['role_id'] = role_id

                    # INSERT NEW PERMISSION OF ROLE
                    self.postgres.insert('account_role', temp)

            # COMPANIES
            if companies:

                # DELETE OLD COMPANY
                self.postgres.delete('account_company', conditions)

                # LOOP NEW COMPANIES
                for company_id in companies:

                    # INIT NEW COMPANY
                    temp = {}
                    temp['account_id'] = account_id
                    temp['company_id'] = company_id

                    # INSERT NEW COMPANY
                    self.postgres.insert('account_company', temp)

            # VESSELS
            if vessels:

                # DELETE OLD VESSELS
                self.postgres.delete('account_vessel', conditions)

                # REMOVE VNP ACCESS ON VESSEL FLAG
                vpn_acces = True

                # LOOP NEW VESSELS
                for vessel in vessels:

                    ntwconf = self.couch_query.get_complete_values(
                        vessel['vessel_id'], "NTWCONF")
                    # INIT NEW VESSEL
                    temp = {}
                    temp['vessel_vpn_ip'] = ntwconf['NTWCONF']['tun1']['IP']
                    temp['account_id'] = account_id
                    temp['vessel_id'] = vessel['vessel_id']
                    temp['allow_access'] = vessel['allow_access']
                    temp['vessel_vpn_state'] = 'pending'

                    # INSERT NEW ACCOUNT VESSEL
                    self.postgres.insert('account_vessel', temp)

                    if not vessel['allow_access']:
                        vpn_acces = False

                if status and self.vpn_db_build.upper() == 'TRUE':

                    if not vpn_acces or sa_role:

                        callback_url = self.my_protocol + "://" + self.my_ip + "/vpn/update"
                        data_url = self.my_protocol + "://" + self.my_ip + "/vpn/data"

                        vpn_type = 'VCLIENT'

                        # INSERT JOB
                        job_id = self.insert_job(callback_url, data_url,
                                                 self.vpn_token, account_id,
                                                 self.vessel_vpn, 'REMOVE',
                                                 vpn_type)

                        # INIT PARAMS FOR REMOVE VPN
                        vpn_params = {}
                        vpn_params['callback_url'] = callback_url
                        vpn_params['data_url'] = data_url
                        vpn_params['job_id'] = job_id

                        self.remove_vpn_ip(vpn_params, self.vpn_token, True)

            # RETURN
            return 1

        # RETURN
        return 0

    def remove_vpn_ip(self, data, vpn_token, flag=False):
        """Remove VPN IP"""

        api_endpoint = self.user_protocol + "://" + self.user_vpn + "/ovpn"

        if flag:

            api_endpoint = self.vessel_protocol + "://" + self.vessel_vpn + "/ovpn"

        headers = {'content-type': 'application/json', 'token': vpn_token}

        req = requests.post(api_endpoint,
                            data=json.dumps(data),
                            headers=headers)
        res = req.json()

        return res

    def insert_job(self, callback_url, data_url, vpn_token, account_id,
                   user_vpn, action, vpn_type):
        """Insert Job"""

        update_on = time.time()

        # INIT NEW JOB
        temp = {}
        temp['callback_url'] = callback_url
        temp['vnp_server_ip'] = user_vpn
        temp['data_url'] = data_url
        temp['token'] = vpn_token
        temp['status'] = 'pending'
        temp['account_id'] = account_id
        temp['vpn_type'] = vpn_type
        temp['action'] = action
        temp['account_os'] = 'WINDOWS'  # LINUX
        temp['update_on'] = update_on
        temp['created_on'] = update_on

        # INSERT NEW JOB
        job_id = self.postgres.insert('job', temp, 'job_id')

        return job_id

    def get_sa_role(self, account_id):
        """Return Super Admin Role"""

        sql_str = "SELECT * FROM account_role where account_id='{0}' ".format(
            account_id)
        sql_str += "AND role_id in (SELECT role_id FROM role "
        sql_str += "WHERE role_name='super admin')"

        account = self.postgres.query_fetch_one(sql_str)

        return account

    def get_super_admin(self):
        """Return Super Admin"""

        sql_str = "SELECT * FROM role WHERE role_name='super admin'"

        super_admin = self.postgres.query_fetch_one(sql_str)

        return super_admin

    def delete_vpn_allow(self, account_id, super_admin=False):
        """Delete VPN Allow"""

        if self.vpn_db_build.upper() == 'TRUE':

            callback_url = self.my_protocol + "://" + self.my_ip + "/vpn/update"
            data_url = self.my_protocol + "://" + self.my_ip + "/vpn/data"
            vpn_token = '269c2c3706886d94aeefd6e7f7130ab08346590533d4c5b24ccaea9baa5211ec'

            vpn_type = 'VCLIENT'
            cvpn_type = 'CLIENT'

            # SUPER ADMIN TO CLIENT
            if super_admin:

                vpn_type = 'VRH'
                cvpn_type = 'RHADMIN'

            # CLIENT
            # INSERT JOB
            job_id = self.insert_job(callback_url, data_url, vpn_token,
                                     account_id, self.user_vpn, 'DELETE',
                                     cvpn_type)

            # INIT PARAMS FOR UPDATE VPN
            vpn_params = {}
            vpn_params['callback_url'] = callback_url
            vpn_params['data_url'] = data_url
            vpn_params['job_id'] = job_id

            # UPDATE VPN
            self.update_vpn(vpn_params, vpn_token)

        # VESSEL
        if self.get_user_vessels(account_id) or super_admin:

            # print("DELETE VCLIENT/VHR VPN")
            # INSERT JOB
            job_id = self.insert_job(callback_url, data_url, vpn_token,
                                     account_id, self.vessel_vpn, 'DELETE',
                                     vpn_type)

            # INIT PARAMS FOR UPDATE VPN
            vpn_params = {}
            vpn_params['callback_url'] = callback_url
            vpn_params['data_url'] = data_url
            vpn_params['job_id'] = job_id

            # UPDATE VPN
            self.update_vpn(vpn_params, vpn_token, True)

        conditions = []

        conditions.append({"col": "id", "con": "=", "val": account_id})

        query_json = {}
        query_json['status'] = False

        if self.postgres.update('account', query_json, conditions):

            return 1

        return 0

    def create_vpn(self, account_id, super_admin=False):
        """Create VPN"""

        query_json = {}
        query_json['status'] = True

        if self.vpn_db_build.upper() == 'TRUE':

            # JOB DATAS
            callback_url = self.my_protocol + "://" + self.my_ip + "/vpn/update"
            data_url = self.my_protocol + "://" + self.my_ip + "/vpn/data"
            vpn_token = '269c2c3706886d94aeefd6e7f7130ab08346590533d4c5b24ccaea9baa5211ec'

            vpn_type = 'VCLIENT'
            cvpn_type = 'CLIENT'

            if super_admin:

                vpn_type = 'VRH'
                cvpn_type = 'RHADMIN'

            # INSERT JOB
            job_id = self.insert_job(callback_url, data_url, vpn_token,
                                     account_id, self.user_vpn, 'CREATE',
                                     cvpn_type)

            # INIT PARAMS FOR CREATE VPN
            vpn_params = {}
            vpn_params['callback_url'] = callback_url
            vpn_params['data_url'] = data_url
            vpn_params['job_id'] = job_id

            # CREATE VPN
            self.new_vpn(vpn_params, vpn_token, False)

            # VESSEL
            if self.get_user_vessels(account_id) or super_admin:

                # INSERT JOB
                job_id = self.insert_job(callback_url, data_url, vpn_token,
                                         account_id, self.vessel_vpn, 'CREATE',
                                         vpn_type)

                # INIT PARAMS FOR CREATE VPN
                vpn_params = {}
                vpn_params['callback_url'] = callback_url
                vpn_params['data_url'] = data_url
                vpn_params['job_id'] = job_id

                # CREATE VPN
                self.update_vpn(vpn_params, vpn_token, True)

        conditions = []

        conditions.append({"col": "id", "con": "=", "val": account_id})

        if self.postgres.update('account', query_json, conditions):

            return 1

        return 0

    def update_vpn(self, data, vpn_token, flag=False):
        """Update VPN"""

        api_endpoint = self.user_protocol + "://" + self.user_vpn + "/ovpn"

        if flag:

            api_endpoint = self.vessel_protocol + "://" + self.vessel_vpn + "/ovpn"

        headers = {'content-type': 'application/json', 'token': vpn_token}

        req = requests.post(api_endpoint,
                            data=json.dumps(data),
                            headers=headers)
        res = req.json()

        return res

    def new_vpn(self, data, vpn_token, flag=False):
        """New VPN"""

        api_endpoint = self.user_protocol + "://" + self.user_vpn + "/ovpn"

        if flag:

            api_endpoint = self.vessel_protocol + "://" + self.vessel_vpn + "/ovpn"

        headers = {'content-type': 'application/json', 'token': vpn_token}

        req = requests.post(api_endpoint,
                            data=json.dumps(data),
                            headers=headers)
        res = req.json()

        return res

    def get_user_vessels(self, account_id):
        """Return User Vessels"""

        sql_str = "SELECT * FROM account_vessel WHERE account_id={0}".format(
            account_id)
        avs = self.postgres.query_fetch_all(sql_str)

        if avs:

            for acct in avs:

                if acct['allow_access']:

                    return 1

        return 0

    def get_user_status(self, account_id):
        """Return User Status"""

        sql_str = "SELECT status FROM account WHERE id={0}".format(account_id)
        account = self.postgres.query_fetch_one(sql_str)

        return account['status']
Beispiel #22
0
class VPNIPUpdate(Common):
    """Class for VPNIPUpdate"""

    # INITIALIZE
    def __init__(self):
        """The Constructor for VPNIPUpdate class"""

        # INIT CONFIG
        self.config = ConfigParser()

        # CONFIG FILE
        self.config.read("config/config.cfg")

        self.couch_query = Queries()
        self.postgres = PostgreSQL()
        self.epoch_default = 26763

        self.vpn_db_build = config_section_parser(self.config,
                                                  "VPNDB")['build']
        super(VPNIPUpdate, self).__init__()

        if self.vpn_db_build.upper() == 'TRUE':
            self.my_ip = config_section_parser(self.config, "IPS")['my']
            self.my_protocol = config_section_parser(self.config,
                                                     "IPS")['my_protocol']

            self.user_vpn = config_section_parser(self.config,
                                                  "IPS")['user_vpn']
            self.user_protocol = config_section_parser(self.config,
                                                       "IPS")['user_protocol']

            self.vessel_vpn = config_section_parser(self.config,
                                                    "IPS")['vessel_vpn']
            self.vessel_protocol = config_section_parser(
                self.config, "IPS")['vessel_protocol']
            # self.generate_token()
            self.vpn_token = '269c2c3706886d94aeefd6e7f7130ab08346590533d4c5b24ccaea9baa5211ec'

    def vpn_ip_update(self):
        """VPN IP Update"""

        if self.vpn_db_build.upper() == 'TRUE':

            account_vpn_state = self.get_account_vpn_state()
            if account_vpn_state:
                self.update_account_vessel(account_vpn_state)

            account_vessels = self.get_account_vessel(True)

            for account_vessel in account_vessels or []:

                self.check_job(account_vessel, 'ADD')

            account_vessels = self.get_account_vessel(False)
            disabled_account = self.get_account_vessel(
                False, 'account_offline_vessel')
            account_vessels += disabled_account

            for account_vessel in account_vessels or []:

                self.check_job(account_vessel, 'REMOVE')

        return 1

    def get_account_vpn_state(self):
        """Return Account VPN State"""

        # INIT SQL QUERY
        sql_str = "SELECT * FROM account WHERE vessel_vpn_state='pending'"

        # FETCH ALL
        res = self.postgres.query_fetch_all(sql_str)

        # RETURN
        return res

    def get_account_vessel(self, allow_access, table="account_vessel"):
        """Return Account Vessel"""

        # INIT SQL QUERY
        sql_str = "SELECT DISTINCT (account_id), allow_access FROM {0}".format(
            table)
        sql_str += " WHERE vessel_vpn_state='pending' AND allow_access={0}".format(
            allow_access)

        # FETCH ALL
        res = self.postgres.query_fetch_all(sql_str)

        # RETURN
        return res

    def update_account_vessel(self, account_vpn_state):
        """Update Account Vessel"""

        for avs in account_vpn_state:
            account_id = avs['id']
            vessel_allow_access = self.get_vessel_allow_access(account_id)

            self.remove_account_vessel(account_id, vessel_allow_access)

            # INIT CONDITION
            conditions = []

            # CONDITION FOR QUERY
            conditions.append({"col": "id", "con": "=", "val": account_id})

            updates = {}
            updates['vessel_vpn_state'] = 'ok'

            # UPDATE VESSEL VPN STATE
            self.postgres.update('account', updates, conditions)

    def check_job(self, account, action):
        """Check Job"""

        account_id = account['account_id']
        # INIT SQL QUERY
        sql_str = "SELECT * FROM job"
        sql_str += " WHERE account_id='{0}' AND status='pending'".format(
            account_id)
        sql_str += " AND action='{0}'".format(action)
        res = self.postgres.query_fetch_one(sql_str)

        # JOB DATAS
        callback_url = self.my_protocol + "://" + self.my_ip + "/vpn/update"
        data_url = self.my_protocol + "://" + self.my_ip + "/vpn/data"

        if not res:
            user_vpn = self.get_user_vpn(account_id)

            if user_vpn:

                sql_str = "SELECT * FROM account_role where account_id='{0}' ".format(
                    account_id)
                sql_str += "AND role_id in (SELECT role_id FROM role "
                sql_str += "WHERE role_name='super admin')"

                super_admin = self.postgres.query_fetch_one(sql_str)

                vpn_type = 'VCLIENT'

                if not super_admin:

                    # vpn_type = 'VRH'

                    update_on = time.time()

                    # INIT NEW JOB
                    temp = {}
                    temp['callback_url'] = callback_url
                    temp['vnp_server_ip'] = user_vpn
                    temp['data_url'] = data_url
                    temp['token'] = self.vpn_token
                    temp['status'] = 'pending'
                    temp['account_id'] = account_id
                    temp['vpn_type'] = vpn_type
                    temp['action'] = action
                    temp['update_on'] = update_on
                    temp['created_on'] = update_on

                    # INSERT NEW JOB
                    job_id = self.postgres.insert('job', temp, 'job_id')

                    # INIT PARAMS FOR CREATE VPN
                    vpn_params = {}
                    vpn_params['callback_url'] = callback_url
                    vpn_params['data_url'] = data_url
                    vpn_params['job_id'] = job_id

                    self.add_vpn_ip(vpn_params, self.vpn_token, True)

                    return job_id

                return 0

        else:

            job_id = res['job_id']

            # INIT PARAMS FOR CREATE VPN
            vpn_params = {}
            vpn_params['callback_url'] = callback_url
            vpn_params['data_url'] = data_url
            vpn_params['job_id'] = job_id

            self.add_vpn_ip(vpn_params, self.vpn_token, True)

        return 0

    def get_user_vpn(self, account_id):
        """Return User VPN"""

        sql_str = "SELECT * FROM account_role where account_id='{0}' ".format(
            account_id)
        sql_str += "AND role_id in (SELECT role_id FROM role "
        sql_str += "WHERE role_name='super admin')"

        super_admin = self.postgres.query_fetch_one(sql_str)

        vpn_type = 'VCLIENT'

        if super_admin:

            vpn_type = 'VRH'

        # INIT SQL QUERY
        sql_str = "SELECT * FROM account_vpn"
        sql_str += " WHERE account_id='{0}' AND vpn_type='{1}'".format(
            account_id, vpn_type)
        sql_str += " ORDER BY created_on DESC LIMIT 1"
        res = self.postgres.query_fetch_one(sql_str)

        if res:

            return res['vpn_ip']

        return 0

    def add_vpn_ip(self, data, vpn_token, flag=False):
        """Add VPN IP"""

        api_endpoint = self.user_protocol + "://" + self.user_vpn + "/ovpn"

        if flag:

            api_endpoint = self.vessel_protocol + "://" + self.vessel_vpn + "/ovpn"

        headers = {'content-type': 'application/json', 'token': vpn_token}
        req = requests.post(api_endpoint,
                            data=json.dumps(data),
                            headers=headers)
        res = req.json()

        return res

    def get_vessel_allow_access(self, account_id):
        """Return Vessel Allow Access"""

        # DATA
        sql_str = "SELECT * FROM account_company WHERE account_id = " + str(
            account_id)

        res = self.postgres.query_fetch_all(sql_str)

        rows = []

        # COMPANY'S VESSELS-ID
        for company in res:
            company_id = company['company_id']
            vessel_res = self.get_company_vessels(company_id)
            vessels = []
            for vessel in vessel_res['rows']:

                temp = {}
                temp['vessel_id'] = vessel['vessel_id']
                temp['allow_access'] = self.get_allow_access(
                    account_id, vessel['vessel_id'])
                vessels.append(temp)

            company['vessels'] = vessels
            rows.append(company)

        temp_vessels = []
        list_vessels = []
        for row in rows:

            for row_vessel in row['vessels'] or []:

                if row_vessel['vessel_id'] not in list_vessels:

                    list_vessels.append(row_vessel['vessel_id'])
                    temp_vessels.append(row_vessel)

        # CLOSE CONNECTION
        self.postgres.close_connection()

        return temp_vessels

    # GET VESSELS OF COMPANY
    def get_company_vessels(self, company_id):
        """Return Company Vessels"""

        assert company_id, "CompanyID is required."

        # DATA
        vessels = []
        sql_str = "SELECT * FROM company_vessels WHERE company_id={0}".format(
            company_id)
        vessels = self.postgres.query_fetch_all(sql_str)

        data = {}
        data['rows'] = vessels

        return data

    def get_allow_access(self, account_id, vessel_id):
        """Return Allow Access"""

        # DATA
        sql_str = "SELECT * FROM account_vessel"
        sql_str += " WHERE account_id={0} AND vessel_id='{1}'".format(
            account_id, vessel_id)
        vessel = self.postgres.query_fetch_one(sql_str)

        if vessel:

            return vessel['allow_access']

        ntwconf = self.couch_query.get_complete_values(vessel_id, "NTWCONF")

        # INSERT ACCOUNT VESSEL
        temp = {}
        temp['vessel_vpn_ip'] = ntwconf['NTWCONF']['tun1']['IP']
        temp['account_id'] = account_id
        temp['vessel_id'] = vessel_id
        temp['allow_access'] = False
        temp['vessel_vpn_state'] = 'ok'
        self.postgres.insert('account_vessel', temp)

        return False

    def remove_account_vessel(self, account_id, vessel_allow_access):
        """Remove Account Vessel"""

        vessel_ids = [x['vessel_id'] for x in vessel_allow_access]

        if vessel_ids:

            # INIT SQL QUERY
            sql_str = "SELECT allow_access, vessel_id FROM account_vessel"

            if len(vessel_ids) == 1:

                sql_str += " WHERE account_id={0}".format(account_id)
                sql_str += " AND vessel_id NOT IN ('{0}')".format(
                    vessel_ids[0])

            else:

                sql_str += " WHERE account_id={0}".format(account_id)
                sql_str += " AND vessel_id NOT IN {0}".format(
                    tuple(vessel_ids))

            # FETCH ALL
            rows = self.postgres.query_fetch_all(sql_str)

            for row in rows or []:

                conditions = []

                conditions.append({
                    "col": "vessel_id",
                    "con": "=",
                    "val": row['vessel_id']
                })

                conditions.append({
                    "col": "account_id",
                    "con": "=",
                    "val": account_id
                })

                account_vessel_data = {}
                account_vessel_data['vessel_vpn_state'] = 'pending'
                account_vessel_data['allow_access'] = False
                self.postgres.update('account_vessel', account_vessel_data,
                                     conditions)

            # RETURN
            return 1

        return 0
Beispiel #23
0
class ObuSummary(Common):
    """Class for ObuSummary"""

    # INITIALIZE
    def __init__(self):
        """The Constructor for ObuSummary class"""

        self.postgres = PostgreSQL()
        self.couch_query = Queries()
        self.unit_conversion = UnitConversion()
        super(ObuSummary, self).__init__()

    def obu_summary(self):
        """
        This API is for Getting OBU Summary per Vessel
        ---
        tags:
          - Alarm OBU Summary
        produces:
          - application/json
        parameters:
          - name: token
            in: header
            description: Token
            required: true
            type: string
          - name: userid
            in: header
            description: User ID
            required: true
            type: string
          - name: vessel_id
            in: query
            description: Vessel ID
            required: true
            type: string
        responses:
          500:
            description: Error
          200:
            description: Alarm OBU Summary
        """
        data = {}

        # GET DATA
        token = request.headers.get('token')
        userid = request.headers.get('userid')
        vessel_id = request.args.get('vessel_id')

        # CHECK TOKEN
        if not self.validate_token(token, userid):
            data['alert'] = "Invalid Token"
            data['status'] = 'Failed'

        data = {}

        system = self.couch_query.get_system(vessel_id)

        system_info = {}

        if system["rows"]:

            row = system["rows"][0]["doc"]
            # START TIME
            start_time = time.localtime(row["data"]["start_time"])
            start_time = time.strftime("%d %b %Y %H:%M:%S %Z", start_time)

            # END TIME
            end_time = time.localtime(row["data"]["end_time"])
            end_time = time.strftime("%d %b %Y %H:%M:%S %Z", end_time)

            # TIME DELTA
            runtime_overall = self.time_span(row["data"]["end_time"],
                                             row["data"]["start_time"])

            # DEVICE
            device_list = self.couch_query.get_device(vessel_id)

            # REMOVE DATA
            data_to_remove = [
                'PARAMETERS', 'COREVALUES', 'FAILOVER', 'NTWCONF'
            ]
            device_list = self.remove_data(device_list, data_to_remove)

            # COREVALUES
            corevalues = self.couch_query.get_complete_values(vessel_id,
                                                              "COREVALUES",
                                                              flag='one_doc')

            days = int(row["data"]["end_time"] -
                       row["data"]["start_time"]) // (24 * 3600) + 1

            table_size = float(row["data"]["data_size"]) / days

            table_size = format(table_size, '.4f')

            database_size = format(row["data"]["data_size"], '.4f')

            parameters = self.couch_query.get_complete_values(
                vessel_id, "PARAMETERS")

            imo = parameters['PARAMETERS']['INFO']['IMO']

            system_info["first_timestamp"] = start_time
            system_info["last_timestamp"] = end_time
            system_info["runtime_overall"] = runtime_overall
            system_info["datapoints"] = row["data"]["doc_count"]
            system_info["devices"] = len(device_list)
            # 3 IS 'COREVALUES', 'FAILOVER', 'NTWCONF'
            system_info["monitored_parameters"] = len(device_list) + 3
            system_info["database_size"] = str(database_size) + " MB"
            system_info["table_size"] = str(table_size) + " MB"
            system_info["main_loop_time"] = str(
                int(
                    float(corevalues["value"]["COREVALUES"]["CoreInfo"]
                          ["MainLoopTime"]))) + " Seconds"
            system_info["imo"] = imo

            if "ui_version" in row["data"].keys():
                system_info["ui_version"] = row["data"]["ui_version"]

            if "api_version" in row["data"].keys():
                system_info["api_version"] = row["data"]["api_version"]

            if "backend_version" in row["data"].keys():
                system_info["backend_version"] = row["data"]["backend_version"]

        data['data'] = system_info

        self.get_vessel_device(vessel_id)

        data['status'] = 'ok'

        return self.return_data(data)

    def get_vessel_summary(self, vessel):
        """Return Vessel Summary"""

        data = {}
        if vessel:

            values = self.couch_query.get_complete_values(vessel,
                                                          "COREVALUES",
                                                          flag='all')

            if values:

                timestamp = [value['timestamp'] for value in values]

                data['min_timestamp'] = min(timestamp)
                data['max_timestamp'] = max(timestamp)

                return data

        return 0

    def get_vessel_device(self, vessel):
        """Return Vessel Device"""

        # DATA
        devices = []
        sql_str = "SELECT * FROM device"
        sql_str += " WHERE vessel_id='{0}'".format(vessel)
        sql_str += " AND device NOT IN ('PARAMETERS', 'COREVALUES', 'FAILOVER', 'NTWCONF')"
        devices = self.postgres.query_fetch_all(sql_str)

        data = {}
        data['rows'] = devices

        return data
Beispiel #24
0
class MapVessels(Common):
    """Class for Vessels"""

    # INITIALIZE
    def __init__(self):
        """The Constructor for Vessels class"""
        self._couch_db = CouchDatabase()
        self.couch_query = Queries()
        self.postgres = PostgreSQL()
        self.epoch_default = 26763
        self.vessel_name = ""
        self.aws3 = AwsS3()

        # INIT CONFIG
        self.config = ConfigParser()

        # CONFIG FILE
        self.config.read("config/config.cfg")

        self.vpn_db_build = config_section_parser(self.config, "VPNDB")['build']

        super(MapVessels, self).__init__()

    # GET VESSEL FUNCTION
    def map_vessels(self):
        """
        This API is for Getting Vessels
        ---
        tags:
          - Map
        produces:
          - application/json
        parameters:
          - name: token
            in: header
            description: Token
            required: true
            type: string
          - name: userid
            in: header
            description: User ID
            required: true
            type: string
          - name: vessel_number
            in: query
            description: Vessel Number
            required: false
            type: string
        responses:
          500:
            description: Error
          200:
            description: Vessel Information
        """
        # INIT DATA
        data = {}

        # VESSEL ID
        vessel_number = request.args.get('vessel_number')
        vessels = []
        # GET DATA
        token = request.headers.get('token')
        userid = request.headers.get('userid')

        # CHECK TOKEN
        token_validation = self.validate_token(token, userid)

        if not token_validation:
            data["alert"] = "Invalid Token"
            data['status'] = 'Failed'

            # RETURN ALERT
            return self.return_data(data)

        sql_str = "SELECT * FROM vessel "
        if not vessel_number:
            # COUCH QUERY - GET VESSELS

            sql_str += " ORDER BY created_on DESC"
            vessels = self.postgres.query_fetch_all(sql_str)

        else:
            # COUCH QUERY - GET VESSEL
            sql_str += "WHERE number='{0}'".format(vessel_number)

            vessels = self.postgres.query_fetch_all(sql_str)

        # CHECK DATABASES
        rows = []
        if vessels:

            vessel_ids = [x['vessel_id'] for x in vessels]

            # INIT SQL QUERY
            if len(vessel_ids) == 1:
                sql_str = "SELECT * FROM vessel WHERE state ='1'"
                sql_str += " AND vessel_id IN ('{0}')".format(vessel_ids[0])
            else:
                sql_str = "SELECT * FROM vessel WHERE state ='1'"
                sql_str += " AND vessel_id IN {0}".format(tuple(vessel_ids))

            # FETCH ALL
            vessels_state = self.postgres.query_fetch_all(sql_str)

            to_be_install = [x['vessel_id'] for x in vessels_state]
            # INIT VARIABLES

            # LOOP DATABASES
            for item in vessels:

                if not item['number'] == item['vessel_id']:

                    row = {}

                    # CONNECT TO DATABASE
                    # GET VESSEL NAME

                    # VESSEL POSIBLE STATE
                    # To be installed = 1
                    # Installed and etc. = 2

                    vessel_name = self.get_vessel_name(item['vessel_id'], item['number'])
                    self.vessel_name = vessel_name


                    mail_enable = self.get_email_schedule(item['vessel_id'])
                    last_update = self.get_last_update_with_option(item['vessel_id'])

                    epoch_time = int(time.time())

                    update_state = 'red'
                    if not item['vessel_id'] == item['number']:

                        update_state = self.check_time_lapse(epoch_time, last_update)

                    vpn_url = self.get_vpn_url(item['number'])

                    # SET THE RETURN VALUE
                    row['vessel_id'] = item['vessel_id']
                    row['vessel_rev'] = self.get_rev_id(item['vessel_id'], item['number'])
                    row['vessel_number'] = item['number']
                    row['vessel_name'] = vessel_name
                    row['mail_enable'] = mail_enable['mail_enable']
                    row['schedules'] = mail_enable['schedules']
                    row['emails'] = mail_enable['emails']
                    row['update_state'] = update_state
                    row['vpn_url'] = vpn_url

                    # GET IMAGE URL
                    row['image_url'] = self.aws3.get_vessel_image(item['vessel_id'])

                    if item['vessel_id'] in to_be_install:
                        row['update_state'] = 'white'

                    rows.append(row)

        # SET RETURN
        data['rows'] = rows
        data['status'] = 'ok'

        # RETURN
        return self.return_data(data)

    # GET THE VESSEL NAME
    def get_vessel_name(self, vessel_id, number):
        """Return Vessel Name"""

        if vessel_id == number:
            vessel_name = ""
            if self.vpn_db_build.upper() == 'TRUE':

                sql_str = "SELECT vessel_name FROM vessel_vpn_job WHERE imo='{0}' ".format(number)
                sql_str += "AND vpn_type='VESSEL' ORDER BY created_on DESC LIMIT 1"
                vname = self.postgres.query_fetch_one(sql_str)
                vessel_name = vname['vessel_name']

            else:

                sql_str = "SELECT vessel_name FROM vessel WHERE number='{0}'".format(number)
                vname = self.postgres.query_fetch_one(sql_str)
                vessel_name = vname['vessel_name']

            return vessel_name

        values = self.couch_query.get_complete_values(
            vessel_id,
            "PARAMETERS"
        )
        if values:
            return values['PARAMETERS']['INFO']['VESSELNAME']
        return ''

    def get_email_schedule(self, vessel_id):
        """Return Email Schedule"""

        # SQL QUERY
        cols = 'email_vessel_id, mail_enable, email'
        sql_str = "SELECT " + cols + " FROM email_vessel"
        sql_str += " WHERE vessel_id='{}'".format(vessel_id)

        # FETCH ONE
        email_vessels = self.postgres.query_fetch_all(sql_str)

        data = {}
        data['emails'] = []
        data['schedules'] = []
        data['mail_enable'] = False

        if email_vessels:
            mail_enable = email_vessels[0]['mail_enable']
            email_vessel_id = email_vessels[0]['email_vessel_id']

            # SQL QUERY
            cols = 'schedule'
            sql_str = "SELECT " + cols + " FROM email_schedule"
            sql_str += " WHERE email_vessel_id='{}'".format(email_vessel_id)
            schedules = self.postgres.query_fetch_all(sql_str)

            data['emails'] = (list(map(lambda x: x['email'], email_vessels)))
            data['schedules'] = (list(map(lambda x: x['schedule'], schedules)))
            data['mail_enable'] = mail_enable

        return data

    def get_last_update_with_option(self, vessel_id):
        """Return Last Update with Option"""
        values = self.couch_query.get_complete_values(
            vessel_id,
            "COREVALUES",
            flag='one_doc'
        )

        if values:
            return values['timestamp']
        return ''

    def get_vpn_url(self, vessel_number):
        """ GET VPN URL """

        vpn_url = ""

        sql_str = "SELECT * FROM vessel_vpn_job WHERE"
        sql_str += " imo='{0}' AND status='active'".format(vessel_number)
        job = self.postgres.query_fetch_one(sql_str)

        if job:

            filename = job['directory'].split("/")[-1]
            created_on = job['created_on']
            # vpn_url = "download/vpn/{0}".format(filename)
            vpn_url = "download/vessel/vpn/{0}_{1}".format(created_on, filename)

        return vpn_url

    def get_rev_id(self, vessel_id, vessel_number):
        """ GET REV ID """

        if vessel_id == vessel_number:

            return vessel_id

        vessel = self.couch_query.get_vessels(vessel_number=vessel_number)
        if vessel:

            return vessel[0]['doc']['_rev']

        return 0
Beispiel #25
0
class UpdateINIFiles(Common):
    """Class for UpdateINIFiles"""

    # INITIALIZE
    def __init__(self):
        """The Constructor for UpdateINIFiles class"""
        # INIT CONFIG
        self.config = ConfigParser()

        # CONFIG FILE
        self.config.read("config/config.cfg")

        self._couch_db = CouchDatabase()
        self.couch_query = Queries()
        self.postgres = PostgreSQL()
        super(UpdateINIFiles, self).__init__()

        self.vpn_db_build = config_section_parser(self.config,
                                                  "VPNDB")['build']

        self.vessel_vpn = config_section_parser(self.config,
                                                "IPS")['vessel_vpn']
        self.vessel_protocol = config_section_parser(self.config,
                                                     "IPS")['vessel_protocol']

        self.vpn_token = '269c2c3706886d94aeefd6e7f7130ab08346590533d4c5b24ccaea9baa5211ec'

    def update_ini_files(self):
        """Update  INI Files"""

        # INIT SQL QUERY
        sql_str = "SELECT * FROM vessel"

        # FETCH ALL
        rows = self.postgres.query_fetch_all(sql_str)

        for row in rows:
            vessel_id = row['vessel_id']
            inifiles = self.couch_query.get_last_ini(vessel_id)

            if inifiles:

                vdatas = inifiles[0]["doc"]["data"]
                vtimestamp = inifiles[0]["doc"]["timestamp"]
                ini_dir = vdatas[list(vdatas.keys())[0]]['dir']

                sql_str = "SELECT * FROM ini_files WHERE "
                sql_str += "vessel_id='{0}' AND dir='{1}'".format(
                    vessel_id, ini_dir)

                sdata = self.postgres.query_fetch_one(sql_str)
                if not sdata:

                    for vdata in vdatas.keys():

                        newdt = {}
                        newdt['vessel_id'] = vessel_id
                        newdt['dir'] = vdatas[vdata]['dir']
                        newdt['content'] = json.dumps(vdatas[vdata]['content'])
                        newdt['lastupdate'] = vtimestamp
                        newdt['vessel_lastupdate'] = vtimestamp
                        newdt['created_on'] = time.time()
                        newdt['update_on'] = time.time()

                        self.postgres.insert('ini_files', newdt,
                                             'ini_files_id')

                else:

                    if int(vtimestamp) > int(sdata['lastupdate']):

                        for vdata in vdatas.keys():

                            ini_dir = vdatas[vdata]['dir']

                            conditions = []

                            conditions.append({
                                "col": "vessel_id",
                                "con": "=",
                                "val": vessel_id
                            })

                            conditions.append({
                                "col": "dir",
                                "con": "=",
                                "val": ini_dir
                            })

                            updata = {}
                            updata['content'] = json.dumps(
                                vdatas[vdata]['content'])
                            updata['lastupdate'] = vtimestamp
                            updata['vessel_lastupdate'] = vtimestamp

                            self.postgres.update('ini_files', updata,
                                                 conditions)

                    if int(vtimestamp) < int(sdata['lastupdate']):

                        # UPDATE VESSEL INIFILES
                        self.send_command(vessel_id)

            vsystem = self.couch_query.get_system(vessel_id)
            if vsystem["rows"]:

                vsystem = vsystem["rows"][0]["doc"]["data"]
                repos = set(["ui_version", "api_version", "backend_version"])
                sys_keys = set(vsystem.keys())

                if repos.issubset(sys_keys):

                    ui_version = vsystem["ui_version"]
                    api_version = vsystem["api_version"]
                    backend_version = vsystem["backend_version"]

                    ui_version = ui_version.split("\n")[0]
                    api_version = api_version.split("\n")[0]
                    backend_version = backend_version.split("\n")[0]

                    sql_str = "SELECT * FROM vessel_version INNER JOIN version ON"
                    sql_str += " vessel_version.version_id=version.version_id WHERE"
                    sql_str += " (vessel_version.vessel_id='{0}')".format(
                        vessel_id)
                    sql_str += " and (version.web!='{0}'".format(ui_version)
                    sql_str += " or version.api!='{0}'".format(api_version)
                    sql_str += " or version.backend!='{0}')".format(
                        backend_version)
                    vversion = self.postgres.query_fetch_one(sql_str)

                    if vversion:

                        # UPDATE VERSION
                        self.update_version(vessel_id, vversion)

        return 1

    def update_version(self, vessel_id, datas):
        """UPDATE VESSEL VERSION"""

        ntwconf_datas = self.couch_query.get_complete_values(vessel_id,
                                                             "NTWCONF",
                                                             flag='one_doc')

        ntwconf = ntwconf_datas['value']
        current_time = time.time()
        ntwconf_timestamp = ntwconf_datas['timestamp']

        if self.check_time_lapse(current_time, ntwconf_timestamp) == 'green':

            host = ntwconf['NTWCONF']['tun1']['IP']
            data = {}
            data['host'] = host
            data['port'] = 8080
            data['ini_data'] = datas

            api_endpoint = self.vessel_protocol + "://" + self.vessel_vpn + "/update/ini/files"

            headers = {
                'content-type': 'application/json',
                'token': self.vpn_token
            }
            req = requests.put(api_endpoint,
                               data=json.dumps(data),
                               headers=headers)
            res = req.json()

            return res

        return 0

    def send_command(self, vessel_id):
        """Send Command"""

        ini_data = self.get_ini_files(vessel_id)

        ntwconf_datas = self.couch_query.get_complete_values(vessel_id,
                                                             "NTWCONF",
                                                             flag='one_doc')

        ntwconf = ntwconf_datas['value']
        current_time = time.time()
        ntwconf_timestamp = ntwconf_datas['timestamp']

        if self.check_time_lapse(current_time, ntwconf_timestamp) == 'green':

            host = ntwconf['NTWCONF']['tun1']['IP']
            data = {}
            data['host'] = host
            data['port'] = 8080
            data['ini_data'] = ini_data['datas']

            api_endpoint = self.vessel_protocol + "://" + self.vessel_vpn + "/update/ini/files"

            headers = {
                'content-type': 'application/json',
                'token': self.vpn_token
            }
            req = requests.put(api_endpoint,
                               data=json.dumps(data),
                               headers=headers)
            res = req.json()

            return res

        return 0

    def get_ini_files(self, vessel_id):
        """Return INI Files"""

        # GET SUPER ADMIN ID
        sql_str = "SELECT * FROM ini_files WHERE vessel_id ='{0}'".format(
            vessel_id)
        ini_files = self.postgres.query_fetch_all(sql_str)

        datas = {}

        for ini_file in ini_files:

            datas[ini_file['dir']] = "\n".join(ini_file['content'])

        final_data = {}
        final_data['filenames'] = [x['dir'] for x in ini_files]
        final_data['datas'] = datas

        return final_data
Beispiel #26
0
class AlarmRecord(Common):
    """Class for AlarmRecord"""

    def __init__(self):
        """The Constructor for AlarmRecord class"""
        self.postgres = PostgreSQL()
        self.log = Log()
        self.calc_trigger = calculate_alarm_trigger.CalculateAlarmTrigger()
        self.calc_value = calculate_alarm_value.CalculateAlarmValue()
        self.format_alarm = FormatAlarmState()
        self.couch_query = Queries()
        self.epoch_default = 26763
        super(AlarmRecord, self).__init__()

    def run(self):
        """Run Alarm Trigger"""

        ats = self.get_alarm_trigger()

        if ats:
            for atrigger in ats:

                current_date = self.epoch_day(time.time())

                epoch_time = self.days_update(current_date)
                epoch_time -= 1

                at_id = atrigger['alarm_trigger_id']

                vessel_ids = self.get_triggered_vessel(at_id)

                for vessel_id in vessel_ids:

                    # last_update = self.get_last_update(at_id, vessel_id, st, epoch_time)

                    self.update_alarm_data(at_id, vessel_id, epoch_time)
        else:
            print("No Alarm Trigger Available")

    def get_alarm_trigger(self):
        """Return Alarm Triggers"""

        sql_str = "SELECT * FROM alarm_trigger WHERE alarm_enabled=true"

        res = self.postgres.query_fetch_all(sql_str)

        return res

    def get_alarm_param(self, condition_id):
        """ Return Alarm Condition Parameter """
        sql_str = "SELECT * FROM alarm_condition"
        sql_str += " WHERE alarm_condition_id={0}".format(condition_id)

        param_id = set()
        alarm_condition = self.postgres.query_fetch_one(sql_str)

        if alarm_condition:

            for param in alarm_condition['parameters']:
                if 'id' in param.keys():
                    param_id.add(param['id'])

        return param_id

    def get_triggered_vessel(self, at_id):
        """Return Triggered Vessel"""

        sql_str = "SELECT * FROM alarm_condition WHERE alarm_condition_id IN ("
        sql_str += " SELECT alarm_condition_id FROM alarm_trigger WHERE"
        sql_str += " alarm_trigger_id={0})".format(at_id)

        alarm_condition = self.postgres.query_fetch_one(sql_str)

        # av_ids = list({x['id'] for x in alarm_condition['parameters'] if 'id' in x.keys()})

        prmtr_ids = set()
        prmtr_conditions = []
        for prmtr in alarm_condition['parameters']:

            if 'id' in prmtr.keys():

                if prmtr['id']:
                    if prmtr['type'] == "values":
                        prmtr_ids.add(prmtr['id'])
                    if prmtr['type'] == "conditions":

                        prmtr_conditions += self.get_alarm_param(prmtr['id'])

        av_ids = list(prmtr_ids) + prmtr_conditions

        # DATA
        sql_str = "SELECT * FROM alarm_value"
        sql_str += " WHERE alarm_value_id in ({0})".format(','.join(map(str, av_ids)))

        alarm_value = self.postgres.query_fetch_all(sql_str)

        arr_vessels_id = []
        arr_options = []

        if alarm_value:

            for avalue in alarm_value:

                if arr_vessels_id:

                    for vessel_id in avalue['vessel'].split(","):
                        arr_vessels_id.append(vessel_id)

                    for opt in avalue['option'].split(","):
                        arr_options.append(opt)
                    # options.append(av['option'].split(","))

                else:

                    arr_vessels_id = avalue['vessel'].split(",")
                    arr_options = avalue['option'].split(",")

        vessels_id = ','.join(map(str, arr_vessels_id))
        options = ','.join(map(str, arr_options))

        vessels_ids = self.calc_value.get_all_vessels(vessels_id, options)

        return vessels_ids

    def update_alarm_data(self, at_id, vessel_id, epoch_time):
        """Update Alarm Data"""

        sql_str = "SELECT epoch_date FROM alarm_data WHERE "
        sql_str += "vessel_id='{0}' AND ".format(vessel_id)
        sql_str += "alarm_trigger_id={0} ".format(at_id)
        sql_str += "ORDER BY epoch_date DESC LIMIT 1"
        epoch_date = self.postgres.query_fetch_one(sql_str)

        timestamp = 0
        if epoch_date:

            timestamp = epoch_date['epoch_date']

        else:

            self.log.low("NEW ALARM RECORD!")
            values = self.couch_query.get_complete_values(
                vessel_id,
                "COREVALUES",
                start=str(9999999999),
                end=str(self.epoch_default),
                flag='one_doc',
                descending=False
            )
            timestamp = values['timestamp']

        late_et = self.days_update(timestamp, 1, True)
        late_st = self.days_update(late_et, 1)

        # late_et -= 1

        new_et = late_et

        while int(new_et) <= int(epoch_time):

            current_time = time.time()
            late_et = self.days_update(late_et, 1, True)
            late_st = self.days_update(late_et, 1)

            if late_st > epoch_time:

                break

            new_et = late_et - 1
            alarm = self.calc_trigger.calculate_trigger([at_id], late_st, late_et, vessel_id)

            alarm_index_0 = ""

            try:

                alarm_index = alarm[0]['results']
                alarm_index_0 = alarm_index[0]
                vessel_name = alarm_index_0['vessel_name']
                vessel_number = alarm_index_0['vessel_number']
                alarm_datas = alarm_index_0['datas']

                # COMPUTE PERCENTAGE
                green, orange, red, blue, violet = self.get_alarm_percentage(alarm_datas)

                average = {}
                average['green'] = green
                average['orange'] = orange
                average['red'] = red
                average['blue'] = blue
                average['violet'] = violet

                module = ""

                if 'module' in alarm_index_0.keys():
                    module = alarm_index_0['module']

                option = ""

                if 'option' in alarm_index_0.keys():
                    option = alarm_index_0['option']

                # SAVE TO ALARM DATA
                data = {}
                data["alarm_trigger_id"] = at_id
                data["average"] = json.dumps(average)
                data["device"] = alarm_index_0['device']
                data["module"] = module
                data["option"] = option
                data["vessel_id"] = vessel_id
                data["vessel_name"] = vessel_name
                data["vessel_number"] = vessel_number
                data["alarm_type_id"] = alarm[0]['alarm_type_id']
                data["epoch_date"] = int(late_st)
                data["created_on"] = current_time
                data["update_on"] = current_time
                data["alarm_description"] = alarm[0]['alarm_description']
                data["err_message"] = alarm[0]['err_message']
                data["device_id"] = alarm_index_0['device_id']
                data["message"] = alarm_index_0['message']
                data["alarm_type"] = alarm[0]['alarm_type']

                self.postgres.insert('alarm_data', data)

                late_et += 1

            except:

                return 1

    def get_alarm_percentage(self, datas):
        """Return Alarm Percentage"""

        green = 0
        orange = 0
        red = 0
        blue = 0
        violet = 0
        for data in datas:

            if data['remarks'] == 'green':

                green += 1

            elif data['remarks'] == 'orange':

                orange += 1

            elif data['remarks'] == 'red':

                red += 1

            elif data['remarks'] == 'blue':

                blue += 1

            elif data['remarks'] == 'violet':

                violet += 1

        # COUMPUTE
        if datas:

            green = green * 100 / len(datas)
            orange = orange * 100 / len(datas)
            red = red * 100 / len(datas)
            blue = blue * 100 / len(datas)
            violet = violet * 100 / len(datas)

        else:

            violet = 100

        return [green, orange, red, blue, violet]

    def run_current_alarm(self):
        """ RUN CURRENT ALARM """

        self.run_alarm_state()
        return 1

    def run_day_alarm(self):
        """ RUN 24 HOURS ALARM """
        self.run_alarm_state("day")
        return 1

    # ALARM FOR CURRENT STATE AND 24 HOURS
    def run_alarm_state(self, epoch_format=""):
        """ RUN ALARM FOR CURRENT STATE """

        atriggers = self.get_alarm_trigger()

        if epoch_format == "day":
            key = epoch_format
            start = 1

        else:
            key = "hours"
            start = 8

        end_time = time.time()
        start_time = self.timediff_update(end_time, start, key=key)
        late_et = self.days_update(end_time, 1, True)
        late_st = self.days_update(late_et, 1)

        for atrigger in atriggers:

            at_id = atrigger['alarm_trigger_id']

            vessel_ids = self.get_triggered_vessel(at_id)

            for vessel_id in vessel_ids:
                print("Key: {0} Alarm Vessel ID: {1}".format(key, vessel_id))
                datas = self.calc_trigger.calculate_trigger(
                    [at_id],
                    start_time,
                    end_time,
                    vessel_id=vessel_id
                )

                if type(datas) == list:

                    datas = self.get_datas(datas, epoch_format, end_time)

                    if not epoch_format:
                        datas = self.format_alarm.filter_current_status(datas)
                else:
                    pass

                # INSERT OR UPDATE TO TABLE
                if datas[0]['results']:

                    self.add_alarm_state(vessel_id, at_id, late_st, datas, key)
                else:
                    pass

    def get_datas(self, datas, epoch_format, end_time):
        """ Set datas """

        for alarm in datas:
            alarm_result = alarm['results']

            if alarm_result and not alarm['err_message']:
                if not epoch_format:
                    self.format_alarm.get_current_alarm(alarm_result)

                else:
                    self.format_alarm.get_alarm24(alarm_result, end_time)

        return datas

    def add_alarm_state(self, vessel_id, at_id, epoch_date, result, key):
        """ Add or Insert Alarm State """

        # CHECK IF DATA EXIST ON CURRENT DATE

        sql_str = "SELECT * FROM alarm_state"
        sql_str += " WHERE alarm_trigger_id='{0}' AND vessel_id='{1}'".format(at_id, vessel_id)
        sql_str += " AND epoch_date='{0}' AND category='{1}'".format(int(epoch_date), key)
        state = self.postgres.query_fetch_one(sql_str)

        current_time = time.time()
        res = result[0]['results']
        data = {}
        if not state:

            # INSERT TO ALARM STATE
            data["alarm_trigger_id"] = at_id
            data["category"] = key
            data["results"] = json.dumps(res)
            data["device"] = res[0]['device']
            data["module"] = res[0]['module']
            data["option"] = res[0]['option']
            data["vessel_id"] = res[0]['vessel_id']
            data["vessel_name"] = res[0]['vessel_name']
            data["alarm_description"] = result[0]['alarm_description']
            data["err_message"] = result[0]['err_message']
            data["device_id"] = res[0]['device_id']
            data["message"] = res[0]['message']
            data["alarm_type"] = result[0]['alarm_type']
            data["vessel_number"] = res[0]['vessel_number']
            data["alarm_type_id"] = result[0]['alarm_type_id']
            data["epoch_date"] = int(epoch_date)
            data["created_on"] = current_time
            data["update_on"] = current_time

            self.postgres.insert('alarm_state', data)

        else:

            # UPDATE ALARM STATE
            data['results'] = json.dumps(res)
            data['update_on'] = current_time
            data["err_message"] = result[0]['err_message']
            data["message"] = res[0]['message']
            conditions = []

            conditions.append({
                "col": "alarm_state_id",
                "con": "=",
                "val": state['alarm_state_id']
            })

            self.postgres.update('alarm_state', data, conditions)

        return 1
Beispiel #27
0
class PortForwarding(Common):
    """Class for PortForwarding"""

    # INITIALIZE
    def __init__(self):
        """The Constructor for PortForwarding class"""
        self._couch_db = CouchDatabase()
        self.couch_query = Queries()
        self.epoch_default = 26763
        super(PortForwarding, self).__init__()

    def port_forwarding(self):
        """
        This API is for getting Port Forwarding of device
        ---
        tags:
          - Devices
        produces:
          - application/json
        parameters:
          - name: token
            in: header
            description: Token
            required: true
            type: string
          - name: userid
            in: header
            description: User ID
            required: true
            type: string
          - name: vessel_id
            in: query
            description: Vessel ID
            required: true
            type: string
          - name: device_id
            in: query
            description: Device ID
            required: false
            type: string
          - name: limit
            in: query
            description: Limit
            required: true
            type: integer
          - name: page
            in: query
            description: Page
            required: true
            type: integer
        responses:
          500:
            description: Error
          200:
            description: Vessel Device Info
        """

        data = {}

        # VESSEL ID
        vessel_id = request.args.get('vessel_id')
        device_id = request.args.get('device_id')
        limit = int(request.args.get('limit'))
        page = int(request.args.get('page'))

        # GET DATA
        token = request.headers.get('token')
        userid = request.headers.get('userid')

        # CHECK TOKEN
        token_validation = self.validate_token(token, userid)

        if not token_validation:
            data["alert"] = "Invalid Token"
            data['status'] = 'Failed'

            # RETURN ALERT
            return self.return_data(data)

        if not vessel_id:
            data["alert"] = "Please complete parameters!"
            data['status'] = 'Failed'

            # RETURN ALERT
            return self.return_data(data)

        # device_list = []
        device_name = ""
        if device_id:

            device_data = self.couch_query.get_by_id(device_id)
            dev_name = device_data['device']
            # device_list.append(value)

        # else:

        #     device_list = self.couch_query.get_device(vessel_id)

        # REMOVE DATA
        # data_to_remove = ['PARAMETERS', 'COREVALUES', 'FAILOVER', 'NTWCONF', 'NTWPERF1']
        # device_list = self.remove_data(device_list, data_to_remove)

        datas = {}
        datas['data'] = []
        final_data = []

        ntwconf = self.couch_query.get_complete_values(vessel_id, "NTWCONF")

        parameters = self.couch_query.get_complete_values(
            vessel_id, "PARAMETERS")

        vpn_ip = ntwconf['NTWCONF']['tun1']['IP']

        keys = parameters['PARAMETERS'].keys()
        for i in range(1, (len(keys))):
            temp_pf = {}
            port = 'PORTFORWARDING' + str(i)
            if port in keys:

                destination_ip_val = parameters['PARAMETERS'][port][
                    'DESTINATIONIP']
                destination_port_val = parameters['PARAMETERS'][port][
                    'DESTINATIONPORT']
                description_val = parameters['PARAMETERS'][port]['DESCRIPTION']
                device_name = parameters['PARAMETERS'][port]['DEVICE']

                link = description_val.lower() + '://'
                link += vpn_ip + ':' + parameters['PARAMETERS'][port][
                    'SOURCEPORT']

                if device_id and dev_name == device_name:

                    temp_pf['destination_ip'] = destination_ip_val
                    temp_pf['destination_port'] = destination_port_val
                    temp_pf['description'] = description_val
                    temp_pf['link'] = link
                    temp_pf['device'] = device_name

                    final_data.append(temp_pf)

                elif not device_id:

                    temp_pf['destination_ip'] = destination_ip_val
                    temp_pf['destination_port'] = destination_port_val
                    temp_pf['description'] = description_val
                    temp_pf['link'] = link
                    temp_pf['device'] = device_name

                    final_data.append(temp_pf)

        total_count = len(final_data) - 1
        total_page = int(math.ceil(int(total_count) / limit)) + 1
        final_data = self.limits(final_data, limit, page)

        datas['total_rows'] = total_count + 1
        datas['total_page'] = total_page
        datas['limit'] = int(limit)
        datas['page'] = int(page)
        datas['data'] = final_data
        datas['status'] = 'ok'

        return self.return_data(datas)
Beispiel #28
0
class VPNData(Common):
    """Class for VPNData"""

    # INITIALIZE
    def __init__(self):
        """The Constructor for VPNData class"""

        self._couch_db = CouchDatabase()
        self.couch_query = Queries()

        self.postgres = PostgreSQL()

        # INIT CONFIG
        self.config = ConfigParser()

        # CONFIG FILE
        self.config.read("config/config.cfg")

        self.vpn_db_build = config_section_parser(self.config,
                                                  "VPNDB")['build']
        super(VPNData, self).__init__()

        if self.vpn_db_build.upper() == 'TRUE':
            self.my_ip = config_section_parser(self.config, "IPS")['my']
            self.my_protocol = config_section_parser(self.config,
                                                     "IPS")['my_protocol']

            self.user_vpn = config_section_parser(self.config,
                                                  "IPS")['user_vpn']
            self.user_protocol = config_section_parser(self.config,
                                                       "IPS")['user_protocol']

            self.vessel_vpn = config_section_parser(self.config,
                                                    "IPS")['vessel_vpn']
            self.vessel_protocol = config_section_parser(
                self.config, "IPS")['vessel_protocol']

            self.vpn_token = '269c2c3706886d94aeefd6e7f7130ab08346590533d4c5b24ccaea9baa5211ec'

    def vpn_data(self):
        """
        This API is for Getting Data for VPN
        ---
        tags:
          - VPN
        produces:
          - application/json
        parameters:
          - name: token
            in: header
            description: Token
            required: true
            type: string
          - name: jobid
            in: header
            description: Job ID
            required: true
            type: string

        responses:
          500:
            description: Error
          200:
            description: Role
        """
        data = {}

        # GET DATA
        job_id = request.headers.get('jobid')
        token = request.headers.get('token')

        # CHECK TOKEN
        datas = self.validate_job_id_token(job_id, token)

        if not datas['data']:
            data["alert"] = "Invalid Token"
            data['status'] = 'Failed'

            # RETURN ALERT
            return self.return_data(data)

        # datas['data']['vpn_type'] ='CLIENT'
        # datas['data']['account_os'] ='WINDOWS' # LINUX
        # datas['data']['action'] ='CREATE'

        datas['status'] = 'ok'

        return self.return_data(datas)

    def validate_job_id_token(self, job_id, token):
        """Validate Job ID Token"""

        # print("*"*50," vpn_data ", "*"*50)
        # print("job_id: ", job_id)
        # print("token: ", token)
        # print("*"*50, " vpn_data ", "*"*50)

        sql_str = "SELECT * FROM job WHERE job_id={0} AND token='{1}'".format(
            job_id, token)
        job = self.postgres.query_fetch_one(sql_str)

        data = {}
        data['data'] = None

        if job:

            vessel_ips = []
            account_ip_address = None

            sql_str = "SELECT username FROM account WHERE id={0}".format(
                job['account_id'])
            account = self.postgres.query_fetch_one(sql_str)

            if account:
                job['account_name'] = account['username']
                data['data'] = job

            if job['action'] == 'ADD':

                sql_str = "SELECT a.id, a.status FROM account a INNER JOIN job j"
                sql_str += " ON a.id=j.account_id WHERE j.job_id={0}".format(
                    job_id)
                account = self.postgres.query_fetch_one(sql_str)
                account_id = account['id']
                account_status = account['status']

                sql_str = "SELECT * FROM account_role where account_id='{0}' ".format(
                    account_id)
                sql_str += "AND role_id in (SELECT role_id FROM role "
                sql_str += "WHERE role_name='super admin')"

                super_admin = self.postgres.query_fetch_one(sql_str)

                vpn_type = 'VCLIENT'

                if super_admin:

                    vpn_type = 'VRH'

                # INIT SQL QUERY
                sql_str = "SELECT vpn_ip FROM account_vpn WHERE "
                sql_str += "vpn_type='{0}' AND account_id='{1}' ".format(
                    vpn_type, account_id)
                sql_str += "AND status = true ORDER BY created_on DESC"

                account_vpn = self.postgres.query_fetch_one(sql_str)
                account_ip_address = account_vpn['vpn_ip']

                sql_str = ""
                if account_status:

                    sql_str = "SELECT * FROM account_vessel WHERE account_id={0}".format(
                        account_id)

                else:

                    sql_str = "SELECT * FROM account_offline_vessel"
                    sql_str += " WHERE account_id={0}".format(account_id)

                vessels = self.postgres.query_fetch_all(sql_str)

                for vessel in vessels:

                    if vessel['allow_access']:

                        parameters = self.couch_query.get_complete_values(
                            vessel['vessel_id'], "PARAMETERS")

                        ntwconf = self.couch_query.get_complete_values(
                            vessel['vessel_id'], "NTWCONF")
                        temp = {}
                        temp['ip'] = ntwconf['NTWCONF']['tun1']['IP']
                        temp['port'] = '22'
                        temp['username'] = parameters['PARAMETERS']['DBOBU'][
                            'USER']
                        temp['password'] = parameters['PARAMETERS']['DBOBU'][
                            'PASS']
                        temp['vessel_name'] = parameters['PARAMETERS']['INFO'][
                            'VESSELNAME']
                        temp['imo'] = parameters['PARAMETERS']['INFO']['IMO']

                        vessel_ips.append(temp)

            elif job['action'] == 'REMOVE':

                sql_str = "SELECT a.id, a.status FROM account a INNER JOIN job j"
                sql_str += " ON a.id=j.account_id WHERE j.job_id={0}".format(
                    job_id)
                account = self.postgres.query_fetch_one(sql_str)
                account_id = account['id']
                account_status = account['status']

                sql_str = "SELECT * FROM account_role where account_id='{0}' ".format(
                    account_id)
                sql_str += "AND role_id in (SELECT role_id FROM role "
                sql_str += "WHERE role_name='super admin')"

                super_admin = self.postgres.query_fetch_one(sql_str)

                vpn_type = 'VCLIENT'

                if super_admin:

                    vpn_type = 'VRH'

                sql_str = ""
                if account_status:

                    # INIT SQL QUERY
                    sql_str = "SELECT vpn_ip FROM account_vpn WHERE "
                    sql_str += "vpn_type='{0}' AND account_id='{1}' ".format(
                        vpn_type, account_id)
                    sql_str += "AND status = true ORDER BY created_on DESC"

                else:

                    # INIT SQL QUERY
                    sql_str = "SELECT vpn_ip FROM account_vpn WHERE "
                    sql_str += "vpn_type='{0}' AND account_id='{1}' ".format(
                        vpn_type, account_id)
                    sql_str += "AND status = false ORDER BY created_on DESC"

                account_vpn = self.postgres.query_fetch_one(sql_str)
                # print("-"*100)
                # print(account_vpn)
                # print("-"*100)

                if account_vpn:

                    account_ip_address = account_vpn['vpn_ip']

                    sql_str = ""
                    # print("-"*100)
                    # print("account_status: ", account_status)
                    # print("-"*100)
                    if account_status:

                        sql_str = "SELECT * FROM account_vessel"
                        sql_str += " WHERE account_id={0}".format(account_id)

                    else:

                        sql_str = "SELECT * FROM account_offline_vessel"
                        sql_str += " WHERE account_id={0}".format(account_id)

                    vessels = self.postgres.query_fetch_all(sql_str)

                    for vessel in vessels:

                        if not vessel['allow_access']:

                            parameters = self.couch_query.get_complete_values(
                                vessel['vessel_id'], "PARAMETERS")

                            ntwconf = self.couch_query.get_complete_values(
                                vessel['vessel_id'], "NTWCONF")
                            temp = {}
                            temp['ip'] = ntwconf['NTWCONF']['tun1']['IP']
                            temp['port'] = '22'
                            temp['username'] = parameters['PARAMETERS'][
                                'DBOBU']['USER']
                            temp['password'] = parameters['PARAMETERS'][
                                'DBOBU']['PASS']
                            temp['vessel_name'] = parameters['PARAMETERS'][
                                'INFO']['VESSELNAME']
                            temp['imo'] = parameters['PARAMETERS']['INFO'][
                                'IMO']

                            vessel_ips.append(temp)

                        else:
                            pass
                            # print("*"*100)
                            # print("User don't have IP!, Account ID", account_id)
                            # print("*"*100)
            data['data']['vessel_ips'] = vessel_ips
            data['data']['account_ip_address'] = account_ip_address

        return data
Beispiel #29
0
class Failover(Common):
    """Class for Failover"""

    # INITIALIZE
    def __init__(self):
        """The Constructor for Failover class"""
        self._couch_db = CouchDatabase()
        self.postgresql_query = PostgreSQL()
        self.couch_query = Queries()
        self.epoch_default = 26763
        super(Failover, self).__init__()

    def failover(self):
        """
        This API is for Getting Failover
        ---
        tags:
          - Vessel
        produces:
          - application/json
        parameters:
          - name: token
            in: header
            description: Token
            required: true
            type: string
          - name: userid
            in: header
            description: User ID
            required: true
            type: string
          - name: vessel_id
            in: query
            description: Vessel ID
            required: true
            type: string
          - name: date
            in: query
            description: Epoch date
            required: false
            type: string
          - name: limit
            in: query
            description: Limit
            required: true
            type: integer
          - name: page
            in: query
            description: Page
            required: true
            type: integer
          - name: sort_type
            in: query
            description: Sort Type
            required: false
            type: string
          - name: sort_column
            in: query
            description: Sort Column
            required: false
            type: string
          - name: filter_column
            in: query
            description: Filter Column
            required: false
            type: string
          - name: filter_value
            in: query
            description: Filter Value
            required: false
            type: string

        responses:
          500:
            description: Error
          200:
            description: Vessel Failover
        """

        data = {}

        # VESSEL ID
        vessel_id = request.args.get('vessel_id')
        epoch_date = request.args.get('date')
        limit = int(request.args.get('limit'))
        page = int(request.args.get('page'))
        sort_type = request.args.get('sort_type')
        sort_column = request.args.get('sort_column')
        filter_column = request.args.get('filter_column')
        filter_value = request.args.get('filter_value')

        # GET DATA
        token = request.headers.get('token')
        userid = request.headers.get('userid')

        # CHECK TOKEN
        token_validation = self.validate_token(token, userid)

        if not token_validation:
            data["alert"] = "Invalid Token"
            data['status'] = 'Failed'

            # RETURN ALERT
            return self.return_data(data)

        if not vessel_id:
            data["alert"] = "Please complete parameters!"
            data['status'] = 'Failed'

            # RETURN ALERT
            return self.return_data(data)

        filter_mod_val = ""
        filter_subcat = ""
        filter_option = ""
        filter_val = ""
        if filter_column and filter_value:

            i = 0
            for filter_col in filter_column.split(","):
                if filter_col == "category":
                    filter_mod_val = filter_value.split(",")[i]
                elif filter_col == "sub_category":
                    filter_subcat = filter_value.split(",")[i]
                elif filter_col == "option":
                    filter_option = filter_value.split(",")[i]
                elif filter_col == "value":
                    filter_val = filter_value.split(",")[i]
                i += 1

        data = {}

        data['data'] = []
        total_page = 0

        if epoch_date:

            values = self.couch_query.get_complete_values(
                vessel_id,
                "FAILOVER",
                str(self.epoch_default),
                str(epoch_date),
                flag='one_doc'
            )

        else:

            values = self.couch_query.get_complete_values(
                vessel_id,
                "FAILOVER",
                flag='one_doc'
            )

        if values:

            all_values = values['value']
            timestamp = values['timestamp']

            temp_datas, options, categories = self.values_wrapper("FAILOVER",
                                                                  all_values,
                                                                  timestamp
                                                                 )
            convert = UnitConversion()
            temp_datas = convert.check_unit(temp_datas)
            temp_datas = self.add_device_subcategory(temp_datas)
            temp_datas = self.add_device_category(temp_datas)
            rows = self.value_filter(filter_mod_val, filter_subcat,
                                     filter_option, filter_val, temp_datas)

            if sort_type and sort_column:
                sort_column = sort_column.lower()

                if sort_type.lower() == "desc" and sort_column in ['category', 'sub_category',
                                                                   'option', 'value']:
                    rows = sorted(rows, key=lambda i: i[sort_column], reverse=True)
                if sort_type.lower() == "asc":
                    rows = sorted(rows, key=lambda i: i[sort_column])
        else:

            rows = []
            options = []
            categories = []

        datas = self.limits(rows, limit, page)
        total_count = len(datas)
        total_page = int(math.ceil(int(total_count - 1) / limit))

        final_data = []
        temp_category = ''
        temp_subcat = ''
        for item in datas:

            if temp_category == item['category']:
                item['category'] = ''
            else:
                temp_category = item['category']

            if temp_subcat == item['sub_category']:
                item['sub_category'] = ''
            else:
                temp_subcat = item['sub_category']

            final_data.append(item)

        data['data'] = final_data
        data['total_page'] = total_page
        data['limit'] = int(limit)
        data['page'] = int(page)
        data['total_rows'] = total_count
        data['options'] = options
        data['categories'] = categories
        data['subcategories'] = self.get_device_subcategory(datas)
        data['status'] = 'ok'

        return self.return_data(data)
Beispiel #30
0
class BlockageData(Common):
    """Class for BlockageData"""

    def __init__(self):
        """The Constructor for BlockageData class"""
        self.blocks = Blockages()
        self.postgres = PostgreSQL()
        self.couch_query = Queries()
        self.epoch_default = 26763
        super(BlockageData, self).__init__()

    def run(self):
        """Run Blockage Data"""
        current_date = self.epoch_day(time.time())

        epoch_time = self.days_update(current_date)
        epoch_time -= 1

        datas = self.get_device_type()

        for data in datas:
            self.add_blockage_data(data['vessel_id'], data['device_id'],
                                   data['device'], data['device_type'], epoch_time)


    def get_device_type(self):
        """ Return Device Type  """
        sql_str = "SELECT DISTINCT vessel_id, device_id, device, device_type"
        sql_str += " FROM device WHERE device_type IN"
        sql_str += " ('Intellian_V100_E2S', 'Intellian_V110_E2S',"
        sql_str += " 'Intellian_V80_IARM', 'Intellian_V100_IARM',"
        sql_str += " 'Intellian_V100', 'Intellian_V80_E2S',"
        sql_str += " 'Sailor_900', 'Cobham_500')"
        # sql_str += " AND vessel_id IN ('be942b8ee9e97f4962258c727c0006bd')"
        device_type = self.postgres.query_fetch_all(sql_str)

        return device_type


    def get_device_data(self, vessel_id, device, start, end):
        """ Return Device Data """

        values = self.couch_query.get_complete_values(
            vessel_id,
            device,
            start=str(start),
            end=str(end),
            flag='all')

        if values:
            return values

        return 0

    def  add_blockage_data(self, vessel_id, device_id, device, dtype, epoch_time):
        """ Insert to Blockage Data """

        sql_str = "SELECT epoch_date FROM blockage_data WHERE "
        sql_str += "vessel_id='{0}' AND ".format(vessel_id)
        sql_str += "device_id='{0}' ".format(device_id)
        sql_str += "ORDER BY epoch_date DESC LIMIT 1"
        epoch_date = self.postgres.query_fetch_one(sql_str)

        timestamp = 0
        if epoch_date:
            timestamp = epoch_date['epoch_date']
        else:
            self.log.low("NEW BLOCKAGE DATA!")
            values = self.couch_query.get_complete_values(
                vessel_id,
                device,
                start=str(9999999999),
                end=str(self.epoch_default),
                flag='one_doc',
                descending=False
            )
            timestamp = values['timestamp']

        late_et = self.days_update(timestamp, 1, True)
        late_st = self.days_update(late_et, 1)

        new_et = late_et

        while int(new_et) <= int(epoch_time):

            late_et = self.days_update(late_et, 1, True)
            late_st = self.days_update(late_et, 1)

            if late_st > epoch_time:

                break

            new_et = late_et - 1
            datas = self.get_device_data(vessel_id, device, late_st, late_et)

            try:

                blockage = self.blocks.get_blockage_data(device, datas)
                block_zone = self.blocks.get_blockage_zones(device, dtype, datas)

                # GET ANTENNA STATUS
                antenna = set(data['antenna_status'] for data in blockage)
                coordinates = self.blocks.get_xydata(blockage, antenna, 0, 0, remarks='cron')
                antenna_status = ["{}".format(tmp.capitalize()) for tmp in antenna]

                # INSERT TO BLOCKAGE DATA
                data = {}
                data["vessel_id"] = vessel_id
                data["device_id"] = device_id
                data["antenna_status"] = json.dumps(antenna_status)
                data["coordinates"] = json.dumps(coordinates)
                data["blockzones"] = json.dumps(block_zone)
                data["epoch_date"] = int(late_st)
                data["created_on"] = time.time()
                data["update_on"] = time.time()

                self.postgres.insert('blockage_data', data)
                # self.log.medium("datas: {0}".format(data))

                late_et += 1

            except:
                pass
                # print("Start Time: {0} End Time: {1} No Data Found ".format(late_st, late_et))

        return 1

    def get_blockzones(self, data, zone_number):
        """ Return Block Zones """

        assert data, "Data is required."

        blockzone = []
        blocks = {}
        if zone_number:
            try:
                az_start_val = None
                el_start_val = None
                az_end_val = None
                el_end_val = None
                bz_type_val = None
                bz_val = None

                for num in range(1, zone_number + 1):

                    bzstatus = "bz{0}Status".format(num)
                    az_start = "bz{0}AzStart".format(num)
                    el_start = "bz{0}ElStart".format(num)
                    az_end = "bz{0}AzEnd".format(num)
                    el_end = "bz{0}ElEnd".format(num)
                    bz_type = "bz{0}Type".format(num)

                    blockzones = "Blockzone{0}".format(num)

                    if bzstatus in data['General']:
                        bz_val = data['General'][bzstatus]

                    if az_start in data['General']:
                        az_start_val = data['General'][az_start]

                    if el_start in data['General']:
                        el_start_val = data['General'][el_start]

                    if az_end in data['General']:
                        az_end_val = data['General'][az_end]

                    if el_end in data['General']:
                        el_end_val = data['General'][el_end]

                    if bz_type in data['General']:
                        bz_type_val = data['General'][bz_type]

                    if blockzones.lower() in data:
                        if not bz_val:
                            bz_val = data[blockzones.lower()]['Status']
                        if not az_start_val:
                            az_start_val = data[blockzones.lower()]['AzStart']
                        if not el_start_val:
                            az_end_val = data[blockzones.lower()]['AzEnd']
                        if not el_end_val:
                            el_end_val = data[blockzones.lower()]['ElEnd']

                    bz_item = {bzstatus: bz_val,
                               az_start: az_start_val,
                               az_end: az_end_val,
                               el_start: el_start_val,
                               el_end: el_end_val,
                               bz_type: bz_type_val}

                    blocks[blockzones] = bz_item

                blockzone.append({"blockage": blocks})

            except ValueError:
                print("No Timestamp found")
        return blockzone