Exemple #1
0
    def sendData(self, uuid, **values):
        """Issue a set command for network."""
        if "uploadSpeed" in values:
            self.uploadSpeed = float(values["uploadSpeed"])
        else:
            raise Exception('Missing parameter "uploadSpeed" for network',
                            ' set command')
            return
			
        if "downloadSpeed" in values:
            self.downloadSpeed = float(values["downloadSpeed"])
        else:
            raise Exception('Missing parameter "downloadSpeed" for network'
                            ' set command')
            return
		
        if "pingTime" in values:
            self.pingTime = float(values["pingTime"])
        else:
            raise Exception('Missing parameter "pingTime" for network'
                            ' set command')
            return

        if "averageRssiDb" in values:
            self.rssi = float(values["averageRssiDb"])
        else:
            raise Exception('Missing parameter "pingTime" for network '
                            'set command')
            return
		
        self.sendLine(('network delay %.5f' % (self.pingTime)).encode('UTF-8'))
        self.sendLine(('network speed  %.5f:%.5f' % (self.uploadSpeed, self.downloadSpeed)).encode('UTF-8'))
        feed.log('NETWORKPROTOCOL-sendData','sent string: netdelay and netspeed %.5f %.5f %.2f %d' % (self.uploadSpeed, self.downloadSpeed, self.pingTime, self.rssi))
        feed.log('NETWORKPROTOCOL-sendData',self.sensorName+" End Time:"+str("%0.20f" % time.time())+" "+str(uuid))
    def sendData(self, uuid, **values):
        """Issue a set command for magnetic-field sensor."""
        if "x" in values:
            self.x = float(values["x"])
        else:
            raise Exception('Missing parameter "x" for network set command')
            return

        if "y" in values:
            self.y = float(values["y"])
        else:
            raise Exception(
                'Missing parameter "downloadSpeed" for network set command')
            return

        if "z" in values:
            self.z = float(values["z"])
        else:
            raise Exception(
                'Missing parameter "pingTime" for network set command')
            return
        command = 'sensor set magnetic-field %.5f:%.5f:%.5f' % (self.x, self.y,
                                                                self.z)
        self.sendLine(command.encode('UTF-8'))

        feed.log('MAGNETICFIELDPROTOCOL-sendData', command)
        feed.log(
            'MAGNETICFIELDPROTOCOL-sendData', self.sensorName + " End Time:" +
            str("%0.20f" % time.time()) + " " + str(uuid))
Exemple #3
0
 def sendValue(self, uuid, **value):
     """Send sensor value to the sensor."""
     if self.connectedProtocol is None:
         raise DisconnectedModalityError('Could not set value - sensor "%s" is not connected!' % self.sensor)
     feed.log('ANDROIDSENSORCLIENTFACTORY-sendValue',str(self.connectedProtocol.sensorName+" Begin Time:"+str("%0.20f" % time.time())+" "+str(uuid)))
     feed.log('ANDROIDSENSORCLIENTFACTORY-sendValue',str(self.connectedProtocol))
     feed.log('ANDROIDSENSORCLIENTFACTORY-sendValue-timestamp:',str(uuid)+" "+str("%0.20f" % time.time()))
     feed.log('ANDROIDSENSORCLIENTFACTORY-sendValue-timestamp:',str(value))
     self.connectedProtocol.sendData(uuid, **value)
Exemple #4
0
    def buildProtocol(self, address):
        """Build protocol and store it in this instance."""
        proto = ClientFactory.buildProtocol(self, address)
        self.connectedProtocol = proto
        return proto

        def connectionLost(self, reason):
            """Handle connection loss."""
        self.connectedProtocol = None
        feed.log('ANDROIDSENSORCLIENTFACTORY-connectionLost','%d - Connection to sensor %s was terminated: %s' % time.time(), self.sensor, reason.getErrorMessage())

        def clientConnectionFailed(self, connector, reason):
            """Handle a failed connection attempt to the sensor."""
        if not self.deferred is None:
            d, self.deferred = self.deferred, None
            d.errback(Failure(Exception('Connection to %s sensor failed' % self.sensor)))
        else:
            contextmonkeyreactor.stop()
            raise SystemError('The connection attempt to %s sensor failed more than once. Check if emulator is running' % self.sensor)
Exemple #5
0
 def connectSensor(self, sensor, uuid, **value):
     """Connect to sensor in Android emulator."""
     clModule = __import__(
         "contextmonkey.feedlayer.emulators.%s.%sSensorClientFactory" %
         (self.emulator.emulatorname.lower(),
          self.emulator.emulatorname.capitalize()),
         fromlist=[
             "%sSensorClientFactory" %
             self.emulator.emulatorname.capitalize()
         ])
     clFactory = getattr(
         clModule,
         "%sSensorClientFactory" % self.emulator.emulatorname.capitalize())
     d = defer.Deferred()
     factory = clFactory(sensor, d)
     d.addCallback(self.connectSensorCallback, sensor, factory, **value)
     d.addErrback(self.connectSensorErrback)
     d.callback(uuid)
     reactor.connectTCP(self.host, self.port, factory)
     feed.log("ANDROIDCONNECTION-connectSensor", "%s connected" % sensor)
    def setModalityValue(self, modality):
        '''issues commands to set a specific sensor to a specific value 
        connects to a sensor, if no connection is present
        '''
        modality.value=dict(self.feedformatmanager.fromProtobuf(modality))
        if self.emulator is None:
            raise SystemError('You must first register an emulator with the EmulatorManager!')
        if modality.name in self.emulator.activeSensors:
            try:
                feed.log("EMULATORMANAGER-setModalityValue",str(self.emulator.activeSensors[modality.name]))
                self.emulator.activeSensors[modality.name].sendValue(modality.modalityid,**modality.value)
                feed.log("EmulatorManager-filterData-timestamp:",str("%0.20f" % time.time())+" "+str(modality.modalityid)) 
            except DisconnectedModalityError:
                feed.log("EMULATORMANAGER-setModalityValue",'Sensor %s seems to be disconnected, removing ...' % modality.name)
                self.emulatorconnection.disconnectSensor(modality.name)
            except:
                feed.log("EMULATORMANAGER-setModalityValue",traceback.format_exc())

        else:
            feed.log("EMULATORMANAGER-setModalityValue","Connecting to "+modality.name+".......")
            self.emulatorconnection.connectSensor(modality.name, modality.modalityid, **modality.value)
Exemple #7
0
 def sendData(self, uuid, **values):
     """Issue set command for setting GPS sensor."""
     if "longitude" in values:
         self.longitude = values["longitude"]
     else:
         raise Exception('Missing parameter "longitude" for GPS set command')
         return
     if "latitude" in values:
         self.latitude = values["latitude"]
     else:
         raise Exception('Missing parameter "latitude" for GPS set command')
         return
     if "altitude" in values:
         self.altitude = values["altitude"]
     else:
         self.altitude = 0
     if "satellites" in values:
         self.satellites = values["satellites"]
     else:
         self.satellites = 5
     command='sending string: geo fix %.5f %.5f %.2f %d' % (self.longitude, self.latitude, self.altitude, self.satellites)
     self.sendLine(command.encode('UTF-8'))
     feed.log('GPSPROTOCOL-sendData', command)
     feed.log('GPSPROTOCOL-sendData',self.sensorName+" End Time:"+str("%0.20f" % time.time())+" "+str(uuid))
Exemple #8
0
    def registerEmulator(self, emulator):
        """Register emulator with the framework."""
        if ':' not in self.emulator.connection['addr']:
            self.host = '127.0.0.1'
            self.port = self.emulator.connection['addr']
        else:
            self.host, self.port = self.emulator.connection['addr'].split(
                ':', 1)

        self.port = int(self.port)
        feed.log("ANDROIDCONNECTION-registerEmulator",
                 'Registered emulator with data:')
        feed.log("ANDROIDCONNECTION-registerEmulator", 'Host: %s' % self.host)
        feed.log("ANDROIDCONNECTION-registerEmulator", 'Port: %d' % self.port)
        return True
Exemple #9
0
 def setSensorValue(self, sensor, uuid, **value):
     """Issue a set command to sensor on Android emulator."""
     if self.emulator is None:
         raise SystemError(
             'You must first register an emulator with the EmulatorManager!'
         )
     if sensor in self.emulator.activeSensors:
         try:
             self.emulator.activeSensors[sensor].sendValue(uuid, **value)
             feed.log("ANDROIDCONNECTION-setSensorValue", str(uuid))
         except DisconnectedModalityError:
             feed.log(
                 "ANDROIDCONNECTION-setSensorValue",
                 'Sensor %s seems to be disconnected, removing ...' %
                 sensor)
             self.disconnectSensor(sensor)
         except:
             feed.log("ANDROIDCONNECTION-setSensorValue",
                      traceback.print_exc())
     else:
         self.connectSensor(sensor, **value)
Exemple #10
0
    def sendData(self, uuid, **values):
        """Issue a set command for camera sensor."""
        try:
            if "image" in values:
                self.image = base64.b64encode(values["image"])
            else:
                raise Exception(
                    'Missing parameter "image" for Camera set command')
                return
            if "encoded" in values:
                self.encoded = values["encoded"]
                self.encoded = len(self.image)
            else:
                raise Exception(
                    'Missing parameter "encoded" size for Camera set command')
                return
            if "original" in values:
                self.original = values["original"]
                self.original = len(values["image"])
            else:
                raise Exception(
                    'Missing parameter "original" size for Camera set command')
                return
            if "camera_type" in values:
                self.camera_type = values["camera_type"]
                self.camera_type = 'back'
            else:
                self.camera_type = "front"

            command = "camera set {} {} {} {}".format(
                self.camera_type, self.original, self.encoded,
                self.image.decode('utf-8'))
            #self.sendLine(command.encode())
            feed.log('CAMERAPROTOCOL-sendData', command)
            feed.log(
                'CAMERAPROTOCOL-sendData', self.sensorName + " timestamp:" +
                str("%0.20f" % time.time()) + " " + str(uuid))

        except:
            feed.log('CAMERAPROTOCOL-sendData', traceback.print_exc())
Exemple #11
0
 def connectSensorErrback(self, reason):
     """Handle error in connectSensor."""
     feed.log(
         "ANDROIDCONNECTION-connectSensorErrback",
         'Connection attempt to sensor failed: %s' %
         (reason.getErrorMessage()))
Exemple #12
0
 def commandFailed(self, reason):
     """Handle a failed set/get command prototype."""
     feed.log('ANDROIDSENSORCLIENTFACTORY-commandFailed',str(reason))