|
| 1 | +package org.phoebus.pv.tga; |
| 2 | + |
| 3 | +import fr.esrf.Tango.DevFailed; |
| 4 | +import fr.esrf.Tango.EventProperties; |
| 5 | +import fr.esrf.TangoApi.AttributeInfoEx; |
| 6 | +import fr.esrf.TangoApi.AttributeProxy; |
| 7 | +import fr.esrf.TangoApi.DeviceAttribute; |
| 8 | +import org.epics.vtype.*; |
| 9 | +import org.phoebus.pv.PV; |
| 10 | +import org.tango.attribute.AttributeTangoType; |
| 11 | +import org.tango.server.events.EventType; |
| 12 | + |
| 13 | +import java.util.concurrent.ConcurrentHashMap; |
| 14 | +import java.util.logging.Level; |
| 15 | + |
| 16 | +; |
| 17 | + |
| 18 | +public class TangoAttrContext { |
| 19 | + private static TangoAttrContext instance; |
| 20 | + private final ConcurrentHashMap<String, AttributeProxy> attributeProxys; |
| 21 | + private final ConcurrentHashMap<String, Integer> events; |
| 22 | + private final ConcurrentHashMap<String, AttributeTangoType> types; |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | + private TangoAttrContext() { |
| 27 | + events = new ConcurrentHashMap<>(); |
| 28 | + attributeProxys = new ConcurrentHashMap<>(); |
| 29 | + types = new ConcurrentHashMap<>(); |
| 30 | + } |
| 31 | + |
| 32 | + public static synchronized TangoAttrContext getInstance() throws Exception { |
| 33 | + if (instance == null) |
| 34 | + instance = new TangoAttrContext(); |
| 35 | + return instance; |
| 36 | + } |
| 37 | + |
| 38 | + public void subscribeAttributeEvent(String deviceName, String attributeName, String baseName, TangoAttr_PV pv) throws DevFailed { |
| 39 | + String name = deviceName +"/" + attributeName; |
| 40 | + AttributeProxy attributeProxy; |
| 41 | + if (attributeProxys.get(baseName) == null) { |
| 42 | + attributeProxy = new AttributeProxy(name); |
| 43 | + subscribeAttributeEvent(baseName, attributeProxy, pv); |
| 44 | + attributeProxys.put(baseName,attributeProxy); |
| 45 | + }else { |
| 46 | + //nothing to do |
| 47 | + } |
| 48 | + |
| 49 | + } |
| 50 | + |
| 51 | + private void subscribeAttributeEvent(String baseName, AttributeProxy attributeProxy, TangoAttr_PV pv) throws DevFailed { |
| 52 | + AttributeInfoEx attribute_info_ex; |
| 53 | + try { |
| 54 | + attribute_info_ex = attributeProxy.get_info_ex(); |
| 55 | + } catch (DevFailed e) { |
| 56 | + throw new RuntimeException(e); |
| 57 | + } |
| 58 | + |
| 59 | + //obtain the type of attribute's value. |
| 60 | + AttributeTangoType type = AttributeTangoType.getTypeFromTango(attribute_info_ex.data_type); |
| 61 | + |
| 62 | + types.putIfAbsent(baseName, type); |
| 63 | + |
| 64 | + |
| 65 | + //obtain the tango event type. |
| 66 | + EventType eventType = EventType.CHANGE_EVENT; |
| 67 | + EventProperties tangoObj = attribute_info_ex.events.getTangoObj(); |
| 68 | + if (tangoObj.ch_event.abs_change.equals("Not specified") && tangoObj.ch_event.rel_change.equals("Not specified")) |
| 69 | + eventType = EventType.PERIODIC_EVENT; |
| 70 | + |
| 71 | + //subscribe the tango event. |
| 72 | + int event_id; |
| 73 | + try { |
| 74 | + event_id = attributeProxy.subscribe_event(eventType.getValue(), pv.new TangoCallBack(type), new String[]{}); |
| 75 | + } catch (DevFailed e) { |
| 76 | + throw new RuntimeException(e); |
| 77 | + } |
| 78 | + events.put(baseName, event_id); |
| 79 | + |
| 80 | + } |
| 81 | + |
| 82 | + public void unSubscribeAttributeEvent(String baseName) throws Exception { |
| 83 | + if (!attributeProxys.containsKey(baseName)){ |
| 84 | + PV.logger.log(Level.WARNING, "Could not unsubscribe Tango attribute \"" + baseName |
| 85 | + + "\" due to no Attribute Proxy."); |
| 86 | + throw new Exception("Tango attribute unsubscribe failed: no Attribute proxy connection."); |
| 87 | + } |
| 88 | + |
| 89 | + AttributeProxy attributeProxy = attributeProxys.get(baseName); |
| 90 | + Integer event_id = events.get(baseName); |
| 91 | + if (event_id == null){ |
| 92 | + PV.logger.log(Level.WARNING, "Could not unsubscribe Tango attribute \"" + baseName |
| 93 | + + "\" due to no internal record of attribute"); |
| 94 | + throw new Exception("Tango attribute unsubscribe failed: no attribute record."); |
| 95 | + } |
| 96 | + attributeProxy.getDeviceProxy().unsubscribe_event(event_id); |
| 97 | + attributeProxys.remove(baseName); |
| 98 | + events.remove(baseName); |
| 99 | + types.remove(baseName); |
| 100 | + } |
| 101 | + |
| 102 | + |
| 103 | + public void writeAttribute(String baseName, String attributeName, Object new_value) throws Exception { |
| 104 | + AttributeProxy attributeProxy = attributeProxys.get(baseName); |
| 105 | + AttributeTangoType type = types.get(baseName); |
| 106 | + if (type == null){ |
| 107 | + PV.logger.log(Level.WARNING, "Could not find type of attribute :" + baseName); |
| 108 | + throw new Exception("Tango attribute write failed: attribute type not found."); |
| 109 | + } |
| 110 | + VType vType; |
| 111 | + String value; |
| 112 | + switch (type){ |
| 113 | + case DEVBOOLEAN: |
| 114 | + vType = TangoTypeUtil.convert(new_value, VBoolean.class); |
| 115 | + value = TangoTypeUtil.ToString(vType); |
| 116 | + attributeProxy.write(new DeviceAttribute(attributeName, Boolean.parseBoolean(value))); |
| 117 | + break; |
| 118 | + case DEVLONG64: |
| 119 | + case DEVULONG64: |
| 120 | + vType = TangoTypeUtil.convert(new_value, VLong.class); |
| 121 | + value = TangoTypeUtil.ToString(vType); |
| 122 | + attributeProxy.write(new DeviceAttribute(attributeName, Long.parseLong(value))); |
| 123 | + break; |
| 124 | + case DEVSHORT: |
| 125 | + case DEVUSHORT: |
| 126 | + vType = TangoTypeUtil.convert(new_value, VShort.class); |
| 127 | + value = TangoTypeUtil.ToString(vType); |
| 128 | + attributeProxy.write(new DeviceAttribute(attributeName, Short.parseShort(value))); |
| 129 | + break; |
| 130 | + case DEVLONG: |
| 131 | + case DEVULONG: |
| 132 | + vType = TangoTypeUtil.convert(new_value, VInt.class); |
| 133 | + value = TangoTypeUtil.ToString(vType); |
| 134 | + attributeProxy.write(new DeviceAttribute(attributeName, Integer.parseInt(value))); |
| 135 | + break; |
| 136 | + case DEVFLOAT: |
| 137 | + vType = TangoTypeUtil.convert(new_value, VFloat.class); |
| 138 | + value = TangoTypeUtil.ToString(vType); |
| 139 | + attributeProxy.write(new DeviceAttribute(attributeName, Float.parseFloat(value))); |
| 140 | + break; |
| 141 | + case DEVDOUBLE: |
| 142 | + vType = TangoTypeUtil.convert(new_value, VDouble.class); |
| 143 | + value = TangoTypeUtil.ToString(vType); |
| 144 | + attributeProxy.write(new DeviceAttribute(attributeName, Double.parseDouble(value))); |
| 145 | + break; |
| 146 | + case DEVSTRING: |
| 147 | + vType = TangoTypeUtil.convert(new_value, VString.class); |
| 148 | + value = TangoTypeUtil.ToString(vType); |
| 149 | + attributeProxy.write(new DeviceAttribute(attributeName, value)); |
| 150 | + break; |
| 151 | + case DEVUCHAR: |
| 152 | + vType = TangoTypeUtil.convert(new_value, VByte.class); |
| 153 | + value = TangoTypeUtil.ToString(vType); |
| 154 | + attributeProxy.write(new DeviceAttribute(attributeName, Byte.parseByte(value))); |
| 155 | + break; |
| 156 | + default: |
| 157 | + throw new IllegalArgumentException("Value " + new_value + " cannot be converted."); |
| 158 | + } |
| 159 | + |
| 160 | + } |
| 161 | + |
| 162 | +} |
0 commit comments