-
Notifications
You must be signed in to change notification settings - Fork 0
/
dia_wirela.py
89 lines (80 loc) · 3.15 KB
/
dia_wirela.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
from getmac import get_mac_address
import time
import pymysql
import datetime
class Wirela_Diagnosis():
"""
This module is a diagnostic solution. a connection to a SQL server is established and then messages can be
passed to help the developer improve the system. the formatting of a message is as follows:
- The Mac address of the Raspberry Pi
- The current date with time
- The runtime of the program/Raspberry Pi
"""
def __init__(self):
self.host = None
self.port = None
self.user = None
self.password = None
self.db = None
try:
data = open('/home/pi/Wirela_Air_Settings/mysql_logging.txt', 'r')
for i in data:
file = re.split('=|"|\n', i)
if file[0] == "host":
self.host = str(file[2])
if file[0] == "port":
self.port = int(file[2])
if file[0] == "user":
self.user= str(file[2])
if file[0] == "passwd":
self.password = str(file[2])
if file[0] == "db":
self.db = str(file[2])
self.my_db = pymysql.connect(host=self.host, port=self.port, user=self.user, passwd=self.password, db=self.db)
self.sql_cursor = self.my_db.cursor()
self.eth_mac = get_mac_address()
except:
print("No access from the mySQL server. The access data to the server are not stored in the settings or there is no internet connection. ")
print("If you do not have access data in the folder /home/pi/Wirela_Air_Settings/database_login.txt, please contact my administrator.")
self.timestamp_start = None
self.timestamp_now = None
def remember_time_now(self):
"""
The system start time is recorded here.
:return:
"""
self.timestamp_start = datetime.datetime.timestamp(datetime.datetime.now())
def writes_to_database(self, message):
"""
Here the data is formatted and transferred to the SQL server.
:param message:
:return:
"""
try:
now = datetime.datetime.now()
self.timestamp_now = datetime.datetime.timestamp(now)
run_time = (self.timestamp_now- self.timestamp_start) / 86400 # sek to day
sql = """INSERT INTO diagnosis(
mac_adr,
timestamp,
runtime,
message
)
VALUES (%s, %s, %s, %s )
"""
ts = time.time()
time_now = datetime.datetime.utcfromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
recordTuple = (self.eth_mac, time_now, run_time, str(message))
try:
self.sql_cursor.execute(sql, recordTuple)
self.my_db.commit()
print("Sent to the database")
except:
self.my_db.rollback()
print("Error - message not received")
except:
pass
if __name__ == '__main__':
diagnosis = Wirela_Diagnosis()
diagnosis.remember_time_now()
diagnosis.writes_to_database("test message")