307 karma · joined September 15, 2013
And no, not safe without precautions.
These days SWI-Prolog even has a library for Redis.
The send_display_vehicle_id(Id) in the end actually does a lot, it queries redis for previously announced display type devices, then looks up the capabilities (bit depth, resolution) of each device and formats accordingly then sends the display request for each. Also send_android_notification sends the request to some Java code that initiates a push message to my phone and watch.
None of this is rocket science in terms of Prolog message(MQTTPathAsList, MessageBody) is my general predicate for handling an incoming MQTT message once I've subscribed to it and it's convenient to deal with a MQTT topic path as a list.
message(['Smarthome','Notify', 'vehicle','motion'],_):- log('brain saw vehicle motion'), get_short_timestamp(S), atomic_list_concat([S, ' Vehicle Motion'], NotificationMessage), send_android_notification('Smarthome', NotificationMessage), request_image('alibi4').
request_image(Camera) :- publish(['Smarthome', 'Video', Camera, 'brain', 'request']).
message(['Smarthome','Video', _ ,'brain','response'],Image):- store_image(Image, 'brain'), analyze_image(Image, 'brain').
analyze_image(Image, Id) :- publish(['Smarthome', 'Sighthound', 'analyze', Id], Image).
message(['Smarthome','Vehicle', 'Sighthound'],VehicleJson):- vehicle_id(VehicleJson, Id), get_short_timestamp(S), atomic_list_concat([S, ' brain saw vehicle: ', Id], NotificationMessage), send_android_notification('vehicle', NotificationMessage), send_display_vehicle_id(Id), atom_concat(Id, VehicleJson, Description), atom_concat('brain saw vehicle: ', Description, Message), log(Message).
I don't distribute the home automation code however it's pretty specific to my house. The MQTT library provides some building block examples.
You can also see some other comments I've made on HN which describe some other details.
It's funny what works.
I worked with a ‘senior developer’ who, on a really big project with lots of different coders around the country, prototyped the implementation in prolog including the JUnit tests (in prolog too).
I think I actually know the answer to this one. I don't remember whether it was while I was still in school or later, but once heard someone say that they always picked a language to implement a prototype in that would never under any circumstances be used for the final system so that it was impossible to try to hack the prototype into the final system.
And for the record I actually like Prolog for certain projects.
On the Prolog side of things with not that much framework code here is a trivial example:
message(['Smarthome','Notify','brain','ping'],_):- publish(['Smarthome', 'Notify', 'brain', 'ping_response']).
This code receives a MQTT message with a topic of Smarthome/Notify/brain/ping ignoring the message body and then publishes a return message on topic Smarthome/Notify/brain/ping_response
I even have a container that runs nothing but cron with a mqtt client installed that sends mqtt messages at various times of the day and all Prolog has to do is subscribe to those topics and handle those messages.
Look in the examples directory for some basic pub/sub code.
The Prolog code that runs in my house is pretty specific to my house so I figured the best way to open source things would be as a framework more than an implementation. You can contact me via issues on the github repo and prod me into adding some more advanced examples - I've learned a lot since my last commit on the repo.
I've started using the Redis functionality recently added to SWI-Prolog, so my code now responds to MQTT messages and uses state queried from Redis to help determine what actions (implemented by sent MQTT messages) to send out. The beauty is that since I don't do anything that blocks significantly in the Prolog code it is now single threaded - even the MQTT listening. It still responds quickly enough and is MUCH easier to deal with than multi-threaded.
An example of what I'm doing is I built a bunch of ESP8266/EESP32 display devices that control neopixels/OLED/LCD displays. When one of those devices boots it sends a MQTT message announcing its location and capabilities (display type, bit depth, dimensions). Prolog receives that message and then stores that info in Redis. So that device info is all dynamic.
So then later Prolog might get a notification that something is in the driveway. All by MQTT it requests an image from the appropriate camera, then sends the image off to Sighthound and deepstack image recognition servers. The Sighthound front end sends a message back to Prolog with a description of any vehicle spotted which Prolog then matches against known vehicles. If it determines for example that a Fedex truck is in the driveway then Prolog sends notifications around the house - it queries all the display devices from Redis and then based on the capabilities of each devices creates a JSON formatted MQTT message to send to each announcing the Fedex truck. It then also sends a MQTT message to some Java code that connects with Google and sends a push message to an Android app I wrote that displays the alert on my phone and watch.
Before I switched to MQTT I was using Prolog with CORBA as the message transport and back then I also had Visual Basic and MS Agent as part of the system. One night I got bored and a little while later had 3 Peedy the Parrot characters singing Row Your Boat in a round across three different computers coordinated by Prolog. It was actually only a page worth of custom Prolog code for that.