-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInitialize.py
executable file
·365 lines (304 loc) · 12.2 KB
/
Initialize.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
##############################################################################
# NetatmoInflux
#
# Python script for importing Netatmo data into an InfluxDB.
#
# (C) 2015-2019, Ulrich Thiel
# ulrich.thiel@sydney.edu.au
##############################################################################
#This file is part of NetatmoInflux.
#
#NetatmoInflux 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.
#
#NetatmoInflux 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 WeatherStats. If not, see <http://www.gnu.org/licenses/>.
##############################################################################
##############################################################################
#This script automatically updates all data from Netatmo
##############################################################################
##############################################################################
#imports
import sqlite3
import os.path
from lib import ColorPrint
from lib import Netatmo
import getpass
import sys
import signal
##############################################################################
# database connection
dbexists = os.path.isfile(os.path.dirname(os.path.realpath(__file__))+"/Netatmo.db")
dbconn = sqlite3.connect(os.path.dirname(os.path.realpath(__file__))+'/Netatmo.db')
dbcursor = dbconn.cursor()
##############################################################################
#Ctrl+C handler
def signal_handler(signal, frame):
ColorPrint.ColorPrint("\nYou pressed Ctrl+C", "error")
SetDates(dbconn, dbcursor)
dbconn.commit()
dbconn.close()
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
##############################################################################
#Creates empty database
def CreateEmptyDB():
dbcursor.execute(\
"CREATE TABLE \"Accounts\" (\n" \
"`User` TEXT,\n" \
"`Password` TEXT,\n" \
"`ClientID` TEXT,\n" \
"`ClientSecret` TEXT,\n" \
"PRIMARY KEY(User) ON CONFLICT REPLACE)\n"\
)
dbcursor.execute(\
"CREATE TABLE \"Modules\" (\n" \
"`Id` TEXT,\n" \
"`Name` TEXT,\n" \
"PRIMARY KEY(Id))\n"\
)
dbcursor.execute(\
"CREATE TABLE \"Sensors\" (\n" \
"`Id` INTEGER,\n" \
"`Module` TEXT,\n" \
"`Measurand` INTEGER,\n" \
"`Unit` INTEGER,\n" \
"`Name` TEXT,\n" \
"`Calibration` REAL,\n" \
"`Interval` INTEGER,\n" \
"PRIMARY KEY(Id))\n" \
)
dbcursor.execute(\
"CREATE TABLE \"Locations\" (\n" \
"`Id` INTEGER,\n" \
"`PositionNorth` REAL,\n" \
"`PositionEast` REAL,\n" \
"`Elevation` INTEGER,\n" \
"`Name` TEXT,\n" \
"`Timezone` TEXT,\n" \
"PRIMARY KEY(Id))\n" \
)
dbcursor.execute(\
"CREATE TABLE \"ModuleLocations\" (\n" \
"`Module` TEXT,\n" \
"`Begin` TEXT,\n" \
"`End` TEXT,\n" \
"`Location` INTEGER,\n" \
"PRIMARY KEY(Module, Begin, End))\n" \
)
dbcursor.execute(\
"CREATE VIEW \"ModulesView\" AS \n" \
"SELECT Modules.Id AS \"Module Id\", \n" \
"Modules.Name AS \"Module Name\", \n" \
"STRFTIME(\"%Y-%m-%d %H:%M:%SZ\", DATETIME(ModuleLocations.Begin, \'unixepoch\')) AS \"Begin\" , \n" \
"STRFTIME(\"%Y-%m-%d %H:%M:%SZ\", DATETIME(ModuleLocations.End, \'unixepoch\')) AS \"End\", \n" \
"Locations.Name AS \"Location Name\", \n"\
"ModuleLocations.Begin AS \"Begin Timestamp\", \n" \
"ModuleLocations.End AS \"End Timestamp\", \n" \
"Locations.Timezone AS \"Timezone\" \n" \
"FROM Modules \n" \
"INNER JOIN ModuleLocations ON ModuleLocations.Module = Modules.Id \n" \
"INNER JOIN Locations ON ModuleLocations.Location = Locations.Id \n" \
)
dbcursor.execute(\
"CREATE TABLE \"InfluxDB\" ( \n" \
"`Id` INTEGER, \n" \
"`Host` TEXT, \n" \
"`Port` INTEGER, \n" \
"`User` TEXT, \n" \
"`Password` TEXT, \n" \
"`Database` TEXT, \n" \
"`SSL` INTEGER, \n" \
"`SSLVerify` INTEGER, \n" \
"PRIMARY KEY(Id)) \n" \
)
dbconn.commit()
##############################################################################
#add all devices/modules for specific account
def InitializeAccount(account):
username = account[0]
password = account[1]
clientId = account[2]
clientSecret = account[3]
print "Getting modules for account " + username
if password == None:
password = getpass.getpass()
netatm = Netatmo.NetatmoClient(username, password, clientId, clientSecret)
netatm.getStationData()
#first, update modules and sensors
for id in netatm.devicemoduleids:
if id[1] == None:
moduleid = dbcursor.execute("SELECT Id FROM Modules WHERE Id IS \""+str(id[0])+"\"").fetchone()
else:
moduleid = dbcursor.execute("SELECT Id FROM Modules WHERE Id IS \""+str(id[1])+"\"").fetchone()
#if not exists, add new device/module and its sensors
if moduleid == None:
#check if location exists
location = netatm.locations[id]
locationid = dbcursor.execute("SELECT Id FROM Locations WHERE PositionNorth IS "+str(location[0][1])+" AND PositionEast IS "+str(location[0][0])+" AND Elevation IS "+str(location[1])+" AND Name IS \""+location[3]+"\" AND Timezone IS \""+location[2]+"\"").fetchone()
if locationid == None:
dbcursor.execute("INSERT INTO Locations (PositionNorth,PositionEast,Elevation,Name,Timezone) VALUES ("+str(location[0][1]) + "," + str(location[0][0]) + "," + str(location[1]) + ",\"" + location[3] + "\",\"" + location[2] + "\")")
locationid = (dbcursor.execute("SELECT last_insert_rowid();").fetchone())[0]
else:
locationid = locationid[0]
if id[1] == None:
moduleid = id[0]
else:
moduleid = id[1]
dbcursor.execute("INSERT INTO Modules (Id,Name) VALUES (\""+moduleid+"\",\"Netatmo "+netatm.types[id]+"\")")
sensorids = []
#set correct units
for measurand in netatm.measurands[id]:
if measurand == "CO2":
unit = "ppm"
if measurand == "Humidity":
unit = "%"
if measurand == "Noise":
unit = "dB"
if measurand == "Temperature":
if netatm.units[id] == 0:
unit = u"\u00b0"+"C"
else:
unit = u"\u00b0"+"F"
if measurand == "Wind":
if netatm.windunits[id] == 0:
unit = "kph"
elif netatm.windunits[id] == 1:
unit = "mph"
elif netatm.windunits[id] == 2:
unit = "ms"
elif netatm.windunits[id] == 3:
unit = "Bft"
elif netatm.windunits[id] == 4:
unit = "kn"
if measurand == "Pressure":
if netatm.pressureunits[id] == 0:
unit = "mbar"
elif netatm.pressureunits[id] == 1:
unit = "inHg"
elif netatm.pressureunits[id] == 2:
unit = "mmHg"
if measurand == "Rain":
measurand = "Precipitation"
unit = "mm"
dbcursor.execute("INSERT INTO Sensors (Module,Measurand,Unit,Name,Calibration,Interval) VALUES (\""+str(moduleid)+"\",\""+measurand+"\",\""+unit+"\",\""+measurand+" sensor\",0,300)") #one point every 5 minutes for Netatmo
sensorid = (dbcursor.execute("SELECT last_insert_rowid();").fetchone())[0]
sensorids.append(sensorid)
#get first time stamp for module and set this as Begin for location of module
measurandsstring = ""
measurands = netatm.measurands[id]
for i in range(0,len(measurands)):
measurandsstring = measurandsstring + measurands[i]
if i < len(measurands)-1:
measurandsstring = measurandsstring + ","
data = netatm.getMeasure(id[0],id[1],"max",measurandsstring,None,None,None,"false")
minservertimestamp = min(map(int,data.keys()))
dbcursor.execute("INSERT INTO ModuleLocations (Module,Begin,Location) VALUES (\""+moduleid+"\","+str(minservertimestamp)+","+str(locationid)+")")
if id[1] == None:
ColorPrint.ColorPrint("Added device "+id[0]+" ("+netatm.types[id]+") at location "+str(locationid)+" ("+netatm.locations[id][3]+")", "okgreen")
else:
ColorPrint.ColorPrint("Added module "+id[1]+" ("+netatm.types[id]+") at location "+str(locationid)+" ("+netatm.locations[id][3]+")", "okgreen")
else:
moduleid = moduleid[0]
dbconn.commit()
##############################################################################
#This iterates through all accounts
def InitializeAccounts():
dbcursor.execute("SELECT * From Accounts")
res = dbcursor.fetchall()
for account in res:
InitializeAccount(account)
##############################################################################
#function to add a Netatmo account to the data base
def AddAccount():
print "-----------------------"
print "| Add Netatmo account |"
print "-----------------------"
username = raw_input("User: ")
password = getpass.getpass()
ColorPrint.ColorPrint("Do you want to save the password as clear text to the database?\nIf not, you have to enter it on any update.", "warning")
savepassw = raw_input("Save (y/n)?: ")
if not (savepassw == "Y" or savepassw == "y"):
savepassw = False
else:
savepassw = True
ColorPrint.ColorPrint("You have to grant client access for WeatherStats. If not done yet,\nlog into\n\thttps://dev.netatmo.com/dev/myaccount\nand add an app called \"WeatherStats\". You will be given a client \nid and a client secret.", "warning")
clientId = raw_input("Client id: ")
clientSecret = raw_input("Client secret: ")
#check if it works
netatm = Netatmo.NetatmoClient(username, password, clientId, clientSecret)
netatm.getStationData()
if savepassw:
dbcursor.execute(\
"INSERT INTO Accounts (User, Password, ClientID, ClientSecret)\n"\
"VALUES (\"" + username + "\",\"" + password + "\",\"" + clientId + "\",\"" + clientSecret + "\")"
)
else:
dbcursor.execute(\
"INSERT INTO Accounts (User, Password, ClientID, ClientSecret)\n"\
"VALUES (\"" + username + "\",NULL,\"" + clientId + "\",\"" + clientSecret + "\")"
)
ColorPrint.ColorPrint("Account added", "okgreen")
dbconn.commit()
##############################################################################
#function to add an InfluxDB
def AddInflux():
print "-----------------------"
print "| Add InfluxDB |"
print "-----------------------"
host = raw_input("Host: ")
port = raw_input("Port: ")
user = raw_input("User: ")
password = getpass.getpass()
ColorPrint.ColorPrint("Do you want to save the password as clear text to the database?\nIf not, you have to enter it on any update.", "warning")
savepassw = raw_input("Save (y/n)?: ")
if not (savepassw == "Y" or savepassw == "y"):
savepassw = False
else:
savepassw = True
db = raw_input("Database: ")
ssl = raw_input("Use SSL (y/n)?: ")
if not (ssl == "Y" or ssl == "y"):
ssl = 0
else:
ssl = 1
sslVerify = 1
if savepassw == True:
dbcursor.execute(\
"INSERT INTO InfluxDB (Host, Port, User, Password, Database,SSL)\n"\
"VALUES (\"" + host + "\",\"" + port + "\",\"" + user + "\",\"" + password + "\", \""+db+"\", "+str(ssl)+" )"
)
else:
dbcursor.execute(\
"INSERT INTO InfluxDB (Host, Port, User, Password, Database,SSL,SSLVerify)\n"\
"VALUES (\"" + host + "\",\"" + port + "\",\"" + user + "\", NULL, \""+db+"\", "+str(ssl)+", "+str(sslVerify)+" )"
)
ColorPrint.ColorPrint("InfluxDB added", "okgreen")
dbconn.commit()
##############################################################################
#Main
#First, check if database exists and create empty one if not
if dbexists == False:
CreateEmptyDB()
ColorPrint.ColorPrint("New database created", "okgreen")
#Now, add accounts
res = dbcursor.execute("SELECT * FROM Accounts").fetchall()
if len(res) == 0:
AddAccount()
# Initialize accounts
InitializeAccounts()
#Add InfluxDB
res = dbcursor.execute("SELECT * FROM InfluxDB").fetchall()
if len(res) == 0:
AddInflux()
dbconn.commit()
dbconn.close()