plugin.Name = "SonyBDPlayer"; plugin.OnChangeRequest = onChangeRequest; plugin.OnConnect = onConnect; plugin.OnDisconnect = onDisconnect; plugin.OnPoll = onPoll; plugin.OnSynchronizeDevices = onSynchronizeDevices; plugin.PollingInterval = -1; plugin.DefaultSettings = { "Host" : "192.168.1.1", "Port" : "50001", // some use 52323 or standard 80 instead "MAC" : "11:22:33:44:55:66", "UseIPforWOL" : "", // set this to anything other than empty string if need to send IP (and port) for WOL; host must be IP address and not name in this case "WOLPort" : "", // standard is 9; if left empty, the port will not be sent }; var VERSION = "2020-04-28"; var http = new HTTPClient(); var powerOn = false; var mediaCommandMappings = { "PowerOn" : "WOL", "PowerOff" : "AAAAAwAAHFoAAAAVAw==", "DirectionUp" : "AAAAAwAAHFoAAAA5Aw==", "DirectionLeft" : "AAAAAwAAHFoAAAA7Aw==", "DirectionDown" : "AAAAAwAAHFoAAAA6Aw==", "DirectionRight": "AAAAAwAAHFoAAAA8Aw==", "Select" : "AAAAAwAAHFoAAAA9Aw==", "Home" : "AAAAAwAAHFoAAABCAw==", "Options" : "AAAAAwAAHFoAAAA/Aw==", "Return" : "AAAAAwAAHFoAAABDAw==", "Number1" : "AAAAAwAAHFoAAAAAAw==", "Number2" : "AAAAAwAAHFoAAAABAw==", "Number3" : "AAAAAwAAHFoAAAACAw==", "Number4" : "AAAAAwAAHFoAAAADAw==", "Number5" : "AAAAAwAAHFoAAAAEAw==", "Number6" : "AAAAAwAAHFoAAAAFAw==", "Number7" : "AAAAAwAAHFoAAAAGAw==", "Number8" : "AAAAAwAAHFoAAAAHAw==", "Number9" : "AAAAAwAAHFoAAAAIAw==", "Number0" : "AAAAAwAAHFoAAAAJAw==", "Display" : "AAAAAwAAHFoAAABBAw==", "Audio" : "AAAAAwAAHFoAAABkAw==", "SubTitle" : "AAAAAwAAHFoAAABjAw==", "Favorites" : "AAAAAwAAHFoAAABeAw==", "Yellow" : "AAAAAwAAHFoAAABpAw==", "Blue" : "AAAAAwAAHFoAAABmAw==", "Red" : "AAAAAwAAHFoAAABnAw==", "Green" : "AAAAAwAAHFoAAABoAw==", "Play" : "AAAAAwAAHFoAAAAaAw==", "Stop" : "AAAAAwAAHFoAAAAYAw==", "Pause" : "AAAAAwAAHFoAAAAZAw==", "Rewind" : "AAAAAwAAHFoAAAAbAw==", "FastForward" : "AAAAAwAAHFoAAAAcAw==", "Prev" : "AAAAAwAAHFoAAABXAw==", "Next" : "AAAAAwAAHFoAAABWAw==", "SkipBackward" : "AAAAAwAAHFoAAAB2Aw==", "SkipForward" : "AAAAAwAAHFoAAAB1Aw==", "Angle" : "AAAAAwAAHFoAAABlAw==", "TopMenu" : "AAAAAwAAHFoAAAAsAw==", "PopUpMenu" : "AAAAAwAAHFoAAAApAw==", "Eject" : "AAAAAwAAHFoAAAAWAw==", "Karaoke" : "AAAAAwAAHFoAAABKAw==", "Qriocity" : "AAAAAwAAHFoAAABMAw==", "Netflix" : "AAAAAwAAHFoAAABLAw==", "Mode3D" : "AAAAAwAAHFoAAABNAw==", }; function onChangeRequest(device, attribute, value) { console.log("onChangeRequest called..."); var host = plugin.Settings["Host"]; var port = plugin.Settings["Port"]; var mac = plugin.Settings["MAC"]; var useipforwol = (plugin.Settings["UseIPforWOL"] == "") ? false : true; var wolport = plugin.Settings["WOLPort"]; switch(attribute) { case "MediaCommand": var cmd = mediaCommandMappings[value]; if(cmd) { if(cmd == "WOL") { console.log(" MAC: " + mac); console.log(" use IP for WOL?: " + useipforwol); console.log(" Host: " + host); console.log(" WOLPort: " + wolport); var wolOpts = {}; if(useipforwol) { wolOpts["address"] = host; if(wolport != "") { wolOpts["port"] = parseInt(wolport); } console.log(" sending WOL to: " + mac + " with " + wolOpts); WOL.wake(mac, wolOpts); } else { console.log(" sending WOL to: " + mac); WOL.wake(mac); } // very simple power state monitoring powerOn = true; } else if(!powerOn && value == "PowerOff") { // we're not on, so don't try to turn us off (this avoid an annoying message in some situations) console.log(" not powered on, so power off command ignored"); } else { var url = ""; var body = ""; var httpOptions = {}; console.log(" Host: " + host); console.log(" Port: " + port); url += "http://" + host; if(port != 80) { url += ":" + port; } // NOTE: some devices apparently use "/sony/IRCC" instead url += "/upnp/control/IRCC"; body += ""; body += cmd; body += ""; httpOptions = { headers: { "Content-Type" : "text/xml", "SOAPACTION" : "\"urn:schemas-sony-com:service:IRCC:1#X_SendIRCC\"", } }; console.log(" sending request to: " + url); console.log(" command: " + cmd); http.post(url, body, httpOptions); if(value == "PowerOff") { // update power state powerOn = false; } // all done } } else { console.log("ERROR: unsupported command: " + value); } break; default: console.log("ERROR: unsupported attribute type: " + attribute); break; } } function onConnect() { console.log("onConnect called..."); var device = plugin.Devices[plugin.Name]; if(device) { console.log(" setting supported media commands..."); device.SupportedMediaCommands = Object.keys(mediaCommandMappings); console.log(" set..."); } } function onDisconnect() { console.log("onDisconnect called..."); } function onPoll() { console.log("onPoll called..."); } function onSynchronizeDevices() { console.log("onSynchronizeDevices called..."); var device = new Device(); device.Name = plugin.Name; // override default naming conversion so it looks nicer device.Id = plugin.Name; device.DisplayName = "Sony BD Player"; device.Icon = "Disc"; device.Capabilities = [ "MediaControl" ]; device.Attributes = [ ]; plugin.Devices[plugin.Name] = device; console.log(" done syncing"); }