23.09.2020, 03:45
(This post was last modified: 23.09.2020, 03:46 by benanderson_475.)
in case anyone can use it, Updated user library for sony tv ip control
Code:
require('json')
Sony_ip = '192.168.1.6'
X_Auth_Psk = "XXXXXXX9"
path_system = '/sony/system'
path_avContent = '/sony/avContent'
path_audio = '/sony/audio'
path_ircc = '/sony/IRCC'
function Sony_request(host, url, payload)
ltn12 = require('ltn12')
http = require('socket.http')
response_body = {}
res, err = http.request{
url = 'http://' .. host .. url,
method = 'POST',
headers =
{
["Accept"] = "*/*",
["Content-Type"] = "application/x-www-form-urlencoded",
["X-Auth-PSK"] = X_Auth_Psk,
["Content-Length"] = payload:len()
},
source = ltn12.source.string(payload),
sink = ltn12.sink.table(response_body)
}
if response_body then
-- log(response_body)
resp = json.encode(response_body)
resp = json.decode(resp)
-- log(resp)
if resp[2] then
resp = table.concat(resp)
resp = json.decode(resp)
else
resp = json.decode(resp[1])
end
return resp
else
return nil, err
end
end
function Sony_Soap_request(host, url, IRCC)
local body, http, ltn12, sink, res, err
ltn12 = require('ltn12')
http = require('socket.http')
body = [[<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"
s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:X_SendIRCC xmlns:u="urn:schemas-sony-com:service:IRCC:1">
<IRCCCode>]]..IRCC..[[</IRCCCode>
</u:X_SendIRCC>
</s:Body></s:Envelope>]]
Soap_response_body = {}
res, err = http.request{
url = 'http://' .. host .. url,
method = 'POST',
headers =
{
["Host"] = host,
["Connection"] = "keep-alive",
["Accept"] = "*/*",
["Content-Type"] = "text/xml; charset=UTF-8",
["X-Auth-PSK"] = X_Auth_Psk,
["Content-Length"] = #body,
["Soapaction"] = '"urn:schemas-sony-com:service:IRCC:1#X_SendIRCC"'
},
source = ltn12.source.string(body),
sink = ltn12.sink.table(Soap_response_body)
}
if Soap_response_body then
return Soap_response_body
else
return nil, err
end
end
function encode_json_message(method, id, params, version)
json_msg = json.encode({
method = method,
id = id,
params = {params},
version = version
})
return json_msg
end
--************************* Command functions ****************************
function Sony_pwr(state)
Sony_request(Sony_ip, path_system, encode_json_message("setPowerStatus", 55, {status = state }, "1.0"))
end
function Sony_set_input_hdmi1()
Sony_request(Sony_ip, path_avContent, encode_json_message("setPlayContent", 101, {uri = "extInput:hdmi?port=1"}, "1.0"))
end
function Sony_set_input_hdmi2()
Sony_request(Sony_ip, path_avContent, encode_json_message("setPlayContent", 101, {uri = "extInput:hdmi?port=2"}, "1.0"))
end
function Sony_set_input_hdmi3()
Sony_request(Sony_ip, path_avContent, encode_json_message("setPlayContent", 101, {uri = "extInput:hdmi?port=3"}, "1.0"))
end
function Sony_set_input_tv()
Sony_request(Sony_ip, path_avContent, encode_json_message("setPlayContent", 101, {uri = "tv:dvbt"}, "1.0"))
end
function Sony_set_volume(level)
res = Sony_request(Sony_ip, path_audio, encode_json_message("setAudioVolume", 98, {volume = level, ui = "on", target = ""}, "1.2"))
end
function Sony_set_audio_mute(state)
Sony_request(Sony_ip, path_audio, encode_json_message("setAudioMute", 601, {status = state}, "1.0"))
end
--************************* Request Command functions ****************************
function Sony_get_playing_content()
json_msg = encode_json_message("getPlayingContentInfo", 103, {}, "1.0")
res = Sony_request(Sony_ip, path_avContent, json_msg)
--- update obj with new value
if not res.error then -- cant call this function if the display is off res.error[2] = Display Is Turned off
title = res.result[1].title
programme = res.result[1].programTitle
if title then
grp.write('1/1/1', title)
end
if programme then
grp.write('1/1/2', programme)
end
end
return title, programme
end
function Sony_get_power_status()
pwr_res = Sony_request(Sony_ip, path_system, encode_json_message("getPowerStatus", 50, {}, "1.0"))
pwr_res = pwr_res.result[1].status --active|standby
return pwr_res
end
function Sony_get_volume_info()
vol_res = Sony_request(Sony_ip, path_audio, encode_json_message("getVolumeInformation", 33, {}, "1.0"))
vol_res = vol_res.result[1][1].volume -- returns tv volume level
return vol_res
end
function Sony_get_remoteController_info()
res = Sony_request(Sony_ip, path_system, encode_json_message("getRemoteControllerInfo", 54, {}, "1.0"))
rc_codes = {}
res = res.result[2]
for i = 1, #res do
name = res[i].name
value = res[i].value
rc_codes[name] = value
end
-- log(rc_codes)
storage.set('RC_Codes', rc_codes)
return rc_codes
end
function req_channel_delayed(delay)
os.sleep(delay)
Sony_get_playing_content()
end
function Sony_send_key(IRCC_name) -- needs to send ircc with soap command...
--PartnerApp11, SyncMenu, FeaturedApp, MuteOff, MuteOn, AudioMixUp Teletext, AudioOutput_TVSpeaker, partnerApp10, BS, ActionMenu, SleepTimer
--CS, AudioMixDown, TvInput, PartnerApp1, Forward, Hdmi3, DigitalToggle, Hdmi2, PartnerApp6, ApplicationLauncher, Blue, Stop, TvAntennaCable,
--Wide, Help, AudioOutput_AudioSystem, VolumeDown, Hdmi1, Tv, PartnerApp20, OneTouchTimeRec, Down, PartnerApp19, PopUpMenu, FeaturedAppVOD,
--Component2, Video1, Audio, Media, Digital, Num11, PartnerApp13, Mode3D, AndroidMenu, *AD, Component1, CursorRight, Enter, PhotoFrame, Sleep,
--PartnerApp2, Assists, ChannelUp, Right, DOT, RecorderMenu, FootballMode, Green, PartnerApp14, GooglePlay, AudioOutput_Toggle, EPG, SubTitle,
--FlashMinus, Input, PartnerApp18, PartnerApp5, ClosedCaption, PartnerApp16, PAP, PartnerApp15, PartnerApp12, PartnerApp9, TenKey, WakeUp
--PartnerApp7, PartnerApp17, Next, PartnerApp4, PartnerApp3, Analog, PictureMode, STBMenu, TvSatellite, Jump, BSCS, Rec, Hdmi4, Ddata, YouTube
--Mute, ChannelDown, OneTouchView, Home, AnalogRgb1, PictureOff, Num2, TvAnalog, Tv_Radio, , CursorDown, Num3, Num0, WirelessSubwoofer, Video2,
--Confirm, DemoMode, Left, Num1, ShopRemoteControlForcedDynamic, FlashPlus, Display, Num6, GGuide, PartnerApp8, Prev, Num7, Num4, Play, Red,
--TopMenu, Num5, Analog2, DUX, Return, VolumeUp, DpadCenter, Options, Num12, PicOff, Rewind, iManual, DemoSurround, MediaAudioTrack, Up, TvPower,
rc_codes = storage.get('RC_Codes')
if not rc_codes then
rc_codes = Sony_get_remoteController_info()
end
IRCC_key_name = rc_codes[IRCC_name]
Sony_Soap_request(Sony_ip, path_ircc, IRCC_key_name)
if IRCC_name == "ChannelDown" or IRCC_name == "ChannelUp" then
req_channel_delayed(0.5)
end
end
function Sony_get_content_count()
res = Sony_request(Sony_ip, path_avContent, encode_json_message("getContentCount", 11, {source = "tv:dvbt"}, "1.1"))
return res.result[1].count
end
function Sony_get_content_list()
local num = Sony_get_content_count()
res = Sony_request(Sony_ip, path_avContent, encode_json_message("getContentList", 88, {stIdx = 0, cnt = num, uri = "tv:dvbt"}, "1.5"))
res = res.result[1]
uri_s= {}
for i = 1, #res do
uri = res[i].uri
title = res[i].title
uri_s[title] = uri
end
--- log(uri_s)
storage.set('Chan_uri', uri_s)
return uri_s
end
function Sony_Chanell(chan) -- chan is uri name and is returned by Sony_get_content_list() uncomment log(uri_s) above and call function Sony_get_content_list() to see what they are
uri_C = storage.get('Chan_uri')
if not uri_C then
uri_C = Sony_get_content_count()
end
res = Sony_request(Sony_ip, path_avContent, encode_json_message("setPlayContent", 101, {uri = uri_C[chan]}, "1.0"))
req_channel_delayed(0.2)
return res
end