
# Thanks to the original work of 
# https://github.com/dhague/vpower

import sys
from ant.core import message, node, driver
from ant.core.constants import *
from ant.core.exceptions import ChannelError

from constants import *
from config import NETKEY, VPOWER_DEBUG

CHANNEL_PERIOD = 8182

# Transmitter for Bicycle Power ANT+ sensor

class PowerMeterTx(object):
    class PowerData:
        def __init__(self):
            self.eventCount = 0
            self.eventTime = 0
            self.cumulativePower = 0
            self.instantaneousPower = 0

    def __init__(self, antnode, sensor_id):
        self.antnode = antnode

        # Get the channel
        self.channel = antnode.getFreeChannel()
        try:
            self.channel.name = 'C:POWER'
            network = node.Network(NETKEY, 'N:ANT+')
            self.channel.assign(network, CHANNEL_TYPE_TWOWAY_TRANSMIT)
            self.channel.setID(POWER_DEVICE_TYPE, sensor_id, 0)
            self.channel.period = CHANNEL_PERIOD
            self.channel.frequency = 57
        except ChannelError as e:
            print("Channel config error: " + repr(e))
        self.powerData = PowerMeterTx.PowerData()

    def open(self):
        self.channel.open()

    def close(self):
        self.channel.close()

    def unassign(self):
        self.channel.unassign()

    # Power was updated, so send out an ANT+ message
    def update(self, power, cadence):

        if cadence > 254:
           cadence = 90

        myEventCount = self.powerData.eventCount
        if myEventCount > 254:
           myEventCount = 254

#        print("Updating power & cadence " +str(power) + " " + str(cadence))

        try:
           if VPOWER_DEBUG: print ('PowerMeterTx: update called with power ', power)
           self.powerData.eventCount = (self.powerData.eventCount + 1) & 0xff
           if VPOWER_DEBUG: print ('eventCount ', self.powerData.eventCount)
           self.powerData.cumulativePower = (self.powerData.cumulativePower + int(power)) & 0xffff
           if VPOWER_DEBUG: print ('cumulativePower ', self.powerData.cumulativePower)
           self.powerData.instantaneousPower = int(power)
           if VPOWER_DEBUG: print ('instantaneousPower ', self.powerData.instantaneousPower)
           myPayload = [0x10]  # standard power-only message
           myPayload.append(myEventCount)
           myPayload.append(0xFF)     # Pedal power not used
           myPayload.append(cadence)  # Cadence ??
           myPayload.append(self.powerData.cumulativePower & 0xff)
           myPayload.append(self.powerData.cumulativePower >> 8)
           myPayload.append(self.powerData.instantaneousPower & 0xff)
           myPayload.append(self.powerData.instantaneousPower >> 8)
           payload = bytearray(myPayload)
           ant_msg = message.ChannelBroadcastDataMessage(self.channel.number, data=payload)

           # print("PowerMeter: " +str(payload))

           if VPOWER_DEBUG: print ('Write message to ANT stick on channel ' + repr(self.channel.number))
           self.antnode.send(ant_msg)
        except Exception as e:
               print ("Exception in PowerMeterTX: "+repr(e))
