# имя: 'State Event 1' # описание: трансляция состояний в события # тип триггера: 'EgsObjectUpdated' # создан: 2019.05.20 11.38.49, Сельченков Н.Ю. # изменен: '2019.07.19 13.43.42', Сельченков Н.Ю. # подробности: https://redmine.integra-s.com:11000/projects/eilyacuario/wiki/State_Event_1 use Newtonsoft.Json.Linq.JToken as JToken use Newtonsoft.Json.Linq.JObject as JObject use Newtonsoft.Json.Linq.JArray as JArray use Newtonsoft.Json.JsonConvert as JsonConvert from Newtonsoft.Json use acuario2.types.BaseObject as BaseObject from acuario2.types use acuario2.client.StateEvent as StateEvent from acuario2.types use acuario2.client.Protocol as Protocol from acuario2.client use WampSharp.V2.IWampChannel as IWampChannel from WampSharp use System.Func(Guid, IWampChannel) as GetWampChannelProc use typedef ` ` as Settings const settings = Settings(trigger.settings) const table = JsonConvert.DeserializeObject(settings.table, JObject) as JObject const like_item(item as Item, key as string) = if '-' in key then string(item.Id) is key else key in item.Types end const get_state_masks(value as JToken) = if value is JArray arr then from arr select string(it) to array else new [ string(value) ] end const like_state(state as string, value as JToken) = from get_state_masks(value) any state like it const get_type(typename as string) = Type.GetType("acuario2.types."..typename..",acuario2.types") const check_state(type as Type, value as JToken) = let state_typename = type.GetProperty("state")?.PropertyType?.Name if state_typename is null then error "parameter state not found in "..type let state_type = get_type(state_typename) if state_type is null then error "type "..state_typename.." not found" let state_names = Enum.GetNames(state_type) let state_masks = get_state_masks(value) let valid_mask(mask as string) = from state_names any it like mask let invalid_mask = from state_masks where it isnt valid_mask try first if (invalid_mask isnt null) then error "invalid state mask "..invalid_mask end const check_entry(key as string, value as JToken) = if '-' in key then let item = graph[Guid(key)] if item is null then error "item "..key.." not found" check_state(item.GetType(), value) else let type = get_type(key) if type is null then error "type "..key.." not found" check_state(type, value) end end let emit_event(item as Item) = let event = StateEvent() event.owner = string(item.Id) event.server = string(item.ServerId) event.position = if item is BaseObject obj then obj.position let state = item["state"] event.state = state.Text?.Replace?('_', '.') event.datetime = state.DateTime event.version = item.Version event.itemtype = item.GetType().Name event.name = if item is BaseObject obj then obj.name eval print typeof item.ServerId let rpcCatalog = trigger.Module.GetWampChannel(item.ServerId ?? Guid.Empty).RealmProxy.RpcCatalog let request = event.ExportCreate(Guid(event.server)) Protocol.Put(rpcCatalog, request) end ###################################### eval from table.Properties() do check_entry(Name, Value) now if "state" in changes then if @object is Item item then if from table.Properties() any (item is like_item Name) and (item["state"].Text is like_state Value) then emit_event(item)