def main(): yield from db.connect() users = yield from db.execute( "select callsign, settings from users" ) fname = datetime.now().strftime( "%Y%m%d" ) + ".csv" for user in users: if user['settings'] and 'station' in user['settings'] and \ 'callsign' in user['settings']['station'] and \ user['settings']['station']['callsign'] and \ user['settings']['status']['get'] == 'gpslogger': stationCs = user['settings']['station']['callsign'] stationPath = getStationPath( stationCs ) statusPath = stationPath + '/status.json' status = loadJSON( statusPath ) if not status: status = {} ftpPath = webRoot + '/ftp/' + user['callsign'] + '/GPSLogger' if os.path.isdir( ftpPath ): ftpFilePath = ( ftpPath + '/' + fname ) if os.path.isfile( ftpFilePath ): data = None with open( ftpPath + '/' + fname, 'r' ) as f: data = f.readlines()[-1].split( ',' ) dt = datetime.strptime( data[0], '%Y-%m-%dT%H:%M:%S.%fZ' ) ts = int( dt.timestamp() + tzOffset() ) if not 'ts' in status or status['ts'] < ts: status['date'], status['time'] = dtFmt( dt ) status['year'] = dt.year status['ts'] = ts status['location'] = [ float(data[1]), float(data[2] ) ] status['loc'] = qth( data[1], data[2] ) if status['loc'] in rafa: status['rafa'] = rafa[status['loc']] else: status['rafa'] = None status['speed'] = float( data[6] ) #dt_ = datetime.utcnow() #dt_.replace( tzinfo = timezone.utc ) #status['ts_'] = dt_.timestamp() #status['date_'], status['time_'] = dtFmt( dt_ ) #status['year_'] = dt_.year with open( statusPath, 'w' ) as f: json.dump( status, f, ensure_ascii = False )
def save_qth_now_location(cs, location, path): qth_now_locations = loadJSON(path) if not qth_now_locations: qth_now_locations = [] ts = int(time.time()) dtUTC = datetime.utcnow() dt, tm = dtFmt(dtUTC) qth_now_locations = [item for item in qth_now_locations\ if ts - item['ts'] < 600 and\ (item['location'][0] != location[0] or\ item['location'][1] != location[1])\ and (cs == None or item['callsign'] != cs)] qth_now_locations.append({ 'location': location, 'ts': ts, 'date': dt, 'time': tm, 'callsign': cs }) with open(path, 'w') as f: json.dump(qth_now_locations, f, ensure_ascii=False)
def insertChatMessage(path, msg_data, admin): CHAT_MAX_LENGTH = int(conf['chat']['max_length']) chat = loadJSON(path) if not chat: chat = [] msg = { 'user': msg_data['from'], \ 'text': msg_data['text'], \ 'admin': admin, 'ts': time.time() } msg['date'], msg['time'] = dtFmt(datetime.utcnow()) if msg['text'][0] == '@': to, txt = msg['text'][1:].split(' ', maxsplit=1) txt = txt.strip() if not txt and to in IM_QUEUE: del IM_QUEUE[to] else: IM_QUEUE[to] = { 'user': msg['user'], 'text': txt, 'ts': msg['ts'], 'date': msg['date'], 'time': msg['time'] } logging.debug('------- IM_QUEUE -------') logging.debug(IM_QUEUE) else: if 'name' in msg_data: msg['name'] = msg_data['name'] chat.insert(0, msg) chat_trunc = [] chat_adm = [] for msg in chat: if msg['text'].startswith('***') and msg['admin']: chat_adm.append(msg) elif len(chat_trunc) < CHAT_MAX_LENGTH: chat_trunc.append(msg) chat = chat_adm + chat_trunc with open(path, 'w') as f: json.dump(chat, f, ensure_ascii=False)
def process_qso(qso): try: dt = datetime.strptime(qso['ts'], "%Y-%m-%d %H:%M:%S") qso['date'], qso['time'] = dtFmt(dt) qso['qso_ts'] = (dt - datetime(1970, 1, 1)) / timedelta(seconds=1) except (ValueError, TypeError) as exc: logging.error("Error parsing qso timestamp" + qso['ts']) logging.exception(exc) return {'ts': None} serverTs = qso.pop('serverTs') if 'serverTs' in qso else None if serverTs: qso['ts'] = serverTs qsoIdx = [ i[0] for i in enumerate(log) if i[1]['ts'] == qso['ts'] ] if qsoIdx: log[qsoIdx[0]] = qso else: log.append(qso) dbUpdate = yield from db.execute( """ update log set qso = %(qso)s where callsign = %(callsign)s and (qso->>'ts')::float = %(ts)s""", { 'callsign': callsign, 'ts': qso['ts'], 'qso': json.dumps(qso) }) if not dbUpdate: yield from dbInsertQso(callsign, qso) else: new_qso = True if log: for log_qso in log: sameFl = True for key in qso: if key not in ('ts', 'rda', 'wff', 'comments', 'serverTs', 'qso_ts', 'qth', 'no')\ and (key not in log_qso or qso[key] != log_qso[key]): sameFl = False break if sameFl: logging.debug('prev qso found:') new_qso = False qso['ts'] = log_qso['ts'] log_qso['qso_ts'] = qso['qso_ts'] if new_qso: statusPath = stationPath + '/status.json' statusData = loadJSON(statusPath) ts = dt.timestamp() + tzOffset() if ('freq' not in statusData or statusData['freq']['ts'] < ts): statusData['freq'] = {'value': qso['freq'], 'ts': ts} with open(statusPath, 'w') as f: json.dump(statusData, f, ensure_ascii=False) qso['ts'] = time.time() while [x for x in log if x['ts'] == qso['ts']]: qso['ts'] += 0.00000001 log.insert(0, qso) yield from dbInsertQso(callsign, qso) return {'ts': qso['ts']}
def locationHandler(request): newData = yield from request.json() callsign = None stationPath = None stationSettings = None stationCallsign = None if ('token' in newData and newData['token']): callsign = decodeToken(newData) if not isinstance(callsign, str): return callsign stationPath = yield from getStationPathByAdminCS(callsign) stationSettings = loadJSON(stationPath + '/settings.json') if not stationSettings: return web.HTTPBadRequest( text='Expedition profile is not initialized.') if stationSettings and 'station' in stationSettings and\ 'callsign' in stationSettings['station'] and\ stationSettings['station']['callsign'] and\ 'activityPeriod' in stationSettings['station'] and\ stationSettings['station']['activityPeriod']: act_period = [datetime.strptime(dt, '%d.%m.%Y') for dt in\ stationSettings['station']['activityPeriod'] if dt] if act_period and act_period[0] <= datetime.utcnow() <=\ act_period[1] + timedelta(days=1): stationCallsign = stationSettings['station']['callsign'] if 'location' in newData and newData['location']: qth_now_cs = None if 'callsign' in newData and newData['callsign']: qth_now_cs = newData['callsign'] elif stationCallsign: qth_now_cs = stationCallsign logging.info('map callsign: %s' % qth_now_cs) if qth_now_cs: qth_now_cs = qth_now_cs.upper() save_qth_now_location(qth_now_cs, newData['location'],\ webRoot + '/js/qth_now_locations.json') save_qth_now_location(qth_now_cs, newData['location'],\ webRoot + '/js/qth_now_locations_all.json') if ('token' not in newData or not newData['token']) and 'location' in newData: qth = yield from get_qth_data(newData['location']) return web.json_response({'qth': qth}) fp = stationPath + '/status.json' data = loadJSON(fp) if not data: data = {} if not 'locTs' in data and 'ts' in data: data['locTs'] = data['ts'] dtUTC = datetime.utcnow() data['ts'] = int(time.time()) data['date'], data['time'] = dtFmt(dtUTC) data['year'] = dtUTC.year if 'online' in newData: data['online'] = newData['online'] if 'freq' in newData and newData['freq']: data['freq'] = {'value': newData['freq'], 'ts': data['ts']} fromCallsign = stationSettings['station']['callsign'] insertChatMessage(path=stationPath + '/chat.json',\ msg_data={'from': fromCallsign,\ 'text': '<b><i>' + newData['freq'] + '</b></i>'},\ admin=True) country = stationSettings['qthCountry'] if 'qthCountry' in stationSettings\ else None if 'location' in newData and newData['location']: location = newData['location'] country = get_country(location) data['qth'] = yield from get_qth_data(location, country=country) if 'comments' in newData: data['comments'] = newData['comments'] if 'location' in data and data['location']: data['prev'] = { 'location': data['location'][:], \ 'ts': data['locTs'] } data['locTs'] = data['ts'] data['location'] = newData['location'] if 'prev' in data: lat = [data['location'][1], data['prev']['location'][1]] lon = [data['location'][0], data['prev']['location'][0]] dlon = lon[0] - lon[1] dlat = lat[0] - lat[1] a = (sind(dlat/2))**2 + cosd(lat[0]) * cosd(lat[1]) * (sind(dlon/2)) \ ** 2 c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a)) d = c * 6373 data['d'] = d data['dt'] = data['locTs'] - data['prev']['ts'] if float(data['locTs'] - data['prev']['ts']) != 0: data['speed'] = d / ( float( data['locTs'] - data['prev']['ts'] ) \ / 3600 ) else: data['speed'] = 0 if 'qth' in newData: if 'qth' not in data: data['qth'] = {'fields':\ empty_qth_fields(country=country)} for key in newData['qth']['fields'].keys(): data['qth']['fields']['values'][int( key)] = newData['qth']['fields'][key] if 'loc' in newData['qth']: data['qth']['loc'] = newData['qth']['loc'] with open(fp, 'w') as f: json.dump(data, f, ensure_ascii=False) return web.json_response(data)