JSON, Websocket and live EPG
#1
Greetings! I'm attempting to build a rudimentary web interface to send Kodi a few commands. I'd like to display what is currently airing on a few specific PVR channels. Thanks to a few examples on these forums, I'm able to connect to Kodi via websocket and retrieve the current on-air data. However, when the next program goes to air the EPG data does not update. Am I missing something or does a change to broadcastnow not trigger a websocket message? Is there another way to acquiring live updated JSON data? You help is greatly appreciated!
html:
<html>
<body>
<script language="javascript" type="text/javascript">
    var ws = new WebSocket('ws://localhost:9090/jsonrpc');
    ws.onopen = function (event) {
        send_message("PVR.GetChannelDetails", { 
                                "properties": ["broadcastnow"], 
                                "channelid": 32,
                                });
        }
    ws.onmessage = function (event) {
        var j = JSON.parse(event.data);
        var r = j.result.channeldetails.broadcastnow;
        document.getElementById("name").innerHTML = r.title;
            }
    function send_message(method, params) {
        var msg = {
            "jsonrpc": "2.0", 
            "method": method, 
            "id": method
        };
        if (params) {
            msg.params = params;
        }
        ws.send(JSON.stringify(msg));
    }
</script>
<div id="name"></div>
</body>
</html>
Reply
#2
A change in EPG does not trigger a websocket message. One solution is to send a command at a set interval which will trigger a websocket message.
javascript:
// Automatically refresh EPG data by sending a websocket message at a set interval
function Autoupdate() {
    websocketTL.send(JSON.stringify({
        "jsonrpc": "2.0",
        "method": "Player.GetItem",
        "params": {
        "properties": ["title"],
        "playerid": 1
        },
        "id": "Player.GetChannelTL"
    }));
setInterval(Autoupdate, 60000); // Time in milliseconds
Reply
#3
It looks like you're only calling PVR.GetChannelDetails only when you open the socket connection. When Autoupdate() runs, r will be null, because on_message will be trying to parse the results from Player.GetItem. Try this:

html:

<html>

<body>
    <script language="javascript" type="text/javascript">
        var ws = new WebSocket('ws://localhost:9090/jsonrpc');

        ws.onmessage = function (event) {
            var j = JSON.parse(event.data);
            var r = j.result.channeldetails.broadcastnow;
            document.getElementById("name").innerHTML = r.title;
        };

        function send_message(method, params) {
            var msg = {
                "jsonrpc": "2.0",
                "method": method,
                "id": method
            };
            if (params) {
                msg.params = params;
            }
            ws.send(JSON.stringify(msg));
        }

        setInterval(() => {
            send_message("PVR.GetChannelDetails", {
                "properties": ["broadcastnow"],
                "channelid": 32,
            });
        }, 60000);
    </script>
    <div id="name"></div>
</body>

</html>
Reply
#4
But I hate using timeouts and intervals, so I would check that the value of j.result.channeldetails.broadcastnow is not null when receiving any message, and if it is, then you know you received a notification, and should update the name. This is quick and dirty (and untested):

html:

<html>
<body>
    <script language="javascript" type="text/javascript">
        var ws = new WebSocket('ws://localhost:9090/jsonrpc');

        ws.onmessage = function (event) {
            var j = JSON.parse(event.data);

            if (j.result && j.result.channeldetails && j.result.channeldetails.broadcastnow) {
                var r = j.result.channeldetails.broadcastnow;
                document.getElementById("name").innerHTML = r.title;
            } else {
                send_message("PVR.GetChannelDetails", {
                    "properties": ["broadcastnow"],
                    "channelid": 32,
                });
            }
        };

        function send_message(method, params) {
            var msg = {
                "jsonrpc": "2.0",
                "method": method,
                "id": method
            };
            if (params) {
                msg.params = params;
            }
            ws.send(JSON.stringify(msg));
        }
    </script>
    <div id="name"></div>
</body>
</html>
Reply
#5
@MrTarantula Thank you for the reply. I think I have a slightly better grasp on Kodi's websocket request results and notifications. FWIW I put together a front-end controller for the upcoming NCAA basketball tournament.  It is currently in a working state but could use some cleanup. Hopefully there's something in there that can help out a future developer.
Reply
#6
Hello,

I am able to get EPG data (live PVR TV title) which is what I want to get (I couldn't quote the entry, 3rd entry of this thread is working for me; https://forum.kodi.tv/showthread.php?tid...pid2834357 ).  However, the "channelid" is changing every day as a random number. Therefore, I want to get channelid from channel name in my list. How can I do that?

@danofun  I have seen your codes in github (danofun/MarchMadnessTVControl). In that file, you have just defined channel names and the system can get channelid automatically. I want to do so as simple as the codes above.

I am sorry if my language is bad. I will be thankful of your help.

Thanks in advance.
Reply

Logout Mark Read Team Forum Stats Members Help
JSON, Websocket and live EPG0