[go: nahoru, domu]

Skip to content

Commit

Permalink
Moved HomeManager object to objects.py. Added a few comments. Now dis…
Browse files Browse the repository at this point in the history
…continuing HomeManager support
  • Loading branch information
luistripa committed Jun 23, 2021
1 parent 6b37319 commit 638c1ce
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 46 deletions.
9 changes: 8 additions & 1 deletion SMA.indigoPlugin/Contents/Server Plugin/Devices.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
<Field id="inverterAddress" type="textfield">
<Label>Inverter Address: </Label>
</Field>
<Field id="inverterPort" type="textfield">
<Field id="inverterPort" type="textfield" default="502">
<Label>Inverter Port: </Label>
</Field>
</ConfigUI>
Expand Down Expand Up @@ -199,6 +199,13 @@

<ConfigUI>

<Field id="label1" type="label">
<Label>
Attention! This device type is being discontinued by this plugin.
The values stored in the states will not be correct and it is not guaranteed to work 100% correctly.
</Label>
</Field>

<Field id="multicastGroup" type="textfield" defaultValue="239.12.255.254">
<Label>Multicast Group:</Label>
</Field>
Expand Down
54 changes: 52 additions & 2 deletions SMA.indigoPlugin/Contents/Server Plugin/objects.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from pymodbus.client.sync import ModbusTcpClient as ModbusClient
from comms import Client

from typing import List
import socket
from struct import pack
from binascii import b2a_hex


class Inverter:
def __init__(self, device, host, port):
Expand All @@ -16,6 +21,10 @@ def connect(self) -> bool:
def disconnect(self):
self.client.close()

def reconnect(self):
self.disconnect()
return self.connect()

def update_states(self):
indigo_states = []
self.states = self.client.generate_states()
Expand All @@ -25,9 +34,12 @@ def update_states(self):


class InverterBundle:
"""
This object is not operational yet
"""
def __init__(self, device):
self.device = device
self.inverters = list()
self.inverters: List[Inverter] = list()

def add_inverter(self, inverter: Inverter):
self.inverters.append(inverter)
Expand All @@ -40,7 +52,7 @@ def update_all_states(self):
"""
Updates all bundle states.
"""
pass
raise NotImplementedError("Function update_all_states is not yet implemented")

def get_count(self) -> int:
return len(self.inverters)
Expand All @@ -49,3 +61,41 @@ def reconnect(self):
for inverter in self.inverters:
inverter.disconnect()
inverter.connect()


class HomeManager:
"""
Home Manager support is being discontinued by this plugin. The values that are output are not correct and should
not be used.
"""

def __init__(self, device):
self.device = device
self.mcastGroup = device.pluginProps['multicastGroup']
self.mcastPort = int(device.pluginProps['multicastPort'])
self.setup()

def hex2dec(self, s):
return int(s, 16)

def setup(self):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.sock.bind(('', self.mcastPort))
mreq = pack("4sl", socket.inet_aton(self.mcastGroup), socket.INADDR_ANY)
self.sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)

def getReading(self):
info = self.sock.recv(600)
info_ascii = b2a_hex(info)
powerFromGrid = self.hex2dec(info_ascii[64:72]) / 10
totalPowerFromGrid = self.hex2dec(info_ascii[80:96]) / 3600000
powerToGrid = self.hex2dec(info_ascii[104:112]) / 10
totalPowerToGrid = self.hex2dec(info_ascii[120:136]) / 3600000
return [{'key': 'powerFromGrid', 'value': powerFromGrid},
{'key': 'totalPowerFromGrid', 'value': totalPowerFromGrid},
{'key': 'powerToGrid', 'value': powerToGrid},
{'key': 'totalPowerToGrid', 'value': totalPowerToGrid}]

def close(self):
self.sock.close()
68 changes: 25 additions & 43 deletions SMA.indigoPlugin/Contents/Server Plugin/plugin.py
Original file line number Diff line number Diff line change
@@ -1,51 +1,12 @@
import indigo
import socket
from struct import pack
from binascii import b2a_hex

from objects import Inverter, InverterBundle
from objects import Inverter, InverterBundle, HomeManager
import traceback

DISPLAY_NAME = 'Energy Meter'


class Plugin(indigo.PluginBase):
class HomeManager:
"""
Output values are not correct
"""

def __init__(self, device):
self.device = device
self.mcastGroup = device.pluginProps['multicastGroup']
self.mcastPort = int(device.pluginProps['multicastPort'])
self.setup()

def hex2dec(self, s):
return int(s, 16)

def setup(self):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.sock.bind(('', self.mcastPort))
mreq = pack("4sl", socket.inet_aton(self.mcastGroup), socket.INADDR_ANY)
self.sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)

def getReading(self):
info = self.sock.recv(600)
info_ascii = b2a_hex(info)
powerFromGrid = self.hex2dec(info_ascii[64:72]) / 10
totalPowerFromGrid = self.hex2dec(info_ascii[80:96]) / 3600000
powerToGrid = self.hex2dec(info_ascii[104:112]) / 10
totalPowerToGrid = self.hex2dec(info_ascii[120:136]) / 3600000
return [{'key': 'powerFromGrid', 'value': powerFromGrid},
{'key': 'totalPowerFromGrid', 'value': totalPowerFromGrid},
{'key': 'powerToGrid', 'value': powerToGrid},
{'key': 'totalPowerToGrid', 'value': totalPowerToGrid}]

def close(self):
self.sock.close()

def __init__(self, pluginId, pluginDisplayName, pluginVersion, pluginPrefs):
indigo.PluginBase.__init__(self, pluginId, pluginDisplayName, pluginVersion, pluginPrefs)

Expand Down Expand Up @@ -112,7 +73,7 @@ def deviceStartComm(self, dev):
self.bundles[dev.name] = bundle

elif dev.deviceTypeId == 'homeManager':
home_manager = self.HomeManager(dev)
home_manager = HomeManager(dev)
self.homeManagers[dev.name] = home_manager

else:
Expand All @@ -130,6 +91,10 @@ def deviceStopComm(self, dev):
###########################

def update_inverters(self):
"""
Updates the states of all Inverters.
If, for some reason, a ConnectionError is found, it attempts to reconnect the inverter
"""
for inv in self.inverters.values():
try:
inv.update_states()
Expand All @@ -141,10 +106,18 @@ def update_inverters(self):
inv.connect() # Reconnect inverter

def update_inverter_bundles(self):
"""
Updates the states of all Inverter Bundles
"""
for bundle in self.bundles.values():
bundle.update_all_states()

def update_home_managers(self):
"""
Updates all Home Managers.
Home Manager support is being discontinued by this plugin. The values that are output are not correct and should
not be used.
"""
for hm in self.homeManagers.values():
try:
states = hm.getReading()
Expand All @@ -153,16 +126,25 @@ def update_home_managers(self):
indigo.server.log(traceback.print_exc(), type=DISPLAY_NAME)

def reconnect_all(self):
"""
Reconnects all devices
"""
indigo.server.log('Reconnecting all devices........%d devices' % len(self.devices))
for inv in self.inverters.values():
if inv.connect():
if inv.reconnect():
indigo.server.log(' - {} --- OK'.format(inv.device.name), type=DISPLAY_NAME)
else:
indigo.server.log(' - {} --- FAILED'.format(inv.device.name), type=DISPLAY_NAME)

def reconnect_device(self, valuesDict, typeId):
indigo.server.log('Not yet implemented', type=DISPLAY_NAME)
"""
Reconnects a specific device
"""
indigo.server.log('Reconnect Device is not yet implemented', type=DISPLAY_NAME)

def reconnect_bundle(self, valuesDict, typeId):
"""
Reconnects all devices in a specific bundle
"""
bundle = indigo.devices[valuesDict['targetBundle']]
bundle.reconnectAll()

0 comments on commit 638c1ce

Please sign in to comment.