<commit_before>#!/usr/bin/env python
# Python class to connect to an E3/DC system through the internet portal
#
# Copyright 2017 <NAME> <<EMAIL>>
# Licensed under a MIT license. See LICENSE for details

import requests
import hashlib
import time
import dateutil.parser
import datetime
import json
import uuid
from ._e3dc_rscp_web import E3DC_RSCP_web
from ._e3dc_rscp_local import E3DC_RSCP_local, RSCPAuthenticationError, RSCPNotAvailableError
from ._rscpLib import rscpFindTag

REMOTE_ADDRESS='https://s10.e3dc.com/s10/phpcmd/cmd.php'
REQUEST_INTERVAL_SEC = 10 # minimum interval between requests
REQUEST_INTERVAL_SEC_LOCAL = 1 # minimum interval between requests

class AuthenticationError(Exception):
    pass

class NotAvailableError(Exception):
    pass

class PollError(Exception):
    pass

class SendError(Exception):
    pass

class E3DC:
    """A class describing an E3DC system, used to poll the status from the portal
    """
    
    CONNECT_LOCAL = 1
    CONNECT_WEB = 2
    
    DAY_MONDAY = 0
    DAY_TUESDAY = 1
    DAY_WEDNESDAY = 2
    DAY_THURSDAY = 3
    DAY_FRIDAY = 4
    DAY_SATURDAY = 5
    DAY_SUNDAY = 6

    IDLE_TYPE = {'idleCharge': 0, 'idleDisharge': 1}
    
    def __init__(self, connectType, **kwargs):
        """Constructor of a E3DC object (does not connect)
    
        Args:
            connectType: CONNECT_LOCAL: use local rscp connection
                Named args for CONNECT_LOCAL:
                username (string): username
                password (string): password (plain text)
                ipAddress (string): IP address of the E3DC system
                key (string): encryption key as set in the E3DC settings
            
            connectType: CONNECT_WEB: use web connection
                Named args for CONNECT_WEB:
                username (string): username
                password (string): password (plain text or md5 hash)
                serialNumber (string): the serial number of the system to monitor
                isPasswordMd5 (boolean, optional): indicates whether the password is already md5 digest (recommended, default = True)
        """
        
        self.connectType = connectType
        self.username = kwargs['username']
        self.serialNumber  = None
        self.serialNumberPrefix  = None
        if connectType == self.CONNECT_LOCAL:
            self.ip = kwargs['ipAddress']
            self.key = kwargs['key']
            self.password = kwargs['password']
            self.rscp = E3DC_RSCP_local(self.username, self.password, self.ip, self.key)
            self.poll = self.poll_rscp
        else:
            self._set_serial(kwargs['serialNumber'])
            if 'isPasswordMd5' in kwargs:
                if kwargs['isPasswordMd5'] == True:
                    self.password = kwargs['password']
                else:
                    self.password = hashlib.md5(kwargs['password'].encode('utf-8')).hexdigest()
            self.rscp = E3DC_RSCP_web(self.username, self.password, '{}{}'.format(self.serialNumberPrefix, self.serialNumber))
            self.poll = self.poll_ajax
        
        self.jar = None
        self.guid = "GUID-" + str(uuid.uuid1())
        self.lastRequestTime = -1
        self.lastRequest = None
        self.connected = False

        # static values
        self.deratePercent = None
        self.deratePower  = None
        self.installedPeakPower  = None
        self.installedBatteryCapacity = None
        self.macAddress = None
        self.model = None
        self.maxAcPower  = None
        self.maxBatChargePower = None
        self.maxBatDischargePower = None
        self.startDischargeDefault = None
        self.pmIndex = None

        self.get_system_info_static(keepAlive=True)
        
    def _set_serial(self, serial):
        if serial[0].isdigit():
            self.serialNumber = serial
        else:
            self.serialNumber = serial[4:]
            self.serialNumberPrefix = serial[:4]
        if self.serialNumber.startswith("4"):
            self.model = "S10E"
            self.pmIndex = 0
            if not self.serialNumberPrefix:
                self.serialNumberPrefix = 'S10-'
        elif self.serialNumber.startswith("5"):
            self.model = "S10mini"
            self.pmIndex = 6
            if not self.serialNumberPrefix:
                self.serialNumberPrefix = 'S10-'
        elif self.serialNumber.startswith("6"):
            self.model = "Quattroporte"
            self.pmIndex = 0 #not validated
            if not self.serialNumberPrefix:
                self.serialNumberPrefix = 'S10-' # Not sure if this is corrent<commit_msg>correct serial prefix for Quattroporte<commit_after>#!/usr/bin/env python
# Python class to connect to an E3/DC system through the internet portal
#
# Copyright 2017 <NAME> <<EMAIL>>
# Licensed under a MIT license. See LICENSE for details

import requests
import hashlib
import time
import dateutil.parser
import datetime
import json
import uuid
from ._e3dc_rscp_web import E3DC_RSCP_web
from ._e3dc_rscp_local import E3DC_RSCP_local, RSCPAuthenticationError, RSCPNotAvailableError
from ._rscpLib import rscpFindTag

REMOTE_ADDRESS='https://s10.e3dc.com/s10/phpcmd/cmd.php'
REQUEST_INTERVAL_SEC = 10 # minimum interval between requests
REQUEST_INTERVAL_SEC_LOCAL = 1 # minimum interval between requests

class AuthenticationError(Exception):
    pass

class NotAvailableError(Exception):
    pass

class PollError(Exception):
    pass

class SendError(Exception):
    pass

class E3DC:
    """A class describing an E3DC system, used to poll the status from the portal
    """
    
    CONNECT_LOCAL = 1
    CONNECT_WEB = 2
    
    DAY_MONDAY = 0
    DAY_TUESDAY = 1
    DAY_WEDNESDAY = 2
    DAY_THURSDAY = 3
    DAY_FRIDAY = 4
    DAY_SATURDAY = 5
    DAY_SUNDAY = 6

    IDLE_TYPE = {'idleCharge': 0, 'idleDisharge': 1}
    
    def __init__(self, connectType, **kwargs):
        """Constructor of a E3DC object (does not connect)
    
        Args:
            connectType: CONNECT_LOCAL: use local rscp connection
                Named args for CONNECT_LOCAL:
                username (string): username
                password (string): password (plain text)
                ipAddress (string): IP address of the E3DC system
                key (string): encryption key as set in the E3DC settings
            
            connectType: CONNECT_WEB: use web connection
                Named args for CONNECT_WEB:
                username (string): username
                password (string): password (plain text or md5 hash)
                serialNumber (string): the serial number of the system to monitor
                isPasswordMd5 (boolean, optional): indicates whether the password is already md5 digest (recommended, default = True)
        """
        
        self.connectType = connectType
        self.username = kwargs['username']
        self.serialNumber  = None
        self.serialNumberPrefix  = None
        if connectType == self.CONNECT_LOCAL:
            self.ip = kwargs['ipAddress']
            self.key = kwargs['key']
            self.password = kwargs['password']
            self.rscp = E3DC_RSCP_local(self.username, self.password, self.ip, self.key)
            self.poll = self.poll_rscp
        else:
            self._set_serial(kwargs['serialNumber'])
            if 'isPasswordMd5' in kwargs:
                if kwargs['isPasswordMd5'] == True:
                    self.password = kwargs['password']
                else:
                    self.password = hashlib.md5(kwargs['password'].encode('utf-8')).<PASSWORD>()
            self.rscp = E3DC_RSCP_web(self.username, self.password, '{}{}'.format(self.serialNumberPrefix, self.serialNumber))
            self.poll = self.poll_ajax
        
        self.jar = None
        self.guid = "GUID-" + str(uuid.uuid1())
        self.lastRequestTime = -1
        self.lastRequest = None
        self.connected = False

        # static values
        self.deratePercent = None
        self.deratePower  = None
        self.installedPeakPower  = None
        self.installedBatteryCapacity = None
        self.macAddress = None
        self.model = None
        self.maxAcPower  = None
        self.maxBatChargePower = None
        self.maxBatDischargePower = None
        self.startDischargeDefault = None
        self.pmIndex = None

        self.get_system_info_static(keepAlive=True)
        
    def _set_serial(self, serial):
        if serial[0].isdigit():
            self.serialNumber = serial
        else:
            self.serialNumber = serial[4:]
            self.serialNumberPrefix = serial[:4]
        if self.serialNumber.startswith("4"):
            self.model = "S10E"
            self.pmIndex = 0
            if not self.serialNumberPrefix:
                self.serialNumberPrefix = 'S10-'
        elif self.serialNumber.startswith("5"):
            self.model = "S10mini"
            self.pmIndex = 6
            if not self.serialNumberPrefix:
                self.serialNumberPrefix = 'S10-'
        elif self.serialNumber.startswith("6"):
            self.model = "Quattroporte"
            self.pmIndex = 0 #not validated
            if not self.serialNumberPrefix:
                self.serialNumberPrefix = 'Q10-'
{'fraction_non_alphanumeric': 0.08272058823529412, 'fraction_numerical': 0.013624567474048443, 'mean_word_length': 2.1631326949384406, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '564645', 'n_tokens_mistral': 2741, 'n_tokens_neox': 2621, 'n_words': 689}