| [186affa] | 1 | '''
|
|---|
| 2 | Copyright (C) 2019 - 2020 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
|
|---|
| 3 |
|
|---|
| 4 | This program is free software: you can redistribute it and/or modify
|
|---|
| 5 | it under the terms of the GNU General Public License as published by
|
|---|
| 6 | the Free Software Foundation, either version 3 of the License, or
|
|---|
| 7 | (at your option) any later version.
|
|---|
| 8 |
|
|---|
| 9 | This program is distributed in the hope that it will be useful,
|
|---|
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 12 | GNU General Public License for more details.
|
|---|
| 13 |
|
|---|
| 14 | You should have received a copy of the GNU General Public License
|
|---|
| 15 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|---|
| 16 | '''
|
|---|
| 17 |
|
|---|
| 18 | import os
|
|---|
| 19 | import pytz
|
|---|
| [be66b78] | 20 | import flask
|
|---|
| [186affa] | 21 |
|
|---|
| 22 | from ..database.Model import db, User, ApiKey, Water, OsmType, Gauge, Device, Log, Queue, ProcessingStatus, db_setup
|
|---|
| 23 | from ..tasks.tasks import log_process
|
|---|
| 24 |
|
|---|
| [7be94aa] | 25 |
|
|---|
| [186affa] | 26 | def setup():
|
|---|
| [7be94aa] | 27 | db_setup()
|
|---|
| 28 |
|
|---|
| 29 | print("CREATING DATABASE DEMO CONTENT...")
|
|---|
| 30 | with db.session.no_autoflush:
|
|---|
| 31 | test_user_john = None
|
|---|
| 32 | query = User.query.filter_by(username='john').all()
|
|---|
| 33 | if len(query) == 1:
|
|---|
| 34 | test_user_john = query[0]
|
|---|
| 35 | else:
|
|---|
| 36 | test_user_john = User.factory(
|
|---|
| 37 | 'john', 'testpass2', 'john@example.org'
|
|---|
| 38 | )
|
|---|
| 39 | test_user_john.activation_code = 'dac9630aec642a428cd73f4be0a03569'
|
|---|
| 40 | test_user_john.license_accepted = True
|
|---|
| 41 | db.session.add(test_user_john)
|
|---|
| 42 | db.session.commit()
|
|---|
| 43 |
|
|---|
| 44 | test_user_jane = None
|
|---|
| 45 | query = User.query.filter_by(username='jane').all()
|
|---|
| 46 | if len(query) == 1:
|
|---|
| 47 | test_user_jane = query[0]
|
|---|
| 48 | else:
|
|---|
| 49 | test_user_jane = User.factory(
|
|---|
| 50 | 'jane', 'testpass', 'jane@example.org'
|
|---|
| 51 | )
|
|---|
| 52 | test_user_jane.activated = True
|
|---|
| 53 | test_user_jane.first_name = 'Jane'
|
|---|
| 54 | test_user_jane.license_accepted = True
|
|---|
| 55 | db.session.add(test_user_jane)
|
|---|
| 56 | db.session.commit()
|
|---|
| 57 |
|
|---|
| 58 | if len(ApiKey.query.filter_by(key='DelApiKey4711').all()) < 1:
|
|---|
| 59 | db.session.add(ApiKey(user=test_user_jane, key='DelApiKey4711'))
|
|---|
| 60 | db.session.commit()
|
|---|
| 61 | if len(ApiKey.query.filter_by(key='test_key_jane').all()) < 1:
|
|---|
| 62 | db.session.add(ApiKey(user=test_user_jane, key='test_key_jane'))
|
|---|
| 63 | db.session.commit()
|
|---|
| 64 |
|
|---|
| 65 | mississippi = None
|
|---|
| 66 | query = Water.query.filter_by(name='Mississippi').all()
|
|---|
| 67 | if len(query) == 1:
|
|---|
| 68 | mississippi = query[0]
|
|---|
| 69 | else:
|
|---|
| 70 | mississippi = Water(
|
|---|
| 71 | name='Mississippi',
|
|---|
| 72 | manager=test_user_jane,
|
|---|
| 73 | osm_id=1756854,
|
|---|
| 74 | osm_type=OsmType.Relation
|
|---|
| 75 | )
|
|---|
| 76 | db.session.add(mississippi)
|
|---|
| 77 | db.session.commit()
|
|---|
| 78 |
|
|---|
| 79 | if len(Water.query.filter_by(name='Forggensee').all()) < 1:
|
|---|
| 80 | forggensee = Water(
|
|---|
| 81 | name='Forggensee',
|
|---|
| 82 | manager=test_user_john,
|
|---|
| 83 | osm_id=897899,
|
|---|
| 84 | osm_type=OsmType.Relation
|
|---|
| 85 | )
|
|---|
| 86 | db.session.add(forggensee)
|
|---|
| 87 | db.session.commit()
|
|---|
| 88 |
|
|---|
| 89 | pfuhlersee = None
|
|---|
| 90 | query = Water.query.filter_by(name='Pfuhler See').all()
|
|---|
| 91 | if len(query) == 1:
|
|---|
| 92 | pfuhlersee = query[0]
|
|---|
| 93 | else:
|
|---|
| 94 | pfuhlersee = Water(
|
|---|
| 95 | name='Pfuhler See',
|
|---|
| 96 | manager=test_user_jane,
|
|---|
| 97 | osm_id=225888565,
|
|---|
| 98 | osm_type=OsmType.Way
|
|---|
| 99 | )
|
|---|
| 100 | db.session.add(pfuhlersee)
|
|---|
| 101 | db.session.commit()
|
|---|
| 102 |
|
|---|
| 103 | new_orleans = None
|
|---|
| 104 | query = Gauge.query.filter_by(name='New Orleans').all()
|
|---|
| 105 | if len(query) == 1:
|
|---|
| 106 | new_orleans = query[0]
|
|---|
| 107 | else:
|
|---|
| 108 | new_orleans = Gauge(
|
|---|
| 109 | name='New Orleans',
|
|---|
| 110 | manager=test_user_jane,
|
|---|
| 111 | water=mississippi,
|
|---|
| 112 | location='SRID=4326;POINT(-90.136111 29.934722)',
|
|---|
| 113 | reference_water_level_gauge_height=3
|
|---|
| 114 | )
|
|---|
| 115 | db.session.add(new_orleans)
|
|---|
| 116 | db.session.commit()
|
|---|
| 117 |
|
|---|
| 118 | pfuhlersee_gauge = None
|
|---|
| 119 | query = Gauge.query.filter_by(name='Stairs at Kiosk').all()
|
|---|
| 120 | if len(query) == 1:
|
|---|
| 121 | pfuhlersee_gauge = query[0]
|
|---|
| 122 | else:
|
|---|
| 123 | pfuhlersee_gauge = Gauge(
|
|---|
| 124 | name='Stairs at Kiosk',
|
|---|
| 125 | manager=test_user_jane,
|
|---|
| 126 | water=pfuhlersee,
|
|---|
| 127 | location='SRID=4326;POINT(10.0427 48.4275)',
|
|---|
| 128 | reference_water_level_gauge_height=0.4,
|
|---|
| 129 | comment=
|
|---|
| 130 | 'Level zero is the level of the step where the handrail is fixed.'
|
|---|
| 131 | )
|
|---|
| 132 | db.session.add(pfuhlersee_gauge)
|
|---|
| 133 | db.session.commit()
|
|---|
| 134 |
|
|---|
| 135 | gnuboat_one = None
|
|---|
| 136 | query = Device.query.filter_by(name='GnuBoat 1').all()
|
|---|
| 137 | if len(query) == 1:
|
|---|
| 138 | gnuboat_one = query[0]
|
|---|
| 139 | else:
|
|---|
| 140 | gnuboat_one = Device(
|
|---|
| 141 | name='GnuBoat 1',
|
|---|
| 142 | owner=test_user_jane,
|
|---|
| 143 | sensor_depth=0.2,
|
|---|
| 144 | comment='SUP attachment'
|
|---|
| 145 | )
|
|---|
| 146 | db.session.add(gnuboat_one)
|
|---|
| 147 | db.session.commit()
|
|---|
| 148 |
|
|---|
| 149 | log_file = 'sample-logs/nmea0183-2017-12-09.log'
|
|---|
| 150 | sample_log = os.path.join('/usr/share/doc/gnuboat', log_file)
|
|---|
| 151 | if os.path.isfile(sample_log):
|
|---|
| 152 | _sample_log_add(
|
|---|
| 153 | pfuhlersee, pfuhlersee_gauge, 0, gnuboat_one, test_user_jane,
|
|---|
| 154 | sample_log
|
|---|
| 155 | )
|
|---|
| 156 | else:
|
|---|
| 157 | sample_log = os.path.join(
|
|---|
| 158 | os.path.dirname(__file__), '../../../../../../', log_file
|
|---|
| 159 | )
|
|---|
| 160 | if os.path.isfile(sample_log):
|
|---|
| 161 | _sample_log_add(
|
|---|
| 162 | pfuhlersee, pfuhlersee_gauge, 0, gnuboat_one,
|
|---|
| 163 | test_user_jane, sample_log
|
|---|
| 164 | )
|
|---|
| 165 | else:
|
|---|
| 166 | print('Demo log not found --> Skipping')
|
|---|
| 167 |
|
|---|
| 168 | print("...Finished")
|
|---|
| 169 |
|
|---|
| [186affa] | 170 |
|
|---|
| 171 | def _sample_log_add(water, gauge, gauge_height, device, submitter, log_file):
|
|---|
| [7be94aa] | 172 | if len(Log.query.filter_by(submitter=submitter).all()) > 0:
|
|---|
| 173 | return None
|
|---|
| 174 |
|
|---|
| 175 | log = Log(
|
|---|
| 176 | submitter=submitter,
|
|---|
| 177 | water=water,
|
|---|
| 178 | device=device,
|
|---|
| 179 | gauge=gauge,
|
|---|
| 180 | gauge_height=0,
|
|---|
| 181 | comment='Demo Log from {}'.format(log_file)
|
|---|
| 182 | )
|
|---|
| 183 | db.session.add(log)
|
|---|
| 184 | db.session.commit()
|
|---|
| 185 |
|
|---|
| 186 | if not 'CELERY_BROKER_URL' in flask.current_app.config or not flask.current_app.config[
|
|---|
| 187 | 'CELERY_BROKER_URL']:
|
|---|
| 188 | print('Do not add log, since their is no CELERY_BROKER_URL configured')
|
|---|
| 189 | return log
|
|---|
| 190 |
|
|---|
| 191 | with open(log_file, 'r') as logfile:
|
|---|
| 192 | data = logfile.read()
|
|---|
| 193 |
|
|---|
| 194 | processing_state = ProcessingStatus.query.filter_by(
|
|---|
| 195 | label='Queued for Parsing'
|
|---|
| 196 | ).all()
|
|---|
| 197 | if len(processing_state) != 1:
|
|---|
| 198 | raise MapserviceBadRequest(
|
|---|
| 199 | detail='Processing status Queued for Parsing not found database'
|
|---|
| 200 | )
|
|---|
| 201 | processing_state = processing_state[0]
|
|---|
| 202 |
|
|---|
| 203 | # Add Log to Queued table and change the processing state of Log
|
|---|
| 204 | queue = Queue(log=log)
|
|---|
| 205 | db.session.add(queue)
|
|---|
| 206 | log.processingstatus = processing_state
|
|---|
| 207 | db.session.commit()
|
|---|
| 208 |
|
|---|
| 209 | # request log parsing
|
|---|
| 210 | log_process.delay(log.id, data)
|
|---|
| 211 | return log
|
|---|