def test_empty_server_get_data(self):
     try:
         ServerConnection.get_data(
             ServerConnection.get_web_source(
                 "http://data.nba.net/data/10s/prod/v"))
     except NoDataFoundError:
         pass
Beispiel #2
0
def pre_run():

    cconn = Camera()

    server_config = {
        'provisioning_token_path': './provisioning_token.json',
        'url_base': 'https://skunkworks.lbl.gov/turkeycam',
        'credentials_path': './credentials.json',
        'params_path': './local_config.json',
        'device_name': None,
        'device_type': 'picamera',
        'device_serial': bytes(cconn.getSerial(), 'utf-8'),
    }
    sconn = ServerConnection(server_config)

    cfg = {k: base_config[k] for k in base_config}
    cfg['sconn'] = sconn
    cfg['cconn'] = cconn
    sconn.getParams(cfg)
    cconn.setParams(cfg['cam_params'])

    if cfg.get('wdog_use_i2c', False):
        from Wdog2 import Wdog
        cfg['wdog'] = Wdog(cfg['wdog2'])
    if cfg.get('wdog_use_spi', False):
        from Wdog3 import Wdog
        cfg['wdog'] = Wdog(cfg['wdog2'])
    else:
        from Wdog import Wdog
        cfg['wdog'] = Wdog(cfg['wdog'])

    cfg['wdog'].setup()

    return cfg
Beispiel #3
0
    def handleHeader(self, key, value):
        if self.hsts:
            if (key.lower() == 'set-cookie'):
                newvalues =[]
                value = SSLServerConnection.cookieExpression.sub("\g<1>", value)
                values = value.split(';')
                for v in values:
                    if v[:7].lower()==' domain':
                        dominio=v.split("=")[1]
                        mitmf_logger.debug("[SSLServerConnection][HSTS] Parsing cookie domain parameter: %s"%v)
                        real = self.urlMonitor.real
                        if dominio in real:
                            v=" Domain=%s"%real[dominio]
                            mitmf_logger.debug("[SSLServerConnection][HSTS] New cookie domain parameter: %s"%v)
                    newvalues.append(v)
                value = ';'.join(newvalues)

            if (key.lower() == 'access-control-allow-origin'):
                value='*'

        else:
            if (key.lower() == 'set-cookie'):
                value = SSLServerConnection.cookieExpression.sub("\g<1>", value)

        
        ServerConnection.handleHeader(self, key, value)
    def handleHeader(self, key, value):
        if self.hsts:
            if (key.lower() == 'set-cookie'):
                newvalues = []
                value = SSLServerConnection.cookieExpression.sub(
                    "\g<1>", value)
                values = value.split(';')
                for v in values:
                    if v[:7].lower() == ' domain':
                        dominio = v.split("=")[1]
                        log.debug("Parsing cookie domain parameter: %s" % v)
                        real = self.urlMonitor.real
                        if dominio in real:
                            v = " Domain=%s" % real[dominio]
                            log.debug("New cookie domain parameter: %s" % v)
                    newvalues.append(v)
                value = ';'.join(newvalues)

            if (key.lower() == 'access-control-allow-origin'):
                value = '*'

        else:
            if (key.lower() == 'set-cookie'):
                value = SSLServerConnection.cookieExpression.sub(
                    "\g<1>", value)

        ServerConnection.handleHeader(self, key, value)
Beispiel #5
0
    def __init__(self, nets=[]):
        super().__init__()

        self.imgToPredict = None
        self.imgToPredictClass = None
        self.imgPredictedClass = None
        self.imgTrueClassLabel = QLabel()
        self.imgPredictedClassLabel = QLabel()

        self.model = convModel()
        self.nets = nets

        host = 'localhost'
        serverPort = getServerPort()
        self.clientsInNet = getClientsNumber()
        self.firstPort = getTesterPort()

        self.serverConnection = ServerConnection(host, serverPort, serverPort + 1)

        self.clients = []
        self.accBtns = []
        for i in range(self.clientsInNet):
            self.clients.append(ClientConnection('client-' + str(i), self.firstPort + (i * 2),
                                                 self.firstPort + (i * 2) + 1, self))

        for client in self.clients:
            client.start()

        self.setWindowTitle('Federated learning controller')

        self.mainWidget = QWidget()

        self.setCentralWidget(self.mainWidget)

        qss_file = open('style.qss').read()
        self.centralWidget().setStyleSheet(qss_file)

        self.layout = QGridLayout()
        self.layout.setAlignment(Qt.AlignTop)

        self.mainWidget.setLayout(self.layout)

        self.__addDropdown(nets)
        self.addClientInfo()
        self.__addImageFrame()
        self.addTrainButton()
        self.addPredictImage()

        self.serverConnection.addQtControls(self.preTrainVal, self.postTrainVal, self.trainBtn, self.netBtn,
                                                self.accBtns, self.downloadBtn, self.currentModel, self.modelDownloadedAcc)
        self.serverConnection.addModelRef(self.model)
        self.serverConnection.setCallbacks(self.imageIsSet, lambda: self.predictChangeState(True))
        self.serverConnection.start()

        self.disableButtons()

        self.show()
Beispiel #6
0
def checkUsername(username):	
	notRegistered = isNotRegistered(username)
	if(notRegistered == True):
		if(app.yesNoBox("Register", "The username you chose isn't registered. \nWould you like to register it?") == False):
			return False,None
	rsa = RSA.load_pub_key('keys/key.pem')
	return True,ClientServer(ServerConnection('127.0.0.1',5000,None,rsa),username,not notRegistered)
Beispiel #7
0
 def __init__(self):
     print("Telldus Live! loading")
     self.email = ''
     self.supportedMethods = 0
     self.connected = False
     self.registered = False
     self.serverList = ServerList()
     Application().registerShutdown(self.stop)
     self.s = Settings('tellduslive.config')
     self.uuid = self.s['uuid']
     self.conn = ServerConnection()
     self.pingTimer = 0
     self.thread = threading.Thread(target=self.run)
     if self.conn.publicKey != '':
         # Only connect if the keys has been set.
         self.thread.start()
Beispiel #8
0
    def replaceSecureLinks(self, data):
        data = ServerConnection.replaceSecureLinks(self, data)
        data = self.replaceCssLinks(data)

        iterator = re.finditer(SSLServerConnection.linkExpression, data)

        for match in iterator:
            self.buildAbsoluteLink(match.group(10))

        return data
        def setup(self):
                try:
                        btResult=subprocess.call(["sudo","service","bluetooth","start"])
                        if btResult < 0:
                                print >>sys.stderr, "Child terminated by signal", -btResult
                        else:
                                print >>sys.stderr, "Child returned", btResult
                        piscan=subprocess.call(["sudo","hciconfig","hci0","piscan"])
                        if piscan < 0:
                                print >>sys.stderr, "Child terminated by signal", -piscan
                        else:
                                print >>sys.stderr, "Child returned", piscan
                except OSError as e:
                        print >>sys.stderr, "Execution failed", e

                self.server_sock=BluetoothSocket(RFCOMM)
                self.server_sock.bind(("",PORT_ANY))
                self.server_sock.listen(1)
                self.port=self.server_sock.getsockname()[1]
                self.uuid="00001101-0000-1000-8000-00805f9b34fb"
                advertise_service(self.server_sock,"decorations",service_id=self.uuid,service_classes=[self.uuid,SERIAL_PORT_CLASS],profiles=[SERIAL_PORT_PROFILE])
                led=Led(0,"leds",0)
                server=ServerConnection(self.server_sock)
                led.start()
                time.sleep(8)
		if server.accept() == True:
                        led.stop()
                        for i in range(5):
                                led.connect()
                led.starFade()
                state=1
                while True:
                        for case in switch(state):
                                if case(0):
                                        if server.accept()==True:
                                                state=1
                                                break
                                        else:
                                                state=0
                                                break
                                if case(1):
					if server.hasConnection()==False:
                                                state=0
                                                break
                                        else:
                                                data=server.listen()
                                                led.stop()
                                                led=Led(0,"leds",1,data)
                                                led.start()
                                                state=2
                                                break
                                if case(2):
                                        if server.hasConnection()==False:
                                                state=0
                                                break
                                        else:
						state=1
                                                break
Beispiel #10
0
 def __init__(self, command, uri, postData, headers, client):
     ServerConnection.__init__(self, command, uri, postData, headers, client)
     self.urlMonitor = URLMonitor.getInstance()
     self.hsts       = URLMonitor.getInstance().hsts
Beispiel #11
0
import datetime
import unittest
from ServerConnection import ServerConnection
from NoDataFoundError import NoDataFoundError
from NBATeams import NBATeams

date = datetime.date(2017, 12, 12)
server = ServerConnection("http://data.nba.net/data/10s/prod/v1", date)


class TestNBATeams(unittest.TestCase):
    def test_init_correct_server(self):
        self.assertIsNotNone(NBATeams(server))

    def test_get_team_wrong_name(self):
        try:
            NBATeams(server).get_team("Team")
        except NameError:
            pass

    def test_get_team_wrong_id(self):
        try:
            NBATeams(server).get_team(1)
        except NameError:
            pass

    def test_get_team_wrong_tricode(self):
        try:
            NBATeams(server).get_team("ABC")
        except NameError:
            pass
Beispiel #12
0
    def handleHeader(self, key, value):
        if (key.lower() == 'set-cookie'):
            value = SSLServerConnection.cookieExpression.sub("\g<1>", value)

        ServerConnection.handleHeader(self, key, value)
Beispiel #13
0
 def __init__(self, command, uri, postData, headers, client):
     ServerConnection.__init__(self, command, uri, postData, headers,
                               client)
Beispiel #14
0
    def handleHeader(self, key, value):
        if (key.lower() == 'set-cookie'):
            value = SSLServerConnection.cookieExpression.sub("\g<1>", value)

        ServerConnection.handleHeader(self, key, value)
Beispiel #15
0
class TelldusLive(Plugin):
    observers = ObserverCollection(ITelldusLiveObserver)

    def __init__(self):
        print("Telldus Live! loading")
        self.email = ''
        self.supportedMethods = 0
        self.connected = False
        self.registered = False
        self.serverList = ServerList()
        Application().registerShutdown(self.stop)
        self.s = Settings('tellduslive.config')
        self.uuid = self.s['uuid']
        self.conn = ServerConnection()
        self.pingTimer = 0
        self.thread = threading.Thread(target=self.run)
        if self.conn.publicKey != '':
            # Only connect if the keys has been set.
            self.thread.start()

    @mainthread
    def handleMessage(self, message):
        if (message.name() == "notregistered"):
            self.email = ''
            self.connected = True
            self.registered = False
            params = message.argument(0).dictVal
            self.s['uuid'] = params['uuid'].stringVal
            print(
                "This client isn't activated, please activate it using this url:\n%s"
                % params['url'].stringVal)
            self.observers.liveConnected()
            return

        if (message.name() == "registered"):
            self.connected = True
            self.registered = True
            data = message.argument(0).toNative()
            if 'email' in data:
                self.email = data['email']
            self.observers.liveRegistered(data)
            return

        if (message.name() == "command"):
            # Extract ACK and handle it
            args = message.argument(0).dictVal
            if 'ACK' in args:
                msg = LiveMessage("ACK")
                msg.append(args['ACK'].intVal)
                self.send(msg)

        if (message.name() == "pong"):
            return

        if (message.name() == "disconnect"):
            self.conn.close()
            self.__disconnected()
            return

        handled = False
        for o in self.observers:
            for f in getattr(o, '_telldusLiveHandlers',
                             {}).get(message.name(), []):
                f(o, message)
                handled = True
        if not handled:
            print "Did not understand: %s" % message.toByteArray()

    def isConnected(self):
        return self.connected

    def isRegistered(self):
        return self.registered

    def run(self):
        self.running = True

        wait = 0
        pongTimer, self.pingTimer = (0, 0)
        while self.running:
            if wait > 0:
                wait = wait - 1
                time.sleep(1)
                continue
            state = self.conn.process()
            if state == ServerConnection.CLOSED:
                server = self.serverList.popServer()
                if not server:
                    wait = random.randint(60, 300)
                    print("No servers found, retry in %i seconds" % wait)
                    continue
                if not self.conn.connect(server['address'], int(
                        server['port'])):
                    wait = random.randint(60, 300)
                    print("Could not connect, retry in %i seconds" % wait)

            elif state == ServerConnection.CONNECTED:
                pongTimer, self.pingTimer = (time.time(), time.time())
                self.__sendRegisterMessage()

            elif state == ServerConnection.MSG_RECEIVED:
                msg = self.conn.popMessage()
                if msg is None:
                    continue
                pongTimer = time.time()
                self.handleMessage(msg)

            elif state == ServerConnection.DISCONNECTED:
                wait = random.randint(10, 50)
                print("Disconnected, reconnect in %i seconds" % wait)
                self.__disconnected()

            else:
                if (time.time() - pongTimer >= 360):  # No pong received
                    self.conn.close()
                    wait = random.randint(10, 50)
                    print(
                        "No pong received, disconnecting. Reconnect in %i seconds"
                        % wait)
                    self.__disconnected()
                elif (time.time() - self.pingTimer >= 120):
                    # Time to ping
                    self.conn.send(LiveMessage("Ping"))
                    self.pingTimer = time.time()

    def stop(self):
        self.running = False

    def send(self, message):
        self.conn.send(message)
        self.pingTimer = time.time()

    def pushToWeb(self, module, action, data):
        msg = LiveMessage("sendToWeb")
        msg.append(module)
        msg.append(action)
        msg.append(data)
        self.send(msg)

    def __disconnected(self):
        self.email = ''
        self.connected = False
        self.registered = False

        def sendNotification():
            self.observers.liveDisconnected()

        # Syncronize signal with main thread
        Application().queue(sendNotification)

    @staticmethod
    def handler(message):
        def call(fn):
            import sys
            frame = sys._getframe(1)
            frame.f_locals.setdefault('_telldusLiveHandlers',
                                      {}).setdefault(message, []).append(fn)
            return fn

        return call

    def __sendRegisterMessage(self):
        print("Send register")
        msg = LiveMessage('Register')
        msg.append({
            'key': self.conn.publicKey,
            'mac': TelldusLive.getMacAddr(Board.networkInterface()),
            'secret': Board.secret(),
            'hash': 'sha1'
        })
        msg.append({
            'protocol': 2,
            'version': Board.firmwareVersion(),
            'os': 'linux',
            'os-version': 'telldus'
        })
        self.conn.send(msg)

    @staticmethod
    def getMacAddr(ifname):
        addrs = netifaces.ifaddresses(ifname)
        try:
            mac = addrs[netifaces.AF_LINK][0]['addr']
        except IndexError, KeyError:
            return ''
        return mac.upper().replace(':', '')
 def __init__(self, command, uri, postData, headers, client):
     ServerConnection.__init__(self, command, uri, postData, headers,
                               client)
     self.urlMonitor = URLMonitor.getInstance()
     self.hsts = URLMonitor.getInstance().hsts
Beispiel #17
0
from ClientServer import ClientServer
from M2Crypto import RSA
from ServerConnection import ServerConnection
from Connection import Connection
from socket import socket, gethostbyname, AF_INET, SOCK_DGRAM

from random import randint
from P2PChat import P2PChat

rsa = RSA.load_pub_key('keys/key.pem')
username = '******'
c = ClientServer(ServerConnection('127.0.0.1', 5000, None, rsa), username,
                 True)
c.close()
mySocket = socket(AF_INET, SOCK_DGRAM)
hostName = gethostbyname('0.0.0.0')
mySocket.bind((hostName, 5002))
msg, addr = mySocket.recvfrom(2048)
mySocket.close()
p = P2PChat()
p.gotChatRequest(addr[0], addr[1], msg, c.getKey())
p.LoadChat()
Beispiel #18
0
 def run(self):
     self.serverConnection = ServerConnection(self.onMasterEvent)
     self.serverConnection.client.call("players", [], self.startPlayers)
 def handleHeader(self, key, value):
     ServerConnection.handleHeader(self, key, value)
Beispiel #20
0
class MainApp(QMainWindow):
    def __init__(self, nets=[]):
        super().__init__()

        self.imgToPredict = None
        self.imgToPredictClass = None
        self.imgPredictedClass = None
        self.imgTrueClassLabel = QLabel()
        self.imgPredictedClassLabel = QLabel()

        self.model = convModel()
        self.nets = nets

        host = 'localhost'
        serverPort = getServerPort()
        self.clientsInNet = getClientsNumber()
        self.firstPort = getTesterPort()

        self.serverConnection = ServerConnection(host, serverPort, serverPort + 1)

        self.clients = []
        self.accBtns = []
        for i in range(self.clientsInNet):
            self.clients.append(ClientConnection('client-' + str(i), self.firstPort + (i * 2),
                                                 self.firstPort + (i * 2) + 1, self))

        for client in self.clients:
            client.start()

        self.setWindowTitle('Federated learning controller')

        self.mainWidget = QWidget()

        self.setCentralWidget(self.mainWidget)

        qss_file = open('style.qss').read()
        self.centralWidget().setStyleSheet(qss_file)

        self.layout = QGridLayout()
        self.layout.setAlignment(Qt.AlignTop)

        self.mainWidget.setLayout(self.layout)

        self.__addDropdown(nets)
        self.addClientInfo()
        self.__addImageFrame()
        self.addTrainButton()
        self.addPredictImage()

        self.serverConnection.addQtControls(self.preTrainVal, self.postTrainVal, self.trainBtn, self.netBtn,
                                                self.accBtns, self.downloadBtn, self.currentModel, self.modelDownloadedAcc)
        self.serverConnection.addModelRef(self.model)
        self.serverConnection.setCallbacks(self.imageIsSet, lambda: self.predictChangeState(True))
        self.serverConnection.start()

        self.disableButtons()

        self.show()

    def addClientInfo(self):
        clientWidget = QWidget()
        clientLayout = QVBoxLayout()
        clientWidget.setLayout(clientLayout)
        # clientLayout.setAlignment(Qt.AlignTop)

        clientsInNet = QWidget()
        clientNumberLayout = QHBoxLayout()
        clientNumberLayout.setContentsMargins(0, 0, 0, 0)
        clientsInNet.setLayout(clientNumberLayout)
        clientNumberLayout.addWidget(QLabel("Clients in symulation: " + str(self.clientsInNet)))

        clientLayout.addWidget(clientsInNet)

        for client in self.clients:
            clientLayout.addWidget(self.__addClientPanel(client))

        self.layout.addWidget(clientWidget, 1, 0)

    def addTrainButton(self):
        trainWidget = QWidget()
        trainLayout = QVBoxLayout()
        trainWidget.setLayout(trainLayout)

        trainingDetailsWidget = QWidget()
        trainDetLayout = QHBoxLayout()
        trainingDetailsWidget.setLayout(trainDetLayout)

        preLabel = QLabel('Pre train:')
        self.preTrainVal = QLabel()
        postLabel = QLabel('Post train:')
        self.postTrainVal = QLabel()

        trainDetLayout.addWidget(preLabel)
        trainDetLayout.addWidget(self.preTrainVal)
        trainDetLayout.addWidget(postLabel)
        trainDetLayout.addWidget(self.postTrainVal)

        def handleTrain():
            self.disableButtons()
            for client in self.clients:
                client.setServerStatusText('-')
                client.setAccuracyText('-')
                client.send([ComCodes.RETRAIN_MODEL])

        ########################################################### Add click event! Done
        btnWidget = QWidget()
        btnLayout = QHBoxLayout()
        btnLayout.setContentsMargins(0, 0, 0, 0)
        btnWidget.setLayout(btnLayout)
        self.trainBtn = QPushButton('Train')
        self.trainBtn.setFixedWidth(100)
        self.trainBtn.clicked.connect(handleTrain)
        btnLayout.addWidget(self.trainBtn)

        trainLayout.addWidget(QLabel('Accuracy:'))
        trainLayout.addWidget(trainingDetailsWidget)
        trainLayout.addWidget(btnWidget)

        self.layout.addWidget(trainWidget, 3, 0)

    def addPredictImage(self):
        predictWidget = QWidget()
        predictLayout = QVBoxLayout()
        predictLayout.setAlignment(Qt.AlignTop)

        predictWidget.setLayout(predictLayout)

        resultWidget = QWidget()
        resultLayout = QHBoxLayout()
        resultWidget.setLayout(resultLayout)

        trueClass = QLabel('True: ')
        predictedClass = QLabel('Predicted: ')
        resultLayout.addWidget(trueClass)
        resultLayout.addWidget(self.imgTrueClassLabel)
        resultLayout.addWidget(predictedClass)
        resultLayout.addWidget(self.imgPredictedClassLabel)

        def predict():
            im = self.pixmapToPIL()
            prediction = self.model.predict(im)
            self.imgPredictedClassLabel.setText(prediction)

        btnWidget = QWidget()
        btnWidget.setFixedWidth(255)
        btnLayout = QHBoxLayout()
        btnLayout.setContentsMargins(0, 0, 0, 0)
        btnWidget.setLayout(btnLayout)
        self.predictBtn = QPushButton('Predict')
        self.predictBtn.clicked.connect(predict)
        self.predictBtn.setFixedWidth(100)
        self.predictBtn.setEnabled(False)
        btnLayout.addWidget(self.predictBtn)

        predictLayout.addWidget(QLabel('Class:'))
        predictLayout.addWidget(resultWidget)
        predictLayout.addWidget(btnWidget)

        self.layout.addWidget(predictWidget, 3, 1)

    def __addDownloadModel(self):
        downloadWidget = QWidget()
        downloadLayout = QVBoxLayout()
        downloadLayout.setAlignment(Qt.AlignTop)
        downloadWidget.setLayout(downloadLayout)
        downloadWidget.setFixedWidth(255)
        downloadLayout.setContentsMargins(0, 0, 0, 0)

        downloadLayout.addWidget(QLabel('Model:'))

        modelDetailsWidget = QWidget()
        modelDetailsLayout = QHBoxLayout()
        modelDetailsWidget.setLayout(modelDetailsLayout)

        modelDetailsLayout.addWidget(QLabel('Current: '))
        self.currentModel = QLabel('-')
        modelDetailsLayout.addWidget(self.currentModel)

        modelDetailsLayout.addWidget(QLabel('Accuracy:'))
        self.modelDownloadedAcc = QLabel('-')
        modelDetailsLayout.addWidget(self.modelDownloadedAcc)

        def download():
            self.serverConnection.send([ComCodes.GET_STRUCTURE])

        ################################################################ Add event!
        self.downloadBtn = QPushButton('Download')
        self.downloadBtn.setFixedWidth(100)
        self.downloadBtn.clicked.connect(download)
        downloadBtnWidget = QWidget()
        downloadBtnLayout = QHBoxLayout()
        downloadBtnWidget.setLayout(downloadBtnLayout)
        downloadBtnLayout.addWidget(self.downloadBtn)

        downloadLayout.addWidget(modelDetailsWidget)
        downloadLayout.addWidget(downloadBtnWidget)

        return downloadWidget

    def __addClientPanel(self, conn):

        panel = QWidget()
        panelLayout = QVBoxLayout()
        panelLayout.setContentsMargins(0, 5, 0, 0)
        panelLayout.setAlignment(Qt.AlignTop)
        panel.setLayout(panelLayout)

        nameWidget = QWidget()
        nameLayout = QHBoxLayout()
        nameWidget.setLayout(nameLayout)
        nameLayout.setContentsMargins(0, 0, 0, 0)
        nameLayout.setAlignment(Qt.AlignTop)

        name = QLabel(conn.getName())
        server = QLabel('Server:')
        self.serverStatus = QLabel('-')
        nameLayout.addWidget(name)
        nameLayout.addWidget(server)
        nameLayout.addWidget(self.serverStatus)

        connection = QLabel('Connection: ' + conn.getConnectionDetails())

        accWidget = QWidget()
        accLayout = QHBoxLayout()
        accLayout.setContentsMargins(0, 0, 0, 0)
        accWidget.setLayout(accLayout)

        accuracy = QLabel('Accuracy: -')
        accLayout.addWidget(accuracy)

        conn.setQLabels(accuracy, self.serverStatus)

        getAccBtn = QPushButton('Update model')
        getAccBtn.setFixedWidth(100)
        getAccBtn.clicked.connect(lambda: conn.send([ComCodes.GET_ACCURACY]))
        accLayout.addWidget(getAccBtn)
        self.accBtns.append(getAccBtn)

        bottomLine = QFrame()
        bottomLine.setFixedHeight(1)
        bottomLine.setFrameStyle(1)

        panelLayout.addWidget(nameWidget)
        panelLayout.addWidget(connection)
        panelLayout.addWidget(accWidget)
        panelLayout.addWidget(bottomLine)

        return panel

    def __addDropdown(self, options):
        netSelect = QWidget()
        dropdownLayout = QHBoxLayout()
        # dropdownLayout.setAlignment(Qt.AlignTop)
        dropdownLayout.setContentsMargins(0, 0, 0, 0)
        netSelect.setFixedHeight(30)
        netSelect.setLayout(dropdownLayout)

        netLabel = QLabel("Selected network: ")
        netLabel.setFixedWidth(100)
        dropdownLayout.addWidget(netLabel)

        netsDropdown = QComboBox()
        netsDropdown.addItems(options)
        netsDropdown.setFixedWidth(50)
        dropdownLayout.addWidget(netsDropdown)

        def handleSetNet(net):
            self.disableButtons()
            print('sending')
            self.serverConnection.send([ComCodes.LOAD_MODEL, net])

        ############################################################# Add click event!
        self.netBtn = QPushButton("Set/ Reset Net")
        self.netBtn.setFixedWidth(100)
        self.netBtn.clicked.connect(lambda: handleSetNet(netsDropdown.currentText()))
        dropdownLayout.addWidget(self.netBtn)

        self.layout.addWidget(netSelect, 0, 0)

    def __addImageFrame(self):
        imageWidget = QWidget()
        widgetLayout = QVBoxLayout()
        widgetLayout.setAlignment(Qt.AlignTop)
        imageWidget.setLayout(widgetLayout)
        widgetLayout.addWidget(QLabel('Image to predict:'))

        self.imgFrame = QFrame()
        self.imgFrame.setFrameStyle(1)
        self.imgFrame.setFixedWidth(255)
        self.imgFrame.setFixedHeight(255)
        imgFrameLayout = QGridLayout()
        imgFrameLayout.setContentsMargins(0, 0, 0, 0)
        self.imgFrame.setLayout(imgFrameLayout)
        widgetLayout.addWidget(self.imgFrame)

        self.image = QLabel()
        self.__showImage()
        imgFrameLayout.addWidget(self.image)

        imageBtnWidget = QWidget()
        imageBtnWidget.setFixedWidth(255)
        imageBtnLayout = QHBoxLayout()
        imageBtnWidget.setLayout(imageBtnLayout)

        loadImageBtn = QPushButton("Open Image")
        loadImageBtn.setFixedWidth(100)
        loadImageBtn.clicked.connect(self.loadImg)
        imageBtnLayout.addWidget(loadImageBtn)

        clearImageBtn = QPushButton("Clear Image")
        clearImageBtn.setFixedWidth(100)
        clearImageBtn.clicked.connect(self.__clearImage)
        imageBtnLayout.addWidget(clearImageBtn)

        download = self.__addDownloadModel()

        widgetLayout.addWidget(imageBtnWidget)
        widgetLayout.addWidget(download)
        self.layout.addWidget(imageWidget, 0, 1, 2, 2)

    def setImgClass(self, classLabel):
        self.imgToPredictClass = classLabel
        if classLabel is None:
            self.imgTrueClassLabel.setText('-')
        else:
            self.imgTrueClassLabel.setText(classLabel)
        self.imgPredictedClassLabel.setText('-')

    def __showImage(self):
        if self.imgToPredict is not None and self.imgToPredict != '':
            self.image.setPixmap(QPixmap(self.imgToPredict).scaled(255, 255))
            iClass = self.imgToPredict.split('/')
            iClass = iClass[len(iClass) - 2]
            self.setImgClass(iClass)
        else:
            self.setImgClass(None)
            self.image.setText('No image selected.')
            self.image.setAlignment(Qt.AlignCenter)

    def pixmapToPIL(self):
        pmap = self.image.pixmap()
        imageBuffer = QBuffer()
        imageBuffer.open(QBuffer.ReadWrite)
        pmap.save(imageBuffer, "PNG")
        img = Image.open(io.BytesIO(imageBuffer.data()))
        return img

    def loadImg(self):
        img = QFileDialog.getOpenFileName(self, 'Open File', r'C:\Users\Pawel\Studia\inz\COVID-19 Radiography Database',
                                          'Image files (*.png *.jpg)')
        self.imgToPredict = img[0]
        if self.currentModel != '-':
            self.predictChangeState(True)
        self.__showImage()

    def __clearImage(self):
        self.imgToPredict = None
        self.predictChangeState(False)
        self.__showImage()

    def imageIsSet(self):
        return self.imgToPredict is not None

    def disableUpdateBtns(self):
        for btn in self.accBtns:
            btn.setEnabled(False)

    def disableButtons(self):
        self.disableUpdateBtns()
        self.trainBtn.setEnabled(False)
        self.netBtn.setEnabled(False)
        self.downloadBtn.setEnabled(False)

    def predictChangeState(self, state):
        self.predictBtn.setEnabled(state)
 def test_wrong_server_get_web_source(self):
     try:
         ServerConnection.get_web_source("http://2017/teams.json")
     except ConnectionError:
         pass
def main():
    """
    Main program loop. Sets up the connections to the AVR and the server, then
    reads key presses and sends them to the server.
    """
    avr = AVRChip()
    avr.reset_keys() # clear the key buffer on the AVR
    avr.led_off()
    logger = Logger()

    try:
        with open('/etc/device_key.txt', 'r') as f:
            for line in f:
                line = line.strip()
                DEVICE_KEY = line
    except IOError:
        DEVICE_KEY = '12345'

    try:
        server = ServerConnection(logger, DEVICE_KEY)
        
        server.connect()

        buf = ""

        while (True):
            c = avr.read_key()

            # read_key() will always return a character. NULL means no new
            # key presses.
            if c == '\0':
                time.sleep(0.1)
                continue

            buf += c

            logger.info('KEY PRESSED - ' + str(c) + '\n')

            # maximum size to avoid running out of memory
            if len(buf) > 16:
                logger.error('Reached maximum buffer size')
                buf = ""

            if c == '#':
                # These are the cases where we should continue reading input.
                # Otherwise, the # character always terminates the input.
                if buf in ('*#', '*#*#', '*#*#*#'):
                    continue

                if buf == '*#*#*#*#':
                    if server.register_device():
                        logger.info('Registration successful')
                        avr.avr_indicate_success()
                    else:
                        logger.info('Registration unsuccessful')
                        avr.avr_indicate_failure()
                elif len(buf) == 7:
                    password = buf[:6]

                    if server.open_door(password):
                        logger.info('Door open successful')
                        avr.avr_indicate_success()
                    else:
                        logger.info('Door open unsuccessful')
                        avr.avr_indicate_failure()
                elif len(buf) == 14:
                    if buf[6] != '*':
                        logger.error('Invalid entry')
                        buf = ""
                        continue

                    current_password = buf[:6]
                    new_password = buf[7:-1]

                    if server.change_password(current_password, new_password):
                        logger.info('Password change successful')
                        avr.avr_indicate_success()
                    else:
                        logger.info('Password change unsuccessful')
                        avr.avr_indicate_failure()
                elif len(buf) == 16:
                    if buf[8] != '*':
                        logger.error('Invalid entry')
                        buf = ""
                        continue

                    master_password = buf[:8]
                    new_password = buf[9:-1]

                    if server.change_password_master(master_password, new_password):
                        logger.info('Password change successful')
                        avr.avr_indicate_success()
                    else:
                        logger.info('Password change unsuccessful')
                        avr.avr_indicate_failure()
                else:
                    logger.error('Invalid entry')

                buf = ""
    except KeyboardInterrupt:
        pass
    finally:
        logger.close()
Beispiel #23
0
 def __init__(self):
     server = ServerConnection()
     server.open_port()
Beispiel #24
0
        gpgnets[playerId].processMessage(args[0], args[1])
    elif event == 'onIceAdapterOutput':
        print("onIceAdapterOutput {}: {}".format(playerId, args))
    elif event == 'onIceOnConnected':
        print("onIceOnConnected {}: {}".format(playerId, args))
    elif event == 'onIceOnGpgNetMessageReceived':
        print("onIceOnGpgNetMessageReceived {}: {}".format(playerId, args))
    elif event == 'onIceOnIceMsg':
        print("onIceOnIceMsg {}: {}".format(playerId, args))
    else:
        print("unhandled master event: {}".format((event, playerId, args)))


def startPlayers(playerList):
    global host_id, host_login
    print("{} players ready".format(len(playerList)))
    for i, player in enumerate(playerList):
        if i == 0:
            host_id = player["id"]
            host_login = player["login"]
        clients[player["id"]] = TestClient(c, player["id"], player["login"],
                                           i == 0)
        clients[player["id"]].call("status",
                                   callback_result=initIceAdapterForPlayer)


c = ServerConnection(onMasterEvent)
c.client.call("players", [], startPlayers)

app.exec_()
Beispiel #25
0
 def __init__(self, command, uri, postData, headers, client):
     ServerConnection.__init__(self, command, uri, postData, headers, client)
Beispiel #26
0
def set_data():
    while True:
        while True:
            curses.start_color()
            curses.init_pair(2, curses.COLOR_CYAN, curses.COLOR_BLACK)

            stdscr.clear()
            height, width = stdscr.getmaxyx()

            stdscr.clear()
            stdscr.refresh()
            msg = "Enter year:"
            start_x = int((width // 2) - (len(msg) // 2) - len(msg) % 2)
            start_y = int((height // 2) - 2)
            stdscr.addstr(start_y, start_x, msg)

            tmp = stdscr.getstr(start_y, start_x + len(msg), 4)
            try:
                year = int(tmp.decode("utf-8"))
            except ValueError:
                msg = "Please enter valid year"
                print_middle(msg)
                stdscr.getch()
                continue
            if year < 2000 or year > today_date.year:
                msg = "Wrong year :("
                print_middle(msg)
                stdscr.getch()
                continue
            break

        while True:
            msg = "Enter month:"
            start_x = int((width // 2) - (len(msg) // 2) - len(msg) % 2)
            start_y = int((height // 2) - 2)
            stdscr.clear()
            stdscr.refresh()
            stdscr.addstr(start_y, start_x, msg)

            tmp = stdscr.getstr(start_y, start_x + len(msg), 2)
            try:
                month = int(tmp.decode("utf-8"))
            except ValueError:
                msg = "Please enter valid month"
                print_middle(msg)
                stdscr.getch()
                continue
            if month <= 0 or month > 12:
                msg = "Wrong month :("
                print_middle(msg)
                stdscr.getch()
                continue
            break

        while True:
            msg = "Enter day:"
            start_x = int((width // 2) - (len(msg) // 2) - len(msg) % 2)
            start_y = int((height // 2) - 2)
            stdscr.clear()
            stdscr.refresh()
            stdscr.addstr(start_y, start_x, msg)

            tmp = stdscr.getstr(start_y, start_x + len(msg), 4)
            try:
                day = int(tmp.decode("utf-8"))
            except ValueError:
                msg = "Please enter valid day"
                print_middle(msg)
                stdscr.getch()
                continue

            if day <= 0 or day > 31:
                msg = "Wrong day :("
                print_middle(msg)
                stdscr.getch()
                continue
            break

        try:
            global date
            date = datetime.date(year, month, day)
            global server
            server = ServerConnection(server_name, date)
        except ValueError:
            msg = "Wrong date, please check and try again :("
            print_middle(msg)
            stdscr.getch()
            continue
        break
 def handleHeader(self, key, value):
     ServerConnection.handleHeader(self, key, value)