Skip to content

Commit

Permalink
Fix gateway address estimation
Browse files Browse the repository at this point in the history
  • Loading branch information
falkena committed Feb 10, 2024
1 parent 5e1eaca commit b91b3fc
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 42 deletions.
43 changes: 34 additions & 9 deletions BridgeEmulator/configManager/configInit.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,46 @@
import logManager
import uuid
import netifaces
from random import randrange

logging = logManager.logger.get_logger(__name__)

def _generate_unique_id():
rand_bytes = [randrange(0, 256) for _ in range(3)]
return "00:17:88:01:00:%02x:%02x:%02x-0b" % (rand_bytes[0],rand_bytes[1],rand_bytes[2])


def write_args(args, yaml_config):
host_ip = args["HOST_IP"]
ip_pieces = host_ip.split(".")
yaml_config["config"]["ipaddress"] = host_ip
yaml_config["config"]["gateway"] = ip_pieces[0] + "." + ip_pieces[1] + "." + ip_pieces[2] + ".1"
yaml_config["config"]["mac"] = args["FULLMAC"]
yaml_config["config"]["bridgeid"] = (args["MAC"][:6] + 'FFFE' + args["MAC"][-6:]).upper()
return yaml_config
host_ip = args['HOST_IP']

devices = []
for interface in netifaces.interfaces():
for family, addresses in netifaces.ifaddresses(interface).items():
if family not in (netifaces.AF_INET, netifaces.AF_INET6):
continue
for address in addresses:
if address['addr'] == host_ip:
devices.append((family, interface))
logging.debug('Found network devices ' + str(devices))

gateway_ips = []
for family, gateways in netifaces.gateways().items():
for device in devices:
if family != device[0]:
continue
for gateway in gateways:
if gateway[1] == device[1]:
gateway_ips.append(gateway[0])
logging.debug('Found gateways ' + str(gateway_ips))

if not gateway_ips:
ip_pieces = host_ip.split('.')
gateway_ips.append(ip_pieces[0] + '.' + ip_pieces[1] + '.' + ip_pieces[2] + '.1'
logging.info('Found no gateways and use fallback ' + str(gateway_ips))

yaml_config['config']['ipaddress'] = host_ip
yaml_config['config']['gateway'] = gateway_ips[0]
yaml_config['config']['mac'] = args['FULLMAC']
yaml_config['config']['bridgeid'] = (args['MAC'][:6] + 'FFFE' + args['MAC'][-6:]).upper()
return yaml_config

def generate_security_key(yaml_config):
# generate security key for Hue Essentials remote access
Expand Down
45 changes: 13 additions & 32 deletions BridgeEmulator/flaskUI/v2restapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,24 +142,17 @@ def v2BridgeHome():


def v2Bridge():
bridge_id = bridgeConfig["config"]["bridgeid"]
return {
"bridge_id": bridgeConfig["config"]["bridgeid"].lower(),
"id": str(uuid.uuid5(uuid.NAMESPACE_URL, bridgeConfig["config"]["bridgeid"] + 'bridge')),
"bridge_id": bridge_id.lower(),
"id": str(uuid.uuid5(uuid.NAMESPACE_URL, bridge_id + 'bridge')),
"id_v1": "",
"identify": {},
"owner": {
"rid": str(uuid.uuid5(
uuid.NAMESPACE_URL, bridgeConfig["config"]["bridgeid"] + 'device')),
"rtype": "device"
},
"time_zone": {
"time_zone": bridgeConfig["config"]["timezone"]
},

"owner": {"rid": str(uuid.uuid5(uuid.NAMESPACE_URL, bridge_id + 'device')), "rtype": "device"},
"time_zone": {"time_zone": bridgeConfig["config"]["timezone"]},
"type": "bridge"
}


def geoLocation():
return {
"id": str(uuid.uuid5(uuid.NAMESPACE_URL, bridgeConfig["config"]["bridgeid"] + 'geolocation')),
Expand All @@ -169,38 +162,26 @@ def geoLocation():


def v2BridgeDevice():
result = {"id": str(uuid.uuid5(
uuid.NAMESPACE_URL, bridgeConfig["config"]["bridgeid"] + 'device')), "type": "device"}
config = bridgeConfig["config"]
bridge_id = config["bridgeid"]
result = {"id": str(uuid.uuid5(uuid.NAMESPACE_URL, bridge_id + 'device')), "type": "device"}
result["id_v1"] = ""
result["metadata"] = {
"archetype": "bridge_v2",
"name": bridgeConfig["config"]["name"]
}
result["metadata"] = {"archetype": "bridge_v2", "name": config["name"]}
result["product_data"] = {
"certified": True,
"manufacturer_name": "Signify Netherlands B.V.",
"model_id": "BSB002",
"product_archetype": "bridge_v2",
"product_name": "Philips hue",
"software_version": bridgeConfig["config"]["apiversion"][:5] + bridgeConfig["config"]["swversion"]
"software_version": config["apiversion"][:5] + config["swversion"]
}
result["services"] = [
{
"rid": str(uuid.uuid5(uuid.NAMESPACE_URL, bridgeConfig["config"]["bridgeid"] + 'bridge')),
"rtype": "bridge"
},
{
"rid": str(uuid.uuid5(uuid.NAMESPACE_URL, bridgeConfig["config"]["bridgeid"] + 'zigbee_connectivity')),
"rtype": "zigbee_connectivity"
},
{
"rid": str(uuid.uuid5(uuid.NAMESPACE_URL, bridgeConfig["config"]["bridgeid"] + 'entertainment')),
"rtype": "entertainment"
}
{"rid": str(uuid.uuid5(uuid.NAMESPACE_URL, bridge_id + 'bridge')), "rtype": "bridge"},
{"rid": str(uuid.uuid5(uuid.NAMESPACE_URL, bridge_id + 'zigbee_connectivity')), "rtype": "zigbee_connectivity"},
{"rid": str(uuid.uuid5(uuid.NAMESPACE_URL, bridge_id + 'entertainment')), "rtype": "entertainment"}
]
return result


class AuthV1(Resource):
def get(self):
authorisation = authorizeV2(request.headers)
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ yeelight
python-kasa==0.4.1
bleak==0.14.3
rgbxy==0.5

netifaces==0.11.0

0 comments on commit b91b3fc

Please sign in to comment.