Dec-06-2022, 11:00 AM
(This post was last modified: Dec-06-2022, 02:15 PM by Yoriz.
Edit Reason: Added code tags
)
I am working on a simple telnet program that interacts with a Lutron lighting system. The Lutron system allows me to enter a monitoring mode where it will display the status of a device/area. When a device activates, it changes state and triggers an area scene.
The following is output to the telnet command line:
If I were to store the information like this:
locations = {'Dressing Room 3': {'Device ID': '279,1', 'Device Status': '1',
'Area ID': '161', 'Area Status': '1' }}
1. Is there a way to print the Key name (Dressing Room 3)from the value? e.g when the system reports '~DEVICE,179,1,3,' how do i make it print' Dressing Room 3
2. Could this be done a better way with a database?
Thank you.
Here's the full code:
The following is output to the telnet command line:
Output:~DEVICE,179,1,3
~AREA,161,6,3
Currently I have 2 dictionariesdevice_ids = {'179,1': 'Dressing 3', '179,2': 'Dressing 2', '179,3': 'Dressing 1'} area_status ={'161': 'Dressing 3', '11': 'Dressing 2', '170': 'Dressing 1'}Using regex I extract the information I need to print the location name. I would like to incorporate some error checking into my code to ensure that when a device activates, the scene is triggered too. Something like - If Device 179,1 is active check to see if Area 161 is active. The dictionaries are quite large so manually writing 'if' statements seems like the wrong way to go about things.
If I were to store the information like this:
locations = {'Dressing Room 3': {'Device ID': '279,1', 'Device Status': '1',
'Area ID': '161', 'Area Status': '1' }}
1. Is there a way to print the Key name (Dressing Room 3)from the value? e.g when the system reports '~DEVICE,179,1,3,' how do i make it print' Dressing Room 3
2. Could this be done a better way with a database?
Thank you.
Here's the full code:
def lms(): occ_pattern = re.compile("^[DEVIC]+[,]([(0-9)]+[,][0-9]+)[,]([0-9]+)") area_pattern = re.compile("^[ARE]+[,]([(0-9)]+)[,][0-9]+[,]([0-9]+)") tn.write(occupancy_mon_enable.encode('ascii') + b'\r\n') print('Occupancy Monitoring Enabled') tn.read_until(b'QNET> ') tn.write(scene_mon_enable.encode('ascii') + b'\r\n\n') print('Scene Monitoring Enabled') while True: tn.read_until(b'~') output = tn.read_very_eager().decode('utf-8') try: status = ' ' occ_id = occ_pattern.search(output).group(1) occ_status = occ_pattern.search(output).group(2) if occ_status == '3': status = 'Active' elif occ_status == '4': status = 'Inactive' print('DEVICE,' + occ_id + "," + occ_status + ": " + device_ids[occ_id] + " - " + status) except AttributeError: pass try: scene = ' ' area_id = area_pattern.search(output).group(1) area_scene = area_pattern.search(output).group(2) if area_scene == '1': scene = 'Midnight' elif area_scene == '2': scene = 'Sunrise' elif area_scene == '3': scene = 'Day' elif area_scene == '4': scene = 'Sunset' elif area_scene == '5': scene = 'Evening' elif area_scene == '0': scene = 'Off' else: pass print("AREA," + area_id + "," + area_scene + ": " + area_status[area_id] + " - " + scene) except AttributeError: pass