import "pkg:/components/parts/AutoBind/OnNodeReadyNoOp.bs" import "pkg:/components/Services/LoungeService/LoungeApi.bs" function Init() m.top.functionName = "LoungeServiceLoop" deviceInfo = CreateObject("roDeviceInfo") m.top.deviceId = deviceInfo.GetRandomUUID() deviceFriendlyName = deviceInfo.GetFriendlyName() if StringUtils.IsNullOrEmpty(deviceFriendlyName) deviceFriendlyName = "TV" end if m.top.deviceName = "Playlet on " + deviceFriendlyName end function function StartService(_unused as dynamic) as boolean if m.top.isRunning = true return true end if m.top.control = "run" return true end function function StopService(_unused as dynamic) as void if m.top.isRunning = false return end if m.top.shouldQuit = true end function function LoungeServiceLoop() as void api = new LoungeApi(m.top, m.top.videoQueue, m.top.notifications, m.top.invidious) ' Mobile app can remain connected to devices even after restarts. ' Since we create a new lounge every time, we signal to the last lounge ' that we're no longer there, and we're not coming back. api.DisconnectFromLastLounge() messagePort = CreateObject("roMessagePort") ' We do not join a lounge until something asks us to like ' - A remote connects through DIAL ' - Generating a TV link code m.top.observeField("join", messagePort) wait(0, messagePort) m.top.unobserveField("join") screenId = api.GetScreenId() if StringUtils.IsNullOrEmpty(screenId) Shutdown("No screen ID found") return end if m.top.screenId = screenId loungeToken = api.GetToken() if StringUtils.IsNullOrEmpty(loungeToken) Shutdown("No lounge token found") return end if m.top.loungeToken = loungeToken m.top.observeField("outgoingMessages", messagePort) if not api.GetSessionData() Shutdown("Failed to get session data") return end if api.SaveCurrentLoungeData() api.FetchCommandsRpc(messagePort) m.top.shouldQuit = false isOpen = false outgoingLoungeMessages = [] portMessages = [] socketMessages = [] while true if m.top.shouldQuit = true exit while end if message = wait(60000, messagePort) portMessages.Clear() portMessages.Push(message) ' There's a chance messages were piling up while we were sending messages to the lounge ' This could happen if we were sending things to the lounge and the player state ' changed many times. while messagePort.PeekMessage() <> invalid portMessages.Push(messagePort.GetMessage()) end while ' At this point: ' - We check if we need to send messages to the lounge based on player state ' - We check the lounge for new commands, which might need responding to ' - Finally, we send the outgoing messages to the lounge in one go outgoingLoungeMessages.Clear() socketMessages.Clear() for each portMessage in portMessages messageType = type(portMessage) if messageType = "roSocketEvent" socketMessages.Push(portMessage) else if messageType = "roSGNodeEvent" field = portMessage.GetField() if field = "outgoingMessages" messages = portMessage.GetData() outgoingLoungeMessages.Append(messages) end if end if end for isOpen = api.Poll(socketMessages, outgoingLoungeMessages) api.SendMessages(outgoingLoungeMessages) outgoingLoungeMessagesResults = api.ProcessPendingOutgoingMessages() if not isOpen sleep(3000) unknownSid = false ' TODO:P2 move this check somewhere else if api.poller.status_code = 400 and ValidString(api.poller.status_text) = "Unknown SID" api.poller.status_code = 0 unknownSid = true end if if not unknownSid for each result in outgoingLoungeMessagesResults if result.status = 400 and result.error.InStr("Unknown SID") <> -1 unknownSid = true exit for end if end for end if if unknownSid ' We lost the session for some reason, so we need to rejoin the lounge LogWarn("Lost session, rejoining") if not api.GetSessionData() Shutdown("Failed to rejoin session data") return end if api.SaveCurrentLoungeData() end if api.FetchCommandsRpc(messagePort) end if end while Shutdown("Service stopped") end function function Shutdown(reason as string) LogInfo("Shutting down:", reason) m.top.unobserveField("outgoingMessages") m.top.isRunning = false end function