[{"server":null,"owner":null,"id":"77bb1bed-e2c1-4bad-9f50-d7ae59dee096","params":{"result":{"value":null,"datetime":1555659041,"status":null,"version":0},"hash":{"value":"wh5m7vM378dfGZcwEEKLRg==","datetime":1558082910,"status":null,"version":0},"enabled":{"value":true,"datetime":1555659309,"status":null,"version":0},"error_text":{"value":null,"datetime":1557828642,"status":null,"version":0},"type":{"value":"EgsObjectUpdated","datetime":1558082246,"status":null,"version":0},"settings":{"value":"{}","datetime":1557299536,"status":null,"version":0},"state":{"value":"ok.normal","datetime":1557828642,"status":null,"version":0},"script":{"value":"# имя: 'Smart zone 1'\r\n# описание: рассчитывание состояния зоны\r\n# тип триггера: 'EgsObjectUpdated'\r\n# создан: 2019.05.07 14.28.41, Сельченков Н.Ю.\r\n# изменен: '2019.05.17 12.48.29', Сельченков Н.Ю.\r\n# подробности: https://redmine.integra-s.com:11000/projects/eilyacuario/wiki/Smart_zone_1\r\n\r\nuse acuario2.types.Zone         from acuario2.types\r\nuse acuario2.types.ZoneLink     from acuario2.types\r\nuse acuario2.types.ZoneInputPin from acuario2.types\r\n\r\nuse typedef\r\n`\r\n    <root>\r\n    </root>\r\n` as Settings\r\n\r\nconst settings = Settings(trigger.settings)\r\n\r\nconst process_state(zone as Zone) = \r\n    let items     = from zone.zoneOutputPin.GetOppositeItems() select it to array\r\n    let states    = from items group by State bind Key to (from it count all)\r\n    let threshold = if items.Length > 1 then 1 else 0\r\n\r\n    let has_alarm = (\"alarm\" in states) and (states[\"alarm\"] > threshold) \r\n    let has_error = (\"error\" in states) or (\"none\" in states) or (\"DEFAULT\" in states)\r\n    let all_ok    = (\"ok\" in states) and ((zone.State isnt \"alarm\") or (states[\"ok\"] is items.Length)) \r\n\r\n    if items is empty then zone.state = \"none_unknown\"    \r\n    else if has_alarm then zone.state = \"alarm_alarm\"\r\n    else if has_error then zone.state = \"error_defective\"\r\n    else if all_ok    then zone.state = \"ok_normal\"\r\nend\r\n\r\nlet process_command(zone as Zone, item as Item) = \r\n    let source = zone[\"command\"].Old ?? zone[\"command\"]\r\n    let target = item[\"command\"]\r\n    if source.Text in target.EnumNames then target.Set(source.Text, source.Status, source.DateTime) \r\n    else target.Set(\"DEFAULT\", Status.invalid, source.DateTime)\r\nend\r\n\r\neval (from graph.Values of type Zone do process_state(it) now)\r\n\r\nif \"state\" in changes then\r\n    if @object is Item item then from item.GetItemsLinkedTo(ZoneInputPin) of type Zone do process_state(it) now\r\n    if @object is Zone zone then process_state(zone)\r\nend\r\n\r\nif \"command\" in changes then\r\n    if @object is Zone zone then from zone.zoneOutputPin.GetOppositeItems() do process_command(zone, it) now\r\nend\r\n\r\n","datetime":1558082909,"status":null,"version":0},"name":{"value":"Smart zone 1","datetime":1557224439,"status":null,"version":0}},"entity":"item","operation":"create"}]