-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwemo_data.py
145 lines (110 loc) · 5.37 KB
/
wemo_data.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
'''
This Python Code will be the one which dynamically stores the data of the Wemo
Switch when it is executed. The data is stored on average every 2-3 seconds.
'''
# PYMYSQL MODULES
import pymysql
import pymysql.cursors
# OUIMEAUX MODULES
import ouimeaux
from ouimeaux.environment import Environment
from ouimeaux.signals import receiver, statechange, devicefound
# Connect to the database
connection = pymysql.connect(host="localhost",
user='root',
passwd='ditpwd',
db="DATA")
# Initializing the ouimeaux environment
env = Environment()
class DATA:
def kv_pairs(self,dict):
'''OBTAINING THE KEY-VALUE PAIR PARAMATERS'''
'''this function will be used to seperate
key-value pairs in the dictionary for inserting
data into MYSQL'''
keys = str(list(dict.keys()))[:].replace('[', '(').replace(']', ')')
vals = str(list(dict.values()))[:].replace('[', '(').replace(']', ')')
keys = keys.replace("'","")
vals = vals.replace("'","")
return keys,vals
def SWITCH(self,list_switches):
'''This function intakes the list of switches
and returns the attributes given by the environment
NOTE: Returns a Tuple
'''
# Obtaining the switch parameters
switch_name = list_switches
switch_wemo = env.get_switch(switch_name)
# Note: Parameters are stored in dictionary
switch_param = switch_wemo.insight_params
# Delete lastchange: Unnecessary Parameter
switch_param.pop('lastchange')
# Adding DATE and TIME dictionary to SWITCH_PARAM
switch_param['TIME'] = 'CURTIME()'
switch_param['DATE'] = 'CURDATE()'
return switch_name,switch_wemo,switch_param
def CREATE_DATA(self):
try:
# Commit every data as by default it is False
connection.autocommit(True)
# Create a cursor object
cursorObject = connection.cursor()
env.start()
env.discover(3)
'''using MYSQL functions to insert date and time
USEFUL FUNCTIONS IN MYSQL:
CURRENT_TIMESTAMP
CURRENT_DATE
CURRENT_TIME
'''
try:
env.start()
env.discover(5)
while True:
for i in range(len(env.list_switches())):
#Calling the helper function
switch = self.SWITCH(list(env.list_switches())[i])
# Obtaining the switch parameters
switch_name = switch[0]
switch_wemo = switch[1]
# Note: Parameters are stored in dictionary
switch_param = switch[2]
keys = self.kv_pairs(switch_param)[0]
vals = self.kv_pairs(switch_param)[1]
print('------------')
print('Retrieving Data')
print('------------')
# 1 indicates switch is on and load is on
if switch_wemo.get_state() == 1:
#print("name:",SWITCH,"STATE:ON")
insertStatement = ("INSERT INTO DATA_"+switch_name+
"(DATE,TIME,STATE)"
"VALUES(CURDATE(),CURTIME(),\"S:ON | L:ON\")")
cursorObject.execute(insertStatement)
# 8 indicates switch is on but load is off
elif switch_wemo.get_state() == 8:
#print("name:",SWITCH,"STATE:ON")
insertStatement = ("INSERT INTO DATA_"+switch_name+
"(DATE,TIME,STATE)"
"VALUES(CURDATE(),CURTIME(),\"S:ON | L:OFF\")")
cursorObject.execute(insertStatement)
# 0 indicates switch if off
else:
#print("name:",SWITCH,"STATE:OFF")
insertStatement = ("INSERT INTO DATA_"+switch_name+
"(DATE,TIME,STATE)"
"VALUES(CURDATE(),CURTIME(),\"S:OFF | L:OFF\")")
cursorObject.execute(insertStatement)
#inserting new params
cursorObject.execute('INSERT INTO ' +switch_name+ ' %s VALUES %s' % (keys, vals))
env.wait(1)
except (KeyboardInterrupt, SystemExit):
print("Goodbye!")
except Exception as e:
print("Exeception occured:{}".format(e))
pass
finally:
connection.close()
if __name__ == '__main__':
DATA = DATA()
DATA.CREATE_DATA()