Exemple #1
0
 def inject_var_to_workspace(self):
     c = Client()
     c.write('mat', {
         'type': 'Matrix',
         'value': [[1, 2, 3], [3, 2, 1]]
     }, 'user')
     self.text_edit.append('命令已执行,请查看工作空间。\n')
Exemple #2
0
    def get_data(self):
        c = Client()

        try:
            mat = c.read('mat')
        except BaseException:
            self.text_edit.append('mat变量不存在,请先点击左侧按钮对mat赋值。\n')
            return
        self.text_edit.append('成功获取变量‘mat’,值为' + repr(mat) +
                              '\n你可以在控制台中运行命令改变mat的值,'
                              '并且再次点击右侧按钮获取它,查看有无变化。\n')
Exemple #3
0
    def __init__(self):
        super(QW.QMainWindow, self).__init__()
        self.setupUi(self)

        # set up dynamic figures
        self.widget_2.scale = 2.0
        for key in self.widget_5.figs.keys(
        ):  # set the title of foot end force figures to LFX, LFY, LFZ, ...
            if key[-1] == 'D': self.widget_5.figs[key].setTitle(key[:2] + 'Y')
            if key[-1] == 'K': self.widget_5.figs[key].setTitle(key[:2] + 'Z')

        # set up attributes
        self.client = Client()
        self.connected = False

        self.dialpose = DialogPose()
        self.dialpose.accepted.connect(self.on_dialpose_accepted)

        self.prot = Protocol()
        self.sens = self.prot.sens
        self.stat = self.prot.stat
        self.comd = self.prot.comd
        self.para = self.prot.para
        self.datashow = SensorPackage(buflen_max=25)

        # set up timers
        self.timer0 = QC.QTimer()  # Check connection state
        self.timer1 = QC.QTimer()  # Communication (receive data)

        self.timer0.timeout.connect(self.checkConnection)
        self.timer1.timeout.connect(self.hear)

        self.timer0.start(
            1000
        )  # check connection state every 1 second, start as soon as the UI is launched
        self.timer1.setInterval(40)
Exemple #4
0
from random import randint

from communication import Client, server
from communication.decrypt import decrypt
from communication.encrypt import encrypt
from utils.helpers import read_from_keyboard, prime_numbers

MAX_PRIME = 101
BASE = randint(5, MAX_PRIME)
TEXT = 'TEXT to send'

primes = prime_numbers(MAX_PRIME)
prime = read_from_keyboard(primes)

client = Client(prime, BASE)
send_to_server = client.calculate_send_distribute()
send_to_client = server(prime, BASE)
client.value_modifier(send_to_client)
client_xor_key = client.calculate_response_distribute()
server_xor_key = server(prime, BASE, send_to_server)

encrypted_text = encrypt(TEXT, client_xor_key)
decrypted_text = decrypt(encrypted_text, server_xor_key)
print(TEXT)
print(encrypted_text)
print(decrypted_text)
assert TEXT == decrypted_text, 'Decrypt failed'
Exemple #5
0
class MainWindow(QW.QMainWindow, interface_Main.Ui_MainWindow):
    def __init__(self):
        super(QW.QMainWindow, self).__init__()
        self.setupUi(self)

        # set up dynamic figures
        self.widget_2.scale = 2.0
        for key in self.widget_5.figs.keys(
        ):  # set the title of foot end force figures to LFX, LFY, LFZ, ...
            if key[-1] == 'D': self.widget_5.figs[key].setTitle(key[:2] + 'Y')
            if key[-1] == 'K': self.widget_5.figs[key].setTitle(key[:2] + 'Z')

        # set up attributes
        self.client = Client()
        self.connected = False

        self.dialpose = DialogPose()
        self.dialpose.accepted.connect(self.on_dialpose_accepted)

        self.prot = Protocol()
        self.sens = self.prot.sens
        self.stat = self.prot.stat
        self.comd = self.prot.comd
        self.para = self.prot.para
        self.datashow = SensorPackage(buflen_max=25)

        # set up timers
        self.timer0 = QC.QTimer()  # Check connection state
        self.timer1 = QC.QTimer()  # Communication (receive data)

        self.timer0.timeout.connect(self.checkConnection)
        self.timer1.timeout.connect(self.hear)

        self.timer0.start(
            1000
        )  # check connection state every 1 second, start as soon as the UI is launched
        self.timer1.setInterval(40)

########## set up slots ##########

##### connection #####

    @pyqtSlot()
    def on_pushButton_5_clicked(self):
        """ connect """
        self.client.serverIP = self.lineEdit_7.text()
        self.client.open()
        self.checkConnection()

    @pyqtSlot()
    def on_pushButton_6_clicked(self):
        """ disconnect """
        self.client.close()
        self.checkConnection()
######################

##### parameter setting #####

    @pyqtSlot()
    def on_pushButton_3_clicked(self):
        """ pop out the dialog for parameter setting """
        self.dialpose.para.decode('copy', datacopy=self.para.data)
        self.dialpose.set_text()
        self.dialpose.show()

    @pyqtSlot(
    )  # no reponse using @pyqtSlot, do not know why..., so this function is explicitly connected to self.dialpose
    def on_dialpose_accepted(self):
        """ save and send out the parameters if the 'save' button is clicked """
        self.para.decode('copy', datacopy=self.dialpose.para.data)
        self.para.save()
        self.client.send(self.prot.collect(typ=0x04, ack=0x04))
#############################

##### command buttons #####

    @pyqtSlot()
    def on_pushButton_11_clicked(self):
        """ send walk gait command """
        self.comd.switch, self.comd.gait, self.comd.rc = 0x00, 0x01, 0x00
        self.client.send(self.prot.collect(typ=0x03, ack=0x02))
###########################

##### figure tab #####

    @pyqtSlot(int)
    def on_tabWidget_currentChanged(self):
        self.update_figure_1()

    @pyqtSlot(int)
    def on_tabWidget_2_currentChanged(self):
        self.update_figure_2()
######################

########## other methods ##########

    def checkConnection(self):
        connected = self.client.get_connection_state()
        connection_changed = self.connected != connected
        self.connected = connected

        if connection_changed and connected:
            self.sens = self.prot.sens = SensorPackage(
            )  # if reconnected, new log files are created and old buffers are dumped
            self.timer1.start()
            print("Start hearing ...")
            self.label_9.setText('Connected')
        elif connection_changed and not connected:
            self.timer1.stop()
            print("Hearing over. Total %i frames heard." % self.prot.cnt)
            self.label_9.setText('Disconnected')

    def hear(self):
        last_cnt = self.prot.cnt
        # self.client.interact(self.prot.process)	# this do not dump any frame
        self.prot.distrib(self.client.recv(
        ))  # this returns the newest frame and dump the others
        if self.prot.cnt > last_cnt:
            if self.prot.cnt % 5 == 0:
                self.update_figdata(
                )  # set different update frequencies and phases to stagger these time-consuming operations
            if self.prot.cnt % 5 == 1:
                self.update_figure_2(
                )  # meter figures should update more frequently to look smooth
            if self.prot.cnt % 15 == 3: self.update_figure_1()

    def update_figdata(self):
        """ update figure data (only data, not figure) """
        if not self.sens.checkBufferEmpty():
            self.datashow.decode('copy', datacopy=self.sens.filter())
            if self.datashow.checkBufferFull(): self.datashow.bufferShift()
            self.datashow.bufferIn()

    def update_figure_1(self):
        """ refresh curve figures """
        if not self.datashow.checkBufferEmpty():
            buf = self.datashow.bufferGet()
            idx = self.tabWidget.currentIndex()
            if idx == 0: self.widget.update(buf['forc_time'], buf['forc'])
            elif idx == 1: self.widget_4.update(buf['disp_time'], buf['disp'])
            elif idx == 2: self.widget_5.update(buf['foot_time'], buf['foot'])

    def update_figure_2(self):
        """ refresh meter figures """
        if not self.datashow.checkBufferEmpty():
            frame = self.datashow.last()
            idx = self.tabWidget_2.currentIndex()
            if idx == 0:
                self.widget_3.update(*frame['imu'][0])
                self.widget_2.update(*frame['imu'][1])
            elif idx == 1:
                pass
Exemple #6
0
from random import randint

from communication import Client, Server
from utils.helpers import read_from_keyboard, prime_numbers

MAX_PRIME = 101
BASE = randint(17, MAX_PRIME)
TEXT = 'Text that will be sent'

primes = prime_numbers(MAX_PRIME)
prime = read_from_keyboard(primes)

server = Server(prime, BASE)
client = Client(prime, BASE, 'localhost')

server.start()
client.start()

client.send(TEXT)
server.stop()

assert TEXT == server.messages[0][1], 'Decrypt failed'