Beispiel #1
0
    def alarm(self, message):
        alarm_v = IotValue()
        alarm_v.string = message
        alarm_data = IotNvpSeq()
        alarm_data.append(IotNvp('alarm', alarm_v))

        self._thing.write('alarm', alarm_data)
    def __init__(self, engine_id='', frame_id=0):
        self.__engine_id = IotValue()
        self.__frame_id = IotValue()
        self.__data = IotNvpSeq()

        self.engine_id = engine_id
        self.frame_id = frame_id
Beispiel #3
0
 def set_state(self, state):
     state_v = IotValue()
     state_v.string = state
     data = IotNvpSeq()
     data.append(IotNvp('state', state_v))
     
     self._thing.write('state', data)
Beispiel #4
0
 def write_sample(self, illuminance):
     illuminance_v = IotValue()
     illuminance_v.uint32 = illuminance
     sensor_data = IotNvpSeq()
     sensor_data.append(IotNvp('illuminance', illuminance_v))
     
     self._thing.write('illuminance', sensor_data)
Beispiel #5
0
class PyDetectionBox:
    def __init__(self, engine_id='', stream_id='', frame_id=0):
        self.__engine_id = IotValue()
        self.__stream_id = IotValue()
        self.__frame_id = IotValue()
        self.__data = IotNvpSeq()

        self.engine_id = engine_id
        self.stream_id = stream_id
        self.frame_id = frame_id

    @property
    def engine_id(self):
        return self.__engine_id.string

    @engine_id.setter
    def engine_id(self, value):
        self.__engine_id.string = value

    @property
    def stream_id(self):
        return self.__stream_id.string

    @stream_id.setter
    def stream_id(self, value):
        self.__stream_id.string = value

    @property
    def frame_id(self):
        return self.__frame_id.uint32

    @frame_id.setter
    def frame_id(self, value):
        self.__frame_id.uint32 = value

    def add_data(self, value: PyDetectionBoxData):
        seq = value.dr_data
        value = IotValue()
        value.nvp_seq = seq
        it = IotNvp()
        it.value = value
        self.__data.push_back(it)

    def add_box(self, obj_id, obj_label, class_id, class_label, x1, y1, x2, y2,
                probability, meta):
        self.add_data(
            PyDetectionBoxData(obj_id, obj_label, class_id, class_label, x1,
                               y1, x2, y2, probability, meta))

    @property
    def dr_data(self) -> IotNvpSeq:
        data = IotNvpSeq()
        data.append(IotNvp('engine_id', self.__engine_id))
        data.append(IotNvp('stream_id', self.__stream_id))
        data.append(IotNvp('frame_id', self.__frame_id))
        dbox_value = IotValue()
        dbox_value.nvp_seq = self.__data
        data.append(IotNvp('data', dbox_value))
        return data
Beispiel #6
0
 def init_payload(self, payload_size):
     payload = IotValue()
     payload.byte_seq = ()
     for i in range(payload_size):
         payload.byte_seq.append(ord('a'))
     
     self._sample_data = IotNvpSeq()
     self._sample_data.append(IotNvp('payload', payload))
Beispiel #7
0
    def write_sample(self, temperature):
        temperature_v = IotValue()
        temperature_v.float32 = temperature

        sensor_data = IotNvpSeq()
        sensor_data.append(IotNvp('temperature', temperature_v))

        self._thing.write('temperature', sensor_data)
class PyDetectionBox:
    def __init__(self, engine_id='', frame_id=0):
        self.__engine_id = IotValue()
        self.__frame_id = IotValue()
        self.__data = IotNvpSeq()

        self.engine_id = engine_id
        self.frame_id = frame_id

    @property
    def engine_id(self):
        return self.__engine_id.string

    @engine_id.setter
    def engine_id(self, value):
        self.__engine_id.string = value

    @property
    def frame_id(self):
        return self.__frame_id.uint32

    @frame_id.setter
    def frame_id(self, value):
        self.__frame_id.uint32 = value

    def add_data(self, value: PyDetectionBoxData):
        seq = value.dr_data
        value = IotValue()
        value.nvp_seq = seq
        it = IotNvp()
        it.value = value
        self.__data.push_back(it)

    def add_box(self,
                tracker_obj_id: int = 0,
                category_id: int = 0,
                category_label: str = '',
                x1: float = 0.0,
                y1: float = 0.0,
                x2: float = 0.0,
                y2: float = 0.0,
                probability: float = 0.0,
                color: str = '',
                metadata: str = ''):
        self.add_data(
            PyDetectionBoxData(tracker_obj_id, category_id, category_label, x1,
                               y1, x2, y2, probability, color, metadata))

    @property
    def dr_data(self) -> IotNvpSeq:
        data = IotNvpSeq()
        data.append(IotNvp('engine_id', self.__engine_id))
        data.append(IotNvp('frame_id', self.__frame_id))
        dbox_value = IotValue()
        dbox_value.nvp_seq = self.__data
        data.append(IotNvp('detection_box_data', dbox_value))
        return data
Beispiel #9
0
    def write_sample(self, temperature):
        temperature_v = IotValue()
        temperature_v.float32 = temperature

        sensor_data = IotNvpSeq()
        sensor_data.append(IotNvp('temperature', temperature_v))

        try:
            self._thing.write('temperature', sensor_data)
        except RuntimeError as e:
            print('Error writing data: ' + str(e))
    def __init__(self,
                 engine_id: str = '',
                 model_id: str = '',
                 frame_id: int = 0):
        self.__engine_id = IotValue()
        self.__frame_id = IotValue()
        self.__model_id = IotValue()
        self.__data = IotNvpSeq()

        self.engine_id = engine_id
        self.model_id = model_id
        self.frame_id = frame_id
Beispiel #11
0
    def init_payload(self, video):
        payload = IotValue()
        payload.byte_seq = []
        #payload.byte_seq.append(video)
        #payload.byte_seq = video

        for i in video:
            #for j in i:
            #for k in j:
            payload.byte_seq.append(int(i))

        #print(len(video))
        self._send_payload = IotNvpSeq()
        self._send_payload.append(IotNvp('video', payload))
 def dr_data(self) -> IotNvpSeq:
     data = IotNvpSeq()
     data.append(IotNvp('engine_id', self.__engine_id))
     data.append(IotNvp('frame_id', self.__frame_id))
     dbox_value = IotValue()
     dbox_value.nvp_seq = self.__data
     data.append(IotNvp('detection_box_data', dbox_value))
     return data
Beispiel #13
0
    def set_state(self, state):
        data = IotNvpSeq(1)

        data[0].name = 'state'
        data[0].value.string = state

        self._thing.write('state', data)
Beispiel #14
0
    def write_sample(self, level):
        data = IotNvpSeq(1)

        data[0].name = 'level'
        data[0].value.float32 = level

        self._thing.write('level', data)
Beispiel #15
0
 def write_sample(self, temperature):
     sensor_data = IotNvpSeq(1)
     
     sensor_data[0].name = 'temperature'
     sensor_data[0].value.float32 = temperature
     
     self._thing.write('temperature', sensor_data)
 def dr_data(self) -> IotNvpSeq:
     data = IotNvpSeq()
     data.append(IotNvp('engine_id', self.__engine_id))
     data.append(IotNvp('frame_id', self.__frame_id))
     data.append(IotNvp('model_id', self.__model_id))
     value = IotValue()
     value.nvp_seq = self.__data
     data.append(IotNvp('classification_data', value))
     return data
Beispiel #17
0
    def setup_message(self, payload_size):
        self._sample = IotNvpSeq(2)

        self._sample[0].name = 'sequencenumber'
        self._sample[0].value.uint64 = 0

        self._sample[1].name = 'sequencedata'
        # Note: bytearray call is only necessary in Python 2.7
        self._sample[1].value.byte_seq = bytearray(payload_size * b'a')
    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 #19
0
 def write_sample(self, barcode, x, y, z):
     barcode_v = IotValue()
     barcode_v.string = barcode
     position_x_v = IotValue()
     position_x_v.int32 = x
     position_y_v = IotValue()
     position_y_v.int32 = y
     position_z_v = IotValue()
     position_z_v.int32 = z
     
     data = IotNvpSeq()
     data.append(IotNvp('barcode', barcode_v))
     data.append(IotNvp('position_x', position_x_v))
     data.append(IotNvp('position_y', position_y_v))
     data.append(IotNvp('position_z', position_z_v))
     
     self._thing.write('observation', self.get_flow_id(barcode), data)
Beispiel #20
0
 def write_sample(self, speed, last_hour_min, last_hour_max, last_hour_average):
     data = IotNvpSeq(4)
     
     data[0].name = 'speed'
     data[0].value.uint32 = speed
     
     data[1].name = 'lastHourMin'
     data[1].value.uint32 = last_hour_min
     
     data[2].name = 'lastHourMax'
     data[2].value.uint32 = last_hour_max
     
     data[3].name = 'lastHourAverage'
     data[3].value.float32 = last_hour_average
     
     self._thing.write('rotationalSpeed', data)
 def dr_data(self) -> IotNvpSeq:
     data = IotNvpSeq()
     data.append(IotNvp('category_id', self.__category_id))
     data.append(IotNvp('category_label', self.__category_label))
     data.append(IotNvp('super_category_id', self.__super_category_id))
     data.append(IotNvp('super_category_label',
                        self.__super_category_label))
     data.append(IotNvp('metadata', self.__metadata))
     data.append(IotNvp('probability', self.__probability))
     return data
Beispiel #22
0
 def dr_data(self):
     data = IotNvpSeq()
     data.append(IotNvp('engine_id', self._engine_id))
     data.append(IotNvp('stream_id', self._stream_id))
     data.append(IotNvp('frame_id', self._frame_id))
     nvp_seq = IotNvpSeq()
     for value in self._data:
         iot_value = IotValue()
         iot_value.nvp_seq = value.dr_data
         it = IotNvp()
         it.value = iot_value
         nvp_seq.push_back(it)
     seq_value = IotValue()
     seq_value.nvp_seq = nvp_seq
     data.append(IotNvp('data', seq_value))
     return data
 def dr_data(self):
     data = IotNvpSeq()
     data.append(IotNvp('tracker_obj_id', self.__tracker_obj_id))
     data.append(IotNvp('category_id', self.__category_id))
     data.append(IotNvp('category_label', self.__category_label))
     data.append(IotNvp('x1', self.__x1))
     data.append(IotNvp('y1', self.__y1))
     data.append(IotNvp('x2', self.__x2))
     data.append(IotNvp('y2', self.__y2))
     data.append(IotNvp('probability', self.__probability))
     data.append(IotNvp('color', self.__color))
     data.append(IotNvp('metadata', self.__metadata))
     return data
Beispiel #24
0
class VideoStreamSender(object):

    # Initializing
    def __init__(self, thing_properties_uri):
        self._thing_properties_uri = thing_properties_uri

        self._dr = None
        self._thing = None
        self._send_payload = None

    # Enter the runtime context related to the object
    def __enter__(self):
        self._dr = DataRiver.get_instance()
        self._thing = self.create_thing()

        print('Video Stream sender started')

        return self

    # Exit the runtime context related to the object
    def __exit__(self, exc_type, exc_value, exc_traceback):
        if self._dr is not None:
            self._dr.close()
        print('Video Stream sender stopped')

    def create_thing(self):
        # Create and Populate the TagGroup registry with JSON resource files.
        tgr = JSonTagGroupRegistry()
        tgr.register_tag_groups_from_uri(
            get_abs_file_uri(
                'definitions/TagGroup/com.adlinktech.MQ/VideoStreamTagGroup.json'
            ))
        self._dr.add_tag_group_registry(tgr)

        # Create and Populate the ThingClass registry with JSON resource files.
        tcr = JSonThingClassRegistry()
        tcr.register_thing_classes_from_uri(
            get_abs_file_uri(
                'definitions/ThingClass/com.adlinktech.MQ/VideoStreamSenderThingClass.json'
            ))
        self._dr.add_thing_class_registry(tcr)

        # Create a Thing based on properties specified in a JSON resource file.
        tp = JSonThingProperties()
        tp.read_properties_from_uri(self._thing_properties_uri)
        return self._dr.create_thing(tp)

    def init_payload(self, video):
        payload = IotValue()
        payload.byte_seq = []
        #payload.byte_seq.append(video)
        #payload.byte_seq = video

        for i in video:
            #for j in i:
            #for k in j:
            payload.byte_seq.append(int(i))

        #print(len(video))
        self._send_payload = IotNvpSeq()
        self._send_payload.append(IotNvp('video', payload))

    def write_sample(self, video):
        self.init_payload(video)
        self._thing.write('video', self._send_payload)

    def run(self):
        #random.seed()
        #sample_count = (float(running_time) * 1000.0) / SAMPLE_DELAY_MS
        #actual_temperature = 21.5

        # Create a VideoCapture object and read from input file
        # If the input is the camera, pass 0 instead of the video file name
        cap = cv2.VideoCapture(0)
        #path = r'/home/adlink/Desktop/220px-SNice.svg.png'
        #img = cv2.imread('Smiley Face.jpg',-1)
        # Check if camera opened successfully
        if (cap.isOpened() == False):
            print("Error opening video stream or file")

        #if f.mode == "r":
        #while sample_count > 0:
        #print(type(img[0][0][0]))
        #cv2.imshow('Image',img)
        #cv2.waitKey(0)
        #while True:
        #self.write_sample(img)

        while (cap.isOpened()):
            # Capture frame-by-frame
            (ret, frame) = cap.read()

            if ret == True:
                #frame = imutils.resize(frame, width=10)
                frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
                #frame = np.dstack([frame, frame, frame])
                # Display the resulting frame
                #cv2.imshow('Frame',frame)
                #print(len(frame))
                # Press Q on keyboard to  exit
                #if cv2.waitKey(25) & 0xFF == ord('q'):
                #break
                #print(len(frame[0]))
                # Break the loop
                #else:
                #break
                frame = np.reshape(frame, (307200))
                #print(frame)
                self.write_sample(frame)

                #print(frame)

            #time.sleep(SAMPLE_DELAY_MS/1000.0)
        # When everything done, release the video capture object
            else:
                break
        cap.release()

        # Closes all the frames
        cv2.destroyAllWindows()
Beispiel #25
0
 def dr_data(self):
     data = IotNvpSeq()
     data.append(IotNvp('obj_id', self._obj_id))
     data.append(IotNvp('obj_label', self._obj_label))
     data.append(IotNvp('class_id', self._class_id))
     data.append(IotNvp('class_label', self._class_label))
     data.append(IotNvp('x', self._x))
     data.append(IotNvp('y', self._y))
     data.append(IotNvp('radius', self._radius))
     data.append(IotNvp('probability', self._probability))
     data.append(IotNvp('meta', self._meta))
     return data
Beispiel #26
0
class Ping(object):
	
	# Initializing
    def __init__(self, thing_properties_uri):
        self._thing_properties_uri = thing_properties_uri
        
        self._datariver = None
        self._thing = None
        self._sample_data = None
	
	# Enter the runtime context related to the object
    def __enter__(self):
        self._datariver = DataRiver.get_instance()
        self._thing = self.create_thing()
        
        print('# Ping started')
        
        return self
	
	# Exit the runtime context related to the object
    def __exit__(self, exc_type, exc_value, exc_traceback):
        if self._datariver is not None:
            self._datariver.close()
        print('# Ping stopped')
    
	
    def send_terminate(self):
        print('# Sending termination request.')
        self._thing.purge('Ping', 'ping')
        time.sleep(1)
        return 0
    
    def wait_for_pong(self):
        # wait for pong to appear by discovering its thingId and thingClass
        print('# Waiting for pong to run...')
        discovered_thing_registry = self._datariver.discovered_thing_registry
        reader_found = False
        
        while not reader_found:
            try:
				# see if we already know pongs's thing class
                thing = discovered_thing_registry.find_discovered_thing('py-pongThing1', 'Pong:com.adlinktech.example:v1.0')
                reader_found = True
            except:
                pass
            time.sleep(1)

	
    def create_thing(self):
		# Create and Populate the TagGroup registry with JSON resource files.
        tgr = JSonTagGroupRegistry()
        tgr.register_tag_groups_from_uri('file://definitions/TagGroup/com.adlinktech.example/PingTagGroup.json')
        tgr.register_tag_groups_from_uri('file://definitions/TagGroup/com.adlinktech.example/PongTagGroup.json')
        self._datariver.add_tag_group_registry(tgr)
		
		# Create and Populate the ThingClass registry with JSON resource files.
        tcr = JSonThingClassRegistry()
        tcr.register_thing_classes_from_uri('file://definitions/ThingClass/com.adlinktech.example/PingThingClass.json')
        self._datariver.add_thing_class_registry(tcr)
		
		# Create a Thing based on properties specified in a JSON resource file.
        tp = JSonThingProperties()
        tp.read_properties_from_uri(self._thing_properties_uri)
        return self._datariver.create_thing(tp)
	
    def init_payload(self, payload_size):
        payload = IotValue()
        payload.byte_seq = ()
        for i in range(payload_size):
            payload.byte_seq.append(ord('a'))
        
        self._sample_data = IotNvpSeq()
        self._sample_data.append(IotNvp('payload', payload))
	
    def warm_up(self):
        start_time = time.time()
        wait_timeout = 10000
        
        print('# Warming up 5s to stabilise performance...')
        while (time.time() - start_time) < 5:
            self._thing.write('Ping', self._sample_data)
            self._thing.read_iot_nvp('Pong', wait_timeout)
        print('# Warm up complete')
	
    
    def run(self, payload_size, num_samples, running_time):
        start_time = 0
        pre_write_time = 0
        post_write_time = 0
        pre_read_time = 0
        post_read_time = 0
        wait_timeout = 10000
        round_trip = ExampleTimeStats()
        write_access = ExampleTimeStats()
        read_access = ExampleTimeStats()
        global round_trip_overall
        global write_access_overall
        global read_access_overall
		
        print('# Parameters: payload size: {} | number of samples: {} | running time: {}'.format(payload_size, num_samples, running_time))
        
        # Wait for the Pong Thing
        self.wait_for_pong()
        
        # Init payload
        self.init_payload(payload_size)
        
        # Warm-up for 5s
        self.warm_up()
        
        print('# Round trip measurements (in us)')
        print(COLOR_LMAGENTA + '#             Round trip time [us]         Write-access time [us]       Read-access time [us]' + NO_COLOR)
        print(COLOR_LMAGENTA + '# Seconds     Count   median      min      Count   median      min      Count   median      min' + NO_COLOR)
        
        start_time = current_time_microseconds()
        elapsed_seconds = 0
        i = 0
        while num_samples == 0 or i < num_samples:
            # Write a sample that pong can send back
            pre_write_time = current_time_microseconds()
            self._thing.write('Ping', self._sample_data)
            post_write_time = current_time_microseconds()
            
            # Read sample
            pre_read_time = current_time_microseconds()
            samples = self._thing.read_iot_nvp('Pong', wait_timeout)
            post_read_time = current_time_microseconds()
            
            # Validate sample count
            if samples.size() != 1:
                print('ERROR: Ping received {} samples but was expecting 1.'.format(samples.size()))
                return 1
			
            # Update stats
            write_access += (post_write_time - pre_write_time)
            read_access += (post_read_time - pre_read_time)
            round_trip += (post_read_time - pre_write_time)
            write_access_overall += (post_write_time - pre_write_time)
            read_access_overall += (post_read_time - pre_read_time)
            round_trip_overall += (post_read_time - pre_write_time)
            
            # Print stats each second
            if ((post_read_time - start_time) > US_IN_ONE_SEC) or (i == num_samples - 1):
                # Print stats
                elapsed_seconds += 1
                show_stats(False, elapsed_seconds, round_trip, write_access, read_access)
                
                # Reset stats for next run
                round_trip = round_trip.example_reset_time_stats()
                write_access = write_access.example_reset_time_stats()
                read_access = read_access.example_reset_time_stats()
                
                # Set values for next run
                start_time = current_time_microseconds()
                
                # Check for timeout
                if (running_time > 0 and elapsed_seconds >= running_time):
                    break
			
            i += 1
		
        # Print overall stats
        show_stats(True, 0, round_trip_overall, write_access_overall, read_access_overall)
        
        return 0
Beispiel #27
0
    def dr_data(self):
        data = IotNvpSeq()
        data.append(IotNvp('frame_id', self.__frame_id))
        data.append(IotNvp('timestamp', self.__timestamp))
        data.append(IotNvp('video_data', self.__video_data))
        data.append(IotNvp('width', self.__width))
        data.append(IotNvp('height', self.__height))
        data.append(IotNvp('channels', self.__channels))
        data.append(IotNvp('size', self.__size))
        data.append(IotNvp('format', self.__format))
        data.append(IotNvp('compression', self.__compression))
        data.append(IotNvp('framerate', self.__framerate))

        return data
Beispiel #28
0
    def dr_data(self) -> IotNvpSeq:
        data = IotNvpSeq()
        data.append(IotNvp('stream_id', self.__stream_id))
        data.append(IotNvp('mac_address', self.__mac_address))
        data.append(IotNvp('ip_address', self.__ip_address))
        data.append(IotNvp('port', self.__port))
        data.append(IotNvp('uri', self.__uri))
        data.append(IotNvp('manufacturer', self.__manufacturer))
        data.append(IotNvp('model', self.__model))
        data.append(IotNvp('serial', self.__serial))
        data.append(IotNvp('fw_version', self.__fw_version))
        data.append(IotNvp('dev_id', self.__dev_id))
        data.append(IotNvp('status', self.__status))
        data.append(IotNvp('kind', self.__kind))
        data.append(IotNvp('protocol', self.__protocol))

        return data
class PyClassification:
    def __init__(self,
                 engine_id: str = '',
                 model_id: str = '',
                 frame_id: int = 0):
        self.__engine_id = IotValue()
        self.__frame_id = IotValue()
        self.__model_id = IotValue()
        self.__data = IotNvpSeq()

        self.engine_id = engine_id
        self.model_id = model_id
        self.frame_id = frame_id

    @property
    def engine_id(self) -> str:
        return self.__engine_id.string

    @engine_id.setter
    def engine_id(self, value: str) -> None:
        self.__engine_id.string = value

    @property
    def model_id(self) -> str:
        return self.__model_id.string

    @model_id.setter
    def model_id(self, value: str) -> None:
        self.__model_id.string = value

    @property
    def frame_id(self) -> int:
        return self.__frame_id.uint32

    @frame_id.setter
    def frame_id(self, value: int) -> None:
        self.__frame_id.uint32 = value

    def add_data(self, value: PyClassificationData) -> None:
        seq = value.dr_data
        classification_value = IotValue()
        classification_value.nvp_seq = seq
        classification_data: IotNvp = IotNvp()
        classification_data.value = classification_value
        self.__data.push_back(classification_data)

    def add_classification(self,
                           category_id: int = -1,
                           category_label='',
                           super_category_id: int = -1,
                           super_category_label='',
                           metadata: str = '',
                           probability: float = 0.0) -> None:
        self.add_data(
            PyClassificationData(category_id, category_label,
                                 super_category_id, super_category_label,
                                 metadata, probability))

    @property
    def dr_data(self) -> IotNvpSeq:
        data = IotNvpSeq()
        data.append(IotNvp('engine_id', self.__engine_id))
        data.append(IotNvp('frame_id', self.__frame_id))
        data.append(IotNvp('model_id', self.__model_id))
        value = IotValue()
        value.nvp_seq = self.__data
        data.append(IotNvp('classification_data', value))
        return data
Beispiel #30
0
 def dr_data(self):
     data = IotNvpSeq()
     data.append(IotNvp('obj_id', self.__obj_id))
     data.append(IotNvp('obj_label', self.__obj_label))
     data.append(IotNvp('class_id', self.__class_id))
     data.append(IotNvp('class_label', self.__class_label))
     data.append(IotNvp('x1', self.__x1))
     data.append(IotNvp('y1', self.__y1))
     data.append(IotNvp('x2', self.__x2))
     data.append(IotNvp('y2', self.__y2))
     data.append(IotNvp('probability', self.__probability))
     data.append(IotNvp('meta', self.__meta))
     return data