diff --git a/TWCManager.py b/TWCManager.py index 2988dcb1..c44661a1 100644 --- a/TWCManager.py +++ b/TWCManager.py @@ -110,9 +110,7 @@ def time_now(): ".%f" if config['config']['displayMilliseconds'] else ""))) def load_settings(): - global config, scheduledAmpsStartHour, scheduledAmpsEndHour, \ - scheduledAmpsDaysBitmap, hourResumeTrackGreenEnergy, kWhDelivered, \ - carapi, homeLat, homeLon + global config, scheduledAmpsDaysBitmap, kWhDelivered, carapi, homeLat, homeLon try: fh = open(config['config']['settingsPath'] + "/TWCManager.settings", 'r') @@ -134,16 +132,16 @@ def load_settings(): m = re.search(r'^\s*scheduledAmpsStartHour\s*=\s*([-0-9.]+)', line, re.MULTILINE) if(m): - scheduledAmpsStartHour = float(m.group(1)) + master.setScheduledAmpsStartHour(float(m.group(1))) if(config['config']['debugLevel'] >= 10): - print("load_settings: scheduledAmpsStartHour set to " + str(scheduledAmpsStartHour)) + print("load_settings: scheduledAmpsStartHour set to " + str(m.group(1))) continue m = re.search(r'^\s*scheduledAmpsEndHour\s*=\s*([-0-9.]+)', line, re.MULTILINE) if(m): - scheduledAmpsEndHour = float(m.group(1)) + master.setScheduledAmpsEndHour(float(m.group(1))) if(config['config']['debugLevel'] >= 10): - print("load_settings: scheduledAmpsEndHour set to " + str(scheduledAmpsEndHour)) + print("load_settings: scheduledAmpsEndHour set to " + str(m.group(1))) continue m = re.search(r'^\s*scheduledAmpsDaysBitmap\s*=\s*([-0-9.]+)', line, re.MULTILINE) @@ -155,9 +153,9 @@ def load_settings(): m = re.search(r'^\s*hourResumeTrackGreenEnergy\s*=\s*([-0-9.]+)', line, re.MULTILINE) if(m): - hourResumeTrackGreenEnergy = float(m.group(1)) + master.setHourResumeTrackGreenEnergy(float(m.group(1))) if(config['config']['debugLevel'] >= 10): - print("load_settings: hourResumeTrackGreenEnergy set to " + str(hourResumeTrackGreenEnergy)) + print("load_settings: hourResumeTrackGreenEnergy set to " + str(m.group(1))) continue m = re.search(r'^\s*kWhDelivered\s*=\s*([-0-9.]+)', line, re.MULTILINE) @@ -210,17 +208,15 @@ def load_settings(): pass def save_settings(): - global config, scheduledAmpsStartHour, scheduledAmpsEndHour, \ - scheduledAmpsDaysBitmap, hourResumeTrackGreenEnergy, kWhDelivered, \ - carapi, homeLat, homeLon + global config, scheduledAmpsDaysBitmap, kWhDelivered, carapi, homeLat, homeLon fh = open(config['config']['settingsPath'] + "/TWCManager.settings", 'w') fh.write('nonScheduledAmpsMax=' + str(master.getNonScheduledAmpsMax()) + '\nscheduledAmpsMax=' + str(master.getScheduledAmpsMax()) + - '\nscheduledAmpsStartHour=' + str(scheduledAmpsStartHour) + - '\nscheduledAmpsEndHour=' + str(scheduledAmpsEndHour) + + '\nscheduledAmpsStartHour=' + str(master.getScheduledAmpsStartHour()) + + '\nscheduledAmpsEndHour=' + str(master.getScheduledAmpsEndHour()) + '\nscheduledAmpsDaysBitmap=' + str(scheduledAmpsDaysBitmap) + - '\nhourResumeTrackGreenEnergy=' + str(hourResumeTrackGreenEnergy) + + '\nhourResumeTrackGreenEnergy=' + str(master.getHourResumeTrackGreenEnergy()) + '\nkWhDelivered=' + str(kWhDelivered) + '\ncarApiBearerToken=' + str(carapi.getCarApiBearerToken()) + '\ncarApiRefreshToken=' + str(carapi.getCarApiRefreshToken()) + @@ -907,11 +903,8 @@ def check_green_energy(): idxSlaveToSendNextHeartbeat = 0 -scheduledAmpsStartHour = -1 -scheduledAmpsEndHour = -1 scheduledAmpsDaysBitmap = 0x7F -hourResumeTrackGreenEnergy = -1 kWhDelivered = 119 timeLastkWhDelivered = time.time() timeLastkWhSaved = time.time() @@ -1136,13 +1129,13 @@ def check_green_energy(): '`' + "%.2f" % (master.getChargeNowAmps()) + '`' + str(master.getNonScheduledAmpsMax()) + '`' + str(master.getScheduledAmpsMax()) + - '`' + "%02d:%02d" % (int(scheduledAmpsStartHour), - int((scheduledAmpsStartHour % 1) * 60)) + - '`' + "%02d:%02d" % (int(scheduledAmpsEndHour), - int((scheduledAmpsEndHour % 1) * 60)) + + '`' + "%02d:%02d" % (int(master.getScheduledAmpsStartHour()), + int((master.getScheduledAmpsStartHour() % 1) * 60)) + + '`' + "%02d:%02d" % (int(master.getScheduledAmpsEndHour()), + int((master.getScheduledAmpsEndHour() % 1) * 60)) + '`' + str(scheduledAmpsDaysBitmap) + - '`' + "%02d:%02d" % (int(hourResumeTrackGreenEnergy), - int((hourResumeTrackGreenEnergy % 1) * 60)) + + '`' + "%02d:%02d" % (int(master.getHourResumeTrackGreenEnergy()), + int((master.getHourResumeTrackGreenEnergy() % 1) * 60)) + # Send 1 if we need an email/password entered for car api, otherwise send 0 '`' + ('1' if needCarApiBearerToken else '0') + '`' + str(master.countSlaveTWC()) @@ -1171,14 +1164,14 @@ def check_green_energy(): webMsg[17:len(webMsg)], re.MULTILINE) if(m): master.setScheduledAmpsMax(int(m.group(1))) - scheduledAmpsStartHour = int(m.group(2)) + (int(m.group(3)) / 60) - scheduledAmpsEndHour = int(m.group(4)) + (int(m.group(5)) / 60) + master.setScheduledAmpsStartHour(int(m.group(2)) + (int(m.group(3)) / 60)) + master.setScheduledAmpsEndHour(int(m.group(4)) + (int(m.group(5)) / 60)) scheduledAmpsDaysBitmap = int(m.group(6)) save_settings() elif(webMsg[0:30] == b'setResumeTrackGreenEnergyTime='): m = re.search(b'([-0-9]+):([0-9]+)', webMsg[30:len(webMsg)], re.MULTILINE) if(m): - hourResumeTrackGreenEnergy = int(m.group(1)) + (int(m.group(2)) / 60) + master.setHourResumeTrackGreenEnergy(int(m.group(1)) + (int(m.group(2)) / 60)) save_settings() elif(webMsg[0:11] == b'sendTWCMsg='): m = re.search(b'([0-9a-fA-F]+)', webMsg[11:len(webMsg)], re.MULTILINE) diff --git a/lib/TWCManager/Control/HTTPControl.py b/lib/TWCManager/Control/HTTPControl.py index 8ee3909c..70c7f1bc 100644 --- a/lib/TWCManager/Control/HTTPControl.py +++ b/lib/TWCManager/Control/HTTPControl.py @@ -71,10 +71,36 @@ def do_css(self): #vertical thead,#vertical tbody{ display:inline-block; } + """ page += "" return page + def do_navbar(self): + page = """ +

 

+

 

+ """ + return page + def do_GET(self): global master url = urllib.parse.urlparse(self.path) @@ -87,10 +113,13 @@ def do_GET(self): # Send the html message page = "" page += "TWCManager" - page += "" + page += "" page += "" + page += "" page += self.do_css() page += "" + page += "" + page += self.do_navbar() page += "" page += "
" @@ -100,7 +129,9 @@ def do_GET(self): page += self.show_status() page += self.show_twcs() + page += "
" page += "" + page += "" page += "" self.wfile.write(page.encode("utf-8")) @@ -188,7 +219,7 @@ def request_teslalogin(self): page += "

" page += "" page += "" - page += "" + page += "" page += "" page += "" page += "" @@ -202,22 +233,53 @@ def request_teslalogin(self): def show_status(self): global master - page = "
Tesla Account E-Mail:
Password:
" - page += "" - page += "" - page += "" - page += "" - page += "" - page += "" - page += "" - page += "" - page += "" - page += "" - page += "" - page += "" - page += "" - page += "" - page += "
Amps to share across all TWCs:
Current Generation
Current Consumption
Current Charger Load
Number of Cars Charging
" + str(master.getMaxAmpsToDivideAmongSlaves()) + "
" + str(master.getGeneration()) + "
" + str(master.getConsumption()) + "
" + str(master.getChargerLoad()) + "
" + str(master.num_cars_charging_now()) + "
" + page = "" + + page += "
" + page += "" + page += "" + page += "" + page += "" + + page += "" + page += "" + page += "" + genamps = 0 + if (master.getGeneration()): + genamps = (master.getGeneration()/240) + page += "" + + page += "" + page += "" + page += "" + conamps = 0 + if (master.getConsumption()): + conamps = (master.getConsumption()/240) + page += "" + + page += "" + page += "" + page += "" + page += "" + + page += "" + page += "" + page += "
Amps to share across all TWCs:" + str(master.getMaxAmpsToDivideAmongSlaves()) + "amps
Current Generation" + str(master.getGeneration()) + "watts" + str(genamps)+"amps
Current Consumption" + str(master.getConsumption()) + "watts" + str(conamps)+"amps
Current Charger Load" + str(master.getChargerLoad()) + "watts
Number of Cars Charging" + str(master.num_cars_charging_now()) + "cars
" + page += "" + page += "" + page += "" + + page += "" + page += "" + + page += "" + page += "" + page += "" + + page += "" + page += "" + page += "" + page += "
Scheduled Charging Amps" + str(master.getScheduledAmpsMax()) + "
Scheduled Charging Start Hour" + str(master.getScheduledAmpsStartHour()) + "
Scheduled Charging End Hour" + str(master.getScheduledAmpsEndHour()) + "
Resume Tracking Green Energy at" + str(master.getHourResumeTrackGreenEnergy()) + "
" return page def show_twcs(self): diff --git a/lib/TWCManager/TWCMaster.py b/lib/TWCManager/TWCMaster.py index 48c159c8..594f9ee2 100644 --- a/lib/TWCManager/TWCMaster.py +++ b/lib/TWCManager/TWCMaster.py @@ -19,12 +19,15 @@ class TWCMaster: consumptionValues = {} generationValues = {} hassstatus = None + hourResumeTrackGreenEnergy = -1 masterTWCID = '' maxAmpsToDivideAmongSlaves = 0 mqttstatus = None nonScheduledAmpsMax = -1 overrideMasterHeartbeatData = b'' scheduledAmpsMax = -1 + scheduledAmpsStartHour = -1 + scheduledAmpsEndHour = -1 ser = None slaveTWCs = {} slaveTWCRoundRobin = [] @@ -89,6 +92,9 @@ def getBackgroundTasksLock(self): def getChargeNowAmps(self): return (self.chargeNowAmps) + def getHourResumeTrackGreenEnergy(self): + return self.hourResumeTrackGreenEnergy + def getMasterTWCID(self): # This is called when TWCManager is in Slave mode, to track the # master's TWCID @@ -109,6 +115,12 @@ def getNonScheduledAmpsMax(self): def getScheduledAmpsMax(self): return self.scheduledAmpsMax + def getScheduledAmpsStartHour(self): + return self.scheduledAmpsStartHour + + def getScheduledAmpsEndHour(self): + return self.scheduledAmpsEndHour + def getSlaveSign(self): return self.slaveSign @@ -452,6 +464,9 @@ def sethassstatus(self, hass): # Stores the hassstatus object self.hassstatus = hass + def setHourResumeTrackGreenEnergy(self, hour): + self.hourResumeTrackGreenEnergy = hour + def setMasterTWCID(self, twcid): # This is called when TWCManager is in Slave mode, to track the # master's TWCID @@ -500,6 +515,12 @@ def setNonScheduledAmpsMax(self, amps): def setScheduledAmpsMax(self, amps): self.scheduledAmpsMax = amps + def setScheduledAmpsStartHour(self, hour): + self.scheduledAmpsStartHour = hour + + def setScheduledAmpsEndHour(self, hour): + self.scheduledAmpsEndHour = hour + def setSpikeAmps(self, amps): self.spikeAmpsToCancel6ALimit = amps diff --git a/lib/TWCManager/TWCSlave.py b/lib/TWCManager/TWCSlave.py index b28d7d1c..84453989 100644 --- a/lib/TWCManager/TWCSlave.py +++ b/lib/TWCManager/TWCSlave.py @@ -429,8 +429,8 @@ def receive_slave_heartbeat(self, heartbeatData): yesterday += 7 # Check if it's time to resume tracking green energy. - if(self.master.getNonScheduledAmpsMax() != -1 and hourResumeTrackGreenEnergy > -1 - and hourResumeTrackGreenEnergy == hourNow): + if(self.master.getNonScheduledAmpsMax() != -1 and self.master.getHourResumeTrackGreenEnergy() > -1 + and self.master.getHourResumeTrackGreenEnergy() == hourNow): self.master.setNonScheduledAmpsMax(-1) save_settings() @@ -438,8 +438,8 @@ def receive_slave_heartbeat(self, heartbeatData): # of nonScheduledAmpsMax blnUseScheduledAmps = 0 if(self.master.getScheduledAmpsMax() > 0 and scheduledAmpsStartHour > -1 - and scheduledAmpsEndHour > -1 and scheduledAmpsDaysBitmap > 0): - if(scheduledAmpsStartHour > scheduledAmpsEndHour): + and self.master.getScheduledAmpsEndHour() > -1 and scheduledAmpsDaysBitmap > 0): + if(scheduledAmpsStartHour > self.master.getScheduledAmpsEndHour()): # We have a time like 8am to 7am which we must interpret as the # 23-hour period after 8am or before 7am. Since this case always # crosses midnight, we only ensure that scheduledAmpsDaysBitmap @@ -448,12 +448,12 @@ def receive_slave_heartbeat(self, heartbeatData): # 7am, we apply scheduledAmpsMax from Monday at 8am to Monday at # 11:59pm, and on Tuesday at 12am to Tuesday at 6:59am. if((hourNow >= scheduledAmpsStartHour and (scheduledAmpsDaysBitmap & (1 << ltNow.tm_wday))) - or (hourNow < scheduledAmpsEndHour and (scheduledAmpsDaysBitmap & (1 << yesterday)))): + or (hourNow < self.master.getScheduledAmpsEndHour() and (scheduledAmpsDaysBitmap & (1 << yesterday)))): blnUseScheduledAmps = 1 else: # We have a time like 7am to 8am which we must interpret as the # 1-hour period between 7am and 8am. - if(hourNow >= scheduledAmpsStartHour and hourNow < scheduledAmpsEndHour + if(hourNow >= scheduledAmpsStartHour and hourNow < self.master.getScheduledAmpsEndHour() and (scheduledAmpsDaysBitmap & (1 << ltNow.tm_wday))): blnUseScheduledAmps = 1