Wednesday, February 19, 2014

Logic Layer Part2

Ok the Simple Logic layer script is just that pretty basic but as I mentioned earlier is serves as a place to make changes to the system without taking the system down. It basically a buffer between the nodes in the house and the main controller thats doing all the switching.....

So to better understand what I am doing this picture is a better representation

So for the House at least its not all that complicated really. In this blog we will be dealing with the LogicScript....

The Code:

 def on_message(mosq, obj, msg):
    ts =datetime.datetime.now().strftime("%B, %d %Y %I:%M%p")
   
    
    print ("---------------------------------------")
   
    print ("TIME RECEIVED: ")+ ts
    print ("FROM NODE: ") + (msg.topic)
    print ("NODE MSG: ") + (msg.payload)
   
 
    cli.publish('/HOUSE/Control/' , msg.payload) # <----- sends message to Controller (KIT1,1)
    print ("Message forwarded to Controller")
    print ("acknowledged @ ") + ts
    print
    cli.publish('/HOUSE/LogicClient/logicmsg/' , ' forwarded +( msg.payload)+ to Controller')
    print ("MSG update sent to webserver")
    
    if (msg.topic) == ("/ALARMS/Status"): # <-- leave off trailing / when comparing
      print (Fore.WHITE + Back.RED + Style.BRIGHT + ("STATUS: ALARM! "))
      print (Fore.RESET + Back.RESET + Style.RESET_ALL)
  

  # --------------------------------------PopUp Message------------------------------------
    subprocess.Popen(['notify-send', "System Message:",msg.payload])
    return              

    else:
     print ("STATUS: Normal")        
     print ("End of Event")
     print ("---------------------------------------")
     print
   
 
  def on_publish(mosq, obj, mid):
    pass

 

cli = mosquitto.Mosquitto("LogicClient")
cli.on_message = on_message
cli.on_publish = on_publish
cli.connect("192.168.30.4", 1883, 60)
cli.publish('/mainTopic/Announce/LogicClient/','Online')


cli.subscribe("/HOUSE/Kitchen/", 0)
cli.subscribe("/HOUSE/Livingroom/", 0)
cli.subscribe("/HOUSE/Bath/", 0)
cli.subscribe("/HOUSE/Bedrm1/", 0)
cli.subscribe("/HOUSE/Bedrm2/", 0)
cli.subscribe("/HOUSE/FrontPorch/", 0)
cli.subscribe("/HOUSE/Backporch/", 0)
cli.subscribe("/HOUSE/Garage1/", 0)
cli.subscribe("/HOUSE/Hallway/", 0)
cli.subscribe("/HOUSE/Office/", 0)
cli.subscribe("/HOUSE/Test/", 0)
cli.subscribe("/ALARMS/Status/" ,0)



while cli.loop() == 0:
    pass


The script itself is a loop of sorts. It is constantly listening to the network waiting to hear messages that are being published to topis it has subscribed to. If I have tio add or remove anything, then I can edit this script off line and then save and restart. Down time is next to zero.

The script is pretty self explanatory. The output is to a terminal windows which I can comment out once I get everything done. I like to see whats going on visually as I test and finalize parts of the system. The only funky thing really is the subprocess code. This is for my Ubuntu box, so it wont work on Windows. If there is an alarm detected, this causes my linux box to throw up a OSD for 10 secs in the upper right corner. Ubuntu users will know what I mean. Its a way to get my attention should a node post to the Alarms topic

Also since Im currently using a web page for my system to monitor and display statuses, the line cli.publish('/mainTopic/Announce/LogicClient/','Online')  posts to the webpage that this script is running. I still need to add a Last will and testament to this so if the script fails, then the MQTT server will post the testament message and Ill know something is wrong.

There are also some comments on here for me to remember. I found that the trailing "/" had to be ommited on the script or it would ignore messages published to topics....a "gotcha" I found out. 

This is a typical output from the script to the termimal. FP1,1 is the message that the front porch light has been turned on.



and this is an alarm event



So thats it for the Logic Script. It will evolve and refine as I progress but its a start amd it is working well right now in the house.

We will look at the Trigger script and the MySQL update script in future posts. In the meantime

Happy Coding!

No comments:

Post a Comment