-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpairtrk.py
executable file
·161 lines (149 loc) · 7.49 KB
/
pairtrk.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/python3
#
# Silent Wings interface --- pairing flarms and trackers
#
import json
import time
import sys
import os
import MySQLdb
import datetime
import urllib.parse
from ognddbfuncs import *
import config
from gistfuncs import unobscure
#
# This script set the pairing between OGN trackers and flarms that are on the same glider, so it become a virtual single device
#
action='list'
trk='ALL'
tflarmid=''
towner=''
deleteyn='N'
active='N'
#print (sys.argv)
if len(sys.argv) >1:
arg1 = sys.argv[1]
action = arg1[0:]
if len(sys.argv) >2:
arg2 = sys.argv[2]
trk = arg2[0:9].upper()
if len(sys.argv) > 4:
arg3 = sys.argv[3]
tflarmid = arg3[0:9].upper()
arg4 = sys.argv[4]
towner = arg4[0:]
if len(sys.argv) > 6:
arg5 = sys.argv[5]
deleteyn = arg5[0:1].upper()
arg6 = sys.argv[6]
active = arg6[0:1]
#print (len(sys.argv), "Action=", action, "Tracker=", trk, "FlarmID=", tflarmid, "Owner=", towner, deleteyn, active)
localtime = datetime.datetime.now() # get today's date
today = localtime.strftime("%y/%m/%d %H:%M:%S") # in string format yymmdd
DBpath = config.DBpath # use the configuration DB path
DBname = config.DBname # use the configuration DB name
DBtable = config.DBtable # use the configuration DB table
DBname ='APRSLOG'
DBtable = 'TRKDEVICES'
html1 = """<head><meta charset="UTF-8"></head><TITLE>Get the pairing of trackers with flarms</TITLE> <IMG src="./gif/ogn-logo-150x150.png" border=1 alt=[image]><H1>The pairing so far are: </H1> <HR> <P>Today is: %s and we have %d Pairs on TRKDEVICES table. <br /> <br /> Do you want to <a href=%s/SWS/pairtrkadd.html >Add a new pairing device: </a> <br /> </p> </HR> """
html2 = """<center><table><tr><td><pre>"""
html3 = """</pre></td></tr></table></center>"""
html4 = '<a href='+config.SWSserver+'SWS/pairtrk.php?action=edit&trk=%s&flarmid=%s&owner=%s&active=%s'
#
conn = MySQLdb.connect(host=config.DBhost, user=config.DBuser, passwd=unobscure(config.DBpasswd).decode(), db=DBname, connect_timeout=1000) # connect with the database
cursD = conn.cursor() # connect with the DB set the cursor
if action == 'update': # the update order
if trk[0:3] != 'OGN' and (tflarmid[0:3] == 'FLR' or tflarmid[0:3] == 'ICA'): # check that we are pairing OGN to FLR/ICA
print ("Pairing error, you can only pair OGN tracker with Flarms")
conn.close()
exit(1)
if deleteyn == 'Y': # if we want to delete the record
cmd1 = "DELETE FROM "+DBtable+" WHERE id = '"+trk+"' ;"
else:
cmd1 = "UPDATE "+DBtable+" SET " # if we want just to update the record
if towner != '':
cmd1 += " owner = '"+towner+"' , " # if updated the owner
if active != '':
cmd1 += " active = '"+active+"' , " # if updated the active
if tflarmid != '':
cmd1 += " flarmid = '"+tflarmid+"' WHERE id = '"+trk+"' ;" # or just the Flarmid
#print (cmd1)
if getognchk(trk[3:]) and getognchk(tflarmid[3:]): # check that the devices are registered in order to be consistent
try:
cursD.execute(cmd1) # delete or update the DB
except MySQLdb.Error as e:
print ("SQL error: ",e)
conn.commit()
else: # warn about the error
print ("UPDATE Pairing Error either the OGN tracker "+trk+" or the FlarmID "+tflarmid+" are not registered on the OGN DDB")
conn.close()
exit(0)
if action == 'add': # adding a new pair ogn <==> flarm
#print ("ADD Action=", action, "Tracker=", trk, "FlarmID", tflarmid, "Owner=", towner)
if trk[0:3] != 'OGN' and (tflarmid[0:3] == 'FLR' or tflarmid[0:3] == 'ICA'):
print ("Pairing error, you can only pair OGN tracker with Flarms")
conn.close()
exit(1)
ognreg=getognreg(trk[3:]) # the the information fro the OGN DDB
flrreg=getognreg(tflarmid[3:]) # glider registration
cn=getogncn(tflarmid[3:]) # glider competition ID
model=getognmodel(tflarmid[3:]) # glider model
if getognchk(trk[3:]) and getognchk(tflarmid[3:]): # checkk that the devices are rgistered on the OGN DDB
cmd1 = "INSERT INTO "+DBtable+" (id, owner, spotid, compid, model, registration, active, devicetype, flarmid) VALUES ( '"+trk+"', '"+towner+"', '"+ognreg+"' , '"+cn+"', '"+model+"', '"+flrreg+"', '1', 'OGNT', '"+tflarmid+"' ) ; "
#print ("cmd1:",cmd1)
try:
cursD.execute(cmd1)
except MySQLdb.Error as e:
print ("Pairing error, ID already exist on the DB --- SQL error: ",e)
conn.commit()
else:
print ("ADD Pairing Error either the OGN tracker "+trk+" or the FlarmID "+tflarmid+" are not registered on the OGN DDB")
conn.close()
exit(0)
#
# action LIST ALL pair
#
cmd1 = "select count(*) from "+DBtable+" where devicetype = 'OGNT' ;"
try:
cursD.execute(cmd1)
except MySQLdb.Error as e:
print ("SQL error: ",e)
row = cursD.fetchone()
nrecs=row[0]
if trk == 'ALL':
cmd1 = "select * from "+DBtable+" where id like '%OGN%' and devicetype = 'OGNT' and active = '1' ;"
else:
cmd1 = "select * from "+DBtable+" where id = '"+trk+"' and devicetype = 'OGNT' ;"
#print cmd
try:
cursD.execute(cmd1)
except MySQLdb.Error as e:
print ("SQL error: ",e)
print (html1% (today,nrecs, config.SWSserver))
print (html2)
print ("<b> <a> TRKDEV IDTRK REGTRK REGIST CID ACT DEVTYP Flarm Owner </a> </b> <br />")
for row in cursD.fetchall(): # search for the first 20 the rows
# flarmid is the first field
id1 = row[0]
owner = row[1]
spotid = row[2]
cn = row[4]
regis = row[6]
active = row[7]
devtype = row[8]
flarmid = row[9]
if flarmid == None or flarmid == '' or len(flarmid) <9:
flarmid = getognflarmid(regis)
else:
if regis.strip() != "" and flarmid != getognflarmid(regis):
print("Warning the registered flarmid and pairing flarmid does not match:", regis, getognflarmid(regis))
if regis == '':
regis = getognreg(flarmid[3:9])
idfromdb = getognreg(id1[3:9])
if spotid != idfromdb:
print ("Warning reg and name do not match")
print ("<a> TRKDEV: %-9s %-9s %-7s %-3s %-3s %-4s %-9s %-36s "% (id1, idfromdb, regis, cn, active, devtype, flarmid, owner), "</a>", html4%(id1, flarmid, urllib.parse.quote(owner), active), ">EDIT</a>")
print (html3)
conn.close()
exit(0)