1
2
3
4
5 package org.epics.pvmanager.jca;
6
7 import org.epics.pvmanager.DataSourceTypeAdapter;
8 import gov.aps.jca.Channel;
9 import gov.aps.jca.dbr.DBR;
10 import gov.aps.jca.dbr.DBRType;
11 import org.epics.pvmanager.ValueCache;
12
13
14
15
16
17
18
19 public abstract class JCATypeAdapter implements DataSourceTypeAdapter<JCAConnectionPayload, JCAMessagePayload> {
20
21 private final Class<?> typeClass;
22 private final DBRType epicsValueType;
23 private final DBRType epicsMetaType;
24 private final Boolean array;
25
26
27
28
29
30
31
32
33
34 public JCATypeAdapter(Class<?> typeClass, DBRType epicsValueType, DBRType epicsMetaType, Boolean array) {
35 this.typeClass = typeClass;
36 this.epicsValueType = epicsValueType;
37 this.epicsMetaType = epicsMetaType;
38 this.array = array;
39 }
40
41 @Override
42 public int match(ValueCache<?> cache, JCAConnectionPayload connPayload) {
43 Channel channel = connPayload.getChannel();
44
45
46 if (!cache.getType().isAssignableFrom(typeClass))
47 return 0;
48
49
50 if (!dbrTypeMatch(epicsValueType, connPayload.getFieldType()))
51 return 0;
52
53
54 if (array != null &&array && channel.getElementCount() == 1)
55 return 0;
56
57
58 if (array != null && !array && channel.getElementCount() != 1)
59 return 0;
60
61
62 return 1;
63 }
64
65 private static boolean dbrTypeMatch(DBRType aType, DBRType anotherType) {
66 if (aType.getClass() == null && anotherType.getClass() != null) {
67 return false;
68 }
69 if (aType.getClass() != null && anotherType.getClass() == null) {
70 return false;
71 }
72 return aType.isBYTE() && anotherType.isBYTE() ||
73 aType.isDOUBLE() && anotherType.isDOUBLE() ||
74 aType.isENUM() && anotherType.isENUM() ||
75 aType.isFLOAT() && anotherType.isFLOAT() ||
76 aType.isINT() && anotherType.isINT() ||
77 aType.isSHORT() && anotherType.isSHORT() ||
78 aType.isSTRING() && anotherType.isSTRING();
79 }
80
81 @Override
82 public Object getSubscriptionParameter(ValueCache cache, JCAConnectionPayload connPayload) {
83 throw new UnsupportedOperationException("Not implemented: JCAChannelHandler is multiplexed, will not use this method");
84 }
85
86 @Override
87 @SuppressWarnings("unchecked")
88 public boolean updateCache(ValueCache cache, JCAConnectionPayload connPayload, JCAMessagePayload message) {
89 Channel channel = connPayload.getChannel();
90
91
92 if (epicsMetaType != null && message.getMetadata() == null)
93 return false;
94
95
96 if (message.getEvent() == null)
97 return false;
98
99 Object value = createValue(message.getEvent().getDBR(), message.getMetadata(), connPayload);
100 cache.writeValue(value);
101 return true;
102 }
103
104
105
106
107
108
109
110
111
112 public abstract Object createValue(DBR value, DBR metadata, JCAConnectionPayload connPayload);
113
114 }