Exemple #1
0
def main():
    deviceName, key, sensorName, channelName = sys.argv[1:]

    device = sensorcloud.Device(deviceName, key)
    sensor = device.sensor(sensorName)
    channel = sensor.channel(channelName)

    channel.timeseries_append(sensorcloud.SampleRate.seconds(10),
                              [sensorcloud.Point(timeNow(), 10.5)])
    def test_truncatedUpload(self):
        uploadRequest = Mock()
        uploadRequest.doRequest = Mock()
        uploadRequest.status_code = 400
        uploadRequest.reason = ""
        uploadRequest.text = '{"errorcode": "400-038", "message": ""}'
        sensorcloud.webrequest.Requests.Request = Mock()
        sensorcloud.webrequest.Requests.Request.side_effect = [authRequest(), uploadRequest]

        with self.assertRaises(sensorcloud.TruncatedUploadError):
            device = sensorcloud.Device("FAKE", "fake")
            sensor = device.sensor("sensor")
            channel = sensor.channel("channel")
            channel.timeseries_append(sensorcloud.SampleRate.hertz(10), [sensorcloud.Point(12345, 10.5)])
    def test_timeseriesGatewayTimeout(self):
        uploadRequest = Mock()
        uploadRequest.doRequest = Mock()
        uploadRequest.status_code = 504
        uploadRequest.reason = ""
        uploadRequest.text = "text"
        sensorcloud.webrequest.Requests.Request = Mock()
        sensorcloud.webrequest.Requests.Request.side_effect = [authRequest(), uploadRequest]

        with self.assertRaises(sensorcloud.ServerError):
            device = sensorcloud.Device("FAKE", "fake")
            sensor = device.sensor("sensor")
            channel = sensor.channel("channel")
            channel.timeseries_append(sensorcloud.SampleRate.hertz(10), [sensorcloud.Point(12345, 10.5)])
    def test_cacheSetOnUpload(self):
        request = Mock()
        request.side_effect = [authRequest(), created(), created()]
        sensorcloud.webrequest.Requests.Request = request

        fd, path = tempfile.mkstemp()
        os.close(fd)
        device = sensorcloud.Device("FAKE", "fake", cache_file=path)
        sensor = device.sensor("sensor")
        channel = sensor.channel("channel")
        channel.timeseries_append(sensorcloud.SampleRate.hertz(10), [sensorcloud.Point(12345, 10.5)])
        channel.histogram_append(sensorcloud.SampleRate.hertz(10), [sensorcloud.Histogram(123455, 0.0, 1.0, [10.5, 4328.3])])

        self.assertEqual(channel._cache.timeseries_partition(sensorcloud.SampleRate.hertz(10)).last_timestamp, 12345)
        self.assertEqual(channel._cache.histogram_partition(sensorcloud.SampleRate.hertz(10), 0.0, 1.0, 2).last_timestamp, 123455)
        os.unlink(path)
    def test_uploadTimeseries(self):
        packer = xdrlib.Packer()
        packer.pack_int(1)
        packer.pack_int(0)
        noPartitions = Mock()
        noPartitions.status_code = 200
        noPartitions.raw = packer.get_buffer()

        request = Mock()
        request.side_effect = [authRequest(), created(), noPartitions]
        sensorcloud.webrequest.Requests.Request = request

        device = sensorcloud.Device("FAKE", "fake")
        sensor = device.sensor("sensor")
        channel = sensor.channel("channel")
        channel.timeseries_append(sensorcloud.SampleRate.hertz(10), [sensorcloud.Point(12345, 10.5)])
        self.assertEqual(channel.last_timestamp_nanoseconds, 12345)
    def test_createAndRetryChannel404(self):
        sensorNotFound = Mock()
        sensorNotFound.status_code = 404
        sensorNotFound.text = '{"errorcode": "404-002", "message": ""}'

        created = Mock()
        created.status_code = 201

        request = Mock()
        request.side_effect = [authRequest(), sensorNotFound, created, created]
        sensorcloud.webrequest.Requests.Request = request

        device = sensorcloud.Device("FAKE", "fake")
        sensor = device.sensor("sensor")
        channel = sensor.channel("channel")
        channel.timeseries_append(sensorcloud.SampleRate.hertz(10), [sensorcloud.Point(12345, 10.5)])

        calls = [
                mock.call('POST', 'https://dsx.sensorcloud.microstrain.com/SensorCloud/devices/FAKE/sensors/sensor/channels/channel/streams/timeseries/data/', mock.ANY),
                mock.call('PUT', 'https://dsx.sensorcloud.microstrain.com/SensorCloud/devices/FAKE/sensors/sensor/channels/channel/', mock.ANY),
                mock.call('POST', 'https://dsx.sensorcloud.microstrain.com/SensorCloud/devices/FAKE/sensors/sensor/channels/channel/streams/timeseries/data/', mock.ANY)
        ]
        request.assert_has_calls(calls)