Skip to content
This repository has been archived by the owner on Nov 6, 2024. It is now read-only.

Commit

Permalink
Better threading.
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelarnauts committed Jun 16, 2017
1 parent de2fde1 commit 79d6b14
Show file tree
Hide file tree
Showing 7 changed files with 299 additions and 248 deletions.
2 changes: 1 addition & 1 deletion PROTOCOL.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ Overview of known pdids:
| 291 | 1 | Temperature & Humidity: Exhaust Air (`57` = 87%) |
| 292 | 1 | Temperature & Humidity: Outdoor Air (`43` = 67%) |
| 294 | 1 | Temperature & Humidity: Supply Air (`23` = 35%) |
| 227 | 1 | Bypass state (`64` = 100%) |

Unknown/uncertain messages:

Expand Down Expand Up @@ -319,7 +320,6 @@ Unknown/uncertain messages:
| 224 | 1 | *Unknown* (`03` = 3) |
| 225 | 1 | *Unknown* (`01`) |
| 226 | 2 | *Unknown* (`6400` = 100) |
| 227 | 1 | *Unknown* (`05`) |
| 228 | 1 | *Unknown* (`00`) |
| 321 | 2 | *Unknown* (`0700` = 7) |
| 325 | 2 | *Unknown* (`0100` = 1) |
Expand Down
237 changes: 125 additions & 112 deletions example/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,117 +14,130 @@
local_name = 'Computer'
local_uuid = bytes.fromhex('00000000000000000000000000000005')

## Bridge discovery ####################################################################################################

# Method 1: Use discovery to initialise Bridge
# bridges = Bridge.discover(timeout=1)
# if bridges:
# bridge = bridges[0]
# else:
# bridge = None

# Method 2: Use direct discovery to initialise Bridge
bridges = Bridge.discover(args.ip)
if bridges:
bridge = bridges[0]
else:
bridge = None

# Method 3: Setup bridge manually
# bridge = Bridge(args.ip, bytes.fromhex('0000000000251010800170b3d54264b4'))

if bridge is None:
print("No bridges found!")
exit(1)

print("Bridge found: %s (%s)" % (bridge.uuid.hex(), bridge.host))
bridge.debug = True


## Setting up a session ########################################################################################

def callback(var, value):
# print("%s = %s" % (var, value))
pass


# Setup a Comfoconnect session
comfoconnect = ComfoConnect(bridge, local_uuid, local_name, pin)
comfoconnect.callback = callback

try:
# Connect to the bridge
# comfoconnect.connect(False) # Don't disconnect existing clients.
comfoconnect.connect(True) # Disconnect existing clients.

except Exception as e:
print('ERROR: %s' % e)
exit(1)

## Execute functions ###############################################################################################

# ListRegisteredApps
# for app in comfoconnect.cmd_list_registered_apps():
# print('%s: %s' % (app['uuid'].hex(), app['devicename']))

# DeregisterApp
# comfoconnect.cmd_deregister_app(bytes.fromhex('00000000000000000000000000000001'))

# VersionRequest
# version = comfoconnect.cmd_version_request()
# print(version)

# TimeRequest
# timeinfo = comfoconnect.cmd_time_request()
# print(timeinfo)

## Register sensors ################################################################################################

# comfoconnect.cmd_rpdo_request(SENSOR_FAN_NEXT_CHANGE) # General: Countdown until next fan speed change
comfoconnect.cmd_rpdo_request(SENSOR_FAN_SPEED_MODE) # Fans: Fan speed setting
# comfoconnect.cmd_rpdo_request(SENSOR_FAN_SUPPLY_DUTY) # Fans: Supply fan duty
# comfoconnect.cmd_rpdo_request(SENSOR_FAN_EXHAUST_DUTY) # Fans: Exhaust fan duty
# comfoconnect.cmd_rpdo_request(SENSOR_FAN_SUPPLY_FLOW) # Fans: Supply fan flow
# comfoconnect.cmd_rpdo_request(SENSOR_FAN_EXHAUST_FLOW) # Fans: Exhaust fan flow
# comfoconnect.cmd_rpdo_request(SENSOR_FAN_SUPPLY_SPEED) # Fans: Supply fan speed
# comfoconnect.cmd_rpdo_request(SENSOR_FAN_EXHAUST_SPEED) # Fans: Exhaust fan speed
# comfoconnect.cmd_rpdo_request(SENSOR_POWER_CURRENT) # Power Consumption: Current Ventilation
# comfoconnect.cmd_rpdo_request(SENSOR_POWER_TOTAL_YEAR) # Power Consumption: Total year-to-date
# comfoconnect.cmd_rpdo_request(SENSOR_POWER_TOTAL) # Power Consumption: Total from start
# comfoconnect.cmd_rpdo_request(SENSOR_DAYS_TO_REPLACE_FILTER) # Days left before filters must be replaced
# comfoconnect.cmd_rpdo_request(SENSOR_AVOIDED_HEATING_CURRENT) # Avoided Heating: Avoided actual
# comfoconnect.cmd_rpdo_request(SENSOR_AVOIDED_HEATING_TOTAL_YEAR) # Avoided Heating: Avoided year-to-date
# comfoconnect.cmd_rpdo_request(SENSOR_AVOIDED_HEATING_TOTAL) # Avoided Heating: Avoided total
# comfoconnect.cmd_rpdo_request(SENSOR_TEMPERATURE_SUPPLY) # Temperature & Humidity: Supply Air (temperature)
# comfoconnect.cmd_rpdo_request(SENSOR_TEMPERATURE_EXTRACT) # Temperature & Humidity: Extract Air (temperature)
# comfoconnect.cmd_rpdo_request(SENSOR_TEMPERATURE_EXHAUST) # Temperature & Humidity: Exhaust Air (temperature)
# comfoconnect.cmd_rpdo_request(SENSOR_TEMPERATURE_OUTDOOR) # Temperature & Humidity: Outdoor Air (temperature)
# comfoconnect.cmd_rpdo_request(SENSOR_HUMIDITY_SUPPLY) # Temperature & Humidity: Supply Air (temperature)
# comfoconnect.cmd_rpdo_request(SENSOR_HUMIDITY_EXTRACT) # Temperature & Humidity: Extract Air (temperature)
# comfoconnect.cmd_rpdo_request(SENSOR_HUMIDITY_EXHAUST) # Temperature & Humidity: Exhaust Air (temperature)
# comfoconnect.cmd_rpdo_request(SENSOR_HUMIDITY_OUTDOOR) # Temperature & Humidity: Outdoor Air (temperature)

# Executing functions #########################################################################################

# comfoconnect.cmd_rmi_request(CMD_FAN_MODE_AWAY) # Go to auto mode
# comfoconnect.cmd_rmi_request(CMD_FAN_MODE_LOW) # Set fan speed to 1
# comfoconnect.cmd_rmi_request(CMD_FAN_MODE_MEDIUM) # Set fan speed to 2
# comfoconnect.cmd_rmi_request(CMD_FAN_MODE_HIGH) # Set fan speed to 3

## Example interaction #########################################################################################

try:
print('Waiting... Stop with CTRL+C')
while True:
# Callback messages will arrive in the callback method.
sleep(1)

if not comfoconnect.is_connected():
print('We are not connected anymore...')

except KeyboardInterrupt:
pass

## Closing the session #################################################################################################
def bridge_discovery():
## Bridge discovery ################################################################################################

# Method 1: Use discovery to initialise Bridge
# bridges = Bridge.discover(timeout=1)
# if bridges:
# bridge = bridges[0]
# else:
# bridge = None

comfoconnect.disconnect()
# Method 2: Use direct discovery to initialise Bridge
bridges = Bridge.discover(args.ip)
if bridges:
bridge = bridges[0]
else:
bridge = None

# Method 3: Setup bridge manually
# bridge = Bridge(args.ip, bytes.fromhex('0000000000251010800170b3d54264b4'))

if bridge is None:
print("No bridges found!")
exit(1)

print("Bridge found: %s (%s)" % (bridge.uuid.hex(), bridge.host))
bridge.debug = True

return bridge


def callback_sensor(var, value):
## Callback sensors ################################################################################################

print("%s = %s" % (var, value))


def main():
# Discover the bridge
bridge = bridge_discovery()

## Setup a Comfoconnect session ###################################################################################

comfoconnect = ComfoConnect(bridge, local_uuid, local_name, pin)
comfoconnect.callback_sensor = callback_sensor

try:
# Connect to the bridge
# comfoconnect.connect(False) # Don't disconnect existing clients.
comfoconnect.connect(True) # Disconnect existing clients.

except Exception as e:
print('ERROR: %s' % e)
exit(1)

## Register sensors ################################################################################################

# comfoconnect.register_sensor(SENSOR_FAN_NEXT_CHANGE) # General: Countdown until next fan speed change
# comfoconnect.register_sensor(SENSOR_FAN_SPEED_MODE) # Fans: Fan speed setting
# comfoconnect.register_sensor(SENSOR_FAN_SUPPLY_DUTY) # Fans: Supply fan duty
# comfoconnect.register_sensor(SENSOR_FAN_EXHAUST_DUTY) # Fans: Exhaust fan duty
# comfoconnect.register_sensor(SENSOR_FAN_SUPPLY_FLOW) # Fans: Supply fan flow
# comfoconnect.register_sensor(SENSOR_FAN_EXHAUST_FLOW) # Fans: Exhaust fan flow
# comfoconnect.register_sensor(SENSOR_FAN_SUPPLY_SPEED) # Fans: Supply fan speed
# comfoconnect.register_sensor(SENSOR_FAN_EXHAUST_SPEED) # Fans: Exhaust fan speed
# comfoconnect.register_sensor(SENSOR_POWER_CURRENT) # Power Consumption: Current Ventilation
# comfoconnect.register_sensor(SENSOR_POWER_TOTAL_YEAR) # Power Consumption: Total year-to-date
# comfoconnect.register_sensor(SENSOR_POWER_TOTAL) # Power Consumption: Total from start
# comfoconnect.register_sensor(SENSOR_DAYS_TO_REPLACE_FILTER) # Days left before filters must be replaced
# comfoconnect.register_sensor(SENSOR_AVOIDED_HEATING_CURRENT) # Avoided Heating: Avoided actual
# comfoconnect.register_sensor(SENSOR_AVOIDED_HEATING_TOTAL_YEAR) # Avoided Heating: Avoided year-to-date
# comfoconnect.register_sensor(SENSOR_AVOIDED_HEATING_TOTAL) # Avoided Heating: Avoided total
comfoconnect.register_sensor(SENSOR_TEMPERATURE_SUPPLY) # Temperature & Humidity: Supply Air (temperature)
comfoconnect.register_sensor(SENSOR_TEMPERATURE_EXTRACT) # Temperature & Humidity: Extract Air (temperature)
comfoconnect.register_sensor(SENSOR_TEMPERATURE_EXHAUST) # Temperature & Humidity: Exhaust Air (temperature)
comfoconnect.register_sensor(SENSOR_TEMPERATURE_OUTDOOR) # Temperature & Humidity: Outdoor Air (temperature)
comfoconnect.register_sensor(SENSOR_HUMIDITY_SUPPLY) # Temperature & Humidity: Supply Air (temperature)
comfoconnect.register_sensor(SENSOR_HUMIDITY_EXTRACT) # Temperature & Humidity: Extract Air (temperature)
comfoconnect.register_sensor(SENSOR_HUMIDITY_EXHAUST) # Temperature & Humidity: Exhaust Air (temperature)
comfoconnect.register_sensor(SENSOR_HUMIDITY_OUTDOOR) # Temperature & Humidity: Outdoor Air (temperature)
comfoconnect.register_sensor(SENSOR_BYPASS_STATE) # Bypass state

## Execute functions ###############################################################################################

# ListRegisteredApps
# for app in comfoconnect.cmd_list_registered_apps():
# print('%s: %s' % (app['uuid'].hex(), app['devicename']))

# DeregisterApp
# comfoconnect.cmd_deregister_app(bytes.fromhex('00000000000000000000000000000001'))

# VersionRequest
version = comfoconnect.cmd_version_request()
print(version)

# TimeRequest
# timeinfo = comfoconnect.cmd_time_request()
# print(timeinfo)

## Executing functions #############################################################################################

# comfoconnect.cmd_rmi_request(CMD_FAN_MODE_AWAY) # Go to away mode
# comfoconnect.cmd_rmi_request(CMD_FAN_MODE_LOW) # Set fan speed to 1
# comfoconnect.cmd_rmi_request(CMD_FAN_MODE_MEDIUM) # Set fan speed to 2
# comfoconnect.cmd_rmi_request(CMD_FAN_MODE_HIGH) # Set fan speed to 3

## Example interaction #############################################################################################

try:
print('Waiting... Stop with CTRL+C')
while True:
# Callback messages will arrive in the callback method.
sleep(1)

if not comfoconnect.is_connected():
print('We are not connected anymore...')

except KeyboardInterrupt:
pass

## Closing the session #############################################################################################

comfoconnect.disconnect()


if __name__ == "__main__":
main()
21 changes: 12 additions & 9 deletions example/manually_decode.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@
if message.cmd.type == zehnder_pb2.GatewayOperation.CnRpdoRequestType:
if not message.msg.pdid in pdids:
pdids[message.msg.pdid] = {}
pdids[message.msg.pdid]['tx'] = [message.msg.zone, message.msg.type]
try:
pdids[message.msg.pdid]['tx'].append(message.msg.type)
except KeyError:
pdids[message.msg.pdid]['tx'] = [message.msg.type]

if message.cmd.type == zehnder_pb2.GatewayOperation.CnRpdoConfirmType:
pass
Expand All @@ -54,11 +57,11 @@
except KeyError:
pdids[message.msg.pdid]['rx'] = [message.msg.data.hex()]

# print("CnRpdoRequestType")
# for pdid in pdids:
# print(pdid, pdids[pdid])

print("CnRmiRequestType")
for rmi in rmis:
print(rmi, rmis[rmi])

print("CnRpdoRequestType")
for pdid in pdids:
print(pdid, pdids[pdid])
#
# # print("CnRmiRequestType")
# # for rmi in rmis:
# # print(rmi, rmis[rmi])
#
3 changes: 3 additions & 0 deletions pycomfoconnect/bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class Bridge(object):

@staticmethod
def discover(host=None, timeout=5):
"""Broadcast the network and look for local bridges."""

# Setup socket
udpsocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udpsocket.setblocking(0)
Expand Down Expand Up @@ -81,6 +83,7 @@ def disconnect(self) -> bool:

def is_connected(self):
"""Returns weather there is an open socket."""

return self._socket is not None

def read_message(self, timeout=1) -> Message:
Expand Down
Loading

0 comments on commit 79d6b14

Please sign in to comment.