def testCtrlStruct_ValueMonitor(self): '''Monitor PV (value events) through GW - change value and properties directly - check CTRL structure consistency''' diffs = [] # gwcachetest is an ai record with full set of alarm limits: -100 -10 10 100 gw = ca.create_channel("gateway:gwcachetest") connected = ca.connect_channel(gw, timeout=.5) self.assertTrue(connected, "Could not connect to gateway channel " + ca.name(gw)) (gw_cbref, gw_uaref, gw_eventid) = ca.create_subscription(gw, mask=dbr.DBE_VALUE, use_ctrl=True, callback=self.onChangeGW) ioc = ca.create_channel("ioc:gwcachetest") connected = ca.connect_channel(ioc, timeout=.5) self.assertTrue(connected, "Could not connect to ioc channel " + ca.name(ioc)) (ioc_cbref, ioc_uaref, ioc_eventid) = ca.create_subscription(ioc, mask=dbr.DBE_VALUE, use_ctrl=True, callback=self.onChangeIOC) # set value on IOC ioc_value = ca.create_channel("ioc:gwcachetest") ca.put(ioc_value, 10.0, wait=True) gwtests.wait_until( lambda: self.eventsReceivedIOC == self.eventsReceivedGW, 5.0) self.assertTrue( self.eventsReceivedIOC == self.eventsReceivedGW, "After setting value, no. of received updates differ: GW {0}, IOC {1}" .format(str(self.eventsReceivedGW), str(self.eventsReceivedIOC))) (are_diff, diffs) = self.compareStructures() self.assertTrue( are_diff == False, "At update {0} (change value), received structure updates differ:\n\t{1}" .format(str(self.eventsReceivedIOC), "\n\t".join(diffs))) # set property on IOC ioc_hihi = ca.create_channel("ioc:gwcachetest.HIHI") ca.put(ioc_hihi, 123.0, wait=True) ca.put(ioc_value, 11.0, wait=True) # trigger update gwtests.wait_until( lambda: self.eventsReceivedIOC == self.eventsReceivedGW, 5.0) self.assertTrue( self.eventsReceivedIOC == self.eventsReceivedGW, "After setting property, no. of received updates differ: GW {0}, IOC {1}" .format(str(self.eventsReceivedGW), str(self.eventsReceivedIOC))) (are_diff, diffs) = self.compareStructures() self.assertTrue( are_diff == False, "At update {0} (change property), received structure updates differ:\n\t{1}" .format(str(self.eventsReceivedIOC), "\n\t".join(diffs)))
def connectGwStats(self): self.gw_vctotal = ca.create_channel("gwtest:vctotal") ca.connect_channel(self.gw_vctotal, timeout=.5) self.gw_pvtotal = ca.create_channel("gwtest:pvtotal") ca.connect_channel(self.gw_pvtotal, timeout=.5) self.gw_connected = ca.create_channel("gwtest:connected") ca.connect_channel(self.gw_connected, timeout=.5) self.gw_active = ca.create_channel("gwtest:active") ca.connect_channel(self.gw_active, timeout=.5) self.gw_inactive = ca.create_channel("gwtest:inactive") ca.connect_channel(self.gw_inactive, timeout=.5)
def testEnumPropCache_ValueMonitorCTRLget(self): '''Monitor PV (value events) through GW - change ENUM string directly - get the DBR_CTRL of the PV through GW''' # gateway should show no VC (client side connection) and no PV (IOC side connection) self.updateGwStats() self.assertTrue(self.vctotal == 0, "Expected GW VC total count: 0, actual: " + str(self.vctotal)) self.assertTrue(self.pvtotal == 0, "Expected GW PV total count: 0, actual: " + str(self.pvtotal)) self.assertTrue(self.connected == 0, "Expected GW connected PV count: 0, actual: " + str(self.connected)) self.assertTrue(self.active == 0, "Expected GW active PV count: 0, actual: " + str(self.active)) self.assertTrue(self.inactive == 0, "Expected GW inactive PV count: 0, actual: " + str(self.inactive)) # enumtest is an mbbi record with three strings defined: zero one two gw = ca.create_channel("gateway:enumtest") connected = ca.connect_channel(gw, timeout=.5) self.assertTrue(connected, "Could not connect to gateway channel " + ca.name(gw)) (gw_cbref, gw_uaref, gw_eventid) = ca.create_subscription(gw, mask=dbr.DBE_VALUE, callback=self.onChange) ioc = ca.create_channel("ioc:enumtest") connected = ca.connect_channel(ioc, timeout=.5) self.assertTrue(connected, "Could not connect to ioc channel " + ca.name(gw)) (ioc_cbref, ioc_uaref, ioc_eventid) = ca.create_subscription(ioc, mask=dbr.DBE_VALUE, callback=self.onChange) # gateway should show one VC and one connected active PV self.updateGwStats() self.assertTrue(self.vctotal == 1, "Expected GW VC total count: 1, actual: " + str(self.vctotal)) self.assertTrue(self.pvtotal == 1, "Expected GW PV total count: 1, actual: " + str(self.pvtotal)) self.assertTrue(self.connected == 1, "Expected GW connected PV count: 1, actual: " + str(self.connected)) self.assertTrue(self.active == 1, "Expected GW active PV count: 1, actual: " + str(self.active)) self.assertTrue(self.inactive == 0, "Expected GW inactive PV count: 0, actual: " + str(self.inactive)) # enum string should not have been updated ioc_ctrl = ca.get_ctrlvars(ioc) oneStr = ioc_ctrl['enum_strs'][1] self.assertTrue(oneStr == 'one', "Expected IOC enum[1]: one; actual enum[1]: "+ oneStr) gw_ctrl = ca.get_ctrlvars(gw) oneStr = gw_ctrl['enum_strs'][1] self.assertTrue(oneStr == 'one', "Expected GW enum[1]: one; actual enum[1]: "+ oneStr) # set enum string on IOC ioc_enum1 = ca.create_channel("ioc:enumtest.ONST") ca.put(ioc_enum1, 'uno', wait=True) time.sleep(.1) # Now the enum string should have been updated (if IOC supports DBE_PROPERTY) ioc_ctrl = ca.get_ctrlvars(ioc) oneStr = ioc_ctrl['enum_strs'][1] self.assertTrue(oneStr == 'uno', "Expected IOC enum[1]: uno; actual enum[1]: "+ oneStr) if self.propSupported: gw_expected = 'uno' else: gw_expected = 'one' gw_ctrl = ca.get_ctrlvars(gw) oneStr = gw_ctrl['enum_strs'][1] self.assertTrue(oneStr == gw_expected, "Expected GW enum[1]: {0}; actual enum[1]: {1}".format(gw_expected, oneStr))
def test_DoubleVal(self): pvn = pvnames.double_pv chid = ca.create_channel(pvn,connect=True) cdict = ca.get_ctrlvars(chid) write( 'CA testing CTRL Values for a Double (%s)' % (pvn)) self.failUnless('units' in cdict) self.failUnless('precision' in cdict) self.failUnless('severity' in cdict) hostname = ca.host_name(chid) self.failUnless(len(hostname) > 1) count = ca.element_count(chid) self.assertEqual(count,1) ftype= ca.field_type(chid) self.assertEqual(ftype,ca.dbr.DOUBLE) prec = ca.get_precision(chid) self.assertEqual(prec, pvnames.double_pv_prec) units= ca.bytes2str(ca.get_ctrlvars(chid)['units']) self.assertEqual(units, pvnames.double_pv_units) rwacc= ca.access(chid) self.failUnless(rwacc.startswith('read'))
def test_type_converions_1(self): write("CA type conversions scalars") pvlist = (pvnames.str_pv, pvnames.int_pv, pvnames.float_pv, pvnames.enum_pv, pvnames.long_pv, pvnames.double_pv2) chids = [] pause_updating() for name in pvlist: chid = ca.create_channel(name) ca.connect_channel(chid) chids.append((chid, name)) ca.poll(evt=0.025, iot=5.0) ca.poll(evt=0.05, iot=10.0) values = {} for chid, name in chids: values[name] = ca.get(chid, as_string=True) for promotion in ('ctrl', 'time'): for chid, pvname in chids: write('=== %s chid=%s as %s' % (ca.name(chid), repr(chid), promotion)) time.sleep(0.01) if promotion == 'ctrl': ntype = ca.promote_type(chid, use_ctrl=True) else: ntype = ca.promote_type(chid, use_time=True) val = ca.get(chid, ftype=ntype) cval = ca.get(chid, as_string=True) if ca.element_count(chid) > 1: val = val[:12] self.assertEqual(cval, values[pvname]) resume_updating()
def _test_array_callback(self, arrayname, array_type, length, element_type): """ Helper function to subscribe to a PV array and check it receives at least one subscription callback w/ specified type, length & uniform element type. Checks separately for normal, TIME & CTRL subscription variants. Returns the array or fails an assertion.""" results = {} for form in [ 'normal', 'time', 'ctrl' ]: chid = ca.create_channel(arrayname,connect=True) cb, uarg, eventID = ca.create_subscription(chid, use_time=form=='time', use_ctrl=form=='ctrl', callback=onChanges) CHANGE_DAT.pop(arrayname, None) timeout=0 # wait up to 6 seconds, if no callback probably due to simulator.py # not running... while timeout<120 and not arrayname in CHANGE_DAT: time.sleep(0.05) timeout = timeout+1 val = CHANGE_DAT.get(arrayname, None) ca.clear_subscription(eventID) self.assertIsNot(val, None) self.assertEqual(type(val), array_type) self.assertEqual(len(val), length) self.assertEqual(type(val[0]), element_type) self.assertTrue(all( type(e)==element_type for e in val)) results[form] = val return results
def test_DoubleVal(self): pvn = pvnames.double_pv chid = ca.create_channel(pvn,connect=True) cdict = ca.get_ctrlvars(chid) write( 'CA testing CTRL Values for a Double (%s)' % (pvn)) self.failUnless('units' in cdict) self.failUnless('precision' in cdict) self.failUnless('severity' in cdict) hostname = ca.host_name(chid) self.failUnless(len(hostname) > 1) count = ca.element_count(chid) self.assertEqual(count,1) ftype= ca.field_type(chid) self.assertEqual(ftype,ca.dbr.DOUBLE) prec = ca.get_precision(chid) self.assertEqual(prec, pvnames.double_pv_prec) units= ca.BYTES2STR(ca.get_ctrlvars(chid)['units']) self.assertEqual(units, pvnames.double_pv_units) rwacc= ca.access(chid) self.failUnless(rwacc.startswith('read'))
def test_type_converions_2(self): write("CA type conversions arrays") pvlist = (pvnames.char_arr_pv, pvnames.long_arr_pv, pvnames.double_arr_pv) with no_simulator_updates(): chids = [] for name in pvlist: chid = ca.create_channel(name) ca.connect_channel(chid) chids.append((chid, name)) ca.poll(evt=0.025, iot=5.0) ca.poll(evt=0.05, iot=10.0) values = {} for chid, name in chids: values[name] = ca.get(chid) for promotion in ('ctrl', 'time'): for chid, pvname in chids: write('=== %s chid=%s as %s' % (ca.name(chid), repr(chid), promotion)) time.sleep(0.01) if promotion == 'ctrl': ntype = ca.promote_type(chid, use_ctrl=True) else: ntype = ca.promote_type(chid, use_time=True) val = ca.get(chid, ftype=ntype) cval = ca.get(chid, as_string=True) for a, b in zip(val, values[pvname]): self.assertEqual(a, b)
def setup_callback(pvname): def my_cb(pvname=None, value=None, **kw): sys.stdout.write( 'get: %s value=%s, kw=%s\n' %( pvname, str(value), repr(kw))) sys.stdout.flush() chid = ca.create_channel(pvname) return ca.create_subscription(chid, callback=my_cb)
def generate_live_data(duration, period, pvnames): pvnamelist = tuple(pvnames) cols = list(pvnamelist) cols.insert(0, 'timestamp') pvconn = {} alldata = [] ca.initialize_libca() for name in pvnamelist: chid = ca.create_channel(name, connect=False, auto_cb=False) pvconn[name] = (chid, None) for key in list(pvconn): state = ca.connect_channel(pvconn.get(key)[0]) if not state: print('PV:', key, 'not connected') pvconn.pop(key) ca.poll() while duration > 0 and pvconn: pvdata = [] for name, data in pvconn.items(): ca.get(data[0], wait=False) ca.poll() pvdata.append(datetime.datetime.now()) for name, data in pvconn.items(): val = ca.get_complete(data[0]) pvdata.append(val) alldata.append(pvdata) time.sleep(period) duration = duration - period ca.finalize_libca(maxtime=1.0) df = pd.DataFrame(alldata, columns=cols) return df
def test_CreateChidWithConn(): write('Simple Test: create chid with conn callback') chid = ca.create_channel(pvnames.int_pv, callback=onConnect) val = ca.get(chid) global CONN_DAT conn = CONN_DAT.get(pvnames.int_pv, None) assert conn
def test_type_converions_2(self): write("CA type conversions arrays\n") pvlist = (pvnames.char_arr_pv, pvnames.long_arr_pv, pvnames.double_arr_pv) with no_simulator_updates(): chids = [] for name in pvlist: chid = ca.create_channel(name) ca.connect_channel(chid) chids.append((chid, name)) ca.poll(evt=0.025, iot=5.0) ca.poll(evt=0.05, iot=10.0) values = {} for chid, name in chids: values[name] = ca.get(chid) for promotion in ('ctrl', 'time'): for chid, pvname in chids: write('=== %s chid=%s as %s\n' % (ca.name(chid), repr(chid), promotion)) time.sleep(0.01) if promotion == 'ctrl': ntype = ca.promote_type(chid, use_ctrl=True) else: ntype = ca.promote_type(chid, use_time=True) val = ca.get(chid, ftype=ntype) cval = ca.get(chid, as_string=True) for a, b in zip(val, values[pvname]): self.assertEqual(a, b)
def test_type_converions_1(self): write("CA type conversions scalars") pvlist = (pvnames.str_pv, pvnames.int_pv, pvnames.float_pv, pvnames.enum_pv, pvnames.long_pv, pvnames.double_pv2) chids = [] with no_simulator_updates(): for name in pvlist: chid = ca.create_channel(name) ca.connect_channel(chid) chids.append((chid, name)) ca.poll(evt=0.025, iot=5.0) ca.poll(evt=0.05, iot=10.0) values = {} for chid, name in chids: values[name] = ca.get(chid, as_string=True) for promotion in ('ctrl', 'time'): for chid, pvname in chids: write('=== %s chid=%s as %s' % (ca.name(chid), repr(chid), promotion)) time.sleep(0.01) if promotion == 'ctrl': ntype = ca.promote_type(chid, use_ctrl=True) else: ntype = ca.promote_type(chid, use_time=True) val = ca.get(chid, ftype=ntype) cval = ca.get(chid, as_string=True) if ca.element_count(chid) > 1: val = val[:12] self.assertEqual(cval, values[pvname])
def test_promote_type(self): pvn = pvnames.double_pv chid = ca.create_channel(pvn,connect=True) write( 'CA promote type (%s)' % (pvn)) f_t = ca.promote_type(chid,use_time=True) f_c = ca.promote_type(chid,use_ctrl=True) self.assertEqual(f_t, ca.dbr.TIME_DOUBLE) self.assertEqual(f_c, ca.dbr.CTRL_DOUBLE)
def test_xArray1(self): write('Array Test: get(wait=False) / get_complete()') chid = ca.create_channel(pvnames.double_arrays[0]) val0 = ca.get(chid) aval = ca.get(chid, wait=False) self.assertTrue(aval is None) val1 = ca.get_complete(chid) self.assertTrue(all(val0 == val1))
def setup_callback(pvname): def my_cb(pvname=None, value=None, **kw): sys.stdout.write('get: %s value=%s, kw=%s\n' % (pvname, str(value), repr(kw))) sys.stdout.flush() chid = ca.create_channel(pvname) return ca.create_subscription(chid, callback=my_cb)
def testA_CreateChidWithConn(self): write('Simple Test: create chid with conn callback\n') chid = ca.create_channel(pvnames.int_pv, callback=onConnect) val = ca.get(chid) global CONN_DAT conn = CONN_DAT.get(pvnames.int_pv, None) self.assertEqual(conn, True)
def test_Connected(): pvn = pvnames.double_pv chid = ca.create_channel(pvn, connect=True) isconn = ca.isConnected(chid) write('CA test Connected (%s) = %s' % (pvn, isconn)) assert isconn assert ca.state(chid) == ca.dbr.CS_CONN assert ca.access(chid) == 'read/write'
def testA_CreateChidWithConn(self): write("Simple Test: create chid with conn callback") chid = ca.create_channel(pvnames.int_pv, callback=onConnect) val = ca.get(chid) global CONN_DAT conn = CONN_DAT.get(pvnames.int_pv, None) self.assertEqual(conn, True)
def test_xArray3(): write('Array Test: get char array as string') chid = ca.create_channel(pvnames.char_arrays[0]) val = ca.get(chid) assert isinstance(val, numpy.ndarray) char_val = ca.get(chid, as_string=True) assert isinstance(char_val, str) conv = ''.join([chr(i) for i in val]) assert conv == char_val
def test_xArray3(self): write('Array Test: get char array as string') chid = ca.create_channel(pvnames.char_arrays[0]) val = ca.get(chid, count=0) self.assertTrue(isinstance(val, numpy.ndarray)) char_val = ca.get(chid, as_string=True) self.assertTrue(isinstance(char_val, str)) conv = ''.join([chr(i) for i in val]) self.assertTrue(conv == char_val)
def get(d, pv_name, size, start, pid): ch = ca.create_channel(pv_name, connect=False, auto_cb=False) if ca.connect_channel(ch, timeout=1.0): d[pv_name] = ca.get(ch, wait=True) else: d[pv_name] = 'not connected' if len(d) == size: print int(round((time.time() - start) * 1000)) os.kill(pid, signal.SIGTERM)
def testA_GetNonExistentPV(self): write('Simple Test: get on a non-existent PV') chid = ca.create_channel('Definitely-Not-A-Real-PV') val, out = None, False try: val = ca.get(chid) except ca.ChannelAccessException: out = True assert(out)
def test_xArray3(self): write('Array Test: get char array as string') chid = ca.create_channel(pvnames.char_arrays[0]) val = ca.get(chid) self.assertTrue(isinstance(val, numpy.ndarray)) char_val = ca.get(chid, as_string=True) self.assertTrue(isinstance(char_val, str)) conv = ''.join([chr(i) for i in val]) self.assertEqual(conv, char_val)
def test_Enum(self): pvn = pvnames.enum_pv chid = ca.create_channel(pvn, connect=True) write('CA test Enum (%s)\n' % (pvn)) enumstrs = ca.get_enum_strings(chid) self.failUnless(len(enumstrs) > 1) self.failUnless(isinstance(enumstrs[0], str)) write('CA EnumStrings (%s) = %s\n' % (pvn, repr(enumstrs))) self.failUnless(enumstrs, pvnames.enum_pv_strs)
def test_putwait(): 'test put with wait' pvn = pvnames.non_updating_pv chid = ca.create_channel(pvn, connect=True) o = ca.put(chid, -1, wait=True) time.sleep(0.01) assert ca.get(chid) == -1 ca.put(chid, 2, wait=True) assert ca.get(chid) == 2
def test_putwait(self): 'test put with wait' pvn = pvnames.non_updating_pv chid = ca.create_channel(pvn, connect=True) o = ca.put(chid, -1, wait=True) val = ca.get(chid) self.assertEqual(val, -1) o = ca.put(chid, 2, wait=True) val = ca.get(chid) self.assertEqual(val, 2)
def test_Connected(self): pvn = pvnames.double_pv chid = ca.create_channel(pvn,connect=True) isconn = ca.isConnected(chid) write( 'CA test Connected (%s) = %s' % (pvn,isconn)) self.assertEqual(isconn,True) state= ca.state(chid) self.assertEqual(state,ca.dbr.CS_CONN) acc = ca.access(chid) self.assertEqual(acc,'read/write')
def test_Enum(self): pvn = pvnames.enum_pv chid = ca.create_channel(pvn,connect=True) write( 'CA test Enum (%s)' % (pvn)) enumstrs = ca.get_enum_strings(chid) self.failUnless(len(enumstrs)>1) self.failUnless(isinstance(enumstrs[0],str)) write( 'CA EnumStrings (%s) = %s' % (pvn,repr(enumstrs))) self.failUnless(enumstrs,pvnames.enum_pv_strs)
def test_ProcPut(self): pvn = pvnames.enum_pv chid = ca.create_channel(pvn, connect=True) write( 'CA test put to PROC Field (%s)' % (pvn)) for input in (1, '1', 2, '2', 0, '0', 50, 1): ret = None try: ret = ca.put(chid, 1) except: pass self.assertIsNot(ret, None)
def test_xArray2(self): write('Array Test: get fewer than max vals with ca.get(count=0)') chid = ca.create_channel(pvnames.double_arrays[0]) maxpts = ca.element_count(chid) npts = max(2, maxpts/2 - 1) dat = numpy.random.normal(size=npts) ca.put(chid, dat) out = ca.get(chid, count=0) self.assertTrue(isinstance(out, numpy.ndarray)) self.assertTrue(len(out) == npts)
def test_xArray2(self): write('Array Test: get fewer than max vals with ca.get(count=0)') chid = ca.create_channel(pvnames.double_arrays[0]) maxpts = ca.element_count(chid) npts = max(2, maxpts/2 - 1) dat = numpy.random.normal(size=npts) ca.put(chid, dat) out = ca.get(chid, count=0) self.assertTrue(isinstance(out, numpy.ndarray)) self.assertEqual(len(out), npts)
def test_ProcPut(self): pvn = pvnames.enum_pv chid = ca.create_channel(pvn, connect=True) write("CA test put to PROC Field (%s)" % (pvn)) for input in (1, "1", 2, "2", 0, "0", 50, 1): ret = None try: ret = ca.put(chid, 1) except: pass self.assertNotEqual(ret, None)
def testCtrlStruct_ValueMonitor(self): '''Monitor PV (value events) through GW - change value and properties directly - check CTRL structure consistency''' diffs = [] # gwcachetest is an ai record with full set of alarm limits: -100 -10 10 100 gw = ca.create_channel("gateway:gwcachetest") connected = ca.connect_channel(gw, timeout=.5) self.assertTrue(connected, "Could not connect to gateway channel " + ca.name(gw)) (gw_cbref, gw_uaref, gw_eventid) = ca.create_subscription(gw, mask=dbr.DBE_VALUE, use_ctrl=True, callback=self.onChangeGW) ioc = ca.create_channel("ioc:gwcachetest") connected = ca.connect_channel(ioc, timeout=.5) self.assertTrue(connected, "Could not connect to ioc channel " + ca.name(ioc)) (ioc_cbref, ioc_uaref, ioc_eventid) = ca.create_subscription(ioc, mask=dbr.DBE_VALUE, use_ctrl=True, callback=self.onChangeIOC) # set value on IOC ioc_value = ca.create_channel("ioc:gwcachetest") ca.put(ioc_value, 10.0, wait=True) time.sleep(.1) self.assertTrue(self.eventsReceivedIOC == self.eventsReceivedGW, "After setting value, no. of received updates differ: GW {0}, IOC {1}" .format(str(self.eventsReceivedGW), str(self.eventsReceivedIOC))) (are_diff, diffs) = self.compareStructures() self.assertTrue(are_diff == False, "At update {0} (change value), received structure updates differ:\n\t{1}" .format(str(self.eventsReceivedIOC), "\n\t".join(diffs))) # set property on IOC ioc_hihi = ca.create_channel("ioc:gwcachetest.HIHI") ca.put(ioc_hihi, 123.0, wait=True) ca.put(ioc_value, 11.0, wait=True) # trigger update time.sleep(.1) self.assertTrue(self.eventsReceivedIOC == self.eventsReceivedGW, "After setting property, no. of received updates differ: GW {0}, IOC {1}" .format(str(self.eventsReceivedGW), str(self.eventsReceivedIOC))) (are_diff, diffs) = self.compareStructures() self.assertTrue(are_diff == False, "At update {0} (change property), received structure updates differ:\n\t{1}" .format(str(self.eventsReceivedIOC), "\n\t".join(diffs)))
def test_xArray2(self): write('Array Test: get fewer than max vals using ca.get(count=0)') chid = ca.create_channel(pvnames.double_arrays[0]) maxpts = ca.element_count(chid) npts = int(max(2, maxpts/2.3 - 1)) dat = numpy.random.normal(size=npts) ca.put(chid, dat) out1 = ca.get(chid) self.assertTrue(isinstance(out1, numpy.ndarray)) self.assertEqual(len(out1), maxpts) out2 = ca.get(chid, count=0) self.assertTrue(isinstance(out2, numpy.ndarray)) self.assertEqual(len(out2), npts)
def test_subscription_double(self): pvn = pvnames.updating_pv1 chid = ca.create_channel(pvn,connect=True) cb, uarg, eventID = ca.create_subscription(chid, callback=onChanges) start_time = time.time() global CHANGE_DAT while time.time()-start_time < 5.0: time.sleep(0.01) if CHANGE_DAT.get(pvn, None) is not None: break val = CHANGE_DAT.get(pvn, None) ca.clear_subscription(eventID) self.assertNotEqual(val, None)
def test_subscription_double(self): pvn = pvnames.updating_pv1 chid = ca.create_channel(pvn,connect=True) cb, uarg, eventID = ca.create_subscription(chid, callback=onChanges) start_time = time.time() global CHANGE_DAT while time.time()-start_time < 5.0: time.sleep(0.01) if CHANGE_DAT.get(pvn, None) is not None: break val = CHANGE_DAT.get(pvn, None) ca.clear_subscription(eventID) self.assertIsNot(val, None)
def test_subscription_str(): pvn = pvnames.updating_str1 write(" Subscription on string: %s " % pvn) chid = ca.create_channel(pvn, connect=True) cb, uarg, eventID = ca.create_subscription(chid, callback=onChanges) start_time = time.time() global CHANGE_DAT while time.time() - start_time < 3.0: time.sleep(0.01) ca.put(chid, "%.1f" % (time.time() - start_time)) if CHANGE_DAT.get(pvn, None) is not None: break assert CHANGE_DAT.get(pvn, None) is not None
def __init__(self, visit_id = None): ###import config file self.pypline_dir = os.path.dirname(os.path.realpath(__file__)) with open(self.pypline_dir+'/config.yaml', 'r') as ymlfile: self.myconfig = yaml.load(ymlfile) ###connect to the logger self.logger = myLogger(os.path.basename(__file__)) ###set some parameters self.chid = ca.create_channel(self.myconfig['settings']['visit_id_pv'], connect=True) self.visit_id = visit_id self.type = 'visit'
def test_Array0(self): write('Array Test: get double array as numpy array, ctypes Array, and list') chid = ca.create_channel(pvnames.double_arrays[0]) aval = ca.get(chid) cval = ca.get(chid, as_numpy=False) self.assertTrue(isinstance(aval, numpy.ndarray)) self.assertTrue(len(aval) > 2) self.assertTrue(isinstance(cval, ctypes.Array)) self.assertTrue(len(cval) > 2) lval = list(cval) self.assertTrue(isinstance(lval, list)) self.assertTrue(len(lval) > 2) self.assertTrue(lval == list(aval))
def test_xArray2(self): write('Array Test: get fewer than max vals using ca.get(count=0)') chid = ca.create_channel(pvnames.double_arrays[0]) maxpts = ca.element_count(chid) npts = int(max(2, maxpts/2.3 - 1)) write('max points is %s' % (maxpts, )) dat = numpy.random.normal(size=npts) write('setting array to a length of npts=%s' % (npts, )) ca.put(chid, dat) out1 = ca.get(chid) self.assertTrue(isinstance(out1, numpy.ndarray)) self.assertEqual(len(out1), npts) out2 = ca.get(chid, count=0) self.assertTrue(isinstance(out2, numpy.ndarray)) self.assertEqual(len(out2), npts)
def test_DoubleVal(): pvn = pvnames.double_pv chid = ca.create_channel(pvn, connect=True) cdict = ca.get_ctrlvars(chid) write('CA testing CTRL Values for a Double (%s)' % (pvn)) assert 'units' in cdict assert 'precision' in cdict assert 'severity' in cdict assert len(ca.host_name(chid)) > 2 assert ca.element_count(chid) == 1 assert ca.field_type(chid) == ca.dbr.DOUBLE assert ca.get_precision(chid) == pvnames.double_pv_prec assert ca.get_ctrlvars(chid)['units'] == pvnames.double_pv_units assert ca.access(chid).startswith('read')
def testA_CreateChid_CheckTypeCount(self): write('Simple Test: create chid, check count, type, host, and access') chid = ca.create_channel(pvnames.double_pv) ret = ca.connect_channel(chid) ca.pend_event(1.e-3) ftype = ca.field_type(chid) count = ca.element_count(chid) host = ca.host_name(chid) rwacc = ca.access(chid) self.assertNotEqual(chid, None) self.assertNotEqual(host, None) self.assertEqual(count, 1) self.assertEqual(ftype, 6) self.assertEqual(rwacc,'read/write')
def test_CreateChid_CheckTypeCount(): write('Simple Test: create chid, check count, type, host, and access') chid = ca.create_channel(pvnames.double_pv) ret = ca.connect_channel(chid) ca.pend_event(1.e-3) ftype = ca.field_type(chid) count = ca.element_count(chid) host = ca.host_name(chid) rwacc = ca.access(chid) assert chid is not None assert host is not None assert count == 1 assert ftype == 6 assert rwacc == 'read/write'
def batch_get(pv_list): chids = {} pvdata = {} for name in pv_list: chid = ca.create_channel(name, connect=False, auto_cb=False) chids[name] = chid for name, chid in chids.items(): ca.connect_channel(chid) ca.poll() for name, chid in chids.items(): ca.get(chid, wait=False) ca.poll() for name, chid in chids.items(): val = ca.get_complete(chid) pvdata[name] = val return pvdata
def testA_CreateChid_CheckTypeCount(self): write('Simple Test: create chid, check count, type, host, and access') chid = ca.create_channel(pvnames.double_pv) ret = ca.connect_channel(chid) ca.pend_event(1.e-3) ftype = ca.field_type(chid) count = ca.element_count(chid) host = ca.host_name(chid) rwacc = ca.access(chid) self.assertIsNot(chid, None) self.assertIsNot(host, None) self.assertEqual(count, 1) self.assertEqual(ftype, 6) self.assertEqual(rwacc,'read/write')
def my_node(node,depth,mode='elem',result=None): #This must have the args defined here, and may have a section for each of the 3 modes: #mode='init': Do something before porcessing a node #mode='elem': Called for each element in the node. Can return a value which will be added to the result[] list. #mode='end': Called at the end to process the result[] list for the node global pvname global nChid if mode == 'elem': fullpv=node+":"+pvname #create full pvname chid = ca.create_channel(fullpv, connect=False, auto_cb=False) #create channel print fullpv,chid allnames.append(fullpv) #store name and channel allids.append(chid) nChid += 1
def test_Values(self): write("CA test Values (compare 5 values with caget)") os.system("rm ./caget.tst") vals = {} pause_updating() for pvn in (pvnames.str_pv, pvnames.int_pv, pvnames.float_pv, pvnames.enum_pv, pvnames.long_pv): os.system("caget -n -f5 %s >> ./caget.tst" % pvn) chid = ca.create_channel(pvn) ca.connect_channel(chid) vals[pvn] = ca.get(chid) rlines = open("./caget.tst", "r").readlines() for line in rlines: pvn, sval = [i.strip() for i in line[:-1].split(" ", 1)] tval = str(vals[pvn]) if pvn in (pvnames.float_pv, pvnames.double_pv): # use float precision! tval = "%.5f" % vals[pvn] self.assertEqual(tval, sval) resume_updating()
def test_subscription_str(self): pvn = pvnames.updating_str1 write(" Subscription on string: %s " % pvn) chid = ca.create_channel(pvn,connect=True) cb, uarg, eventID = ca.create_subscription(chid, callback=onChanges) start_time = time.time() global CHANGE_DAT while time.time()-start_time < 3.0: time.sleep(0.01) ca.put(chid, "%.1f" % (time.time()-start_time) ) if CHANGE_DAT.get(pvn, None) is not None: break val = CHANGE_DAT.get(pvn, None) # ca.clear_subscription(eventID) self.assertNotEqual(val, None) time.sleep(0.2)
def my_node(node,depth,mode='elem',result=None): #This must have the args defined here, and may have a section for each of the 3 modes: #mode='init': Do something before porcessing a node #mode='elem': Called for each element in the node. Can return a value which will be added to the result[] list. #mode='end': Called at the end to process the result[] list for the node global pvdata global pvf global mon_line global pvfull if mode == 'elem': fullpv=node+":"+pvdata[mon_line][pvf.NAME] #create full pvname chid = ca.create_channel(fullpv, connect=False, auto_cb=False) #create channel pvdata[mon_line][pvf.ID].append(chid) #save the chid pvfull.append(fullpv) #save the name temporarily
def test_Values(self): write( 'CA test Values (compare 5 values with caget)') os.system('rm ./caget.tst') vals = {} with no_simulator_updates(): for pvn in (pvnames.str_pv, pvnames.int_pv, pvnames.float_pv, pvnames.enum_pv, pvnames.long_pv): os.system('caget -n -f5 %s >> ./caget.tst' % pvn) chid = ca.create_channel(pvn) ca.connect_channel(chid) vals[pvn] = ca.get(chid) rlines = open('./caget.tst', 'r').readlines() for line in rlines: pvn, sval = [i.strip() for i in line[:-1].split(' ', 1)] tval = str(vals[pvn]) if pvn in (pvnames.float_pv,pvnames.double_pv): # use float precision! tval = "%.5f" % vals[pvn] self.assertEqual(tval, sval)
def test_subscription_custom(self): pvn = pvnames.updating_pv1 chid = ca.create_channel(pvn, connect=True) global change_count change_count = 0 def my_callback(pvname=None, value=None, **kws): write( ' Custom Callback %s value=%s' %(pvname, str(value))) global change_count change_count = change_count + 1 cb, uarg, eventID = ca.create_subscription(chid, callback=my_callback) start_time = time.time() while time.time()-start_time < 2.0: time.sleep(0.01) ca.clear_subscription(eventID) time.sleep(0.2) self.assertTrue(change_count > 2)