Last active
March 15, 2024 17:17
-
-
Save bbartling/6a771fb2fcb4a9491db481d7edcb7eec to your computer and use it in GitHub Desktop.
concept idea rest API for global share values in typical BAS contracting... Usually the boiler plant controller ALWAYS has the outside air temperature sensor hardwired and its ALWAYS needed to share that sensor value to other devices like air handling units that need that value to execute sequencing.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import random | |
import time | |
BASE_URL = "http://localhost:8000" | |
def simulate_sensor_reading(sensor_name): | |
# This function simulates reading a sensor by generating a random value. | |
# Replace this logic with actual sensor reading if integrating with real sensors or systems. | |
if sensor_name == "outside_air_temperature": | |
return random.uniform(30, 50) # Simulate to sometimes trigger the alarm | |
elif sensor_name == "hot_water_supply_temperature": | |
return random.uniform(90, 120) | |
else: # Zone temperature sensors | |
return random.uniform(65, 75) | |
def check_conditions_and_notify(outside_air_temp, hot_water_supply_temp, zone_temps): | |
critical_low_temp = 40.0 | |
critical_low_hot_water_temp = 100.0 | |
zone_critical_low = 60.0 | |
zone_critical_high = 85.0 | |
if ( | |
outside_air_temp < critical_low_temp | |
and hot_water_supply_temp < critical_low_hot_water_temp | |
): | |
print( | |
f"Critical alarm: Outside air temp is {outside_air_temp}F, hot water supply temp is {hot_water_supply_temp}F." | |
) | |
for zone, temp in enumerate(zone_temps, start=1): | |
if temp < zone_critical_low: | |
print(f"Critical low temperature alarm in zone {zone}: {temp}F.") | |
elif temp > zone_critical_high: | |
print(f"Critical high temperature alarm in zone {zone}: {temp}F.") | |
def main(): | |
while True: | |
# Simulate reading from sensors | |
outside_air_temp = simulate_sensor_reading("outside_air_temperature") | |
hot_water_supply_temp = simulate_sensor_reading("hot_water_supply_temperature") | |
zone_temps = [ | |
simulate_sensor_reading(f"zone_{i}_temp") for i in range(1, 6) | |
] # Simulating 5 zone temps | |
# Check and notify based on conditions | |
check_conditions_and_notify(outside_air_temp, hot_water_supply_temp, zone_temps) | |
print("Waiting for 5 minutes before the next update...") | |
time.sleep(300) | |
if __name__ == "__main__": | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
BASE_URL = "http://localhost:8000" | |
def add_device(device_data): | |
"""Add a new BACnet device to the network.""" | |
response = requests.post(f"{BASE_URL}/devices", json=device_data) | |
if response.status_code == 200: | |
print("Device added successfully.") | |
return response.json() | |
else: | |
print(f"Error adding device: {response.text}") | |
return None | |
def delete_device(device_id): | |
"""Delete a device from the network.""" | |
response = requests.delete(f"{BASE_URL}/devices/{device_id}") | |
if response.status_code == 200: | |
print(f"Device {device_id} deleted successfully.") | |
else: | |
print(f"Error deleting device {device_id}: {response.text}") | |
def add_point_to_device(device_id, point_data): | |
"""Add a new point to a device.""" | |
response = requests.post(f"{BASE_URL}/devices/{device_id}/points", json=point_data) | |
if response.status_code == 200: | |
print("Point added successfully.") | |
return response.json() | |
else: | |
print(f"Error adding point: {response.text}") | |
return None | |
def delete_point_from_device(device_id, point_id): | |
"""Delete a point from a device.""" | |
response = requests.delete(f"{BASE_URL}/devices/{device_id}/points/{point_id}") | |
if response.status_code == 200: | |
print(f"Point {point_id} deleted successfully.") | |
else: | |
print(f"Error deleting point {point_id}: {response.text}") | |
def main(): | |
# Example of adding a new device | |
new_device = { | |
"name": "Boiler Controller", | |
"instance_id": 1001, | |
"type": "boiler" | |
} | |
device = add_device(new_device) | |
device_id = device.get('instance_id') if device else None | |
if device_id: | |
# Add points to the device | |
points = [ | |
{"name": "Outside Air Temperature", "type": "analog-input", "alarms": "enabled", "trends": "enabled"}, | |
{"name": "Hot Water Supply Temperature", "type": "analog-value", "alarms": "enabled", "trends": "enabled"} | |
] | |
for point in points: | |
add_point_to_device(device_id, point) | |
# Example to delete a point, assuming point_id is known or retrieved from previous responses | |
delete_point_from_device(device_id, 1) # Simulated point_id | |
# Delete the device | |
delete_device(device_id) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment