Beispiel #1
0
def make_delete(msgDict, session):
    """
    Responses to a delete messsage from the meter.
    """
    originMsg = None
    messageBody = None
    # locate the job from the job id
    job = session.query(Job).get(msgDict["jobid"])
    if job:
        circuit = job.circuit
        interface = circuit.meter.communication_interface

        originMsg = find_job_message(job, session)
        update_job(job, session)
        update_circuit(msgDict, circuit, session)
        if isinstance(job, AddCredit):
            messageBody = respond_to_add_credit(job, circuit, session)
        elif job._type == "turnon" or job._type == "turnoff":
            messageBody = make_message_body("toggle.txt",
                                            lang=circuit.account.lang,
                                            account=circuit.pin,
                                            status=circuit.get_rich_status(),
                                            credit=circuit.credit)
        else:
            pass
            # double to check we have a message to send
        if messageBody and originMsg:
            interface.sendMessage(
                originMsg.number,
                messageBody,
                incoming=originMsg.uuid)
            session.merge(job)
        else:
            pass
Beispiel #2
0
def make_emax(message, circuit, session):
    interface = circuit.meter.communication_interface
    interface.sendMessage(
        circuit.account.phone,
        make_message_body("energy-max-alert.txt",
                     lang=circuit.account.lang,
                     account=circuit.pin),
        incoming=message['meta'].uuid)
Beispiel #3
0
def make_delete(msgDict, session):
    """
    Responses to a delete messsage from the meter.
    """
    session.add(SystemLog("%s" % msgDict))
    job = session.query(Job).get(msgDict["jobid"])
    if job:
        if len(job.job_message) is not 0:
            incoming_uuid = job.job_message[0]
        elif len(job.kannel_job_message) is not 0:
            incoming_uuid = job.kannel_job_message[0].incoming
        originMsg = session.query(IncomingMessage).\
                            filter_by(uuid=incoming_uuid).first()
        circuit = job.circuit
        interface = circuit.meter.communication_interface
        job.state = False
        messageBody = None
        # update circuit
        circuit.status = int(msgDict.get("status", circuit.status))
        circuit.credit = float(msgDict.get("cr", circuit.credit))
        session.merge(circuit)
        session.flush()
        if job._type == "addcredit":
            messageBody = make_message_body("credit.txt",
                                       lang=circuit.account.lang,
                                       account=circuit.pin,
                                       status=circuit.get_rich_status(),
                                       credit=circuit.credit)
        elif job._type == "turnon" or job._type  == "turnoff":
            messageBody = make_message_body("toggle.txt",
                                       lang=circuit.account.lang,
                                       account=circuit.pin,
                                       status=circuit.get_rich_status(),
                                       credit=circuit.credit)
        # double to check we have a message to send
        if messageBody and originMsg:
            interface.sendMessage(
                originMsg.number,
                messageBody,
                incoming=originMsg.uuid)
        session.merge(job)
    else:
        pass
Beispiel #4
0
def make_emax(message, circuit, session):
    interface = circuit.meter.communication_interface
    msg = interface.sendMessage(
        circuit.account.phone,
        make_message_body("energy-max-alert.txt",
                     lang=circuit.account.lang,
                     account=circuit.pin),
        incoming=message['meta'].uuid)
    alert = EnergyMax(datetime.now(), circuit.meter, circuit, message['meta'], msg)
    session.add(alert)
    session.flush()
Beispiel #5
0
def get_balance(message):
    """Allows users to check blance"""
    circuit = get_circuit(message)
    langauge = message.langauge
    if circuit:
        interface = circuit.meter.communication_interface
        interface.sendMessage(
            message.number,
            make_message_body("bal.txt", lang=langauge, account=circuit.pin, credit=circuit.credit),
            incoming=message.uuid,
        )
Beispiel #6
0
def make_nocw(message, circuit, session):
    interface = circuit.meter.communication_interface
    interface.sendMessage(
        circuit.account.phone,
        make_message_body("nocw-alert.txt",
                     lang=circuit.account.lang,
                     account=circuit.pin),
        incoming=message['meta'].uuid)
    session.flush()
    log = SystemLog(
        "Low credit alert for circuit %s sent to %s" % (circuit.pin,
                                                        circuit.account.phone))
    session.add(log)
Beispiel #7
0
def make_nocw(message, circuit, session):
    """
    Sends a no credit alert to the consumer
    """
    interface = circuit.meter.communication_interface
    msg = interface.sendMessage(
        circuit.account.phone,
        make_message_body("nocw-alert.txt",
                     lang=circuit.account.lang,
                     account=circuit.pin))
    alert = NoCredit(datetime.now(), circuit.meter, circuit, message['meta'], msg)
    session.add(alert)
    session.flush()
Beispiel #8
0
def get_token(message):
    """Tries to match message to token."""
    session = DBSession()
    tokenNumber = message.text.split(delimiter)[2]
    token = session.query(Token).filter_by(state="new").filter_by(token=tokenNumber).first()
    if token:
        return token
    else:
        interface = message.communication_interface
        interface.sendMessage(
            message.number, make_message_body("no-token.txt", lang=message.langauge), incoming=message.uuid
        )
        return False
Beispiel #9
0
def get_circuit(message):
    """Queris the database to find circuit
    Returns the circuit or false
    """
    session = DBSession()
    pin = message.text.split(delimiter)[1].lower()
    circuit = session.query(Circuit).filter_by(pin=pin).first()
    if circuit:
        return circuit
    else:
        interface = message.communication_interface
        interface.sendMessage(
            message.number,
            make_message_body("no-circuit.txt", lang=interface.lang, pin=pin),
            incoming=message.uuid)
Beispiel #10
0
def respond_to_add_credit(job, circuit, session):
    """
    Function to excute the actions need after the gateways gets a job
    delete about a add credit job.
    """
    if hasattr(job, 'token'):
        token = job.token
        token.state = 'used'
        session.merge(token)
        session.flush()
    return make_message_body("credit.txt",
                             lang=circuit.account.lang,
                             account=circuit.pin,
                             status=circuit.get_rich_status(),
                             credit=circuit.credit)
Beispiel #11
0
def set_primary_contact(message):
    """Allows users to set their primary contact number"""
    session = DBSession()
    circuit = get_circuit(message)
    if circuit:
        interface = circuit.meter.communication_interface
        new_number = message.text.split(delimiter)[2]
        old_number = circuit.account.phone
        messageBody = make_message_body("tel.txt", lang=message.langauge, old_number=old_number, new_number=new_number)
        interface.sendMessage(message.number, messageBody, incoming=message.uuid)
        if new_number != message.number:
            interface.sendMessage(new_number, messageBody, incoming=message.uuid)
        account = circuit.account
        account.phone = new_number
        session.merge(account)
Beispiel #12
0
def get_balance(message):
    """Allows users to check blance"""
    circuit = get_circuit(message)
    if circuit is not None:
        last_log = circuit.getLastLog()
        if last_log is not None:
            last_time = last_log.date
        language = circuit.account.lang
        interface = circuit.meter.communication_interface
        interface.sendMessage(
            message.number,
            make_message_body("bal.txt",
                              lang=language,
                              account=circuit.pin,
                              last_time=last_time,
                              credit=circuit.credit),
            incoming=message.uuid)
Beispiel #13
0
def use_history(message, **kwargs):
    """
    Calculates use based on last 30 days of account activity
    account-number
    avg watt hours pre day :  (total watthours for time/number of days)
    max watt hours         :  max(watthours)
    min watt hours         :  min(watthours)
    """
    circuit = get_circuit(message)
    if circuit:
        now = datetime.now()
        last_month = now - timedelta(days=30)
        interface = circuit.meter.communication_interface
        message_response = make_message_body('use.txt',
                                    lang=circuit.account.lang,
                                    account=circuit.pin,
                                    amount=circuit.calculateCreditConsumed(dateStart=last_month, dateEnd=now))
        interface.sendMessage(message.number, message_response)