Esempio n. 1
0
    def read_state(self, days=1) -> EnergyData:
        raw = self._get_daily_data(days)
        state = [{
            'date': a,
            'produced': b
        } for a, b in zip(raw['datetime'], raw['production'])]
        device_meta = {
            'manufacturer': 'Unknown',
            'model': 'Unknown',
            'serial_number': 'Unknown',
            'geolocation': (raw['site']['latitude'], raw['site']['longitude'])
        }
        device = Device(**device_meta)
        accumulated_power = state[-1]['produced'] * pow(10, 3)

        # instance of mini utc class (tzinfo)
        cest = CEST()

        now = datetime.datetime.now().astimezone()
        access_timestamp = now.isoformat()

        measurement_timestamp = datetime.datetime.strptime(
            state[-1]['date'], "%Y-%m-%d")
        measurement_timestamp = measurement_timestamp.replace(
            tzinfo=cest).isoformat()  # forcing the france timezone

        return EnergyData(device, access_timestamp, raw, accumulated_power,
                          measurement_timestamp)
Esempio n. 2
0
 def read_state(self, path=None) -> EnergyData:
     if path:
         tree = ElementTree.parse('test_examples/EumelXMLv2.1.1.xml')
         with open(path) as file:
             raw = file.read()
     else:
         http_packet = requests.get(self.eumel_api_url, auth=self.auth)
         raw = http_packet.content.decode()
         tree = ElementTree.ElementTree(ElementTree.fromstring(raw))
     tree_root = tree.getroot()
     tree_header = tree_root[0].attrib
     tree_leaves = {
         child.attrib['id']: child.text
         for child in tree_root[0][1]
     }
     device = Device(manufacturer=tree_header['man'],
                     model=tree_header['mod'],
                     serial_number=tree_header['sn'],
                     geolocation=None)
     access_timestamp = int(time.time())
     time_format = '%Y-%m-%dT%H:%M:%SZ'
     accumulated_power = float(tree_leaves['TotWhImp'])
     measurement_timestamp = int(
         time.mktime(time.strptime(tree_header['t'], time_format)))
     return EnergyData(device, access_timestamp, str(raw),
                       accumulated_power, measurement_timestamp)
Esempio n. 3
0
 def read_state(self, path=None) -> EnergyData:
     # raw
     if path:
         tree = ElementTree.parse('test_examples/EumelXMLv2.1.1.xml')
         with open(path) as file:
             raw = file.read()
     else:
         http_packet = requests.get(self.eumel_api_url, auth=self.auth)
         raw = http_packet.content.decode()
         tree = ElementTree.fromstring(raw)
     tree_root = tree.getroot()
     tree_header = tree_root[0].attrib
     tree_leaves = {
         child.attrib['id']: child.text
         for child in tree_root[0][1]
     }
     # device
     device = Device(manufacturer=tree_header['man'],
                     model=tree_header['mod'],
                     serial_number=tree_header['sn'],
                     geolocation=None)
     # accumulated power
     accumulated_power = int(float(tree_leaves['TotWhImp']))
     # access_epoch
     now = datetime.datetime.now().astimezone()
     access_epoch = calendar.timegm(now.timetuple())
     # measurement epoch
     measurement_timestamp = datetime.datetime.strptime(
         tree_header['t'], "%Y-%m-%dT%H:%M:%S%z")
     measurement_epoch = calendar.timegm(measurement_timestamp.timetuple())
     return EnergyData(device=device,
                       access_epoch=access_epoch,
                       raw=str(raw),
                       accumulated_energy=accumulated_power,
                       measurement_epoch=measurement_epoch)
Esempio n. 4
0
    def read_state(self) -> EnergyData:
        raw = self._get_daily_data()
        # get the object with the right site_id
        state = {}

        for specific_site in raw["production"]:
            if specific_site["assetPublicAddress"] == self.site:
                state = specific_site
                break

        # build the device object
        device_meta = {
            'manufacturer': 'Unknown',
            'model': 'Unknown',
            'serial_number': 'Unknown',
            'geolocation': (0, 0)
        }
        device = Device(**device_meta)

        # get produced energy from filtered object
        # accumulated_power = specific_site['energy']['data']
        accumulated_power = specific_site['amount']

        # build access_timestamp
        now = datetime.datetime.now().astimezone()
        access_timestamp = now.isoformat()

        # build measurement_timestamp
        measurement_timestamp = datetime.datetime.strptime(
            specific_site['endTime'], '%Y-%m-%dT%H:%M:%S.%fZ')
        measurement_timestamp = measurement_timestamp.replace(
            tzinfo=UTC()).isoformat()

        return EnergyData(device, access_timestamp, raw, accumulated_power,
                          measurement_timestamp)
Esempio n. 5
0
 def read_state(self) -> EnergyData:
     # raw
     raw = self._get_daily_data()
     data = {}  # get the object with the right site_id
     for specific_site in raw["production"]:
         if specific_site["assetPublicAddress"] == self.site:
             data = specific_site
             break
     # device
     device_meta = {
         'manufacturer': 'Unknown',
         'model': 'Unknown',
         'serial_number': 'Unknown',
         'geolocation': (0, 0)
     }
     device = Device(**device_meta)
     # accumulated power in ?
     accumulated_power = int(data['amount'])
     # access_epoch
     now = datetime.datetime.now().astimezone()
     access_epoch = calendar.timegm(now.timetuple())
     # measurement epoch
     measurement_timestamp = datetime.datetime.strptime(data['endTime'], '%Y-%m-%dT%H:%M:%S.%fZ')
     measurement_epoch = calendar.timegm(measurement_timestamp.timetuple())
     return EnergyData(device=device, access_epoch=access_epoch, raw=raw, accumulated_energy=accumulated_power,
                       measurement_epoch=measurement_epoch)
Esempio n. 6
0
 def read_state(self, days=1) -> EnergyData:
     # raw
     raw = self._get_daily_data(days)
     data = [{
         'date': a,
         'produced': b
     } for a, b in zip(raw['datetime'], raw['production'])]
     # device
     device_meta = {
         'manufacturer': 'Unknown',
         'model': 'Unknown',
         'serial_number': 'Unknown',
         'geolocation': (raw['site']['latitude'], raw['site']['longitude'])
     }
     device = Device(**device_meta)
     # accumulated power in KWh to Wh
     accumulated_power = int(data[-1]['produced'] * pow(10, 3))
     # access_epoch
     now = datetime.datetime.now().astimezone()
     access_epoch = calendar.timegm(now.timetuple())
     # measurement epoch
     measurement_timestamp = datetime.datetime.strptime(
         data[-1]['date'], "%Y-%m-%d")
     measurement_epoch = calendar.timegm(measurement_timestamp.timetuple())
     return EnergyData(device=device,
                       access_epoch=access_epoch,
                       raw=raw,
                       accumulated_energy=accumulated_power,
                       measurement_epoch=measurement_epoch)
Esempio n. 7
0
 def read_state(self) -> EnergyData:
     # raw
     raw, data = self._reach_source()
     # device
     device_meta = {
         'manufacturer': 'Loxone',
         'model': 'Miniserver',
         'serial_number': 'Unknown',
         'geolocation': ('Unknown', 'Unknown')
     }
     device = Device(**device_meta)
     # accumulated power in Wh
     accumulated_power = int(
         data['Body']['Data']['TOTAL_ENERGY']['Value']) * pow(10, -6)
     # access_epoch
     now = datetime.datetime.now().astimezone()
     access_epoch = calendar.timegm(now.timetuple())
     # measurement epoch
     measurement_timestamp = data['Head']['Timestamp'][:-6]
     measurement_timestamp = datetime.datetime.strptime(
         measurement_timestamp, "%Y-%m-%dT%H:%M:%S")
     measurement_epoch = calendar.timegm(measurement_timestamp.timetuple())
     return EnergyData(device=device,
                       access_epoch=access_epoch,
                       raw=raw,
                       accumulated_energy=accumulated_power,
                       measurement_epoch=measurement_epoch)
Esempio n. 8
0
    def read_state(self) -> EnergyData:
        raw = self._get_daily_data()
        '''
        {
            "sites": [
                {
                    "site_id": "b1",
                    "start_time": "2018-03-26T08:21:20Z",
                    "end_time": "2018-03-26T09:21:20Z",
                    "energy": {
                        "unit": "wh",
                        "data": 875.4090909090909
                    }
                },
                ...
            ]
        }
        '''
        # get the object with the right site_id
        state = {}

        for specific_site in raw["sites"]:
            if specific_site["site_id"] == self.site:
                state = specific_site
                break

        # build the device object
        device_meta = {
            'manufacturer': 'Unknown',
            'model': 'Unknown',
            'serial_number': 'Unknown',
            'geolocation': (0, 0)
        }
        device = Device(**device_meta)

        # get produced energy from filtered object
        # accumulated_power = specific_site['energy']['data']
        accumulated_power = specific_site['energy']['data']

        # instance of mini utc class (tzinfo)
        utc = UTC()

        # build access_timestamp
        now = datetime.datetime.now().astimezone()
        access_timestamp = now.isoformat()

        # build measurement_timestamp
        measurement_timestamp = datetime.datetime.strptime(
            specific_site['end_time'], '%Y-%m-%dT%H:%M:%SZ')
        measurement_timestamp = measurement_timestamp.replace(
            tzinfo=utc).isoformat()

        return EnergyData(device, access_timestamp, raw, accumulated_power,
                          measurement_timestamp)
Esempio n. 9
0
 def read_state(self) -> EnergyData:
     access_timestamp = int(time.time())
     device = Device(
         manufacturer='Slock.it',
         model='Virtual Energy Meter',
         serial_number='0001000',
         geolocation=(1.123, 1.321))
     accumulated_power = random.randint(self.memory, (self.memory + 1) + 20)
     measurement_timestamp = int(time.time())
     device_str = device.manufacturer + device.model + device.serial_number
     raw = str(device_str + str(access_timestamp) + str(accumulated_power) + str(measurement_timestamp))
     return EnergyData(device, access_timestamp, raw, accumulated_power, measurement_timestamp)
Esempio n. 10
0
    def read_state(self) -> EnergyData:
        raw = self._get_daily_data()
        '''
        {
            "production": [
                {
                    "assetPublicAddress": "0x6e953cc665e527d10989172def6a91fd489e7cf11",
                    "amount": 6876.4,
                    "startTime": "2015-03-17T06:00:00.000Z",
                    "endTime": "2015-03-17T06:59:59.999Z"
                },
                ...
            ]
        }
        '''
        # get the object with the right site_id
        state = {}

        for specific_site in raw["production"]:
            if specific_site["assetPublicAddress"] == self.site:
                state = specific_site
                break

        # build the device object
        device_meta = {
            'manufacturer': 'Unknown',
            'model': 'Unknown',
            'serial_number': 'Unknown',
            'geolocation': (0, 0)
        }
        device = Device(**device_meta)

        # get produced energy from filtered object
        # accumulated_power = specific_site['energy']['data']
        accumulated_power = specific_site['amount']

        # instance of mini utc class (tzinfo)
        utc = UTC()

        # build access_timestamp
        now = datetime.datetime.now().astimezone()
        access_timestamp = now.isoformat()

        # build measurement_timestamp
        measurement_timestamp = datetime.datetime.strptime(
            specific_site['endTime'], '%Y-%m-%dT%H:%M:%S.%fZ')
        measurement_timestamp = measurement_timestamp.replace(
            tzinfo=utc).isoformat()

        return EnergyData(device, access_timestamp, raw, accumulated_power,
                          measurement_timestamp)
Esempio n. 11
0
File: Sonnen.py Progetto: ricmm/bond
    def read_state(self) -> EnergyData:
        raw = self._get_daily_data()
        '''
            {
                "message": "Query executed sucessfully.",
                "data": {
                    "asset_id": 101,
                    "sum_charge_kWh": 22.101,
                    "utc_offset": "01:00",
                    "sum_discharge_kWh": 11.101,
                    "requested_hour": 11,
                    "requested_date": "2018-03-27"
                }
            }
        '''

        # build the device object
        device_meta = {
            'manufacturer': 'Unknown',
            'model': 'Unknown',
            'serial_number': 'Unknown',
            'geolocation': (0, 0)
        }
        device = Device(**device_meta)

        # get produced energy
        accumulated_power = int(
            ("%.2f" % (raw['data']['sum_discharge_kWh'] * 1000)).replace(
                '.', ''))

        utc = UTC()

        # build access_epoch
        now = datetime.datetime.now().astimezone()
        access_timestamp = now.isoformat()

        # build measurement_epoch
        measurement_timestamp = datetime.datetime.strptime(
            raw['data']['requested_date'] + 'T' +
            str(datetime.timedelta(hours=int(raw['data']['requested_hour']))),
            '%Y-%m-%dT%H:%M:%S')
        measurement_timestamp = measurement_timestamp.replace(
            tzinfo=utc).isoformat()

        return EnergyData(device, access_timestamp, raw, accumulated_power,
                          measurement_timestamp)
Esempio n. 12
0
 def read_state(self, days=1) -> EnergyData:
     raw = self._get_daily_data(days)
     state = [{'date': a, 'produced': b} for a, b in zip(raw['datetime'], raw['production'])]
     device_meta = {
         'manufacturer': 'Unknown',
         'model': 'Unknown',
         'serial_number': 'Unknown',
         'geolocation': (raw['site']['latitude'], raw['site']['longitude'])
     }
     device = Device(**device_meta)
     accumulated_power = state[0]['produced']
     now = datetime.datetime.now()
     access_epoch = calendar.timegm(now.timetuple())
     measurement_timestamp = datetime.datetime.strptime(state[0]['date'], "%Y-%m-%d")
     measurement_epoch = calendar.timegm(measurement_timestamp.timetuple())
     return EnergyData(device, access_epoch, raw, accumulated_power, measurement_epoch)
     pass
Esempio n. 13
0
 def read_state(self) -> EnergyData:
     # raw
     raw, data, measurement_list = self._reach_source(self.api_url)
     # device
     device_meta = data[-1]['device']
     device = Device(**device_meta)
     # accumulated energy in Wh
     if device.is_accumulated:
         accumulated_energy = self.to_wh(measurement_list[-1]['energy'], device.energy_unit)
     else:
         accumulated_energy = self.to_wh(sum(i['energy'] for i in measurement_list), device.energy_unit)
     # access_epoch
     now = datetime.datetime.now().astimezone()
     access_epoch = calendar.timegm(now.timetuple())
     #  measurement epoch
     measurement_time = datetime.datetime.strptime(measurement_list[-1]['measurement_time'], "%Y-%m-%dT%H:%M:%S%z")
     measurement_epoch = calendar.timegm(measurement_time.timetuple())
     return EnergyData(device=device, access_epoch=access_epoch, raw=raw, accumulated_energy=accumulated_energy,
                       measurement_epoch=measurement_epoch)
 def read_state(self) -> EnergyData:
     # raw
     token_request = self.api_url + 'oauth2/token'
     marginal_query = {
         'grant_type': 'password',
         'client_id': self.client_id,
         'client_secret': self.client_secret,
         'username': self.username,
         'password': self.password
     }
     r = requests.post(token_request, data=marginal_query)
     ans = r.json()
     if len(ans['access_token']) < 1:
         raise AttributeError('Empty/Error response from api.')
     raw = self._get_daily_data(ans['access_token'])
     # device
     device_meta = {
         'manufacturer': 'Unknown',
         'model': 'Unknown',
         'serial_number': 'Unknown',
         'geolocation': (0, 0)
     }
     device = Device(**device_meta)
     # accumulated power
     accumulated_power = 0
     latest_timestamp = 0
     for element in raw[
             "consumptions"]:  # add all consumption together and save latest timestamp
         # KWh to Wh
         accumulated_power += int(element['consumption'] * pow(10, 3))
         if element["timestamp"] > latest_timestamp:
             latest_timestamp = element["timestamp"]
     # access_epoch
     now = datetime.datetime.now().astimezone()
     access_epoch = calendar.timegm(now.timetuple())
     # measurement epoch
     measurement_epoch = int(latest_timestamp)
     return EnergyData(device=device,
                       access_epoch=access_epoch,
                       raw=raw,
                       accumulated_energy=accumulated_power,
                       measurement_epoch=measurement_epoch)
Esempio n. 15
0
 def read_state(self) -> EnergyData:
     # raw
     raw, measured_power = self._get_daily_data()
     # device
     device_meta = {
         'manufacturer': 'Unknown',
         'model': 'Unknown',
         'serial_number': 'Unknown',
         'geolocation': (0, 0)
     }
     device = Device(**device_meta)
     # accumulated power in KWh to Mh
     accumulated_power = int(measured_power * pow(10, 3))
     # access_epoch
     now = datetime.datetime.now().astimezone()
     access_epoch = calendar.timegm(now.timetuple())
     # measurement epoch
     measurement_timestamp = now - datetime.timedelta(hours=12)
     measurement_epoch = calendar.timegm(measurement_timestamp.timetuple())
     return EnergyData(device=device, access_epoch=access_epoch, raw=str(raw), accumulated_energy=accumulated_power,
                       measurement_epoch=measurement_epoch)
Esempio n. 16
0
 def read_state(self) -> EnergyData:
     # raw
     raw, file_list = self._reach_source()
     # device
     device_meta = {
         'manufacturer': 'Unknown',
         'model': 'Unknown',
         'serial_number': 'Unknown',
         'geolocation': ('Unknown', 'Unknown')
     }
     device = Device(**device_meta)
     # accumulated power in Wh
     accumulated_power = int(sum(locale.atof(power.p_minus) for power in raw if power.p_minus))
     # access_epoch
     now = datetime.datetime.now().astimezone()
     access_epoch = calendar.timegm(now.timetuple())
     # measurement epoch
     measurement_timestamp = datetime.datetime.strptime(raw[-1].timestamp, "%Y-%m-%dT%H:%M:%S")
     measurement_epoch = calendar.timegm(measurement_timestamp.timetuple())
     self._del_files(file_list)
     return EnergyData(device=device, access_epoch=access_epoch, raw=str(raw), accumulated_energy=accumulated_power,
                       measurement_epoch=measurement_epoch)
Esempio n. 17
0
    def read_state(self) -> EnergyData:

        raw, accumulated_power = self._get_daily_data()

        # build the device object
        device_meta = {
            'manufacturer': 'Unknown',
            'model': 'Unknown',
            'serial_number': 'Unknown',
            'geolocation': (0, 0)
        }
        device = Device(**device_meta)

        # build access_epoch
        now = datetime.datetime.now().astimezone()
        access_timestamp = now.isoformat()

        # build measurement_epoch
        measurement_timestamp = now - datetime.timedelta(hours=12)
        measurement_timestamp = measurement_timestamp.isoformat()

        return EnergyData(device, access_timestamp, raw, accumulated_power, measurement_timestamp)
Esempio n. 18
0
 def read_state(self) -> EnergyData:
     file_list = self.__get_files()
     raw = self.__parse_data(file_list)
     device_meta = {
         'manufacturer': 'Unknown',
         'model': 'Unknown',
         'serial_number': 'Unknown',
         'geolocation': (0, 0)
     }
     device = Device(**device_meta)
     locale.setlocale(locale.LC_NUMERIC, 'de_DE')
     accumulated_power = sum(
         locale.atof(twl_data.p_minus)
         for twl_data in raw if twl_data.p_minus) * pow(10, 3)
     locale.setlocale(locale.LC_NUMERIC, '')
     now = datetime.datetime.now().astimezone()
     access_timestamp = now.isoformat()
     measurement_timestamp = datetime.datetime.strptime(
         raw[-1].timestamp, "%Y-%m-%d %H:%M:%S")
     measurement_timestamp = measurement_timestamp.replace(
         tzinfo=CEST()).isoformat()
     self.__del_files(file_list)
     return EnergyData(device, access_timestamp, raw, accumulated_power,
                       measurement_timestamp)
Esempio n. 19
0
 def read_state(self) -> EnergyData:
     # raw
     raw, data = self._reach_source()
     # device
     device_meta = {
         'manufacturer': data['manufacturer'],
         'model': data['model'],
         'serial_number': data['serial_number'],
         'geolocation': (data['latitude'], data['longitude'])
     }
     device = Device(**device_meta)
     # accumulated power in KWh to Wh
     accumulated_power = int(float(data['accumulated_power']) * pow(10, 3))
     # access_epoch
     now = datetime.datetime.now().astimezone()
     access_epoch = calendar.timegm(now.timetuple())
     # measurement epoch
     # TODO: ask them to send it as string to parse as json
     measurement_epoch = access_epoch
     return EnergyData(device=device,
                       access_epoch=access_epoch,
                       raw=raw,
                       accumulated_energy=accumulated_power,
                       measurement_epoch=measurement_epoch)
Esempio n. 20
0
    def read_state(self) -> EnergyData:

        token_request = self.api_url + 'oauth2/token'
        marginal_query = {
            'grant_type': 'password',
            'client_id': self.client_id,
            'client_secret': self.client_secret,
            'username': self.username,
            'password': self.password
        }

        r = requests.post(token_request, data=marginal_query)
        ans = r.json()
        if len(ans['access_token']) < 1:
            raise AttributeError('Empty/Error response from api.')

        # get raw data
        raw = self._get_daily_data(ans['access_token'])
        '''
        {
            "serviceLocationId": 26145,
            "consumptions": [
                {
                    "timestamp": 1508709600000,
                    "consumption": 24,
                    "solar": 0,
                    "alwaysOn": 108
                },
                {
                    "timestamp": 1508796000000,
                    "consumption": 34.9,
                    "solar": 0,
                    "alwaysOn": 0
                },
                ...                 
              ] 
            }
        '''
        # add all consumption together and save latest timestamp
        total_consumption = 0
        latest_timestamp = 0
        for element in raw["consumptions"]:
            total_consumption += element['consumption']
            if element["timestamp"] > latest_timestamp:
                latest_timestamp = element["timestamp"]

        # build the device object
        device_meta = {
            'manufacturer': 'Unknown',
            'model': 'Unknown',
            'serial_number': 'Unknown',
            'geolocation': (0, 0)
        }
        device = Device(**device_meta)

        # get produced energy from filtered object
        accumulated_power = total_consumption

        # instance of mini utc class (tzinfo)
        utc = UTC()

        # build access_timestamp
        now = datetime.datetime.now().astimezone()
        access_timestamp = now.isoformat()

        # build measurement_timestamp
        # measurement_timestamp = datetime.datetime.strptime(str(latest_timestamp/1000), '%S')
        measurement_timestamp = datetime.datetime.fromtimestamp(
            latest_timestamp / 1000).strftime("%A, %B %d, %Y %I:%M:%S")
        measurement_timestamp = datetime.datetime.strptime(
            measurement_timestamp, '%A, %B %d, %Y %I:%M:%S')
        measurement_timestamp = measurement_timestamp.replace(
            tzinfo=utc).isoformat()

        return EnergyData(device, access_timestamp, raw, accumulated_power,
                          measurement_timestamp)