﻿-- vim:set ft=lua:
dofile(mg.document_root..'\\api\\util.lua')

edcb.htmlEscape=15

epg=mg.get_var(mg.request_info.query_string,'epg')
notify=mg.get_var(mg.request_info.query_string,'notify')
suspend=mg.get_var(mg.request_info.query_string,'suspend')

if epg or suspend then
	AssertCsrf(mg.request_info.query_string)
end

if epg then
  if epg=='reload' then
    messege='EPG再読み込みを開始'..(edcb.ReloadEpg() and 'しました' or 'できませんでした')
  elseif epg=='epgcap' then
    messege='EPG取得開始'..(edcb.EpgCapNow() and 'しました' or 'できませんでした')
  end
elseif notify then
  messege=edcb.GetNotifyUpdateCount(notify)
elseif suspend then
  if suspend=='standby' then
    --ok=edcb.os.execute('start "SetSuspendState" powershell -NoProfile -Command "& {[Threading.Thread]::Sleep(5000);Add-Type -AssemblyName System.Windows.Forms;[System.Windows.Forms.Application]::SetSuspendState([System.Windows.Forms.PowerState]::Suspend,$false,$false)}"')
  	messege='スタンバイに移行'..((ok or edcb.SendSuspend and edcb.SendSuspend(1)) and 'します' or 'できません (スクリプトのコメントアウトを解除してください)')
  elseif suspend=='hibernation' then
  	messege='休止に移行'..(edcb.SendSuspend and edcb.SendSuspend(2) and 'します' or 'できません')
  end
end

ct='<?xml version="1.0" encoding="UTF-8" ?'..'><entry><info>'
  ..(messege or '不正値入力')..'</info></entry>'
mg.write('HTTP/1.1 200 OK\r\nContent-Type: text/xml\r\nContent-Length: '..#ct..'\r\nConnection: close\r\n\r\n', ct)
