Ejemplo n.º 1
0
    def fetch(self):
        try:
            conn = http.client.HTTPConnection(self.url)
            conn.request(
                'GET', '/data/2.5/weather?q={}&APPID={}'.format(
                    self.city, self.appid))
            r = conn.getresponse()
            res = r.read().decode()
            conn.close()
            if r.status != 200:
                raise ValueError(res)

            jdoc = json.loads(res)
            reads = {
                'city': jdoc['name'],
                'wind_speed': jdoc['wind']['speed'],
                'clouds': jdoc['clouds']['all'],
                'temperature': jdoc['main']['temp'],
                'pressure': jdoc['main']['pressure'],
                'humidity': jdoc['main']['humidity'],
                'visibility': jdoc['visibility']
            }
            data = {
                'asset': self.asset_name,
                'timestamp': utils.local_timestamp(),
                'key': str(uuid.uuid4()),
                'readings': reads
            }
            async_ingest.ingest_callback(c_callback, c_ingest_ref, data)
        except ValueError as ex:
            err = "Unable to fetch information from api.openweathermap: {}".format(
                str(ex))
            _LOGGER.error(err)
Ejemplo n.º 2
0
    async def render_put(request):
        """Store sensor readings from Roxtec to FogLAMP

        Args:
            request:
                The payload block decodes to JSON similar to the following:

                .. code-block:: python

        {
                    "guard_id": "444DF705F0F8",
                    "gateway_id": "device-0",
                    "state": 70,
                    "transit_id": "t11",
                    "battery": 4,
                    "pressure": 722,
                    "temperature": 0,
                    "last_seen": 1533816739126
                }

        Example:
            curl --insecure -X PUT https://localhost:1608/transit -d '[{ "guard_id": "444DF705F0F8", "gateway_id": "device-0", "state": 70, "transit_id": "t11", "battery": 4, "pressure": 722, "temperature": 0, "last_seen": 1533816739126 }]'
            curl -X PUT http://localhost:8608/transit -d '[{ "guard_id": "444DF705F0F8", "gateway_id": "device-0", "state": 70, "transit_id": "t11", "battery": 4, "pressure": 722, "temperature": 0, "last_seen": 1533816739126 }]'
        """
        try:
            message = {'result': 'success'}
            payload_block = await request.json()
            if type(payload_block) is not list:
                raise ValueError('Payload block must be a valid list')

            for payload in payload_block:
                asset = "Guard " + payload['guard_id']
                epoch_ms = payload['last_seen'] / 1000.0
                timestamp = datetime.datetime.fromtimestamp(epoch_ms).strftime('%Y-%m-%d %H:%M:%S.%f')
                key = str(uuid.uuid4())
                readings = {
                    "gateway_id": payload['gateway_id'],
                    "state": payload['state'],
                    "battery": payload['battery'],
                    "pressure": payload['pressure'],
                    "temperature": payload['temperature']
                }
                if 'transit_id' in payload and payload['transit_id'] is not None:
                    readings['transit_id'] = payload['transit_id']

                data = {
                    'asset': asset,
                    'timestamp': timestamp,
                    'key': key,
                    'readings': readings
                }
                async_ingest.ingest_callback(c_callback, c_ingest_ref, data)
        except (KeyError, ValueError, TypeError) as e:
            _LOGGER.exception("%d: %s", web.HTTPBadRequest.status_code, e)
            raise web.HTTPBadRequest(reason=e)
        except Exception as ex:
            _LOGGER.exception("%d: %s", web.HTTPInternalServerError.status_code, str(ex))
            raise web.HTTPInternalServerError(reason=str(ex))

        return web.json_response(message)
Ejemplo n.º 3
0
    def datachange_notification(self, node: Node, val, data):
        """
        Callback for OPC UA Subscription.
        This method will be called when the Client received a data change message from the Server.
        """

        time_stamp = utils.local_timestamp()
        asset_name = str(node)
        #Trim the string to start with ns=
        ns_start = asset_name.find('ns=')
        if ns_start != -1:
             asset_name = asset_name[ns_start:]

        #Some OPC UA servers add extra parentheses, so remove them
        #Remove any extraneous parentheses
        asset_name = asset_name.replace("(","")
        asset_name = asset_name.replace(")","")

        #_LOGGER.info('opcua_py: datachange_notification %r %s', asset_name, val)
        key = str(uuid.uuid4())

        data = {
                'asset': asset_name,
                'timestamp': time_stamp,  # metric.timestamp
                'key' : key,
                'readings': {"value": val}
        } 

        async_ingest.ingest_callback(c_callback, c_ingest_ref, data)
Ejemplo n.º 4
0
    def run(self):
        global wait_event
        while True:
            if wait_event.is_set():  # i.e. shutdown called
                return
            self.condition.acquire()
            while self.queue.qsize() > 0:
                data = self.queue.get()
                reading = data['data']

                if self.handle['historicTimestamps']['value'] == 'true':
                    ts_col = self.handle['timestampCol']['value']
                    raw_time_stamp = reading.pop(ts_col)
                    ts_format = self.handle['timestampFormat']['value']
                    time_stamp = str(
                        datetime.datetime.strptime(raw_time_stamp, ts_format))
                else:
                    time_stamp = data['ts']

                reading = {
                    'asset': self.handle['assetName']['value'],
                    'timestamp': time_stamp,
                    'readings': reading
                }
                async_ingest.ingest_callback(c_callback, c_ingest_ref, reading)
            self.condition.notify()
            self.condition.release()
Ejemplo n.º 5
0
 async def save(self, msg):
     """Store msg content to Fledge """
     # TODO: string and other types?
     payload_json = json.loads(msg.payload.decode('utf-8'))
     _LOGGER.debug("Ingesting %s on topic %s", payload_json, str(msg.topic))
     data = {
         'asset': self.asset,
         'timestamp': utils.local_timestamp(),
         'readings': payload_json
     }
     async_ingest.ingest_callback(c_callback, c_ingest_ref, data)
Ejemplo n.º 6
0
async def _start_aiotest(handle):
    # This plugin adds 6 data points 2 within same min, 2 within same hour and 2 within same day
    # this data is useful when testing asset browsing based on timestamps
    ts_lst = list()
    ts_lst.append(str(datetime.now(timezone.utc).astimezone()))
    ts_lst.append(
        str(datetime.now(timezone.utc).astimezone() - timedelta(seconds=3)))
    ts_lst.append(
        str(datetime.now(timezone.utc).astimezone() - timedelta(minutes=5)))
    ts_lst.append(
        str(datetime.now(timezone.utc).astimezone() - timedelta(minutes=6)))
    ts_lst.append(
        str(datetime.now(timezone.utc).astimezone() - timedelta(hours=3)))
    ts_lst.append(
        str(datetime.now(timezone.utc).astimezone() - timedelta(hours=5)))

    i = 1
    for user_ts in ts_lst:
        try:
            data = list()
            data.append({
                'asset':
                '{}{}'.format(handle['assetPrefix']['value'],
                              handle['loudnessAssetName']['value']),
                'timestamp':
                user_ts,
                'key':
                str(uuid.uuid4()),
                'readings': {
                    "loudness": i
                }
            })
            async_ingest.ingest_callback(c_callback, c_ingest_ref, data)
            await asyncio.sleep(0.1)

        except (Exception, RuntimeError) as ex:
            _LOGGER.exception("TEST exception: {}".format(str(ex)))
            raise exceptions.DataRetrievalError(ex)
        else:
            i += 1
    async def render_post(request):
        """Store sensor readings from SensorPhone to Fledge

        Args:
            request:
                The payload decodes to JSON similar to the following:

                .. code-block:: python

                    {
                        "mode" : "sync",
                        "messages" : [
                             {
                                 "audio": 0.0005980864,
                                 "device": "iOS Device",
                                 "altitude": 30.71550178527832,
                                 "latitude": 51.55842144498452,
                                 "longitude": -0.8672407515484514,
                                 "timestamp": 1514597142,
                                 "gyroscopex": -0.005210537929087877,
                                 "gyroscopey": -0.02013654075562954,
                                 "gyroscopez": -0.005803442094475031,
                                 "accelerometerx": 0.0119628909160383,
                                 "accelerometery": -0.7869872764804313,
                                 "accelerometerz": -0.6097259288653731}
                        ]
                    }
        """
        # TODO: Decide upon the correct format of message
        message = {'result': 'success'}
        code = web.HTTPOk.status_code

        try:
            payload = await request.json()

            asset = 'SensorPhone'
            timestamp = utils.local_timestamp()
            messages = payload.get('messages')

            if not isinstance(messages, list):
                raise ValueError('messages must be a list')

            for readings in messages:
                data = {
                    'asset': asset,
                    'timestamp': timestamp,
                    'readings': readings
                }
                async_ingest.ingest_callback(c_callback, c_ingest_ref, data)
        except (ValueError, TypeError) as e:
            code = web.HTTPBadRequest.status_code
            message = {'error': str(e)}
            _LOGGER.exception(str(e))
        except Exception as e:
            code = web.HTTPInternalServerError.status_code
            message = {'error': str(e)}
            _LOGGER.exception(str(e))

        # expect keys in response:
        # (code = 2xx) result Or busy
        # (code = 4xx, 5xx) error
        message['status'] = code

        return web.json_response(message)
Ejemplo n.º 8
0
    async def render_post(request):
        """Store sensor readings from CoAP to Fledge

        Args:
            request:
                The payload is a cbor-encoded array that decodes to JSON
                similar to the following:

                .. code-block:: python

                    {
                        "timestamp": "2017-01-02T01:02:03.23232Z-05:00",
                        "asset": "pump1",
                        "readings": {
                            "velocity": "500",
                            "temperature": {
                                "value": "32",
                                "unit": "kelvin"
                            }
                        }
                    }
        """
        # aiocoap handlers must be defensive about exceptions. If an exception
        # is raised out of a handler, it is permanently disabled by aiocoap.
        # Therefore, Exception is caught instead of specific exceptions.

        code = aiocoap.numbers.codes.Code.VALID
        message = ''
        try:
            try:
                payload = cbor2.loads(request.payload)
            except Exception:
                raise ValueError('Payload must be a dictionary')

            asset = payload['asset']
            timestamp = payload['timestamp']

            # readings or sensor_values are optional
            try:
                readings = payload['readings']
            except KeyError:
                readings = payload[
                    'sensor_values']  # sensor_values is deprecated

            # if optional then
            # TODO: confirm, do we want to check this?
            if not isinstance(readings, dict):
                raise ValueError('readings must be a dictionary')
            data = {
                'asset': asset,
                'timestamp': timestamp,
                'readings': readings
            }
            async_ingest.ingest_callback(c_callback, c_ingest_ref, data)
        except (KeyError, ValueError, TypeError) as e:
            _LOGGER.exception("%d: %s", aiocoap.numbers.codes.Code.BAD_REQUEST,
                              str(e))
            raise aiocoap.error.BadRequest(str(e))
        except Exception as ex:
            _LOGGER.exception("%d: %s",
                              aiocoap.numbers.codes.Code.INTERNAL_SERVER_ERROR,
                              str(ex))
            raise aiocoap.error.ConstructionRenderableError(str(ex))
        return aiocoap.Message(payload=message.encode('utf-8'), code=code)
Ejemplo n.º 9
0
    async def render_post(self, request):
        """Store sensor readings from http_south to Fledge

        Args:
            request:
                The payload block decodes to JSON similar to the following:

                .. code-block:: python

                    [ {
                        "timestamp": "2017-01-02T01:02:03.23232Z-05:00",
                        "asset": "pump1",
                        "readings": {"humidity": 0.0, "temperature": -40.0}
                      },
                      ...
                    ]
        Example:
            curl -X POST http://localhost:6683/sensor-reading -d '[{"timestamp": "2017-01-02T01:02:03.23232Z-05:00",
                "asset": "pump1", "readings": {"humidity": 0.0, "temperature": -40.0}}]'
        """
        message = {'result': 'success'}
        try:
            try:
                payload_block = await request.json()
            except Exception:
                raise ValueError('Payload block must be a valid json')

            if type(payload_block) is not list:
                raise ValueError('Payload block must be a valid list')

            for payload in payload_block:
                asset = "{}{}".format(
                    self.config_data['assetNamePrefix']['value'],
                    payload['asset'])
                dt_str = payload['timestamp']

                if dt_str.endswith("Z"):
                    fmt = "%Y-%m-%dT%H:%M:%S.%fZ"
                    utc_dt = datetime.strptime(dt_str, fmt)
                    # Convert to local time zone
                    dt_str = str(
                        utc_dt.replace(tzinfo=timezone.utc).astimezone(
                            tz=None))

                # readings or sensor_values are optional
                try:
                    readings = payload['readings']
                except KeyError:
                    readings = payload[
                        'sensor_values']  # sensor_values is deprecated

                # if optional then
                # TODO: confirm, do we want to check this?
                if not isinstance(readings, dict):
                    raise ValueError('readings must be a dictionary')

                data = {
                    'asset': asset,
                    'timestamp': dt_str,
                    'readings': readings
                }
                async_ingest.ingest_callback(c_callback, c_ingest_ref, data)
        except (KeyError, ValueError, TypeError) as e:
            _LOGGER.exception("%d: %s", web.HTTPBadRequest.status_code, str(e))
            raise web.HTTPBadRequest(reason=e)
        except Exception as ex:
            _LOGGER.exception("%d: %s",
                              web.HTTPInternalServerError.status_code, str(ex))
            raise web.HTTPInternalServerError(reason=str(ex))

        return web.json_response(message)
Ejemplo n.º 10
0
async def save_data(data):    
    async_ingest.ingest_callback(c_callback, c_ingest_ref, data)
Ejemplo n.º 11
0
    async def render_post(self, request):
        """Store sensor readings from http_south to FogLAMP

        Args:
            request:
                The payload block decodes to JSON similar to the following:

                .. code-block:: python

                    [ {
                        "timestamp": "2017-01-02T01:02:03.23232Z-05:00",
                        "asset": "pump1",
                        "key": "80a43623-ebe5-40d6-8d80-3f892da9b3b4",
                        "readings": {"humidity": 0.0, "temperature": -40.0}
                      },
                      ...
                    ]
        Example:
            curl -X POST http://localhost:6683/sensor-reading -d '[{"timestamp": "2017-01-02T01:02:03.23232Z-05:00",
                "asset": "pump1", "key": "80a43623-ebe5-40d6-8d80-3f892da9b3b4", "readings": {"humidity": 0.0, "temperature": -40.0}}]'
        """
        message = {'result': 'success'}
        try:
            try:
                payload_block = await request.json()
            except Exception:
                raise ValueError('Payload block must be a valid json')

            if type(payload_block) is not list:
                raise ValueError('Payload block must be a valid list')

            for payload in payload_block:
                asset = "{}{}".format(
                    self.config_data['assetNamePrefix']['value'],
                    payload['asset'])
                timestamp = payload['timestamp']
                key = payload['key']

                # HOTFIX: To ingest readings sent from foglamp sending process
                if not timestamp.rfind("+") == -1:
                    timestamp = timestamp + ":00"

                # readings or sensor_values are optional
                try:
                    readings = payload['readings']
                except KeyError:
                    readings = payload[
                        'sensor_values']  # sensor_values is deprecated

                # if optional then
                # TODO: confirm, do we want to check this?
                if not isinstance(readings, dict):
                    raise ValueError('readings must be a dictionary')

                data = {
                    'asset': asset,
                    'timestamp': timestamp,
                    'key': key,
                    'readings': readings
                }
                async_ingest.ingest_callback(c_callback, c_ingest_ref, data)
        except (KeyError, ValueError, TypeError) as e:
            _LOGGER.exception("%d: %s", web.HTTPBadRequest.status_code, str(e))
            raise web.HTTPBadRequest(reason=e)
        except Exception as ex:
            _LOGGER.exception("%d: %s",
                              web.HTTPInternalServerError.status_code, str(ex))
            raise web.HTTPInternalServerError(reason=str(ex))

        return web.json_response(message)
def camera_loop(**kwargs):
    """ Main function that keeps on fetching frame , 
        performing inference and drawing the result of
        inference on the detection window.
         Args:
           Keyword Arguements -> Each one is listed below
         Returns:
           None 
         Raises:  
           Raises exception if unable to draw the frame on the the window.
    """

    # if floating point model is used we need to subtract the mean and divide
    # by standard deviation
    input_mean = kwargs['input_mean']
    input_std = kwargs['input_std']

    # the height of the detection window on which frames are to be displayed
    camera_height = kwargs['camera_height']

    # the width of the detection window on which frames are to be displayed
    camera_width = kwargs['camera_width']

    # Note : The object contents may change if plugin_reconfigure is called
    # thats why the inference object in the loop is global

    # these variables are used for calculation of frame per seconds (FPS)
    frame_rate_calc = 1
    freq = cv2.getTickFrequency()

    source = kwargs['source']

    enable_window_local = kwargs['enable_window']

    # Initialize the stream object and start the thread that keeps on reading frames
    # This thread is independent of the Camera Processing Thread
    videostream = VideoStream(resolution=(camera_width, camera_height),
                              source=source).start()
    # For using the videostream with threading use the following :
    # videostream = VideoStream(resolution=(camera_width, camera_height), source=source, enable_thread=True).start()

    # The thread is allowed to capture a few frames. See FOGL-4132 for details
    wait_for_frame(videostream)

    # creating a window with a name
    window_name = 'Human detector'
    global BACKGROUND_TASK
    if not BACKGROUND_TASK and enable_window_local:
        foreground_task = True
        cv2.namedWindow(window_name)
    else:
        foreground_task = False

    while True:
        # Capture frame-by-frame
        t1 = cv2.getTickCount()
        global inference, asset_name, FRAME, enable_window

        # we need the height , width to resize the image for feeding into the model
        height_for_model = inference.height_for_model
        width_for_model = inference.width_for_model

        #  check if floating point model is used or not
        floating_model = inference.floating_model

        # The minimum confidence to threshold the detections obtained from model
        min_conf_threshold = inference.min_conf_threshold

        # The list of labels of the supported objects detected by the plugin
        labels = inference.labels

        # Taking the frame the stream
        frame1 = videostream.read()
        frame = frame1.copy()

        # BGR to RGB
        frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        # resizing it to feed into model
        frame_resized = cv2.resize(frame_rgb,
                                   (width_for_model, height_for_model))
        # input_data will now become 4 dimensional
        input_data = np.expand_dims(frame_resized, axis=0)
        # now it will have (batchsize, height, width, channel)

        # Normalize pixel values if using a floating model
        # (i.e. if model is non-quantized)
        if floating_model:
            input_data = (np.float32(input_data) - input_mean) / input_std

        # Perform the actual detection by running the model with the image as input
        boxes, classes, scores = inference.perform_inference(input_data)

        # we could have got  number of objects
        # but it does not work most of the times.

        # num = interpreter.get_tensor(output_details[3]['index'])[0]  #
        # Total number of detected objects (inaccurate and not needed)

        # The readings array to be inserted in the readings table
        objs = []

        # Loop over all detections and draw detection box
        #  if confidence is above minimum then  only
        #  that detected object will  be considered

        # The index of person class is zero.
        for i in range(len(scores)):
            if (scores[i] > min_conf_threshold) and (int(classes[i] == 0)):

                # Get bounding box coordinates and draw box
                # Interpreter can return coordinates that are outside of image dimensions,
                # need to force them to be within image using max() and min()

                ymin_model = round(boxes[i][0], 3)
                xmin_model = round(boxes[i][1], 3)
                ymax_model = round(boxes[i][2], 3)
                xmax_model = round(boxes[i][3], 3)

                # map the bounding boxes from model to the window
                ymin = int(max(1, (ymin_model * camera_width)))
                xmin = int(max(1, (xmin_model * camera_height)))
                ymax = int(min(camera_width, (ymax_model * camera_width)))
                xmax = int(min(camera_height, (xmax_model * camera_height)))

                # draw the rectangle on the frame
                cv2.rectangle(frame, (xmin, ymin), (xmax, ymax), (10, 255, 0),
                              2)

                # Contructing the label

                # Look up object name from "labels" array using class index
                object_name = labels[int(classes[i])]

                # Example: 'person: 72%'
                label = '%s: %d%%' % (object_name, int(scores[i] * 100))

                # Get font size
                labelSize, baseLine = cv2.getTextSize(label,
                                                      cv2.FONT_HERSHEY_SIMPLEX,
                                                      0.7, 2)

                # Make sure not to draw label too close to top of window
                label_ymin = max(ymin, labelSize[1] + 10)

                # Draw white box to put label text in
                cv2.rectangle(
                    frame, (xmin, label_ymin - labelSize[1] - 10),
                    (xmin + labelSize[0], label_ymin + baseLine - 10),
                    (255, 255, 255), cv2.FILLED)

                # Draw the text label
                cv2.putText(frame, label, (xmin, label_ymin - 7),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 0), 2)

                # the readings to be inserted into the table
                objs.append({
                    'label': labels[classes[i]],
                    'score': 100 * scores[i],
                    'bounding_box': [xmin, ymin, xmax, ymax]
                })

        # Draw framerate in corner of frame
        cv2.putText(frame, 'FPS: {0:.2f}'.format(frame_rate_calc), (30, 50),
                    cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 0), 2, cv2.LINE_AA)

        # All the results have been drawn on the frame, so it's time to display it.
        global shutdown_in_progress
        if shutdown_in_progress:
            videostream.stop()
            time.sleep(3)
            cv2.destroyWindow(window_name)
            break
        else:
            # Calculate framerate
            t_end = cv2.getTickCount()
            time1 = (t_end - t1) / freq
            frame_rate_calc = 1 / time1

            data = construct_readings(objs)
            if not shutdown_in_progress:
                async_ingest.ingest_callback(c_callback, c_ingest_ref, data)

            # show the frame on the window
            try:
                if foreground_task and enable_window:
                    cv2.imshow(window_name, frame)
                FRAME = frame.copy()
            except Exception as e:
                _LOGGER.info('exception  {}'.format(e))

            # wait for 1 milli second
            cv2.waitKey(1)