Page 1 of 1
Script to monitor currentwindow and currentcontrol in Kodi
Posted: Mon Feb 05, 2018 1:25 pm
by holdestmade
Hi,
Would like a way to monitor a few of XBMC2's JSONRPC and generate an event if they change.
I can't find a way in the plugin to do this ?
The script below does what I want, ie generates an event when I run the python script but would like it to run in background and generate an event if either changes.
kgschlosser did me a script to monitor Yeelight bulbs and generate events when their status changes but I can't figure out how to change that to suit.
Thanks in advance.
Code: Select all
control = eg.plugins.XBMC2.JSONRPC('GUI.GetProperties', '{"properties":["currentcontrol"]}', False, True)
window = eg.plugins.XBMC2.JSONRPC('GUI.GetProperties', '{"properties":["currentwindow"]}', False, True)
channel = eg.plugins.XBMC2.JSONRPC('Player.GetItem', '{"properties":["title"], "playerid":1}', False, True)
channel_label, channel_title = channel['item']['label'], channel['item']['title']
control_label = control['currentcontrol']['label']
window_id, window_label = window['currentwindow']['id'], window['currentwindow']['label']
eg.TriggerEvent("Window." + str(window_id) + "." + str(window_label), prefix="XBMC3")
eg.TriggerEvent("Control." + str(control_label), prefix="XBMC3")
eg.TriggerEvent("Channel." + str(channel_label) + "." + str(channel_title), prefix="XBMC3")
Re: Script to monitor currentwindow and currentcontrol in Kodi
Posted: Mon Feb 12, 2018 10:30 pm
by yokel22
What are you wanting to trigger this? When the playing item changes? The xbmc2 plugin will generate these type of events, it just needs to be enabled. Within the xbmc2 settings there is a check box to enable events.
Re: Script to monitor currentwindow and currentcontrol in Kodi
Posted: Tue Feb 13, 2018 1:40 pm
by holdestmade
I have the XBMC2 plugin installed and use it for several things, but the XBMC2 plugin doesn't generate the events for currentwindow and currentcontrol.
I would like them so EG can send a TCP via RTI plugin to my RTI processor so it knows what Kodi is currently selected and what window is currently visible.
I only need an event when they change.
The script above does do what I want but I need a way to have it running and generate an event only when the variables change
Cheers
Re: Script to monitor currentwindow and currentcontrol in Kodi
Posted: Tue Feb 13, 2018 2:49 pm
by yokel22
I'm not exactly sure why you'd want to do this as your going to trigger an event on pretty much every mouse/keyboard/remote press. We're here to serve though, so give this a shot. If it works the way you want. I'll work it into it's own thread so it won't eat a bunch of resources & you won't see the timer event every x seconds. If you don't have the timer plugin in your tree's autostart, add it before pasting this code into eg.
Code: Select all
<?xml version="1.0" encoding="UTF-8" ?>
<EventGhost Version="0.5.0-rc4">
<Folder Name="kodi check" XML_Guid="{39FB1E95-7AEC-4E69-8E2C-2BF081F85E58}" Expanded="True">
<Macro Name="Timer: Start kodi timer" XML_Guid="{FB33F3E8-8D3F-46E5-8B8B-CE7EC2F92245}">
<Action XML_Guid="{81EF578E-7ED1-44DC-A57A-89214A96F9C2}">
Timer.TimerAction(u'CheckKodiControl', 0, 0, 1.0, u'CheckKodiControl', False, True, 0, u'00:00:00')
</Action>
</Macro>
<Macro Name="Check for Kodi window changes" XML_Guid="{F95E2C95-9718-462D-8C81-763D3E02C348}" Expanded="True">
<Event Name="Timer.CheckKodiControl" XML_Guid="{DDFC87D7-D6EF-42B1-B9FF-23BA0BD0FF2C}" />
<Action XML_Guid="{37BF60EA-245B-4C59-8F48-1E622354CDE6}">
EventGhost.PythonScript(u'control = eg.plugins.XBMC2.JSONRPC(\'GUI.GetProperties\', \'{"properties":["currentcontrol"]}\', False, True)\nwindow = eg.plugins.XBMC2.JSONRPC(\'GUI.GetProperties\', \'{"properties":["currentwindow"]}\', False, True)\nchannel = eg.plugins.XBMC2.JSONRPC(\'Player.GetItem\', \'{"properties":["title"], "playerid":1}\', False, True)\n\nchannel_label, channel_title = channel[\'item\'][\'label\'], channel[\'item\'][\'title\']\ncontrol_label = control[\'currentcontrol\'][\'label\']\nwindow_id, window_label = window[\'currentwindow\'][\'id\'], window[\'currentwindow\'][\'label\']\n\ncurrDict = {\'channel_label\' : channel_label, \'channel_title\' : channel_title,\n\'control_label\' : control_label, \'window_id\' : window_id, \'window_label\' : window_label}\n\ntry:\n lastDict = eg.globals.lastKodiControlDict\n valueChange = [item for item in lastDict if lastDict[item] != currDict[item]]\n for item in valueChange:\n if item == \'control_label\':\n eg.TriggerEvent("Control." + str(currDict[item]), prefix="XBMC3")\n if item == \'window_label\':\n eg.TriggerEvent("Window." + str(currDict[\'window_id\']) + "." + str(currDict[\'window_label\']), prefix="XBMC3")\n if item == \'channel_label\':\n eg.TriggerEvent("Channel." + str(currDict[\'channel_label\']) + "." + str(currDict[\'channel_title\']), prefix="XBMC3")\nexcept:\n eg.PrintNotice(\'First time command ran, run again\')\n\neg.globals.lastKodiControlDict = currDict')
</Action>
</Macro>
</Folder>
</EventGhost>
Re: Script to monitor currentwindow and currentcontrol in Kodi
Posted: Wed Feb 14, 2018 8:06 am
by holdestmade
Yes, that works just as I want, could you work your magic with a thread ?
This is so I can display on my remote system the current window and current selection when browsing.
Not sure why this is not included in the XMBC2 plugin ? maybe as an option ?
Thanks very much
Re: Script to monitor currentwindow and currentcontrol in Kodi
Posted: Wed Feb 14, 2018 3:57 pm
by holdestmade
So, with your bit of code and kgschlosser's Yeelight script I cobbled this together that works like a charm !
Could you have a look and see if it can be improved / corrected ?
Thanks for your help
Cheers
Code: Select all
import threading, urllib2, json
class XBMC3(object):
def __init__(self):
self._event = None
self._thread = None
def start(self):
if self._thread is None:
self._event = threading.Event()
self._thread = threading.Thread(target=self.run)
self._thread.daemon = True
self._thread.start()
def stop(self):
if self._event is not None:
self._event.set()
self._thread.join(3.0)
def run(self):
while not self._event.isSet():
try:
window = json.loads(urllib2.urlopen('http://192.168.1.51:8080/jsonrpc?request={"jsonrpc":"2.0","method":"GUI.GetProperties","params":{"properties":["currentwindow"]},"id":1}', timeout=0.5).read())
control = json.loads(urllib2.urlopen('http://192.168.1.51:8080/jsonrpc?request={"jsonrpc":"2.0","method":"GUI.GetProperties","params":{"properties":["currentcontrol"]},"id":1}', timeout=0.5).read())
channel = json.loads(urllib2.urlopen('http://192.168.1.51:8080/jsonrpc?request={"jsonrpc":"2.0","method":"Player.GetItem","params":{"properties": ["title","showtitle"],"playerid":1},"id":"1"}', timeout=0.5).read())
except:
eg.PrintError("Error connecting to Kodi")
channel_label, channel_title = channel['result']['item']['label'], channel['result']['item']['title']
control_label = control['result']['currentcontrol']['label']
window_id, window_label = window['result']['currentwindow']['id'], window['result']['currentwindow']['label']
currDict = {'channel_label' : channel_label, 'channel_title' : channel_title,'control_label' : control_label, 'window_id' : window_id, 'window_label' : window_label}
try:
lastDict = eg.globals.lastKodiControlDict
valueChange = [item for item in lastDict if lastDict[item] != currDict[item]]
for item in valueChange:
if item == 'control_label':
eg.TriggerEvent("Control." + str(currDict[item]), prefix="XBMC3")
if item == 'window_label':
eg.TriggerEvent("Window." + str(currDict['window_id']) + "." + str(currDict['window_label']), prefix="XBMC3")
if item == 'channel_label':
eg.TriggerEvent("Channel." + str(currDict['channel_label']) + "." + str(currDict['channel_title']), prefix="XBMC3")
except:
eg.PrintNotice('First time command ran, run again')
eg.globals.lastKodiControlDict = currDict
self._event.wait(0.1)
self._event = None
self._thread = None
eg.globals.XBMC3 = XBMC3()
eg.globals.XBMC3.start()
Re: Script to monitor currentwindow and currentcontrol in Kodi
Posted: Thu Feb 15, 2018 2:12 pm
by holdestmade
Just a quick note to say I've managed to turn this into a plugin and works great.
Only issue is if I add a command from a plugin, ie eg.pluginname etc it fails.
Is it not possible to do this ?
Thanks
Re: Script to monitor currentwindow and currentcontrol in Kodi
Posted: Thu Feb 15, 2018 6:41 pm
by yokel22
Sorry, I was busy & didn't see these. I'm not exactly sure what your asking. Can you paste the plugin code?
I'm just kinda guessing what your asking here.
If your calling another plugin on your plugin start. That could cause problems. Try putting your plugin at the end of your autostart.
Re: Script to monitor currentwindow and currentcontrol in Kodi
Posted: Thu Feb 15, 2018 8:44 pm
by holdestmade
Thanks for your reply,
I meant calling a plugin from inside my plugin (sorry, quite new to this, learning from you guys as I go...)
While pasting it here, I noticed I missed a parentheses at the end of the string !
It was the red one here:
eg.plugins.RTISimpelTCPServer.SendString('Kodi_Control@' + str(currDict['control_label']) + '#')
Works great now, can't believe it was just that !
Cheers
Code: Select all
import eg
eg.RegisterPlugin(
name = "Kodi to RTI",
author = "Simon Tether",
version = "0.1",
kind = "other",
description = "Monitor Kodi for variable changes and generate events to send to RTI processor"
)
import threading, urllib2, json
class KODI2RTI(eg.PluginBase):
def __init__(self):
self._event = None
self._thread = None
def __start__(self):
if self._thread is None:
self._event = threading.Event()
self._thread = threading.Thread(target=self.run)
self._thread.daemon = True
self._thread.start()
def __stop__(self):
if self._event is not None:
self._event.set()
self._thread.join(3.0)
def run(self):
while not self._event.isSet():
try:
currPlayer = json.loads(urllib2.urlopen('http://192.168.1.51:8080/jsonrpc?request={"jsonrpc": "2.0", "method": "Player.GetActivePlayers", "id": 1}', timeout=0.5).read())
window = json.loads(urllib2.urlopen('http://192.168.1.51:8080/jsonrpc?request={"jsonrpc":"2.0","method":"GUI.GetProperties","params":{"properties":["currentwindow"]},"id":1}', timeout=0.5).read())
control = json.loads(urllib2.urlopen('http://192.168.1.51:8080/jsonrpc?request={"jsonrpc":"2.0","method":"GUI.GetProperties","params":{"properties":["currentcontrol"]},"id":1}', timeout=0.5).read())
except:
eg.PrintError("Error connecting to Kodi")
if currPlayer['result']:
playerid = currPlayer['result'][0]['playerid']
if playerid == 0 or playerid == 1:
try:
playing = json.loads(urllib2.urlopen('http://192.168.1.51:8080/jsonrpc?request={"jsonrpc": "2.0", "method": "Player.GetItem", "params": { "properties": ["title", "album", "artist", "season", "episode", "duration", "showtitle"], "playerid": ' + str(playerid) + '}, "id": "1"}', timeout=0.5).read())
except:
pass
channel_label, channel_title = playing['result']['item']['label'], playing['result']['item']['title']
else:
channel_label, channel_title = "-", "-"
control_label = control['result']['currentcontrol']['label']
window_id, window_label = window['result']['currentwindow']['id'], window['result']['currentwindow']['label']
currDict = {'channel_label' : channel_label, 'channel_title' : channel_title,'control_label' : control_label, 'window_id' : window_id, 'window_label' : window_label}
try:
lastDict = eg.globals.lastKodiControlDict
valueChange = [item for item in lastDict if lastDict[item] != currDict[item]]
for item in valueChange:
if item == 'control_label':
eg.TriggerEvent("Control." + str(currDict['control_label']), prefix="Kodi to RTI")
eg.plugins.RTISimpelTCPServer.SendString('Kodi_Control@' + str(currDict['control_label']) + '#')
if item == 'window_label':
eg.TriggerEvent("Window." + str(currDict['window_id']) + "." + str(currDict['window_label']), prefix="Kodi to RTI")
eg.plugins.RTISimpelTCPServer.SendString('Kodi_Window@' + str(currDict['window_id']) + "." + str(currDict['window_label']) + '#')
if item == 'channel_label':
eg.TriggerEvent("Playing." + str(currDict['channel_label']) + "." + str(currDict['channel_title']), prefix="Kodi to RTI")
eg.plugins.RTISimpelTCPServer.SendString('Kodi_Window@' + str(currDict['channel_label']) + "." + str(currDict['channel_title']) + '#')
except:
eg.Print('Kodi to RTI: First time run')
eg.globals.lastKodiControlDict = currDict
self._event.wait(0.1)
self._event = None
self._thread = None
Re: Script to monitor currentwindow and currentcontrol in Kodi
Posted: Thu Feb 15, 2018 10:10 pm
by yokel22
Cool, looks good, glad you figured it out. I gotta crash(been up two days ). Just an fyi, you don't need to write a plugin to do this. Right on if you just want to learn more about e.g.. Just thought I'd mention it. .
Re: Script to monitor currentwindow and currentcontrol in Kodi
Posted: Fri Feb 16, 2018 6:58 am
by holdestmade
Ok, do you mean it would be better to just have it as a script in Autostart ?
Thanks.