-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPsystems.py
executable file
·189 lines (164 loc) · 6.45 KB
/
APsystems.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
import os, time, dateutil.parser, datetime
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# DEBUG
# import pdb; pdb.set_trace()
##
# APsystems
# Web page communications module
# Required class functions
# setCapabilities()
# setProfile()
##
class APsystems:
def __init__(self):
self.urlLogin = "https://apsystemsema.com/ema/index.action"
self.ws = None
self.testing = False
self.lastElement = None
def doLogin(self,credentials):
#print(credentials)
loginUsername = credentials["meterUsername"]
loginPassword = credentials["meterPassword"]
formUsername = self.ws.driver.find_element_by_name("username")
formPassword = self.ws.driver.find_element_by_name("password")
formUsername.send_keys(loginUsername)
formPassword.send_keys(loginPassword)
self.ws.driver.find_element_by_id("Login").click()
self.waitFor("idClick","datetime_val",10)
if self.ws.debug:
print(self.urlLogin)
print(self.ws.meter['siteName'],loginUsername,loginPassword)
return
def doLogout(self):
linkSignout = self.ws.driver.find_element_by_link_text("Sign out")
linkSignout.click()
self.waitFor("idClick","Login",10)
return
# Once we are at the main data page, we have to cycle through
# all available ECUs.
##
def getDataRecords(self):
dataRecs = {}
# Get list of ECUs available and match them up
# with our internal unitNames.
##
ecuUnits = []
ecuUnitObj = self.ws.driver.find_elements_by_xpath("//div[@id='uniform-ecuid']//option")
# Currently selected ECU
##
try:
currentECUText = self.ws.driver.find_element_by_id('ecuidspan').text
except:
self.ws.saveScreen("error.png")
self.ws.dumpLog("error.log")
pass
currentECU = ""
for ecu in ecuUnitObj:
ecuValue = ecu.get_attribute('value')
ecuText = ecu.text
if ecuText == currentECUText:
currentECU = ecuValue
ecuUnits.append(ecuValue)
# For each ECU collect solar data
##
while ecuUnits:
#self.saveScreen(currentECU+".png")
# This adequately changes the date, but individual times are not accessible
# Data is available every 5 minutes. We may not need to actively click,
# data may be scrapable right from the chart by day.
#cmd = "document.getElementById('queryDate').value = '2020-04-11';"
#res = self.ws.driver.execute_script(cmd)
#print(res)
#cmd = "$.getDataByLevel();"
#res = self.ws.driver.execute_script(cmd)
#self.waitFor("idNotPresent","loadingDialog",10)
# Obtaining back data
# (1) select date
# (2) refresh
# (3) var charName = "#ArrayViewforHightChart";
# (4) var chart = $(charName).highcharts();
# (5) var xData = chart.series[0].points;
# (6) Time(timestamp): xData[0].series.xData
# (7) Solar(Watts) : xData[0].series.yData
##
self.ws.saveScreen(currentECU+".png")
self.ws.dumpLog(currentECU+".log")
# Collect data for ECU that do not show
# "No Data".
##
tobObj = self.ws.driver.find_element_by_class_name('selectedTime')
tob = tobObj.text
# Get the current date via javascript
##
dob = self.ws.driver.execute_script("return $('#queryDate').val();")
if tob != "No data":
dataObjs = self.ws.driver.find_elements_by_xpath("//div[@class='Module layout num']")
totalWatts = 0
for rec in dataObjs:
totalWatts = totalWatts + int(rec.text)
tm = "%s %s" % (dob,tob)
dataRecs[currentECU] = [{
'ob': tm,
'power': totalWatts
}]
ecuUnits.remove(currentECU)
if ecuUnits:
currentECU = ecuUnits[0]
# Switch to this ECU
##
fopt = "//div[@id='uniform-ecuid']//option[@value='%s']" % (currentECU)
sopt = self.ws.driver.find_element_by_xpath(fopt)
sopt.click()
self.waitFor("idNotPresent","loadingDialog",10)
return dataRecs
# This always takes us to the main data page
##
def gotoDataPage(self):
if self.ws.driver == None:
return
self.ws.driver.find_element_by_id("module_head").click()
# wait for id='powerSlider' to exist
# wait for id='navigator0' to exist
# wait for class='highcharts-axis-labels'
##
#self.waitFor("class","highcharts-axis-labels",10)
self.waitFor("idNotPresent","loadingDialog",10)
return
def gotoLoginPage(self):
if self.ws.driver == None:
return
self.ws.driver.get(self.urlLogin)
self.waitFor("idClick","Login",10)
return
def setCapabilities(self,cap):
return cap
# APsystems website does not use latest TLS 1.2 protocol
##
def setProfile(self,pro):
pro.set_preference("security.tls.version.min",1)
return pro
def setWebScraper(self,ws):
'''Pass web scraper class to this class'''
self.ws = ws
return
# Wrapper for explicit waits
# https://selenium-python.readthedocs.io/waits.html
##
def waitFor(self,elementType,elementValue,elementTimeout):
element = None
wait = WebDriverWait(self.ws.driver,elementTimeout)
try:
if elementType == "idClick":
element = wait.until(EC.element_to_be_clickable((By.ID,elementValue)))
if elementType == "class":
element = wait.until(EC.element_to_be_clickable((By.CLASS_NAME,elementValue)))
if elementType == "idNotPresent":
element = wait.until(EC.invisibility_of_element_located((By.ID,elementValue)))
if elementType == "xpath":
element = wait.until(EC.visibility_of_element_located((By.XPATH,elementValue)))
except:
pass
self.lastElement = element
return