def testDynamicInvoker(self): client = Session((MessageTransportTest.addr, MessageTransportTest.port), None) invoker = client.toMessageInvoker() m1 = invoker.fun1(1, 2, "3", b=None, c=[1, 2, "3d"]) self.assertEqual(m1.messageType(), Message.Type.Request) self.assertEqual(m1.requestContent(), ("fun1", [1, 2, "3"], {"b": None, "c": [1, 2, "3d"]})) self.assertRaises(ProtocolException, lambda: invoker.fun2(2, "3", b=None, c=[1, 2, "3d"], To=1)) invoker2 = client.toMessageInvoker("OnT") m2 = invoker2.fun2() self.assertEqual(m2.messageType(), Message.Type.Request) self.assertEqual(m2.requestContent(), ("fun2", [], {})) self.assertEqual(m2.getTo(), "OnT")
def testInvokeOtherClient(self): class Target: def v8(self): return "V8 great!" def v9(self): raise ProtocolException("V9 not good.") def v10(self): raise IOError("V10 have problems.") def v(self, i, b): return "OK" mc1 = Session((MessageTransportTest.addr, MessageTransportTest.port), Target(), name="T1-Benz") mc1.start() checker = Session.newSession((MessageTransportTest.addr, MessageTransportTest.port), None, "T1-Checher") benzChecker = checker.blockingInvoker(u"T1-Benz", 10) v8r = benzChecker.v8() self.assertEqual(v8r, "V8 great!") try: benzChecker.v9() self.assertTrue(False) except ProtocolException as e: self.assertEqual(e.__str__(), "V9 not good.") try: benzChecker.v10() self.assertTrue(False) except ProtocolException as e: self.assertEqual(e.__str__(), "V10 have problems.") self.assertEqual(benzChecker.v(1, False), "OK") try: benzChecker.v11() self.assertTrue(False) except ProtocolException as e: self.assertEqual(e.__str__(), "InvokeError: Command v11 not found.") mc1.stop() checker.stop()
def testList(self): mc = Session((StorageServiceTest.addr, StorageServiceTest.port), None) mc.start() service = StorageService(mc) a = service.listElements(StorageServiceTest.testSpacePath) self.assertEqual(a, ["_A1", "_A2", "a1", "a2", "a3", "a4", "a5"]) mc.stop()
def testHBTFile(self): mc = Session((StorageServiceTest.addr, StorageServiceTest.port), None) mc.start() service = StorageService(mc) hbtFile = service.getElement(StorageServiceTest.testSpacePath).resolve( 'HBTFileTest.hbt').toHBTFileElement() hbtFile.initialize([["Column 1", HBTFileElement.BYTE], ["Column 2", HBTFileElement.SHORT], ["Column 3", HBTFileElement.INT], ["Column 4", HBTFileElement.LONG], ["Column 5", HBTFileElement.FLOAT], ["Column 6", HBTFileElement.DOUBLE]]) hbtFile.appendRow([1, 2, 3, 4, 5, 6]) hbtFile.appendRows([[1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6], [1.1, 2.2, 3.3, 4.4, 5.5, 6.6], [1.1, 2.2, 3.3, 4.4, 5.5, 6.6]]) self.assertEqual(hbtFile.readRows(0, 1)[0], [1, 2, 3, 4, 5, 6]) self.assertEqual(hbtFile.readRow(1), [1, 2, 3, 4, 5, 6]) self.assertEqual(hbtFile.readRow(2), [1, 2, 3, 4, 5, 6]) self.assertEqual(hbtFile.readRow(3), [1, 2, 3, 4, 5, 6]) self.assertEqual(hbtFile.readRow(4), [1, 2, 3, 4, 5.5, 6.6]) self.assertEqual(hbtFile.readRow(5), [1, 2, 3, 4, 5.5, 6.6]) self.assertEqual(hbtFile.getColumnCount(), 6) self.assertEqual(hbtFile.getRowCount(), 6) self.assertEqual(hbtFile.getHeadNames(), [ 'Column 1', 'Column 2', 'Column 3', 'Column 4', 'Column 5', 'Column 6' ]) mc.stop()
def testWrite(self): mc = Session((StorageServiceTest.addr, StorageServiceTest.port), None) mc.start() testRoot = StorageService(mc).getElement( StorageServiceTest.testSpacePath) A1 = testRoot.resolve('_A1') A1.write(b"ABCDE", 10) self.assertEqual( A1.metaData(), { "Name": "_A1", "Path": "{}_A1".format(StorageServiceTest.testSpacePath), "Type": "Content", "Size": 36 }) self.assertEqual(A1.readAsString(0, 16), "1234567890ABCDEf") self.assertEqual(A1.readAllAsString(), "1234567890ABCDEfghijklmnopqrstuvwxyz") A1.write(b"defghi", 39) self.assertEqual( A1.metaData(), { "Name": "_A1", "Path": "{}_A1".format(StorageServiceTest.testSpacePath), "Type": "Content", "Size": 45 }) self.assertEqual(A1.readAsString(35, 10), "z\0\0\0defghi") self.assertEqual(A1.readAsString(0, 16), "1234567890ABCDEf") mc.stop()
def testCreateDirectory(self): mc = Session((StorageServiceTest.addr, StorageServiceTest.port), None) mc.start() testRoot = StorageService(mc).getElement( StorageServiceTest.testSpacePath) testRoot.resolve('a2').resolve('NewDir').createDirectory() self.assertEqual(testRoot.resolve('a2').listElements(), ["NewDir"]) mc.stop()
def __init__(self, adapter, port, address, serviceName): self.adapterServer = AdapterServer(port) if adapter is None: self.adapter = SimulatedAdapter(port) else: self.adapter = ExecutableAdapter(adapter, port) self.adapterServer.accept() class PXIFrequencyDividerService: def __init__(self, handler): self.handler = handler def getDivide(self): raise RuntimeError('') def setDivide(self, divide): self.handler.adapterServer.sendCommand(self.__createFrequancyCommand(divide)) def setDelay(self, channel, course, fine): self.handler.adapterServer.sendCommand(self.__createDelayCommand(channel, course, fine)) def __createFrequancyCommand(self, divide): if divide < 1 or divide > 255: raise RuntimeError('Divide out of range.') addr = b'0x28c\0' + b'0x' data = format(divide, '2X').replace(' ', '0').encode('utf-8') cmd = addr + data + b'\0\0\0\0\0\0\0' return cmd def __createDelayCommand(self, channel, courseDelay, fineDelay): if channel < 0 or channel >= 40: raise RuntimeError('Channel out of range.') if courseDelay < 0 or courseDelay >= 60000: raise RuntimeError('Course Delay out of range.') if fineDelay < 0 or fineDelay >= 256: raise RuntimeError('Fine Delay out of range.') addr = b'0x288\0' + b'0x' cn = format(channel, '2X').replace(' ', '0').encode('utf-8') cD = format(courseDelay, '4X').replace(' ', '0').encode('utf-8') fD = format(fineDelay, '2X').replace(' ', '0').encode('utf-8') cmd = addr + cn + cD + fD + b'\0' return cmd time.sleep(1) self.service = PXIFrequencyDividerService(self) self.session = Session.newSession(address, self.service, serviceName) self.service.setDivide(99) for c in range(0, 40): self.service.setDelay(c, 0, 0)
def testRead(self): mc = Session((StorageServiceTest.addr, StorageServiceTest.port), None) mc.start() testRoot = StorageService(mc).getElement( StorageServiceTest.testSpacePath) self.assertEqual(testRoot.resolve('/_A1').read(1, 10), b"234567890a") self.assertEqual(testRoot.resolve('/////_A1').read(30, 6), b"uvwxyz") self.assertEqual(testRoot.resolve('_A2').readAll(), b'0123456789') mc.stop()
def testElementNote(self): mc = Session((StorageServiceTest.addr, StorageServiceTest.port), None) mc.start() testRoot = StorageService(mc).getElement( StorageServiceTest.testSpacePath) self.assertEqual(testRoot.readNote(), "") testRoot.writeNote("Test Note") self.assertEqual(testRoot.readNote(), "Test Note") mc.stop()
def testNote(self): mc = Session((StorageServiceTest.addr, StorageServiceTest.port), None) mc.start() service = StorageService(mc) self.assertEqual(service.readNote(StorageServiceTest.testSpacePath), u"") service.writeNote(StorageServiceTest.testSpacePath, u"Test Note") self.assertEqual(service.readNote(StorageServiceTest.testSpacePath), u"Test Note") mc.stop()
def testCreateDirectory(self): mc = Session((StorageServiceTest.addr, StorageServiceTest.port), None) mc.start() service = StorageService(mc) service.createDirectory("{}a2/NewDir".format( StorageServiceTest.testSpacePath)) self.assertEqual( service.listElements("{}a2".format( StorageServiceTest.testSpacePath)), ["NewDir"]) mc.stop()
def testCreateFile(self): mc = Session((StorageServiceTest.addr, StorageServiceTest.port), None) mc.start() testRoot = StorageService(mc).getElement( StorageServiceTest.testSpacePath) testRoot.resolve('NewFile').createFile() testRoot.resolve('a2/NewFile').createFile() self.assertEqual( testRoot.listElements(), ["NewFile", "_A1", "_A2", "a1", "a2", "a3", "a4", "a5"]) self.assertEqual(testRoot.resolve('a2').listElements(), ["NewFile"]) mc.stop()
def testRemoteInvokeAndAsync(self): client1 = Session((MessageTransportTest.addr, MessageTransportTest.port), None) f1 = client1.start() invoker1 = client1.asynchronousInvoker() future1 = invoker1.co() latch1 = threading.Semaphore(0) future1.onComplete(lambda: latch1.release()) latch1.acquire() self.assertTrue(future1.isDone()) self.assertFalse(future1.isSuccess()) self.assertEqual(future1.exception().description, "Method not found: co.") client1.stop()
def testElementMetaData(self): mc = Session((StorageServiceTest.addr, StorageServiceTest.port), None) mc.start() testRoot = StorageService(mc).getElement( StorageServiceTest.testSpacePath) self.assertEqual( testRoot.resolve("a1").metaData(), { "Name": "a1", "Path": "{}a1".format(StorageServiceTest.testSpacePath), "Type": "Collection" }) mc.stop()
def testMetaData(self): mc = Session((StorageServiceTest.addr, StorageServiceTest.port), None) mc.start() service = StorageService(mc) self.assertEqual( service.metaData(u"{}a1".format(StorageServiceTest.testSpacePath)), { u"Name": u"a1", u"Path": u"{}a1".format(StorageServiceTest.testSpacePath), u"Type": u"Collection" }) mc.stop()
def testElementDelete(self): mc = Session((StorageServiceTest.addr, StorageServiceTest.port), None) mc.start() testRoot = StorageService(mc).getElement( StorageServiceTest.testSpacePath) self.assertEqual(testRoot.listElements(), ["_A1", "_A2", "a1", "a2", "a3", "a4", "a5"]) testRoot.resolve('a1').delete() self.assertEqual(testRoot.listElements(), ["_A1", "_A2", "a2", "a3", "a4", "a5"]) testRoot.resolve('_A2').delete() self.assertEqual(testRoot.listElements(), ["_A1", "a2", "a3", "a4", "a5"]) mc.stop()
def testRead(self): mc = Session((StorageServiceTest.addr, StorageServiceTest.port), None) mc.start() service = StorageService(mc) self.assertEqual( service.read(u"{}_A1".format(StorageServiceTest.testSpacePath), 1, 10), b"234567890a") self.assertEqual( service.read(u"{}_A1".format(StorageServiceTest.testSpacePath), 30, 6), b"uvwxyz") self.assertEqual( service.readAll(u"{}_A2".format(StorageServiceTest.testSpacePath)), b'0123456789') mc.stop()
def testCreateFile(self): mc = Session((StorageServiceTest.addr, StorageServiceTest.port), None) mc.start() service = StorageService(mc) service.createFile("{}NewFile".format( StorageServiceTest.testSpacePath)) service.createFile("{}a2/NewFile".format( StorageServiceTest.testSpacePath)) self.assertEqual( service.listElements(StorageServiceTest.testSpacePath), ["NewFile", "_A1", "_A2", "a1", "a2", "a3", "a4", "a5"]) self.assertEqual( service.listElements("{}a2".format( StorageServiceTest.testSpacePath)), ["NewFile"]) mc.stop()
def setUp(self): mc = Session((StorageServiceTest.addr, StorageServiceTest.port), None) mc.start() storage = mc.blockingInvoker(u"StorageService") if storage.exists(u"", StorageServiceTest.testSpacePath): storage.delete(u"", StorageServiceTest.testSpacePath) storage.createDirectory(u"", StorageServiceTest.testSpacePath) for i in range(1, 6): storage.createDirectory( u"", u"{}a{}".format(StorageServiceTest.testSpacePath, i)) storage.createFile(u"", u"{}_A1".format(StorageServiceTest.testSpacePath)) storage.createFile(u"", u"{}_A2".format(StorageServiceTest.testSpacePath)) storage.write(u"", u"{}_A1".format(StorageServiceTest.testSpacePath), b"1234567890abcdefghijklmnopqrstuvwxyz", 0) storage.write(u"", u"{}_A2".format(StorageServiceTest.testSpacePath), b"0123456789", 0) mc.stop()
def testDelete(self): mc = Session((StorageServiceTest.addr, StorageServiceTest.port), None) mc.start() service = StorageService(mc) self.assertEqual( service.listElements(StorageServiceTest.testSpacePath), ["_A1", "_A2", "a1", "a2", "a3", "a4", "a5"]) service.delete("{}a1".format(StorageServiceTest.testSpacePath)) self.assertEqual( service.listElements(StorageServiceTest.testSpacePath), ["_A1", "_A2", "a2", "a3", "a4", "a5"]) service.delete("{}_A2".format(StorageServiceTest.testSpacePath)) self.assertEqual( service.listElements(StorageServiceTest.testSpacePath), ["_A1", "a2", "a3", "a4", "a5"]) mc.stop()
def testAppend(self): mc = Session((StorageServiceTest.addr, StorageServiceTest.port), None) mc.start() service = StorageService(mc) service.append(u"{}_A1".format(StorageServiceTest.testSpacePath), b"ABCDE") self.assertEqual( service.metaData(u"{}_A1".format( StorageServiceTest.testSpacePath)), { u"Name": u"_A1", u"Path": u"{}_A1".format(StorageServiceTest.testSpacePath), u"Type": u"Content", u"Size": 41 }) self.assertEqual( service.read(u"{}_A1".format(StorageServiceTest.testSpacePath), 35, 6), b"zABCDE") mc.stop()
def testWrite(self): mc = Session((StorageServiceTest.addr, StorageServiceTest.port), None) mc.start() service = StorageService(mc) service.write(u"{}_A1".format(StorageServiceTest.testSpacePath), b"ABCDE", 10) self.assertEqual( service.metaData(u"{}_A1".format( StorageServiceTest.testSpacePath)), { u"Name": u"_A1", u"Path": u"{}_A1".format(StorageServiceTest.testSpacePath), u"Type": u"Content", u"Size": 36 }) self.assertEqual( service.readAsString( u"{}_A1".format(StorageServiceTest.testSpacePath), 0, 16), u"1234567890ABCDEf") self.assertEqual( service.readAllAsString(u"{}_A1".format( StorageServiceTest.testSpacePath)), u"1234567890ABCDEfghijklmnopqrstuvwxyz") service.write(u"{}_A1".format(StorageServiceTest.testSpacePath), b"defghi", 39) self.assertEqual( service.metaData(u"{}_A1".format( StorageServiceTest.testSpacePath)), { u"Name": u"_A1", u"Path": u"{}_A1".format(StorageServiceTest.testSpacePath), u"Type": u"Content", u"Size": 45 }) self.assertEqual( service.readAsString( u"{}_A1".format(StorageServiceTest.testSpacePath), 35, 10), u"z\0\0\0defghi") self.assertEqual( service.readAsString( u"{}_A1".format(StorageServiceTest.testSpacePath), 0, 16), u"1234567890ABCDEf") mc.stop()
def testElementListMetaData(self): mc = Session((StorageServiceTest.addr, StorageServiceTest.port), None) mc.start() testRoot = StorageService(mc).getElement( StorageServiceTest.testSpacePath) elements = testRoot.listElements(True) expected = [["_A1", "_A1", "Content"], ["_A2", "_A2", "Content"], ["a1", "a1", "Collection"], ["a2", "a2", "Collection"], ["a3", "a3", "Collection"], ["a4", "a4", "Collection"], ["a5", "a5", "Collection"]] self.assertEqual(len(elements), len(expected)) for i in range(0, len(expected)): exp = expected[i] res = elements[i] self.assertEqual(res.get("Name"), exp[0]) self.assertEqual(res.get("Path"), StorageServiceTest.testSpacePath + exp[1]) self.assertEqual(res.get("Type"), exp[2]) self.assertEqual(elements[0].get("Size"), 36) self.assertEqual(elements[1].get("Size"), 10) mc.stop()
def testConnectionOfSession(self): # mc = Session((MessageTransportTest.addr, 10211), None) # self.assertRaises(ConnectionRefusedError, mc.start) mc = Session((MessageTransportTest.addr, MessageTransportTest.port), None) mc.start() mc.stop()
def testClientNameDuplicated(self): mc1 = Session((MessageTransportTest.addr, MessageTransportTest.port), None, name="T2-ClientDuplicated") mc1.start() mc2 = Session((MessageTransportTest.addr, MessageTransportTest.port), None, name="T2-ClientDuplicated") self.assertRaises(ProtocolException, lambda: mc2.start()) mc1.stop() time.sleep(0.5) mc2.blockingInvoker().connect(u"T2-ClientDuplicated") mc2.stop()
from Pydra import Session import time if __name__ == '__main__': session = Session.newSession(('192.168.25.27', 20102), None, "PolarizationControlTestLocal") try: invoker = session.blockingInvoker("PolarizationControlService") print(invoker.hello("23")) time.sleep(3) except Exception: print('e') pass session.stop()
temperature = temperatures[i] pids[i].Kp = pidPara[i][0] pids[i].Ki = pidPara[i][1] pids[i].Kd = pidPara[i][2] vs[i] += pids[i].update(temperature) pids[i].setPoint(pidPara[i][3] + tempSetDelta(t, i)) if (vs[i] > 1.5): vs[i] = 1.5 if (vs[i] < 0): vs[i] = 0 dc.setVoltages(vs) print( '{:.1f}\t{:.3f}\t{:.3f}\t{:.1f}\t{:.1f}\t{:.1f}\t{:.3f}\t{:.3f}\t{:.3f}\t{:.3f}\t{:.3f}\t{:.3f}\t{:.3f}' .format(t, r1, r2, ppm1, ppm2, ratioPPM, roomTemp, temperatures[0], vs[0], temperatures[1], vs[1], temperatures[2], vs[2])) file.write( '{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}\n'.format( t, r1, r2, ppm1, ppm2, ratioPPM, roomTemp, temperatures[0], vs[0], temperatures[1], vs[1], temperatures[2], vs[2])) file.flush() if __name__ == '__main__': tc = Control() tc.setSession( Session.newSession(('192.168.25.27', 20102), tc, 'Temperature Control For BS and PDs')) tc.start()
from Pydra import Session import time import threading import random print('In Try.') session = Session.newSession(("localhost", 20102)) bi = session.blockingInvoker(timeout=1) while True: time.sleep(5) try: si = bi.sessionsInformation() print(si) except BaseException as e: print("EXCEPTION!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
from Pydra import Session if __name__ == '__main__': session = Session.newSession(('192.168.25.27', 20102)) invoker = session.blockingInvoker('HMC7044EvalTest') print(invoker.deviceConnected()) for i in range(0, 14): invoker.setDivider(i, 200) import time time.sleep(1) session.stop()
__author__ = 'Hwaipy' import sys from Pydra import Message, ProtocolException, Session import socket import threading import time from Services.Storage import StorageService, HBTFileElement import random import math if __name__ == '__main__': mc = Session(("192.168.25.27", 20102), None) mc.start() service = StorageService(mc) hbtFile = service.getElement('HBTChartTest.hbt').toHBTFileElement() hbtFile.initialize([["Column 1", HBTFileElement.BYTE], ["Column 2", HBTFileElement.SHORT], ["Column 3", HBTFileElement.INT], ["Column 4", HBTFileElement.LONG], ["Column 5", HBTFileElement.FLOAT], ["Column 6", HBTFileElement.DOUBLE]]) for i in range(0, 10): d1 = i d2 = i * i d3 = -i d4 = i * i * i d5 = math.sin(i) d6 = math.exp(i) hbtFile.appendRow([d1, d2, d3, d4, d5, d6]) mc.stop()