-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
executable file
·89 lines (76 loc) · 2.76 KB
/
setup.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
#! /usr/bin/env python3
# Copyright 2017 Marcel Beyer
#
# This file is part of piTemp.
#
# piTemp is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# piTemp is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with piTemp. If not, see <http://www.gnu.org/licenses/>.
import os
import sys
import psycopg2
from os.path import expanduser
import configparser
#is configured?
if os.path.exists(expanduser("~")+'/.piTemp') == False:
print("Creating config-dir ~/.piTemp ...")
os.mkdir(expanduser("~")+'/.piTemp')
if os.path.isfile(expanduser("~")+'/piTemp.ini') == False:
f = open(expanduser("~")+'/.piTemp/piTemp.ini', 'w')
f.write('')
f.close()
#open config
config = configparser.ConfigParser()
config.read(expanduser("~")+'/.piTemp/piTemp.ini')
#input
print('We need an postgresql-database...')
dbname=input('Database: ')
dbuser=input('Username: ')
dbpass=input('Password: ')
dbhost=input('Host: ')
print('Connecting to database...')
try:
connect_str = "dbname='"+dbname+"' user='"+dbuser+"' host='"+dbhost+"' password='"+dbpass+"'"
conn = psycopg2.connect(connect_str)
conn.autocommit = True
cursor = conn.cursor()
except Exception as e:
print("There was an error while connecting to the database.")
sys.exit(1)
# set database-settings in config
if ('DB' in config) == False:
print('Creating DB-settings in config')
config.add_section('DB')
else:
print('Updating DB-settings in config')
config['DB']['host'] = dbhost
config['DB']['user'] = dbuser
config['DB']['pass'] = dbpass
config['DB']['dbname'] = dbname
print('Setting up database...')
# create table sensors
cursor.execute("CREATE TABLE IF NOT EXISTS sensors (sensor CHAR(15) PRIMARY KEY, name VARCHAR(40), apiid INT, apikey TEXT);")
# create table temps
cursor.execute('''CREATE TABLE IF NOT EXISTS temps (id SERIAL PRIMARY KEY, sensor CHAR(15) NOT NULL REFERENCES sensors (sensor),
value DECIMAL, time TIMESTAMP DEFAULT NOW() );''')
cursor.close()
conn.close()
#cursor.close()
# connection to tempSrvr
print("If you're hosting a tempSrvr (https://github.com/marcelb98/tempSrvr) we can send all temperatures to it.")
print("Just provide the URL of the server (e.g. https://tempsrvr.example.org).")
tempsrvr = input('tempSrvr-URL: ')
config['tempSrvr']['url'] = tempsrvr
# write config to file
with open(expanduser("~")+'/.piTemp/piTemp.ini', 'w') as configfile:
config.write(configfile)
print("Finished.")