# имя: 'Point observer 2' # описание: поворот связанной поворотной камеры в предустановленную точку по команде # тип триггера: 'EgsObjectUpdated' # создан: 2018.04.11 10.35.35, Сельченков Н.Ю. # изменен: '2019.06.28 12.48.05', Сельченков Н.Ю. # подробности: https://redmine.integra-s.com:11000/projects/eilyacuario/wiki//Point_observer_2 use System.Uri use System.Convert use System.Math use System.Text.RegularExpressions.Regex use System.Collections.Generic.Dictionary(string, object) as Dictionary use System.Collections.ArrayList use Newtonsoft.Json.JsonConvert from Newtonsoft.Json use Newtonsoft.Json.Linq.JArray use Newtonsoft.Json.Linq.JObject use acuario2.types.BaseObject from acuario2.types use acuario2.types.SpatialObject from acuario2.types use acuario2.types.ObservablePoint from acuario2.types use acuario2.types.VideoCamera from acuario2.types use acuario2.types.PTZDevice from acuario2.types use acuario2.types.Overseer_output from acuario2.types use acuario2.types.IntegraVideo7 from acuario2.types use utils.GeoPoint from GeoUtils use utils.GeoUtils from GeoUtils use utils.Matrix3D from GeoUtils use integravideo.client.Iv7Server from IntegraVideo7Client use new { code = 0, user_code = 0, result = null as ArrayList } as CallMethod2Result use new { pan = 0.0, tilt = 0.0, zoom = 0.0 } as PTZ use typedef ` http://localhost:1986/axis2/services/Iv7Server 1500 1 ` as Settings const settings = Settings(trigger.settings) const uri = Uri(settings.url) ###################################################### let iv7 = Iv7Server() iv7.Url = string(uri) let sys_params = "" let сall_method2(name as string, args as object, log as bool) = let request = JsonConvert.SerializeObject(args) let response = iv7.CallMethod2(name, request, sys_params) let res = JsonConvert.DeserializeObject(response, CallMethod2Result) as CallMethod2Result let ok = (res.code is 0) and (res.user_code is 0) if (not ok) and log then print request print sys_params print response end if ok then res.result end let logon { login = "", password = "" } = sys_params = JsonConvert.SerializeObject(new { session_id = "egs", timeout = 5, relay = string[](0), login = login, password = password }) сall_method2("video-client:Logon", it, on) is not null end let get_ptz { key2 = "", QueryAll = true, login = "", password = "" } = let res = сall_method2("ptzclient:Command", it, on) if res isnt null then let ptz = PTZ() let obj = res[0] as JObject ptz.pan = (obj["pan"] as double) / 100 ptz.tilt = (obj["tilt"] as double) / 100 ptz.zoom = (obj["zoom"] as double) / 10000 ptz end end let _set_ptz { key2 = "", PanTo = -1, TiltTo = -1, ZoomTo = -1, GotoPreset = -1, login = "", password = "" } = сall_method2("ptzclient:Command", it, on) is not null let set_ptz(login as string, password as string, key2 as string, ptz as PTZ) = _set_ptz { key2 = key2, PanTo = ptz.pan * 100 as int, TiltTo = ptz.tilt * 100 as int, ZoomTo = ptz.zoom * 10000 as int, login = login, password = password } let goto_preset(login as string, password as string, key2 as string, preset as int) = _set_ptz { key2 = key2, GotoPreset = preset, login = login, password = password } let get_param_from_urls(obj as VideoCamera, pattern as string) = let regex = Regex(pattern) from obj.url select regex.Match(it) where Success select Groups[1].Value first end let get_key2(obj as VideoCamera) = get_param_from_urls(obj, "key2=(.+)[&|;]?") let get_udp_id(obj as VideoCamera) = get_param_from_urls(obj, "udp_id=([0-9]{1,})&") let get_login(obj as VideoCamera) = get_param_from_urls(obj, "iv7://(.+):.+@") let get_password(obj as VideoCamera) = get_param_from_urls(obj, "iv7://.+:(.+)@") ###################################################### let trace_target(ptzdev as PTZDevice, target as ObservablePoint) = let camera_pos = with ptzdev as SpatialObject do let p = GeoPoint(geo_position) p.Height = geo_height p end let target_pos = with target as BaseObject do let p = GeoPoint(position) p.Height = altitude p end let key2 = get_key2(ptzdev as VideoCamera) let udp_id = get_udp_id(ptzdev as VideoCamera) let videoServer = from ptzdev.GetLinkedItems(IntegraVideo7) try single let login = (videoServer?.login) ??? get_login(ptzdev as VideoCamera) let password = (videoServer?.password) ??? get_password(ptzdev as VideoCamera) logon { login = login, password = password } let v = ptzdev.calibrationMatrix if (v isnt null) and camera_pos.Valid and target_pos.Valid then let matrix = Matrix3D ( v[0], v[1], v[2], 0, v[3], v[4], v[5], 0, v[6], v[7], v[8], 0, 0, 0, 0, 1 ) let arr = GeoUtils.recalibrate(camera_pos, target_pos, matrix) let ptz = PTZ() ptz.tilt = Math.Round(arr[0], 2, "AwayFromZero") ptz.pan = Math.Round(arr[1], 2, "AwayFromZero") let range = arr[2] let max_zoom_distance = settings.max_zoom_distance ?? 1500 let max_zoom_limit_factor = settings.max_zoom_limit_factor ?? 1 ptz.zoom = if range > max_zoom_distance then max_zoom_limit_factor else 1 - (max_zoom_distance - range) / max_zoom_distance ptz.zoom = Math.Round(ptz.zoom, 2, "AwayFromZero") print("TRACE", ptzdev, target, target_pos, ptz) set_ptz(login, password, key2, ptz) else print("TRACE", ptzdev, target, target.preset) goto_preset(login, password, key2, target.preset) end end ###################################################### if (@object is ObservablePoint) and ("command" in changes) then let target = @object as ObservablePoint let command = target["command"].Old ?? target["command"] if command.Text is "observe" then from target.GetItemsLinkedTo(Overseer_output) of type PTZDevice do trace_target(it, target) now end end