obs = require('obslua') os = require('os')
scene_now = "" scene_WEB251 = "WEB251" -- 富士山カメラ scene_WEB252 = "WEB252" -- 水槽カメラ latitude = 35.68039639541995 -- 緯度経度は東京駅にしてある longitude = 139.76788440520536 sunrise_margin = -30 -- 分単位で指定(2時間前=-120) sunset_margin = 30 -- 分単位で指定(2時間後=120)
-- sunrise / sunset calculation function rscalc(latitude, longitude, when) local pi = math.pi local doublepi = pi * 2 local rads = pi / 180.0
local TZ = function(when) local ts = os.time(when) local utcdate, localdate = os.date('!*t', ts), os.date('*t', ts) localdate.isdst = false local diff = os.time(localdate) - os.time(utcdate) return math.floor(diff / 3600) end
local range = function(x) local a = x / doublepi local b = doublepi * (a - math.floor(a)) return b < 0 and (doublepi + b) or b end
when = when or os.date('*t')
local y2k = { year = 2000, month = 1, day = 1 } local y2kdays = os.time(when) - os.time(y2k) y2kdays = math.ceil(y2kdays / 86400)
local meanlongitude = range(280.461 * rads + 0.9856474 * rads * y2kdays) local meananomaly = range(357.528 * rads + 0.9856003 * rads * y2kdays) local lambda = range(meanlongitude + 1.915 * rads * math.sin(meananomaly) + rads / 50 * math.sin(2 * meananomaly))
local obliq = 23.439 * rads - y2kdays * rads / 2500000
local alpha = math.atan2(math.cos(obliq) * math.sin(lambda), math.cos(lambda)) local declination = math.asin(math.sin(obliq) * math.sin(lambda))
local LL = meanlongitude - alpha if meanlongitude < pi then LL = LL + doublepi end
local dfo = pi / 216.45
if latitude < 0 then dfo = -dfo end
local fo = math.min(math.tan(declination + dfo) * math.tan(latitude * rads), 1) local ha = 12 * math.asin(fo) / pi + 6
local timezone = TZ(when) local equation = 12 + timezone + 24 * (1 - LL / doublepi) - longitude / 15
local sunrise, sunset = equation - ha, equation + ha
if sunrise > 24 then sunrise = sunrise - 24 end
if sunset > 24 then sunset = sunset - 24 end
return math.floor(sunrise * 60), math.ceil(sunset * 60) end
--本日の日中かどうか審議する function is_daytime(latitude, longitude, sunrise_margin, sunset_margin) local date = os.date("*t") local sunrise, sunset = rscalc(latitude, longitude, date) -- obs.script_log(obs.LOG_INFO, "Sunrize(min): " .. sunrise) -- obs.script_log(obs.LOG_INFO, "Sunset(min): " .. sunset)
sunrise = sunrise + sunrise_margin sunset = sunset + sunset_margin local zero_oclock = os.time({year = date.year, month = date.month, day = date.day, hour = 0})
-- 与えられた日の日の出と日の入りの時間(秒)を計算 local sunrise_sec = zero_oclock + sunrise * 60 local sunset_sec = zero_oclock + sunset * 60 local now_sec = os.time(date)
--[[ Debug Section START local t = os.date("*t", sunrise_sec) local time_str = string.format("%04d-%02d-%02d %02d:%02d:%02d", t.year, t.month, t.day, t.hour, t.min, t.sec) obs.script_log(obs.LOG_INFO, "Current time: " .. time_str) local t = os.date("*t", sunset_sec) local time_str = string.format("%04d-%02d-%02d %02d:%02d:%02d", t.year, t.month, t.day, t.hour, t.min, t.sec) obs.script_log(obs.LOG_INFO, "Current time: " .. time_str) ]]
if sunrise_sec <= now_sec and now_sec <= sunset_sec then return true else return false end end
--シーンの切り替え function switch_scene(scene_name) local scenes = obs.obs_frontend_get_scenes()
for _, scene in ipairs(scenes) do local scenes_list = obs.obs_source_get_name(scene) if scenes_list == scene_name then if scenes_now ~= scene_name then scenes_now = scene_name obs.obs_frontend_set_current_scene(scene)
local t = os.date("*t") local chg_str = string.format("%02d-%02d %02d:%02d -- %s", t.month, t.day, t.hour, t.min, scene_name) obs.script_log(obs.LOG_INFO, "Switched scene: " .. chg_str)
end break end end end
--日中ならWEB251、夜ならWEB252 function check_time() if is_daytime(latitude, longitude, sunrise_margin, sunset_margin) then switch_scene(scene_WEB251) else switch_scene(scene_WEB252) end end
-- function script_description() return "Script to switch scenes at specified times." end
-- function script_load(settings) obs.timer_add(check_time, 60 * 1000) end
|