def write_distance(self, my_location_flow_id, distance, eta, timestamp):
        dist_v = IotValue()
        dist_v.float64 = distance
        eta_v = IotValue()
        eta_v.float32 = eta
        timestamp_v = IotValue()
        timestamp_v.uint64 = timestamp

        distance_data = IotNvpSeq()
        distance_data.append(IotNvp('distance', dist_v))
        distance_data.append(IotNvp('eta', eta_v))
        distance_data.append(IotNvp('timestampUtc', timestamp_v))

        # Write distance to DataRiver using flow ID from incoming location sample
        self._thing.write('distance', my_location_flow_id, distance_data)
Beispiel #2
0
    def write(self, burst_interval, burst_size, running_time, mode):
        try:
            burst_count = 0
            count = 0
            timed_out = False

            pub_start = current_time_milliseconds()
            burst_start = current_time_milliseconds()
            current_time = current_time_milliseconds()

            output_handler = self._thing.get_output_handler('ThroughputOutput')
            internal_sequencenumber_v = IotValue()

            if mode == WriterMode.OUTPUT_HANDLER_NOT_THREAD_SAFE:
                output_handler.non_reentrant_flow_id = self._thing.context_id
                internal_nvp_seq = output_handler.setup_non_reentrant_nvp_seq(
                    self._sample)

                internal_sequencenumber_nvp = internal_nvp_seq[0]
                internal_sequencenumber_v = internal_sequencenumber_nvp.value

            while not timed_out:
                # Write data until burst size has been reached
                if burst_count < burst_size:
                    burst_count += 1

                    if mode == WriterMode.OUTPUT_HANDLER:
                        # Fill the nvp_seq with updated sequencenr
                        self._sample[0].value.uint64 = count
                        count += 1

                        # Write the data using output handler
                        output_handler.write(self._sample)
                    elif mode == WriterMode.OUTPUT_HANDLER_NOT_THREAD_SAFE:
                        # Fill the nvp_seq with updated sequencenr
                        internal_sequencenumber_v.uint64 = count
                        count += 1

                        # Write the data using non-reentrant write on output handler
                        output_handler.write_non_reentrant()
                    else:
                        # Fill the nvp_seq with updated sequencenr
                        self._sample[0].value.uint64 = count
                        count += 1

                        # Write the data
                        self._thing.write('ThroughputOutput', self._sample)
                elif burst_interval != 0:
                    # Sleep until burst interval has passed
                    current_time = current_time_milliseconds()

                    delta_time = current_time - burst_start
                    if delta_time < burst_interval:
                        time.sleep(float(burst_interval - delta_time) / 1000.0)

                    burst_start = current_time_milliseconds()
                    burst_count = 0
                else:
                    burst_count = 0

                # Check of timeout
                if running_time != 0:
                    current_time = current_time_milliseconds()
                    if float(current_time - pub_start) / 1000.0 > running_time:
                        timed_out = True

            print('Timed out: {} samples written'.format(count))
        except KeyboardInterrupt:
            print('Terminated: {} samples written'.format(count))
            sys.exit(1)