So using the example I was inspired to add some lines to make a nice little utility.
There are several lamp fixtures around the house that can be turned on and off plus can be dimmed and brightened remotely through the use of X10 modules. I have a CM19a X10 USB transceiver that is connected to a DELL desktop. Plugged in outlets there are X10 lamp modules and a X10 receiver/appliance switch. This receiver takes messages sent from the CM19a and transmits these signals through the house's electrical system where the individually addressed lamp modules can pull the message meant for it. The lamp module then can turn on what is plugged into it on,off,dim,brighten.
Also throughout the house there are X10 remote wireless motion detectors that the CM19a can receive a message from, there is motion or it is light or dark. This capability is used by Zoneminder running on the DELL which handles local and network cameras and recording events alarmed by these motion detectors. This lightens the CPU loads by not having to use the motion detection analysis from Zoneminder for each individual camera.
The CM19a is used through a Python2 driver which runs as a daemon.
So for lamp switches I wrote a small GUI in python2 to control the individually addressed lamp modules. Also I have written a switch panel that is a web based page in PHP and can be used via the Internet or LAN.
The problem was the python switches did not have state persistence. So if I turn lets say lamp module B9 ON, and I closed the GUI, when I restarted the program it was always off and did not reflect the state the lamp module B9 was actually in. Same with the PHP web version. Now with this utility an indicator of On/Off state is resident in the tray. Both switches written in the different languages can use the same flag file called /tmp/switch_on.txt which will trigger the correct response from the utility x10_tray and display the red or green indicator.
Python switch GUI example:
Code: Select all
#!/usr/bin/env python2
import pathlib
import os
import requests
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from pathlib import Path
class SwitcherWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="X10 B9 Switch")
self.set_border_width(10)
hbox = Gtk.Box(spacing=6)
self.add(hbox)
button = Gtk.Button.new_with_label("B9 Dim")
button.connect("clicked", self.on_click_me_dimb9)
hbox.pack_start(button, True, True, 0)
button = Gtk.Button.new_with_label("B9 Bright")
button.connect("clicked", self.on_click_me_brightb9)
hbox.pack_start(button, True, True, 0)
switch = Gtk.Switch()
switch.connect("notify::active", self.on_switch_activated,"9")
filename = Path('/tmp/switch_on.txt')
if filename.is_file():
# file exists
switch.set_active(True)
filename.touch(exist_ok=True)
else:
switch.set_active(False)
hbox.pack_start(switch, True, True, 0)
def on_switch_activated(self, switch, gparam,name):
URL = "http://192.168.0.5:8008/"
if switch.get_active():
state = "on"
params = (
('house', 'B'),
('unit', name),
('command', state),
)
response = requests.get(URL, params=params)
filename = Path('/tmp/switch_on.txt')
filename.touch(exist_ok=True) # will create file, if it exists will do nothing
else:
URL = "http://192.168.0.5:8008/"
state = "off"
params = (
('house', 'B'),
('unit', name),
('command', state),
)
response = requests.get(URL, params=params)
os.remove('/tmp/switch_on.txt') #remove state indicator flag file
print("Button", name, "was turned", state)
def on_click_me_dimb9(self, button):
URL = "http://192.168.0.5:8008/"
state = "dim"
params = (
('house', 'B'),
('unit', "9"),
('command', state),
)
response = requests.get(URL, params=params)
print("\"B9 Dim\" button was clicked")
def on_click_me_brightb9(self, button):
URL = "http://192.168.0.5:8008/"
state = "bright"
params = (
('house', 'B'),
('unit', "9"),
('command', state),
)
response = requests.get(URL, params=params)
print("\"B9 Bright\" button was clicked")
win = SwitcherWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
This is the C code I added to the example to allow me to turn on and off the lamp module by right or left clicking the state indicator icon in the tray.
Code: Select all
////// The following two functions handle left & right clicks on the tray icon
void tray_icon_on_left_click(GtkStatusIcon *status_icon, gpointer user_data)
{
char DeleteCmd[80]; // Make a shell command to delete the testfile
strcpy(DeleteCmd, "rm ");
strcat(DeleteCmd, test_file_name);
system(DeleteCmd);
CURL *curl;
CURLcode res;
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL,"http://192.168.0.5:8008/?house=B&unit=9&command=OFF");
curl_easy_perform(curl);
}
void tray_icon_on_right_click(GtkStatusIcon *status_icon, guint button, guint activate_time, gpointer user_data)
{
char TouchCmd[80]; // Recreate the file using 'touch'
strcpy(TouchCmd, "touch ");
strcat(TouchCmd, test_file_name);
system(TouchCmd);
CURL *curl;
CURLcode res;
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL,"http://192.168.0.5:8008/?house=B&unit=9&command=ON");
curl_easy_perform(curl);
// Or .. you could just have it:
// gtk_main_quit();
}
////// End clicks ...
then to compile the program added the CURL linked library to the MakeFile:
Code: Select all
gcc -o x10_tray x10_tray.c -w `pkg-config --libs gtk+-3.0` `pkg-config --cflags gtk+-3.0` -lcurl
- Screenshot(34).png (188.47 KiB) Viewed 1475 times