new server
parent
4b6ff62d26
commit
f6cc44a361
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"wait": 0.5,
|
||||
"gpg_keyid":"25A4CF79414F10FD",
|
||||
"allowed_ip":{"127.0.0.1":"25A4CF79414F10FD"},
|
||||
"device": "rasolar"
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
create database rasolar;
|
||||
create user 'rasolar'@'localhost' identified by 'rasolar';
|
||||
grant all privileges on rasolar.* to 'rasolar'@'localhost';
|
||||
create table if not exists rasolar.measures (time bigint unsigned not null, id int unsigned not null, value int unsigned not null, index(time,id));
|
||||
create table if not exists rasolar.ids (id integer primary key,device varchar(32),varname varchar(32),sensor varchar(32),i2c int unsigned,sensorsub varchar(32));
|
130
pyweb.py
130
pyweb.py
|
@ -1,9 +1,80 @@
|
|||
from bottle import get,post,request,Bottle,run
|
||||
import time
|
||||
from bottle import get,post,request,Bottle,run,template
|
||||
import threading,time,json,zlib,gnupg,socket,psutil,os,sys,pymysql,queue,_thread
|
||||
|
||||
payload_haus=["wasser","gas","kwh_haus","kwh_herd"]
|
||||
payload_kfz=["km","volumen","preis"]
|
||||
|
||||
pathname = os.path.dirname(sys.argv[0])
|
||||
abspath=os.path.abspath(pathname)
|
||||
configfile=abspath+"/config.json"
|
||||
try:
|
||||
cf=open(configfile,"r")
|
||||
except:
|
||||
cf=open(configfile+".template","r")
|
||||
|
||||
log_conf=json.load(cf)
|
||||
cf.close()
|
||||
|
||||
parameter={"device":socket.gethostname(),"allowed_ip":{"127.0.0.1":"25A4CF79414F10FD"},"gpg_keyid":"25A4CF79414F10FD"}
|
||||
for n in parameter:
|
||||
if n in log_conf:
|
||||
parameter[n]=log_conf[n]
|
||||
if "sqlserver" in log_conf:
|
||||
hostname="banana"
|
||||
if "host" in log_conf['sqlserver']:
|
||||
hostname=log_conf['sqlserver']['host']
|
||||
port=24049
|
||||
if "port" in log_conf['sqlserver']:
|
||||
port=int(log_conf['sqlserver']['port'])
|
||||
|
||||
#sqlinsert="insert into measures (time,id,value) values ({0:d},{1:d},{2:d})"
|
||||
sqlinsert="insert into measures (time,id,value) values "
|
||||
|
||||
gpg=gnupg.GPG()
|
||||
|
||||
measdata={}
|
||||
|
||||
_HASH="hash"
|
||||
_SIGNEDGPG="signed_gpg"
|
||||
_PAYLOAD="payload"
|
||||
_MEASURES="measure"
|
||||
_BEGINSIGNATURE="-----BEGIN PGP SIGNATURE-----"
|
||||
_BEGINMESSAGE="-----BEGIN PGP SIGNED MESSAGE-----"
|
||||
_BEGINHASH="Hash:"
|
||||
_JSONDATA="data"
|
||||
_JSONSIGNEDDATA="signed_data"
|
||||
_JSONENCRYPTDATA="encrypted_data"
|
||||
|
||||
def insert_sql(measdata):
|
||||
tsi=sqlinsert
|
||||
for h in measdata:
|
||||
md=measdata[h]
|
||||
for m in md['measures']:
|
||||
try:
|
||||
stime=int(m)
|
||||
except:
|
||||
print("wrong entry")
|
||||
else:
|
||||
tsi=tsi+'('+str(m)+','+h+','+str(md['measures'][m])+'),'
|
||||
tsi=tsi[:-1]+';'
|
||||
try:
|
||||
# mydb=pymysql.connect(read_default_file="~/.my.cnf",database="rasolar")
|
||||
mydb=pymysql.connect(host='localhost',user='rasolar',password='rasolar',database="rasolar")
|
||||
except:
|
||||
print("could not connect to mysql")
|
||||
with open("missed.sql","a") as sqlstore:
|
||||
sqlstore.write(tsi)
|
||||
else:
|
||||
mycursor=mydb.cursor(pymysql.cursors.DictCursor)
|
||||
hash_ids=mycursor
|
||||
mycursor.execute(tsi)
|
||||
mycursor.close()
|
||||
mydb.commit()
|
||||
mydb.close()
|
||||
print(tsi)
|
||||
print(measdata)
|
||||
|
||||
|
||||
app=Bottle()
|
||||
|
||||
@app.get('/')
|
||||
|
@ -52,5 +123,58 @@ def submit_kfz():
|
|||
indata[x]=request.forms.get(x)
|
||||
print(indata)
|
||||
|
||||
@app.post('/data/<hash_id>')
|
||||
def dataimport(hash_id):
|
||||
print(hash_id)
|
||||
timestart=time.time()
|
||||
# check if request comes from allowed ip
|
||||
if (request.remote_addr in parameter['allowed_ip']):
|
||||
# hash must be the used gpg key id
|
||||
if (hash_id in parameter['allowed_ip'][request.remote_addr]):
|
||||
print("correct id")
|
||||
# check, if json is transmitted
|
||||
try:
|
||||
json_in=json.loads(request.json)
|
||||
print(json_in)
|
||||
except:
|
||||
print("no json")
|
||||
else:
|
||||
measdata={}
|
||||
if _JSONDATA in json_in:
|
||||
measdata=json_in[_JSONDATA]
|
||||
if _JSONSIGNEDDATA in json_in:
|
||||
vpgp=gpg.verify(json_in[_JSONSIGNEDDATA])
|
||||
if hash_id != vpgp.key_id:
|
||||
print("signature does not fit hash id")
|
||||
else:
|
||||
signed_in=json_in[_JSONSIGNEDDATA].split("\n")
|
||||
signed_in[signed_in.index(_BEGINSIGNATURE):]=""
|
||||
del signed_in[signed_in.index(_BEGINMESSAGE)]
|
||||
del signed_in[signed_in.index("")]
|
||||
for h in signed_in:
|
||||
if _BEGINHASH in h:
|
||||
del signed_in[signed_in.index(h)]
|
||||
if len(signed_in)>0:
|
||||
measdata=json.loads(signed_in[0])
|
||||
print("signature verified")
|
||||
if _JSONENCRYPTDATA in json_in:
|
||||
dpgp=gpg.decrypt(json_in[_JSONENCRYPTDATA])
|
||||
if hash_id != dpgp.key_id:
|
||||
print("signature of encrypted data does not fit hash id")
|
||||
else:
|
||||
measdata=json.loads(dpgp.data)
|
||||
print("signature of encrypted data verified")
|
||||
if len(measdata)==0:
|
||||
print("no data available")
|
||||
else:
|
||||
_thread.start_new_thread(insert_sql,(measdata,))
|
||||
|
||||
run(app,host="localhost",port=8080)
|
||||
|
||||
else:
|
||||
print("wrong id")
|
||||
else:
|
||||
print("not allowed client address")
|
||||
|
||||
|
||||
|
||||
run(app,host="localhost",port=8081)
|
||||
|
|
Loading…
Reference in New Issue