Skip to content

Commit

Permalink
prep for 2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
britkat1980 committed Feb 5, 2023
1 parent 3d2e8d8 commit 138b6d4
Show file tree
Hide file tree
Showing 12 changed files with 287 additions and 149 deletions.
Empty file added .dayRate
Empty file.
7 changes: 4 additions & 3 deletions GivTCP/GivLUT.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class GivQueue:
from settings import GiV_Settings
redis_connection = Redis(host='127.0.0.1', port=6379, db=0)
q = Queue("GivTCP_"+str(GiV_Settings.givtcp_instance),connection=redis_connection)

class GEType:
def __init__(self,dT,sC,cF,mn,mx,aZ,sM,oI):
self.devType = dT
Expand Down Expand Up @@ -62,8 +62,8 @@ class GivLUT:
schedule=".schedule"
oldDataCount=GiV_Settings.cache_location+"/oldDataCount_"+str(GiV_Settings.givtcp_instance)+".pkl"

timezone=zoneinfo.ZoneInfo(key=os.getenv("TZ"))
# timezone=zoneinfo.ZoneInfo(key="Europe/London")
# timezone=zoneinfo.ZoneInfo(key=os.getenv("TZ"))
timezone=zoneinfo.ZoneInfo(key="Europe/London")

# Standard values for devices
maxInvPower=6000
Expand Down Expand Up @@ -133,6 +133,7 @@ class GivLUT:
"Battery_Type":GEType("sensor","","","","",False,False,False),
"Battery_Capacity_kWh":GEType("sensor","","",0,maxBatPower,True,True,False),
"Invertor_Serial_Number":GEType("sensor","","","","",False,False,False),
"Invertor_Firmware":GEType("sensor","","",0,10000,False,False,False),
"Modbus_Version":GEType("sensor","","",1,10,False,True,False),
"Meter_Type":GEType("sensor","","","","",False,False,False),
"Invertor_Type":GEType("sensor","","","","",False,False,False),
Expand Down
4 changes: 2 additions & 2 deletions GivTCP/HA_Discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class HAMQTT():
def on_connect(client, userdata, flags, rc):
if rc==0:
client.connected_flag=True #set flag
logger.info("connected OK Returned code="+str(rc))
logger.debug("connected OK Returned code="+str(rc))
#client.subscribe(topic)
else:
logger.error("Bad connection Returned code= "+str(rc))
Expand All @@ -46,7 +46,7 @@ def publish_discovery(array,SN): #Recieve multiple payloads with Topics and pu
logger.debug("In wait loop")
time.sleep(0.2)

logger.info("Publishing MQTT: " + HAMQTT.MQTT_Address)
logger.debug("Publishing MQTT: " + HAMQTT.MQTT_Address)

##publish the status message
client.publish(GiV_Settings.MQTT_Topic+"/"+SN+"/status","online", retain=True)
Expand Down
47 changes: 42 additions & 5 deletions GivTCP/REST.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import read as rd #grab passthrough functions from main read file
import write as wr #grab passthrough functions from main write file
import config_dash as cfdash
from GivLUT import GivQueue, GivLUT
from os.path import exists

logger = GivLUT.logger

#set-up Flask details
giv_api = Flask(__name__)
Expand All @@ -31,7 +35,7 @@ def rdData():
#Read from Invertor put in cache
@giv_api.route('/getData', methods=['GET'])
def gtData():
return rd.getData()
return GivQueue.q.enqueue(rd.getData,True)

#Proxy Write Functions
@giv_api.route('/enableChargeTarget', methods=['POST'])
Expand Down Expand Up @@ -97,22 +101,55 @@ def setDischrgSlot2():
@giv_api.route('/tempPauseDischarge', methods=['POST'])
def tmpPauseDischrg():
payload = request.get_json(silent=True, force=True)
return wr.tempPauseDischarge(payload)
if payload == "Cancel":
if exists(".tpdRunning"):
jobid= str(open(".tpdRunning","r").readline())
logger.critical("Retrieved jobID to cancel Temp Pause Discharge: "+ str(jobid))
return wr.cancelJob(jobid)
else:
logger.error("Force Charge is not currently running")
else:
return wr.tempPauseCharge(payload)

@giv_api.route('/tempPauseCharge', methods=['POST'])
def tmpPauseChrg():
payload = request.get_json(silent=True, force=True)
return wr.tempPauseCharge(payload)
if payload == "Cancel":
if exists(".tpcRunning"):
jobid= str(open(".tpcRunning","r").readline())
logger.critical("Retrieved jobID to cancel Temp Pause Charge: "+ str(jobid))
return wr.cancelJob(jobid)
else:
logger.error("Force Charge is not currently running")
else:
return wr.tempPauseCharge(payload)

@giv_api.route('/forceCharge', methods=['POST'])
def frceChrg():
payload = request.get_json(silent=True, force=True)
return wr.forceCharge(payload)
#Check if Cancel then return the right function
if payload == "Cancel":
if exists(".FCRunning"):
jobid= str(open(".FCRunning","r").readline())
logger.critical("Retrieved jobID to cancel Force Charge: "+ str(jobid))
return wr.cancelJob(jobid)
else:
logger.error("Force Charge is not currently running")
else:
return wr.forceCharge(payload)

@giv_api.route('/forceExport', methods=['POST'])
def frceExprt():
payload = request.get_json(silent=True, force=True)
return wr.forceExport(payload)
if payload == "Cancel":
if exists(".FERunning"):
jobid= str(open(".FERunning","r").readline())
logger.critical("Retrieved jobID to cancel Force Export: "+ str(jobid))
return wr.cancelJob(jobid)
else:
logger.error("Force Charge is not currently running")
else:
return wr.forceExport(payload)

@giv_api.route('/setBatteryMode', methods=['POST'])
def setBattMode():
Expand Down
4 changes: 2 additions & 2 deletions GivTCP/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class GivMQTT():
def on_connect(client, userdata, flags, rc):
if rc==0:
client.connected_flag=True #set flag
logger.info("connected OK Returned code="+str(rc))
logger.debug("connected OK Returned code="+str(rc))
#client.subscribe(topic)
else:
logger.error("Bad connection Returned code= "+str(rc))
Expand All @@ -42,7 +42,7 @@ def multi_MQTT_publish(rootTopic,array): #Recieve multiple payloads with Topic
try:
client.on_connect=GivMQTT.on_connect #bind call back function
client.loop_start()
logger.info ("Connecting to broker: "+ GivMQTT.MQTT_Address)
logger.debug ("Connecting to broker: "+ GivMQTT.MQTT_Address)
client.connect(GivMQTT.MQTT_Address,port=GivMQTT.MQTT_Port)
while not client.connected_flag: #wait in loop
logger.debug ("In wait loop")
Expand Down
69 changes: 55 additions & 14 deletions GivTCP/mqtt_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,18 @@
logger.error("No serial_number found in MQTT queue. MQTT Control not available.")
break

logger.info("Serial Number retrieved: "+GiV_Settings.serial_number)
logger.debug("Serial Number retrieved: "+GiV_Settings.serial_number)

def isfloat(num):
try:
float(num)
return True
except ValueError:
return False

def on_message(client, userdata, message):
payload={}
logger.info("MQTT Message Recieved: "+str(message.topic)+"= "+str(message.payload.decode("utf-8")))
logger.debug("MQTT Message Recieved: "+str(message.topic)+"= "+str(message.payload.decode("utf-8")))
writecommand={}
command=str(message.topic).split("/")[-1]
if command=="setDischargeRate":
Expand Down Expand Up @@ -136,20 +143,54 @@ def on_message(client, userdata, message):
payload['finish']=message.payload.decode("utf-8")[:5]
payload['start']=start[:5]
result=GivQueue.q.enqueue(wr.setDischargeSlot2,payload)
# result=wr.setDischargeSlot2(payload)
elif command=="tempPauseDischarge":
writecommand=float(message.payload.decode("utf-8"))
result=GivQueue.q.enqueue(wr.tempPauseDischarge,writecommand)
if isfloat(message.payload.decode("utf-8")):
writecommand=float(message.payload.decode("utf-8"))
result=GivQueue.q.enqueue(wr.tempPauseDischarge,writecommand)
elif message.payload.decode("utf-8") == "Cancel":
# Get the Job ID from the touchfile
if exists(".tpdRunning"):
jobid= str(open(".tpdRunning","r").readline())
logger.critical("Retrieved jobID to cancel Temp Pause Discharge: "+ str(jobid))
result=wr.cancelJob(jobid)
else:
logger.error("Temp Pause Charge is not currently running")
elif command=="tempPauseCharge":
writecommand=float(message.payload.decode("utf-8"))
result=GivQueue.q.enqueue(wr.tempPauseCharge,writecommand)
if isfloat(message.payload.decode("utf-8")):
writecommand=float(message.payload.decode("utf-8"))
result=GivQueue.q.enqueue(wr.tempPauseCharge,writecommand)
elif message.payload.decode("utf-8") == "Cancel":
# Get the Job ID from the touchfile
if exists(".tpcRunning"):
jobid= str(open(".tpcRunning","r").readline())
logger.critical("Retrieved jobID to cancel Temp Pause Charge: "+ str(jobid))
result=wr.cancelJob(jobid)
else:
logger.error("Temp Pause Charge is not currently running")
elif command=="forceCharge":
writecommand=float(message.payload.decode("utf-8"))
#if "Cancel" then get revert jobid and force it to run
result=GivQueue.q.enqueue(wr.forceCharge,writecommand)
if isfloat(message.payload.decode("utf-8")):
writecommand=float(message.payload.decode("utf-8"))
result=GivQueue.q.enqueue(wr.forceCharge,writecommand)
elif message.payload.decode("utf-8") == "Cancel":
# Get the Job ID from the touchfile
if exists(".FCRunning"):
jobid= str(open(".FCRunning","r").readline())
logger.critical("Retrieved jobID to cancel Force Charge: "+ str(jobid))
result=wr.cancelJob(jobid)
else:
logger.error("Force Charge is not currently running")
elif command=="forceExport":
writecommand=float(message.payload.decode("utf-8"))
result=GivQueue.q.enqueue(wr.forceExport,writecommand)
if isfloat(message.payload.decode("utf-8")):
writecommand=float(message.payload.decode("utf-8"))
result=GivQueue.q.enqueue(wr.forceExport,writecommand)
elif message.payload.decode("utf-8") == "Cancel":
# Get the Job ID from the touchfile
if exists(".FERunning"):
jobid= str(open(".FERunning","r").readline())
logger.critical("Retrieved jobID to cancel Force Export: "+ str(jobid))
result=wr.cancelJob(jobid)
else:
logger.error("Force Export is not currently running")
elif command=="switchRate":
writecommand=message.payload.decode("utf-8")
result=GivQueue.q.enqueue(wr.switchRate,writecommand)
Expand All @@ -159,10 +200,10 @@ def on_message(client, userdata, message):
def on_connect(client, userdata, flags, rc):
if rc==0:
client.connected_flag=True #set flag
logger.info("connected OK Returned code="+str(rc))
logger.debug("connected OK Returned code="+str(rc))
#Subscribe to the control topic for this invertor - relies on serial_number being present
client.subscribe(MQTT_Topic+"/control/"+GiV_Settings.serial_number+"/#")
logger.info("Subscribing to "+MQTT_Topic+"/control/"+GiV_Settings.serial_number+"/#")
logger.debug("Subscribing to "+MQTT_Topic+"/control/"+GiV_Settings.serial_number+"/#")
else:
logger.error("Bad connection Returned code= "+str(rc))

Expand Down
Loading

0 comments on commit 138b6d4

Please sign in to comment.