// ChannelPutRequester @Override public void channelPutConnect( final Status status, final ChannelPut channelPut, final Structure structure) { if (!status.isSuccess()) { error = new Exception("Failed to connect 'put' for " + pv.getName() + ": " + status); updates.countDown(); return; } try { final PVStructure write_structure = PVDataFactory.getPVDataCreate().createPVStructure(structure); final BitSet bit_set = new BitSet(write_structure.getNumberFields()); // Locate the value field at deepest level in structure PVField field = null; PVStructure search = write_structure; while (search != null) { final PVField[] fields = search.getPVFields(); if (fields.length != 1) throw new Exception( "Can only write to simple struct.element.value path, got " + structure); if (fields[0].getFieldName().equals("value")) { field = fields[0]; break; } else if (fields[0] instanceof PVStructure) search = (PVStructure) fields[0]; else search = null; } if (field == null) throw new Exception("Cannot locate 'value' to write in " + structure); // Enumerated? Write to value.index if (field instanceof PVStructure && "enum_t".equals(field.getField().getID())) field = ((PVStructure) field).getSubField("index"); // Indicate what's changed & change it bit_set.set(field.getFieldOffset()); PVStructureHelper.setField(field, new_value); // Perform write channelPut.put(write_structure, bit_set); } catch (Exception ex) { logger.log(Level.WARNING, "Failed to write " + pv.getName() + " = " + new_value, ex); error = new Exception("Failed to write " + pv.getName() + " = " + new_value, ex); updates.countDown(); } }
/* (non-Javadoc) * @see org.epics.pvaccess.client.ChannelPutRequester#channelPutConnect(org.epics.pvdata.pv.Status, org.epics.pvaccess.client.ChannelPut, org.epics.pvdata.pv.PVStructure, org.epics.pvdata.misc.BitSet) */ @Override public void channelPutConnect( Status status, ChannelPut channelPut, PVStructure pvStructure, BitSet bitSet) { reportStatus("Failed to create ChannelPut instance", status); if (status.isSuccess()) { this.channelPut = channelPut; if (isChannelEnumType) { // handle inconsistent behavior this.channelPutValueField = pvStructure.getSubField("value"); if (this.channelPutValueField instanceof PVStructure) this.channelPutValueField = ((PVStructure) pvStructure).getSubField("index"); } else { this.channelPutValueField = pvStructure.getSubField("value"); } // set BitSet bitSet.clear(); // re-connect case if (this.channelPutValueField != null) bitSet.set(channelPutValueField.getFieldOffset()); } doNextWrite(); }
// TODO check if non-V types can ever be given as newValue private final void fromObject(PVField field, Object newValue) { // enum support if (isChannelEnumType) { // value.index int field expected PVInt indexPutField = (PVInt) channelPutValueField; int index = -1; if (newValue instanceof Number) { index = ((Number) newValue).intValue(); } else if (newValue instanceof String) { String nv = (String) newValue; PVStructure lastValue = getLastMessagePayload(); if (lastValue == null) throw new IllegalArgumentException( "no monitor on '" + getChannelName() + "' created to get list of valid enum choices"); PVStringArray pvChoices = (PVStringArray) lastValue.getSubField("value.choices"); StringArrayData data = new StringArrayData(); pvChoices.get(0, pvChoices.getLength(), data); final String[] choices = data.data; for (int i = 0; i < choices.length; i++) { if (nv.equals(choices[i])) { index = i; break; } } if (index == -1) throw new IllegalArgumentException("enumeration '" + nv + "' is not a valid choice"); } indexPutField.put(index); return; } if (channelPutValueField instanceof PVScalar) { if (newValue instanceof Double) convert.fromDouble((PVScalar) field, ((Double) newValue).doubleValue()); else if (newValue instanceof Integer) convert.fromInt((PVScalar) field, ((Integer) newValue).intValue()); else if (newValue instanceof String) convert.fromString((PVScalar) field, (String) newValue); else if (newValue instanceof Byte) convert.fromByte((PVScalar) field, ((Byte) newValue).byteValue()); else if (newValue instanceof Short) convert.fromShort((PVScalar) field, ((Short) newValue).shortValue()); else if (newValue instanceof Long) convert.fromLong((PVScalar) field, ((Long) newValue).longValue()); else if (newValue instanceof Float) convert.fromFloat((PVScalar) field, ((Float) newValue).floatValue()); else throw new RuntimeException( "Unsupported write, cannot put '" + newValue.getClass() + "' into scalar '" + channelPutValueField.getField() + "'"); } else if (channelPutValueField instanceof PVScalarArray) { // if it's a ListNumber, extract the array if (newValue instanceof ListNumber) { ListNumber data = (ListNumber) newValue; Object wrappedArray = CollectionNumbers.wrappedArray(data); if (wrappedArray == null) { newValue = CollectionNumbers.doubleArrayCopyOf(data); } else { newValue = wrappedArray; } } else if (!newValue.getClass().isArray()) { // create an array Object newValueArray = Array.newInstance(newValue.getClass(), 1); Array.set(newValueArray, 0, newValue); newValue = newValueArray; } if (newValue instanceof double[]) convert.fromDoubleArray( (PVScalarArray) field, 0, ((double[]) newValue).length, (double[]) newValue, 0); else if (newValue instanceof int[]) convert.fromIntArray( (PVScalarArray) field, 0, ((int[]) newValue).length, (int[]) newValue, 0); else if (newValue instanceof String[]) convert.fromStringArray( (PVScalarArray) field, 0, ((String[]) newValue).length, (String[]) newValue, 0); // special case from string to array else if (newValue instanceof String) { String str = ((String) newValue).trim(); // remove [] if (str.charAt(0) == '[' && str.charAt(str.length() - 1) == ']') str = str.substring(1, str.length() - 1); // split on commas and whitespaces String[] splitValues = str.split("[,\\s]+"); convert.fromStringArray((PVScalarArray) field, 0, splitValues.length, splitValues, 0); } else if (newValue instanceof byte[]) convert.fromByteArray( (PVScalarArray) field, 0, ((byte[]) newValue).length, (byte[]) newValue, 0); else if (newValue instanceof short[]) convert.fromShortArray( (PVScalarArray) field, 0, ((short[]) newValue).length, (short[]) newValue, 0); else if (newValue instanceof long[]) convert.fromLongArray( (PVScalarArray) field, 0, ((long[]) newValue).length, (long[]) newValue, 0); else if (newValue instanceof float[]) convert.fromFloatArray( (PVScalarArray) field, 0, ((float[]) newValue).length, (float[]) newValue, 0); else throw new RuntimeException( "Unsupported write, cannot put '" + newValue.getClass() + "' into array'" + channelPutValueField.getField() + "'"); } else throw new RuntimeException( "Unsupported write, cannot put '" + newValue.getClass() + "' into '" + channelPutValueField.getField() + "'"); }