def device_to_amon(self, device_ids): """Convert a device to AMON. { "utilityId": "Utility Id", "externalId": required string UUID, "meteringPointId": required string UUID, "metadata": { "max": "Max number", "serial": "Device serial", "owner": "empresa/client" }, } """ O = self.O res = [] if not hasattr(device_ids, '__iter__'): device_ids = [device_ids] for dev_id in device_ids: dev = O.GiscedataLecturesComptador.browse(dev_id) if dev.propietat == "empresa": dev.propietat = "company" res.append(remove_none({ "utilityId": "1", "externalId": make_uuid('giscedata.lectures.comptador', dev_id), "meteringPointId": make_uuid('giscedata.cups.ps', dev.polissa.cups.name), "metadata": { "max": dev.giro, "serial": dev.name, "owner": dev.propietat, } })) return res
def device_to_amon(self, device_ids): """Convert a device to AMON. { "utilityId": "Utility Id", "externalId": required string UUID, "meteringPointId": required string UUID, "metadata": { "max": "Max number", "serial": "Device serial", "owner": "empresa/client" }, } """ O = self.O res = [] if not hasattr(device_ids, '__iter__'): device_ids = [device_ids] for dev_id in device_ids: dev = O.GiscedataLecturesComptador.browse(dev_id) if dev.propietat == "empresa": dev.propietat = "company" res.append(remove_none({ "utilityId": "1", "externalId": make_uuid('giscedata.lectures.comptador', dev_id), "meteringPointId": make_uuid('giscedata.cups.ps', dev.polissa.cups.name), "metadata": { "max": dev.giro, "serial": dev.name, "owner": dev.propietat, } })) return res
def cch_to_amon(self, cups, date_last_uploaded, collection_to_find, consolidate): """ Mongo F5D measure to AMON "measurements": [ { "timestamp": "2013-10-11T16:37:05Z", "type": "electricityConsumption", "value": 7.0, "consolidated": True }] """ #Get measures CCH from Mongo O = self.O collection = self.mongo[collection_to_find] measures = collection.find({"name": cups, "datetime" : {"$gt":date_last_uploaded}}) if measures.count() == 0: return {}, {} #Build JSON cups = O.GiscedataCupsPs.get([('name', '=', cups)]) polissa_id = max(cups.polisses.polissa_id.id) meter_name = O.GiscedataLecturesComptador.read([('polissa', '=', polissa_id)])[0]['name'] device_uuid = make_uuid('giscedata.lectures.comptador', meter_name) metering_uuid = make_uuid('giscedata.cups.ps', cups) res = {} res['meteringPointId'] = metering_uuid res['deviceId'] = device_uuid res['measurements'] = [] res['readings'] = [] for measure in measures: measure_json = {} measure_json['type'] = "electricityConsumption" measure_json['consolidated'] = consolidate measure_json['timestamp'] = datetime.strftime(measure['datetime'], "%Y-%m-%dT%H:%M:%SZ") measure_json['value'] = measure.get('ai', 0.0) res['measurements'].append(measure_json) reading_json = {} reading_json['type'] = measure_json['type'] reading_json['period'] = "INSTANT" reading_json['unit'] = 'Wh' res['readings'].append(reading_json) return res, measure_json['timestamp']
def device_to_amon(self, device_ids): compt_obj = self.O.GiscedataLecturesComptador devices = [] comptador_fields = ['data_alta', 'data_baixa'] for comptador in compt_obj.read(device_ids, comptador_fields): devices.append({ 'dateStart': make_utc_timestamp(comptador['data_alta']), 'dateEnd': make_utc_timestamp(comptador['data_baixa']), 'deviceId': make_uuid('giscedata.lectures.comptador', compt_obj.build_name_tg(comptador['id'])) }) return devices
def get_cups_from_device(self, device_id): O = self.O # Remove brand prefix and right zeros serial = get_device_serial(device_id) if serial in CUPS_CACHE: return CUPS_CACHE[serial] else: # Search de meter cid = O.GiscedataLecturesComptador.search([ ('name', '=', serial) ], context={'active_test': False}) if not cid: res = False else: cid = O.GiscedataLecturesComptador.browse(cid[0]) res = make_uuid('giscedata.cups.ps', cid.polissa.cups.name) CUPS_UUIDS[res] = cid.polissa.cups.id CUPS_CACHE[serial] = res return res
def enqueue_measures(bucket=500): # First get all the contracts that are in sync O = setup_peek() em = setup_empowering_api() # TODO: Que fem amb les de baixa? les agafem igualment? només les que # TODO: faci menys de X que estan donades de baixa? pids = O.GiscedataPolissa.search([('etag', '!=', False)]) # Comptadors que tingui aquesta pòlissa i que siguin de telegestió cids = O.GiscedataLecturesComptador.search([ ('tg', '=', 1), ('polissa', 'in', pids) ], context={'active_test': False}) for comptador in O.GiscedataLecturesComptador.read(cids, ['name']): tg_name = O.GiscedataLecturesComptador.build_name_tg(comptador['id']) deviceId = make_uuid('giscedata.lectures.comptador', tg_name) logger.info("Buscant l'última lectura pel comptador: %s " "device_id %s" % (tg_name, deviceId)) res = em.amon_measures_measurements().get(where='"deviceId"=="%s"' % deviceId, sort='[("timestamp", -1)]')['_items'] search_params = [ ('name', '=', tg_name), ('type', '=', 'day'), ('value', '=', 'a'), ('valid', '=', 1), ('period', '=', 0) ] if not res: # Pujar totes logger.info("Les pugem totes") else: res = res[0] local_ts = make_local_timestamp(res['timestamp']) logger.info(u"Última lectura trobada: %s" % local_ts) search_params.append(('date_end', '>', local_ts)) measures_ids = O.TgBilling.search(search_params, limit=0, order="date_end asc") logger.info("S'han trobat %s mesures per pujar" % len(measures_ids)) popper = Popper(measures_ids) pops = popper.pop(bucket) while pops: j = push_amon_measures.delay(pops) logger.info("Job id:%s | %s/%s/%s" % ( j.id, tg_name, len(pops), len(popper.items)) ) pops = popper.pop(bucket)
def cups_to_amon(self, cups_id): cups_obj = self.O.GiscedataCupsPs muni_obj = self.O.ResMunicipi state_obj = self.O.ResCountryState sips_obj = self.O.GiscedataSipsPs cups_fields = ['id_municipi', 'tv', 'nv', 'cpa', 'cpo', 'pnp', 'pt', 'name', 'es', 'pu', 'dp'] if 'empowering' in cups_obj.fields_get(): cups_fields.append('empowering') cups = cups_obj.read(cups_id, cups_fields) muni_id = cups['id_municipi'][0] ine = muni_obj.read(muni_id, ['ine'])['ine'] state_id = muni_obj.read(muni_id, ['state'])['state'][0] state = state_obj.read(state_id, ['code'])['code'] dp = cups['dp'] if not dp: sips_id = sips_obj.search([('name', '=', cups['name'])]) if sips_id: dp = sips_obj.read(int(sips_id[0]), ['codi_postal'])['codi_postal'] else: if ine in ine_to_dp: dp = ine_to_dp[ine] res = { 'meteringPointId': make_uuid('giscedata.cups.ps', cups['name']), 'customer': { 'address': { 'city': cups['id_municipi'][1], 'cityCode': ine, 'countryCode': 'ES', #'street': get_street_name(cups), 'postalCode': dp, 'provinceCode': state } }, 'experimentalGroupUserTest': False, 'experimentalGroupUser': cups.get('empowering', False) } return res
def cups_to_amon(self, cups_id): cups_obj = self.O.GiscedataCupsPs muni_obj = self.O.ResMunicipi cups_fields = ['id_municipi', 'tv', 'nv', 'cpa', 'cpo', 'pnp', 'pt', 'name', 'es', 'pu', 'dp'] cups = cups_obj.read(cups_id, cups_fields) ine = muni_obj.read(cups['id_municipi'][0], ['ine'])['ine'] res = { 'meteringPointId': make_uuid('giscedata.cups.ps', cups['name']), 'customer': { 'address': { 'city': cups['id_municipi'][1], 'cityCode': ine, 'countryCode': 'ES', 'street': get_street_name(cups), 'postalCode': cups['dp'] } } } return res
def get_cups_from_device(self, device_id): def get_device_serial(device_id): field_to_read = 'name' return O.GiscedataLecturesComptador.read([device_id],[field_to_read])[0][field_to_read] O = self.O # Remove brand prefix and right zeros serial = get_device_serial(device_id) if serial in CUPS_CACHE: return CUPS_CACHE[serial] else: # Search de meter cid = O.GiscedataLecturesComptador.search([ ('name', '=', serial) ], context={'active_test': False}) if not cid: res = False else: cid = O.GiscedataLecturesComptador.browse(cid[0]) res = make_uuid('giscedata.cups.ps', cid.polissa.cups.name) CUPS_UUIDS[res] = cid.polissa.cups.id CUPS_CACHE[serial] = res return res
def cups_to_amon(self, cups_id): cups_obj = self.O.GiscedataCupsPs muni_obj = self.O.ResMunicipi cups_fields = ['id_municipi', 'tv', 'nv', 'cpa', 'cpo', 'pnp', 'pt', 'name', 'es', 'pu', 'dp'] if 'empowering' in cups_obj.fields_get(): cups_fields.append('empowering') cups = cups_obj.read(cups_id, cups_fields) ine = muni_obj.read(cups['id_municipi'][0], ['ine'])['ine'] res = { 'meteringPointId': make_uuid('giscedata.cups.ps', cups['name']), 'customer': { 'address': { 'city': cups['id_municipi'][1], 'cityCode': ine, 'countryCode': 'ES', 'street': get_street_name(cups), 'postalCode': cups['dp'] } }, 'experimentalGroupUserTest': 0, 'experimentalGroupUser': int(cups.get('empowering', 0)) } return res
def profile_to_amon(self, profiles): """Return a list of AMON readinds. { "utilityId": "Utility Id", "deviceId": "c1810810-0381-012d-25a8-0017f2cd3574", "meteringPointId": "c1759810-90f3-012e-0404-34159e211070", "readings": [ { "type_": "electricityConsumption", "unit": "kWh", "period": "INSTANT", }, { "type_": "electricityKiloVoltAmpHours", "unit": "kVArh", "period": "INSTANT", } ], "measurements": [ { "type_": "electricityConsumption", "timestamp": "2010-07-02T11:39:09Z", # UTC "value": 7 }, { "type_": "electricityKiloVoltAmpHours", "timestamp": "2010-07-02T11:44:09Z", # UTC "value": 6 } ] } """ O = self.O res = [] if not hasattr(profiles, '__iter__'): profiles = [profiles] for profile in profiles: mp_uuid = self.get_cups_from_device(profile['name']) if not mp_uuid: logger.info("No mp_uuid for &s" % profile['name']) continue device_uuid = make_uuid('giscedata.lectures.comptador', profile['name']) res.append({ "deviceId": device_uuid, "meteringPointId": mp_uuid, "readings": [ { "type": "electricityConsumption", "unit": "%sWh" % UNITS[profile.get('magn', 1000)], "period": "CUMULATIVE", }, { "type": "electricityKiloVoltAmpHours", "unit": "%sVArh" % UNITS[profile.get('magn', 1000)], "period": "CUMULATIVE", } ], "measurements": [ { "type": "electricityConsumption", "timestamp": make_utc_timestamp(profile['date_end']), "value": float(profile['ai']) }, { "type": "electricityKiloVoltAmpHours", "timestamp": make_utc_timestamp(profile['date_end']), "value": float(profile['r1']) } ] }) return res
def contract_to_amon(self, contract_ids, context=None): """Converts contracts to AMON. { "payerId":"payerID-123", "ownerId":"ownerID-123", "signerId":"signerID-123", "power":123, "dateStart":"2013-10-11T16:37:05Z", "dateEnd":null, "contractId":"contractID-123", "customer":{ "customerId":"payerID-123", "address":{ "city":"city-123", "cityCode":"cityCode-123", "countryCode":"ES", "country":"Spain", "street":"street-123", "postalCode":"postalCode-123" } }, "meteringPointId":"c1759810-90f3-012e-0404-34159e211070", "devices":[ { "dateStart":"2013-10-11T16:37:05Z", "dateEnd":null, "deviceId":"c1810810-0381-012d-25a8-0017f2cd3574" } ], "version":1, "activityCode":"activityCode", "tariffId":"tariffID-123", } """ O = self.O if not context: context = {} res = [] pol = O.GiscedataPolissa modcon_obj = O.GiscedataPolissaModcontractual if not hasattr(contract_ids, '__iter__'): contract_ids = [contract_ids] fields_to_read = ['modcontractual_activa', 'name', 'cups', 'comptadors', 'state'] for polissa in pol.read(contract_ids, fields_to_read): if polissa['state'] in ('esborrany', 'validar'): continue if 'modcon_id' in context: modcon = modcon_obj.read(context['modcon_id']) elif polissa['modcontractual_activa']: modcon = modcon_obj.read(polissa['modcontractual_activa'][0]) else: logger.error("Problema amb la polissa %s" % polissa['name']) continue contract = { 'ownerId': make_uuid('res.partner', modcon['titular'][0]), 'payerId': make_uuid('res.partner', modcon['pagador'][0]), 'dateStart': make_utc_timestamp(modcon['data_inici']), 'dateEnd': make_utc_timestamp(modcon['data_final']), 'contractId': polissa['name'], 'tariffId': modcon['tarifa'][1], 'power': int(modcon['potencia'] * 1000), 'version': int(modcon['name']), 'activityCode': modcon['cnae'] and modcon['cnae'][1] or None, 'customer': { 'customerId': make_uuid('res.partner', modcon['titular'][0]), }, 'devices': self.device_to_amon(polissa['comptadors']) } cups = self.cups_to_amon(modcon['cups'][0]) recursive_update(contract, cups) res.append(remove_none(contract, context)) return res
def profile_to_amon(self, profiles): """Return a list of AMON readinds. { "utilityId": "Utility Id", "deviceId": "c1810810-0381-012d-25a8-0017f2cd3574", "meteringPointId": "c1759810-90f3-012e-0404-34159e211070", "readings": [ { "type_": "touElectricityConsumption", "unit": "kWh", "period": "INSTANT", }, { "type_": "touElectricityKiloVoltAmpHours", "unit": "kVArh", "period": "INSTANT", } ], "measurements": [ { "type_": "touElectricityConsumption", "timestamp": "2010-07-02T11:39:09Z", # UTC "value": 7 }, { "type_": "touElectricityKiloVoltAmpHours", "timestamp": "2010-07-02T11:44:09Z", # UTC "value": 6 } ] } """ O = self.O res = [] if not hasattr(profiles, '__iter__'): profiles = [profiles] for profile in profiles: mp_uuid = self.get_cups_from_device(profile['name']) if not mp_uuid: logger.info("No mp_uuid for &s" % profile['name']) continue device_uuid = make_uuid('giscedata.lectures.comptador', profile['name']) res.append({ "deviceId": device_uuid, "meteringPointId": mp_uuid, "readings": [ { "type": "touElectricityConsumption", "unit": "%sWh" % UNITS[profile.get('magn', 1000)], "period": "CUMULATIVE", }, { "type": "touElectricityKiloVoltAmpHours", "unit": "%sVArh" % UNITS[profile.get('magn', 1000)], "period": "CUMULATIVE", } ], "measurements": [ { "type": "touElectricityConsumption", "timestamp": make_utc_timestamp(profile['date_end']), "value": float(profile['ai']) }, { "type": "touElectricityKiloVoltAmpHours", "timestamp": make_utc_timestamp(profile['date_end']), "value": float(profile['r1']) } ] }) return res
def device_to_amon(self, start_date, end_date, device_id): return { 'dateStart': make_utc_timestamp(start_date), 'dateEnd': make_utc_timestamp(end_date), 'deviceId': make_uuid('giscedata.lectures.comptador', device_id) }
def contract_to_amon(self, contract_ids, context=None): """Converts contracts to AMON. { "contractId": "contractId-123", "ownerId": "ownerId-123", "payerId": "payerId-123", "signerId": "signerId-123", "power": 123, "power_": { "power": 123, "dateStart": "2014-10-11T00:00:00Z", "dateEnd": null, }, "powerHistory": [ { "power": 122, "dateStart": "2013-10-11T16:37:05Z", "dateEnd": "2014-10-10T23:59:59Z" } ], "dateStart": "2013-10-11T16:37:05Z", "dateEnd": null, "climaticZone": "climaticZoneId-123", "weatherStationId": "weatherStatioId-123", "version": 1, "activityCode": "activityCode", "tariffId": "tariffID-123", "tariff_": { "tariffId": "tariffID-123", "dateStart": "2014-10-11T00:00:00Z", "dateEnd": null, }, "tariffHistory": [ { "tariffId": "tariffID-122", "dateStart": "2013-10-11T16:37:05Z", "dateEnd": "2014-10-10T23:59:59Z" } ], "meteringPointId": "c1759810-90f3-012e-0404-34159e211070", "experimentalGroupUser": True, "experimentalGroupUserTest": True, "activeUser": True, "activeUserDate": "2014-10-11T16:37:05Z", "customer": { "customerId": "customerId-123", "address": { "buildingId": "building-123", "city": "city-123", "cityCode": "cityCode-123", "countryCode": "ES", "country": "Spain", "street": "street-123", "postalCode": "postalCode-123", "province": "Barcelona", "provinceCode": "provinceCode-123", "parcelNumber": "parcelNumber-123" }, "buildingData": { "buildingConstructionYear": 2014, "dwellingArea": 196, "propertyType": "primary", "buildingType": "Apartment", "dwellingPositionInBuilding": "first_floor", "dwellingOrientation": "SE", "buildingWindowsType": "double_panel", "buildingWindowsFrame": "PVC", "buildingCoolingSource": "electricity", "buildingHeatingSource": "district_heating", "buildingHeatingSourceDhw": "gasoil", "buildingSolarSystem": "not_installed" }, "profile": { "totalPersonsNumber": 3, "minorsPersonsNumber": 0, "workingAgePersonsNumber": 2, "retiredAgePersonsNumber": 1, "malePersonsNumber": 2, "femalePersonsNumber": 1, "educationLevel": { "edu_prim": 0, "edu_sec": 1, "edu_uni": 1, "edu_noStudies": 1 } }, "customisedGroupingCriteria": { "criteria_1": "CLASS 1", "criteria_2": "XXXXXXX", "criteria_3": "YYYYYYY" }, "customisedServiceParameters": { "OT701": "p1;P2;px" } }, "devices": [ { "dateStart": "2013-10-11T16:37:05Z", "dateEnd": null, "deviceId": "c1810810-0381-012d-25a8-0017f2cd3574" } ] } """ O = self.O if not context: context = {} first = context.get('first', None) res = [] pol = O.GiscedataPolissa modcon_obj = O.GiscedataPolissaModcontractual cups_obj = O.GiscedataCupsPs muni_obj = O.ResMunicipi building_obj = O.EmpoweringCupsBuilding profile_obj = O.EmpoweringModcontractualProfile service_obj = O.EmpoweringModcontractualService if not hasattr(contract_ids, '__iter__'): contract_ids = [contract_ids] fields_to_read = ['modcontractual_activa', 'modcontractuals_ids', 'name', 'cups', 'comptadors', 'state', 'data_alta', 'data_baixa'] for polissa in pol.read(contract_ids, fields_to_read): if polissa['state'] in ('esborrany', 'validar'): continue modcon_id = None if 'modcon_id' in context: modcon_id = context['modcon_id'] elif polissa['modcontractual_activa']: modcon_id = polissa['modcontractual_activa'][0] else: logger.error("Problema amb la polissa %s" % polissa['name']) continue modcon = modcon_obj.read(modcon_id) modcons_id = polissa['modcontractuals_ids'] def get_first(x): return x[0] if x else None building_id = get_first(building_obj.search([('cups_id', '=', modcon['cups'][0])])) profile_id = get_first(profile_obj.search([('modcontractual_id', '=', modcon_id)])) service_id = get_first(service_obj.search([('modcontractual_id', '=', modcon_id)])) contract = { 'ownerId': make_uuid('res.partner', modcon['titular'][0]), 'payerId': make_uuid('res.partner', modcon['pagador'][0]), 'dateStart': make_utc_timestamp(polissa['data_alta']), 'dateEnd': make_utc_timestamp(eofday(polissa['data_baixa'])), 'contractId': polissa['name'], 'tariffId': modcon['tarifa'][1], 'tariff_': self.tariff_to_amon(modcons_id), 'tariffHistory': self.tariffHistory_to_amon(modcons_id), 'power': int(modcon['potencia'] * 1000), 'power_': self.power_to_amon(modcons_id), 'powerHistory': self.powerHistory_to_amon(modcons_id), 'version': int(modcon['name']), 'climaticZone': self.cups_to_climaticZone(modcon['cups'][0]), 'activityCode': modcon['cnae'] and modcon['cnae'][1].split(' - ')[0] or None, 'customer': { 'customerId': make_uuid('res.partner', modcon['titular'][0]), 'buildingData': self.building_to_amon(building_id), 'profile': self.eprofile_to_amon(profile_id), 'customisedServiceParameters': self.service_to_amon(service_id) }, 'devices': self.devices_to_amon(polissa['comptadors']), 'report': self.report_to_amon(polissa['data_alta'], modcon['pagador'][0]) } cups = self.cups_to_amon(modcon['cups'][0]) recursive_update(contract, cups) res.append(remove_none(contract, context)) return res
def measure_to_amon(self, measures): """Return a list of AMON readinds. { "utilityId": "Utility Id", "deviceId": "c1810810-0381-012d-25a8-0017f2cd3574", "meteringPointId": "c1759810-90f3-012e-0404-34159e211070", "readings": [ { "type_": "touElectricityConsumption", "unit": "kWh", "period": "INSTANT", }, { "type_": "touElectricityKiloVoltAmpHours", "unit": "kVArh", "period": "INSTANT", } ], "measurements": [ { "type_": "touElectricityConsumption", "timestamp": "2010-07-02T11:39:09Z", # UTC "value": 7 }, { "type_": "touElectricityKiloVoltAmpHours", "timestamp": "2010-07-02T11:44:09Z", # UTC "value": 6 } ] } """ O = self.O res = [] if not hasattr(measures, '__iter__'): measures = [measures] for measure in measures: mp_uuid = self.get_cups_from_device(measure['comptador'][0]) if not mp_uuid: logger.info("No mp_uuid for &s" % measure['comptador'][0]) continue device_uuid = make_uuid('giscedata.lectures.comptador', measure['comptador'][0]) period = measure['periode'][1][measure['periode'][1].find("(")+1:measure['periode'][1].find(")")].lower() if measure['tipus'] == 'A': res.append({ "deviceId": device_uuid, "meteringPointId": mp_uuid, "readings": [ { "type": "touElectricityConsumption", "unit": "%sWh" % UNITS[measure.get('magn', 1000)], "period": "CUMULATIVE", } ], "measurements": [ { "type": "touElectricityConsumption", "timestamp": make_utc_timestamp(measure['date_end']), "values": { period: float(measure['lectura']) } } ] }) elif measure['tipus'] == 'R': res.append({ "deviceId": device_uuid, "meteringPointId": mp_uuid, "readings": [ { "type": "touElectricityKiloVoltAmpHours", "unit": "%sVArh" % UNITS[measure.get('magn', 1000)], "period": "CUMULATIVE", "dailyPeriod": period } ], "measurements": [ { "type": "touElectricityKiloVoltAmpHours", "timestamp": make_utc_timestamp(measure['date_end']), "value": float(measure['lectura']), "dailyPeriod": period } ] }) return res