Created
October 9, 2016 15:47
-
-
Save jamesbulpin/48c6e0c89055fe65ce4b5ecc8259a356 to your computer and use it in GitHub Desktop.
A Kodi add-on main script to spawn a Arduino->MQTT proxy
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
#!/usr/bin/python | |
import os | |
try: | |
import xbmc | |
import xbmcaddon | |
except ImportError, e: | |
import time | |
class xbmc: | |
class Monitor: | |
def abortRequested(self): | |
return False | |
def waitForAbort(self, delay): | |
time.sleep(delay) | |
@staticmethod | |
def log(str): | |
print str | |
@staticmethod | |
def executebuiltin(str): | |
print "executebuiltin(%s)" % (str) | |
class xbmcaddon: | |
class Addon: | |
def getSetting(self, setting): | |
if setting == "ip_address": return "10.52.2.41" | |
ADDON_ID = "script.service.lwrf433" | |
ADDON_NAME = "lwrf433" | |
LISTENER_SCRIPT = "433listener.py" | |
addon = xbmcaddon.Addon() | |
def log(msg): | |
xbmc.log("[" + ADDON_NAME + "] " + msg) | |
def main(): | |
log("Starting main function v1") | |
monitor = xbmc.Monitor() | |
while not monitor.abortRequested(): | |
try: | |
# See if our script is running | |
found = 0 | |
try: | |
if os.path.exists("/tmp/433listener.py.pid"): | |
pid = open("/tmp/433listener.py.pid", "rb").read() | |
if os.path.exists(os.path.join("/proc", pid, "exe")): | |
found = 1 | |
except: | |
pass | |
if not found: | |
addr = addon.getSetting('ip_address') | |
log("Starting listener script " + LISTENER_SCRIPT + " with MQTT broker " + addr) | |
xbmc.executebuiltin("RunScript(special://home/addons/%s/resources/scripts/%s,%s)" % (ADDON_ID, LISTENER_SCRIPT, addr)) | |
except Exception, e: | |
log("Main loop exception: " + str(e)) | |
if monitor.waitForAbort(5): | |
break | |
log("All done") | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment