-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestWemoClass.py
More file actions
91 lines (74 loc) · 3.55 KB
/
Copy pathtestWemoClass.py
File metadata and controls
91 lines (74 loc) · 3.55 KB
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
import wemoFetchClass
import ConfigParser
import logging
import datetime
import sys
workingdir = sys.argv[1]
def writeRecentActivity():
try:
activitycheckfile = open(workingdir + "lastKnownActivity.log", "w")
file.write(activitycheckfile,
"Last known activity in ouimeaux application: " + datetime.datetime.now().strftime(
"%Y-%m-%d %H:%M:%S"))
file.close(activitycheckfile)
except Exception as e:
logging.exception("Couldn't write to activity log file: " + str(e.message))
raise
if __name__ == "__main__":
print("")
print("Object oriented test of Local Wemo Fetch Project")
print("-------------------------------------------------")
logging.basicConfig(level=logging.ERROR, filename= (workingdir + 'wemoLinuxLog.log'))
writeRecentActivity()
config = ConfigParser.ConfigParser()
configpath = (workingdir + "config.cfg")
config.readfp(open(configpath))
infiniteloop_param = int(config.get('LOOP BREAKER', 'infiniteloop'))
localdb_config_parameters = {
"db_type": config.get('Database Information', 'db_type'),
"databasename": config.get('Database Information', 'databasename'),
"serviceaccount": config.get('Database Information', 'serviceaccount'),
"db_password": config.get('Database Information', 'password'),
"server_ip": config.get('Database Information', 'serverip'),
"Seconds For Environment Discovery": float(config.get('API Parameters', 'numsecondsfordiscovery')),
"Minutes to Gather Data": float(config.get('API Parameters', 'numminutestogatherdata')),
"Delay in Seconds When Fetching Data": float(config.get('API Parameters', 'fetchdatadelayseconds')),
}
#Instantiate object to do all our work:
testObject = wemoFetchClass.LocalNetworkWemoFetcher(localdb_config_parameters)
#Used for debugging the shell script:
# while 1 == 1:
# print("Endless loop!!!!!! " + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
# time.sleep(2)
while infiniteloop_param == 1:
try:
#Check to see if the program should continue running based on the config:
config.readfp(open(configpath))
infiniteloop_param = int(config.get('LOOP BREAKER', 'infiniteloop'))
print("Infinite loop still in effect? " + str(infiniteloop_param))
#Start the ouimeaux environment:
testObject.startStopWemoEnvironment("start")
#Fetch wemo data:
currentdevices = testObject.getDeviceHardwareIDs(testObject.wemoenvironment)
print(currentdevices)
testObject.fetchdevicedata()
datatoload = testObject.aggregateusagedata()
print("Rows in local cache: " + str(len(datatoload))) #Show how many rows are in cache waiting to go to external DBMS
#Try to load the data to external DBMS:
try:
testObject.InsertOrUpdateDatabase(datatoload)
except Exception as e:
logging.exception(str(e.message))
pass
#Stop and kill the ouimeaux environment to avoid greenlet hangs:
testObject.startStopWemoEnvironment("stop")
writeRecentActivity()
except Exception as e:
logging.exception("Unknown error in Python application: " + str(e.message))
print(e.message)
raise
print("Ending program. Infinite loop param: " + str(infiniteloop_param))
try:
testObject.closeconnection()
except NameError:
pass