Esempio n. 1
0
    def on_OadrRegisterReport_send(self, venID):
        db_reports = MetadataReports.find({MetadataReports.owned(): True})
        requestID = "0"
        reportRequestID = 0
        reports = [{
            "duration":
            db_r.duration,
            "eiReportID":
            db_r.eiReportID,
            "data_points": [{
                "rID": dp.rID,
                "oadrMinPeriod": dp.oadrMinPeriod,
                "oadrMaxPeriod": dp.oadrMaxPeriod,
                "oadrOnChange": dp.oadrOnChange,
                "marketContext": dp.marketContext,
                "reportType": dp.reportType,
                "readingType": dp.readingType
            } for dp in DataPoint.find({DataPoint.report(): db_r._id})],
            "reportRequestID":
            "0",
            "reportSpecifierID":
            db_r.specifierID,
            "reportName":
            db_r.reportName,
            "createdDateTime":
            db_r.createdDateTime.strftime("%Y-%m-%dT%H:%M:%S")
        } for db_r in db_reports]

        return requestID, reportRequestID, reports
Esempio n. 2
0
 def register_report(self, r_request):
     reportSpecifierID = r_request.find(".//ei:reportSpecifierID",
                                        namespaces=NAMESPACES).text
     reportRequestID = r_request.find(".//ei:reportRequestID",
                                      namespaces=NAMESPACES).text
     granularity = r_request.find(".//xcal:granularity/xcal:duration",
                                  namespaces=NAMESPACES).text
     reportBackDuration = r_request.find(
         ".//ei:reportBackDuration/xcal:duration",
         namespaces=NAMESPACES).text
     relatedDataPoints = []
     for dp in r_request.findall(".//ei:specifierPayload",
                                 namespaces=NAMESPACES):
         rID = dp.find(".//ei:rID", namespaces=NAMESPACES).text
         readingType = dp.find(".//ei:readingType", namespaces=NAMESPACES)
         db_dp = DataPoint.find_one({
             DataPoint.rid(): rID,
             DataPoint.reading_type(): readingType
         })
         relatedDataPoints.append(db_dp._id)
     report = MetadataReports.find_one({
         MetadataReports.owned():
         True,
         MetadataReports.specifier_id():
         reportSpecifierID
     })
     report_subscription = ReportsToSend(report._id, reportRequestID,
                                         granularity, reportBackDuration,
                                         relatedDataPoints)
     report_subscription.save()
Esempio n. 3
0
    def auto_subscription_reports_create(self, venID):
        """ When called sends oadrCreateReport with all the reports recieved by the VEN
        """
        # get all reports from user:
        ven = VEN.find_one({VEN.ven_id(): venID})
        reports = MetadataReports.find({MetadataReports.ven(): ven._id})
        report_data_points = {}
        for report in reports:
            report_data_points[report] = {
                "data_points": DataPoint.find({DataPoint.report():
                                               report._id}),
                "devices": Device.find({Device.report(): report._id})
            }

        register_reports = []
        for report, data_points in report_data_points.items():
            register_data_points = []
            for data_point in data_points['data_points']:
                for k, v in data_point.reporting_items.items():
                    register_data_points.append(
                        (data_point, v['oadr_name'], v['reading_type']))
                    data_point.reporting_items[k]['subscribed'] = True
                    data_point.save()

            for device in data_points['devices']:
                for k, v in device.status.items():
                    register_data_points.append(
                        (device, v['oadr_name'], v['reading_type']))
                    device.status[k]['subscribed'] = True
                    device.save()

            report.subscribed = True
            report.save()
            register_reports.append((report, register_data_points))

        if register_reports:
            createReport = OadrCreateReport()
            report_types = [{
                "reportId":
                x.ei_report_id,
                "specifierId":
                x.specifier_id,
                "data_points": [{
                    'rid':
                    "{}_{}".format(
                        map_rid_device_id.find_one({
                            map_rid_device_id.device_id():
                            d[0].device_id
                        }).rid, d[1]),
                    'reading_type':
                    d[2]
                } for d in y]
            } for x, y in register_reports]
            params = {"requestID": "0", "report_types": report_types}
            from oadr_core.vtn.server_blueprint import send_message
            response = send_message(createReport, ven, params)
Esempio n. 4
0
    def on_OadrCreatedReport_send(self, venID):
        ven = VEN.find_one({VEN.ven_id: venID})
        if ven is None:
            raise InvalidVenException
        pending_reports = []
        for rep in MetadataReports.find({MetadataReports.ven: ven._id}):
            pending_reports.extend([
                r.reportRequestID
                for r in ReportsToSend.find({ReportsToSend.report: rep._id})
            ])

        return "200", "OK", pending_reports
Esempio n. 5
0
    def on_OadrCreateReport_recieved(self, venID, reportRequests):
        ven = VEN.get_ven(venID)
        # respond
        if ven is None:
            raise InvalidVenException

        for r_request in reportRequests:
            self.register_report(r_request)

        pending_reports = []
        for rep in MetadataReports.find({MetadataReports.ven: ven._id}):
            pending_reports.extend([
                r.reportRequestID
                for r in ReportsToSend.find({ReportsToSend.report: rep._id})
            ])

        return "200", "OK", pending_reports
Esempio n. 6
0
    def on_OadrCancelReport_recieved(self, venID, cancel_reports,
                                     report_to_follow):
        # respond
        ven = VEN.get_ven(venID)
        if ven is None:
            raise InvalidVenException

        for i in range(0, len(cancel_reports)):
            report_request = cancel_reports[i]
            report = ReportsToSend.find_one(
                {ReportsToSend.report_request_id: report_request})
            if report_to_follow[i]:
                report.canceled = True
                report.save()
            else:
                report.delete()
        pending_reports = []
        for rep in MetadataReports.find({MetadataReports.ven: ven._id}):
            pending_reports.extend([
                r.reportRequestID
                for r in ReportsToSend.find({ReportsToSend.report: rep._id})
            ])

        return "200", "OK", pending_reports
    def parse(self, oadrReport, ven, *args, **kwargs):
        eiReportID = oadrReport.find('.//ei:eiReportID',
                                     namespaces=NAMESPACES).text
        reportSpecifierID = oadrReport.find('.//ei:reportSpecifierID',
                                            namespaces=NAMESPACES).text
        duration = oadrReport.find('.//xcal:duration/xcal:duration',
                                   namespaces=NAMESPACES).text
        reportName = oadrReport.find('.//ei:reportName',
                                     namespaces=NAMESPACES).text
        createdDateTime = datetime.strptime(
            oadrReport.find('.//ei:createdDateTime',
                            namespaces=NAMESPACES).text[:19],
            "%Y-%m-%dT%H:%M:%S")

        report = MetadataReports(ven._id, eiReportID, reportSpecifierID,
                                 duration, reportName, createdDateTime)
        report.save()
        for d in oadrReport.findall('.//oadr:oadrReportDescription',
                                    namespaces=NAMESPACES):
            rID = d.find('.//ei:rID', namespaces=NAMESPACES).text
            reportSubject_ = d.find(
                ".//ei:reportSubject/power:endDeviceAsset/power:mrid",
                namespaces=NAMESPACES)
            reportSubject = reportSubject_.text if reportSubject_ is not None else None

            reportDataSource_ = d.find(".//ei:reportDataSource/ei:resourceID",
                                       namespaces=NAMESPACES)
            reportDataSource = reportDataSource_.text if reportDataSource_ is not None else None

            reportType_ = d.find(".//ei:reportType", namespaces=NAMESPACES)
            reportType = reportType_.text

            #get next item after report_type, if it is not readingType we have a itemBase
            reportItem_ = reportType_.getnext()
            reportItem = {}
            if reportItem_.xpath('local-name()') != "readingType":
                for c in reportItem_.getchildren():
                    reportItem[c.xpath('local-name()')] = c.text
            readingType = d.find(".//ei:readingType",
                                 namespaces=NAMESPACES).text
            marketContext = d.find(
                ".//emix:marketContext", namespaces=NAMESPACES).text if d.find(
                    ".//emix:marketContext",
                    namespaces=NAMESPACES) is not None else None
            minSampling = d.find(
                ".//oadr:oadrMinPeriod", namespaces=NAMESPACES).text if d.find(
                    ".//oadr:oadrMinPeriod",
                    namespaces=NAMESPACES) is not None else None
            maxSampling = d.find(
                ".//oadr:oadrMaxPeriod", namespaces=NAMESPACES).text if d.find(
                    ".//oadr:oadrMinPeriod",
                    namespaces=NAMESPACES) is not None else None
            onChange = d.find(".//oadr:oadrOnChange",
                              namespaces=NAMESPACES).text if d.find(
                                  ".//oadr:oadrOnChange",
                                  namespaces=NAMESPACES) is not None else None

            phisical_device, pdn, groupID, spaces, load, ln, metric = parse_rid(
                rID)
            deviceID = map_rid_device_id.get_or_create_deviceID(rID)

            reporting_capabilities = {
                convert_snake_case(metric): {
                    "report_type": reportType,
                    "units": reportItem,
                    "reading_type": readingType,
                    "market_context": marketContext,
                    "min_period": minSampling,
                    "max_sampling": maxSampling,
                    "on_change": onChange,
                    "subscribed": False,
                    "oadr_name": metric
                }
            }
            deviceID = map_rid_device_id.get_or_create_deviceID(rID)
            data_point = DataPoint.get_or_create(deviceID, report._id, load,
                                                 reportSubject,
                                                 reportDataSource, spaces,
                                                 reporting_capabilities)
            data_point.save()
Esempio n. 8
0
    def parse(self, oadrReport, ven, *args, **kwargs):
        eiReportID = oadrReport.find('.//ei:eiReportID',
                                     namespaces=NAMESPACES).text
        reportSpecifierID = oadrReport.find('.//ei:reportSpecifierID',
                                            namespaces=NAMESPACES).text
        duration = oadrReport.find('.//xcal:duration/xcal:duration',
                                   namespaces=NAMESPACES).text
        reportName = oadrReport.find('.//ei:reportName',
                                     namespaces=NAMESPACES).text
        createdDateTime = datetime.strptime(
            oadrReport.find('.//ei:createdDateTime',
                            namespaces=NAMESPACES).text[:19],
            "%Y-%m-%dT%H:%M:%S")
        report = MetadataReports(ven._id, eiReportID, reportSpecifierID,
                                 duration, reportName, createdDateTime)
        report.save()
        for d in oadrReport.findall('.//oadr:oadrReportDescription',
                                    namespaces=NAMESPACES):
            rID = d.find('.//ei:rID', namespaces=NAMESPACES).text
            reportSubject_ = d.find(
                ".//ei:reportSubject/power:endDeviceAsset/power:mrid",
                namespaces=NAMESPACES)
            reportSubject = reportSubject_.text if reportSubject_ is not None else None

            reportDataSource_ = d.find(".//ei:reportDataSource/ei:resourceID",
                                       namespaces=NAMESPACES)
            reportDataSource = reportDataSource_.text if reportDataSource_ is not None else None

            reportType_ = d.find(".//ei:reportType", namespaces=NAMESPACES)
            reportType = reportType_.text

            #get next item after report_type, if it is not readingType we have a itemBase
            reportItem_ = reportType_.getnext()
            reportItem = {}
            if reportItem_.xpath('local-name()') != "readingType":
                for c in reportItem_.getchildren():
                    reportItem[c.xpath('local-name()')] = c.text
            readingType = d.find(".//ei:readingType",
                                 namespaces=NAMESPACES).text
            marketContext = d.find(
                ".//emix:marketContext", namespaces=NAMESPACES).text if d.find(
                    ".//emix:marketContext",
                    namespaces=NAMESPACES) is not None else None
            minSampling = d.find(
                ".//oadr:oadrMinPeriod", namespaces=NAMESPACES).text if d.find(
                    ".//oadr:oadrMinPeriod",
                    namespaces=NAMESPACES) is not None else None
            maxSampling = d.find(
                ".//oadr:oadrMaxPeriod", namespaces=NAMESPACES).text if d.find(
                    ".//oadr:oadrMinPeriod",
                    namespaces=NAMESPACES) is not None else None
            onChange = d.find(".//oadr:oadrOnChange",
                              namespaces=NAMESPACES).text if d.find(
                                  ".//oadr:oadrOnChange",
                                  namespaces=NAMESPACES) is not None else None
            phisical_device, pdn, groupID, spaces, load, ln, metric = parse_rid(
                rID)
            if metric not in status_mapping.keys():
                continue
            status_item = {
                status_mapping[metric]: {
                    "value": None,
                    "report_type": reportType,
                    "units": reportItem,
                    "reading_type": readingType,
                    "market_context": marketContext,
                    "min_period": minSampling,
                    "max_sampling": maxSampling,
                    "on_change": onChange,
                    "subscribed": False,
                    "oadr_name": metric
                }
            }
            deviceID = map_rid_device_id.get_or_create_deviceID(rID)
            device = Device.get_or_create(report._id, deviceID, load, spaces,
                                          reportSubject, reportDataSource,
                                          status_item)
            device.save()
        #notify restfulAPI that new DERS are available
        token = get_middleware_token()
        headers = {'Authorization': token}
        requests.get("{}/{}/{}".format(NOTIFICATION_REST_URL,
                                       '1/notify/der_installed', report._id),
                     headers=headers,
                     verify=NOTIFICATION_REST_CERT)