| 1 | #!/usr/bin/python
|
|---|
| 2 | # -*- coding: utf-8 -*-
|
|---|
| 3 | '''
|
|---|
| 4 | Copyright (C) 2020 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
|
|---|
| 5 |
|
|---|
| 6 | This program is free software: you can redistribute it and/or modify
|
|---|
| 7 | it under the terms of the GNU General Public License as published by
|
|---|
| 8 | the Free Software Foundation, either version 3 of the License, or
|
|---|
| 9 | (at your option) any later version.
|
|---|
| 10 |
|
|---|
| 11 | This program is distributed in the hope that it will be useful,
|
|---|
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 14 | GNU General Public License for more details.
|
|---|
| 15 |
|
|---|
| 16 | You should have received a copy of the GNU General Public License
|
|---|
| 17 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|---|
| 18 | '''
|
|---|
| 19 |
|
|---|
| 20 | import os
|
|---|
| 21 | import sys
|
|---|
| 22 | import json
|
|---|
| 23 | import requests
|
|---|
| 24 | import argparse
|
|---|
| 25 |
|
|---|
| 26 | class Log:
|
|---|
| 27 | """
|
|---|
| 28 | Workaround for @dataclass, until python 3.7 is available in main distros
|
|---|
| 29 | """
|
|---|
| 30 | def __init__(self, water_id, device_id, gauge_id, gauge_height, comment, logfile):
|
|---|
| 31 | self.water_id = water_id
|
|---|
| 32 | self.device_id = device_id
|
|---|
| 33 | self.gauge_id = gauge_id
|
|---|
| 34 | self.gauge_height = gauge_height
|
|---|
| 35 | self.comment = comment
|
|---|
| 36 | self.logfile = logfile
|
|---|
| 37 |
|
|---|
| 38 | def log_creation(self):
|
|---|
| 39 | return {
|
|---|
| 40 | 'water': self.water_id,
|
|---|
| 41 | 'device': self.device_id,
|
|---|
| 42 | 'gauge': self.gauge_id,
|
|---|
| 43 | 'gaugeHeight': self.gauge_height,
|
|---|
| 44 | 'comment': self.comment
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | def to_int(value_string):
|
|---|
| 48 | try:
|
|---|
| 49 | return int(value_string)
|
|---|
| 50 | except:
|
|---|
| 51 | return None
|
|---|
| 52 |
|
|---|
| 53 | def to_float(value_string):
|
|---|
| 54 | try:
|
|---|
| 55 | return float(value_string)
|
|---|
| 56 | except:
|
|---|
| 57 | return None
|
|---|
| 58 |
|
|---|
| 59 | def header(api_key):
|
|---|
| 60 | return {
|
|---|
| 61 | 'X-API-KEY': api_key
|
|---|
| 62 | # 'Content-type': 'multipart/form-data'
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | def create_log(log, url, api_key):
|
|---|
| 66 | response = requests.post(url,
|
|---|
| 67 | json = log.log_creation(),
|
|---|
| 68 | headers = header(api_key)
|
|---|
| 69 | )
|
|---|
| 70 |
|
|---|
| 71 | if response.status_code != 200:
|
|---|
| 72 | print('ERROR: Could not create LogMetaData for logfile {}'.format(log.logfile))
|
|---|
| 73 | print(' Response status code: {}'.format(response.status_code))
|
|---|
| 74 | print(' Response: \n{}'.format(response.text))
|
|---|
| 75 | print(' Request headers: {}'.format(response.request.headers))
|
|---|
| 76 | print(' Request body: {}'.format(response.request.body))
|
|---|
| 77 | sys.exit(5)
|
|---|
| 78 |
|
|---|
| 79 | data = response.json()
|
|---|
| 80 | if not 'logId' in data:
|
|---|
| 81 | print('ERROR: No log id returned')
|
|---|
| 82 | sys.exit(6)
|
|---|
| 83 | return data["logId"]
|
|---|
| 84 |
|
|---|
| 85 | def upload_log(log_id, log, url, api_key):
|
|---|
| 86 | fileContent = open(log.logfile, 'rb')
|
|---|
| 87 | headers = header(api_key)
|
|---|
| 88 | headers["Accept"] = "application/json"
|
|---|
| 89 | response = requests.post(
|
|---|
| 90 | "{}/{}".format(url, log_id),
|
|---|
| 91 | data = fileContent,
|
|---|
| 92 | headers = headers
|
|---|
| 93 | )
|
|---|
| 94 | if response.status_code != 200:
|
|---|
| 95 | print('ERROR: Could not upload log for logfile {}'.format(log.logfile))
|
|---|
| 96 | print("Response status code: {}".format(response.status_code))
|
|---|
| 97 | print("Response: \n{}".format(response.text))
|
|---|
| 98 | sys.exit(7)
|
|---|
| 99 |
|
|---|
| 100 | def main(argv):
|
|---|
| 101 | input_file_path = None
|
|---|
| 102 | url = None
|
|---|
| 103 | api_key = None
|
|---|
| 104 |
|
|---|
| 105 | parser = argparse.ArgumentParser(description='Uploads logfiles to GNUBoat server')
|
|---|
| 106 | parser.add_argument('input_file', metavar='input_file_path', type=str,
|
|---|
| 107 | help='JSON file with water, gauge, device, logfiles and correction data to be processed')
|
|---|
| 108 | parser.add_argument('-s', '--server', dest='server', type=str,
|
|---|
| 109 | help='Server host and port to upload the logsfiles to')
|
|---|
| 110 | parser.add_argument('-k', '--api-key', dest='api_key', type=str,
|
|---|
| 111 | help='API Key for authorisation')
|
|---|
| 112 | arguments = parser.parse_args(argv)
|
|---|
| 113 |
|
|---|
| 114 | if arguments.input_file is None:
|
|---|
| 115 | print('ERROR: No input file specified')
|
|---|
| 116 | sys.exit(2)
|
|---|
| 117 | elif not os.path.exists(arguments.input_file):
|
|---|
| 118 | print('ERROR: Specified input file does not exists: {}'.format(arguments.input_file))
|
|---|
| 119 | sys.exit(3)
|
|---|
| 120 | else:
|
|---|
| 121 | input_file_path = arguments.input_file
|
|---|
| 122 |
|
|---|
| 123 | if arguments.server is None:
|
|---|
| 124 | print('ERROR: No server specified')
|
|---|
| 125 | sys.exit(2)
|
|---|
| 126 | else:
|
|---|
| 127 | url = 'http://{}/mapservice/v1/logs'.format(arguments.server)
|
|---|
| 128 |
|
|---|
| 129 | if arguments.api_key is None:
|
|---|
| 130 | print('ERROR: No API key specified')
|
|---|
| 131 | sys.exit(2)
|
|---|
| 132 | else:
|
|---|
| 133 | api_key = arguments.api_key
|
|---|
| 134 |
|
|---|
| 135 | logs_checked = []
|
|---|
| 136 | print('Checking input from {}'.format(input_file_path))
|
|---|
| 137 | with open(input_file_path, 'r') as input_file:
|
|---|
| 138 | logs = json.load(input_file)
|
|---|
| 139 | for log in logs:
|
|---|
| 140 | if not 'water' in log:
|
|---|
| 141 | print('ERROR: No water in log {}'.format(json.dumps(log)))
|
|---|
| 142 | sys.exit(4)
|
|---|
| 143 | water_id = to_int(log['water'])
|
|---|
| 144 | if not water_id:
|
|---|
| 145 | print('ERROR: Could not convert water to int {}'.format(log['water']))
|
|---|
| 146 | sys.exit(4)
|
|---|
| 147 |
|
|---|
| 148 | if not 'gauge' in log:
|
|---|
| 149 | print('ERROR: No gauge in log {}'.format(json.dumps(log)))
|
|---|
| 150 | sys.exit(4)
|
|---|
| 151 | gauge_id = to_int(log['gauge'])
|
|---|
| 152 | if not gauge_id:
|
|---|
| 153 | print('ERROR: Could not convert gauge to int {}'.format(log['gauge']))
|
|---|
| 154 | sys.exit(4)
|
|---|
| 155 |
|
|---|
| 156 | if not 'device' in log:
|
|---|
| 157 | print('ERROR: No device in log {}'.format(json.dumps(log)))
|
|---|
| 158 | sys.exit(4)
|
|---|
| 159 | device_id = to_int(log['device'])
|
|---|
| 160 | if not device_id:
|
|---|
| 161 | print('ERROR: Could not convert device to int {}'.format(log['device']))
|
|---|
| 162 | sys.exit(4)
|
|---|
| 163 |
|
|---|
| 164 | if not 'gaugeHeight' in log:
|
|---|
| 165 | print('ERROR: No gaugeHeight in log {}'.format(json.dumps(log)))
|
|---|
| 166 | sys.exit(4)
|
|---|
| 167 | gauge_height = to_float(log['gaugeHeight'])
|
|---|
| 168 | if not gauge_height:
|
|---|
| 169 | print('ERROR: Could not convert gaugeHeight to float {}'.format(log['gaugeHeight']))
|
|---|
| 170 | sys.exit(4)
|
|---|
| 171 |
|
|---|
| 172 | if not 'file' in log:
|
|---|
| 173 | print('ERROR: No file in log {}'.format(json.dumps(log)))
|
|---|
| 174 | sys.exit(4)
|
|---|
| 175 |
|
|---|
| 176 | comment = ''
|
|---|
| 177 | if 'comment' in log:
|
|---|
| 178 | comment = log['comment']
|
|---|
| 179 |
|
|---|
| 180 | logfile = os.path.join(os.path.dirname(__file__), log['file'])
|
|---|
| 181 | if not os.path.exists(logfile):
|
|---|
| 182 | logfile_rel = os.path.join(os.path.dirname(input_file_path), log['file'])
|
|---|
| 183 | if not os.path.exists(logfile_rel):
|
|---|
| 184 | print('ERROR: Logfile not found at {} or {}'.format(logfile, logfile_rel))
|
|---|
| 185 | sys.exit(4)
|
|---|
| 186 | else:
|
|---|
| 187 | logfile = logfile_rel
|
|---|
| 188 | logs_checked.append(Log(water_id, device_id, gauge_id, gauge_height, comment, logfile))
|
|---|
| 189 |
|
|---|
| 190 | print('Uploading {} logs'.format(len(logs_checked)))
|
|---|
| 191 | for log in logs_checked:
|
|---|
| 192 | log_id = create_log(log, url, api_key)
|
|---|
| 193 | upload_log(log_id, log, url, api_key)
|
|---|
| 194 | print(' Finished {}'.format(log.logfile))
|
|---|
| 195 |
|
|---|
| 196 | if __name__ == "__main__":
|
|---|
| 197 | main(sys.argv[1:])
|
|---|