Bluetooth Low Energy for Smart Plug

Dependencies:   BLE_API mbed nRF51822

Fork of SmartPlugBLE by Pavit Noinongyao

Services/SmartPlugService.cpp

Committer:
Slepnir
Date:
2015-07-07
Revision:
1:e036e77762fa
Parent:
0:25ad6eba7916
Child:
2:6db5c9a2894c

File content as of revision 1:e036e77762fa:

#include "ble/BLE.h"
#include "mbed.h"
#include "SmartPlugService.h"
#include "SmartPlugBLE.h"


SmartPlugService::SmartPlugService(BLE &_ble, SmartPlugBLE &sys):
        ble(_ble), system(sys),
        voltageChar(SPS_UUID_VOLTAGE_CHAR,&voltage,GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
        currentChar(SPS_UUID_CURERNT_CHAR,&current,GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
        powerChar(SPS_UUID_POWER_CHAR,&power,GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
        powerFactorChar(SPS_UUID_POWER_FACTOR_CHAR,&powerFactor,GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
        energyChar(SPS_UUID_ENERGY_CHAR,&energy,GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
        relayChar(SPS_UUID_RELAY_CHAR, relayValue.getDataPointer(),
                relayValue.getLenBytes(), RelayValueBytes::MAX_SIZE_BYTES,
                GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY |
                GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ),
        updateChar(SPS_UUID_UPDATE_CHAR,NULL)
{
    (&system)->addObserver(*this);
}

void SmartPlugService::onDataWritten(const GattWriteCallbackParams *params) 
{
    if(params->handle == updateChar.getValueHandle())
    {
        system.updateData();
    }
}
void SmartPlugService::update(void* data)
{
    updateData((SmartPlug*)data);
}

void SmartPlugService::updateData(SmartPlug* data)
{
    updateVoltage(data->getVoltage());
    updateCurrent(data->getCurrent());
    updatePower(data->getPower());
    updatePowerFactor(data->getPowerFactor());
}

void SmartPlugService::updateVoltage(unsigned long v)
{
    if (ble.getGapState().connected) 
    {
        voltage = v;
        ble.updateCharacteristicValue(voltageChar.getValueHandle(),&voltage,4);
    }
}

void SmartPlugService::updateCurrent(unsigned long c)
{
    if (ble.getGapState().connected) 
    {
        current = c;
        ble.updateCharacteristicValue(currentChar.getValueHandle(),&current,4);
    }
}

void SmartPlugService::updatePower(unsigned long p)
{
    if (ble.getGapState().connected) 
    {
        power = p;
        ble.updateCharacteristicValue(powerChar.getValueHandle(),&power,4);        
    }
}

void SmartPlugService::updatePowerFactor(unsigned long pf)
{
    if (ble.getGapState().connected) 
    {    
        powerFactor = pf;
        ble.updateCharacteristicValue(powerFactorChar.getValueHandle(),&powerFactor,4);
    }
}

void SmartPlugService::updateRelay(Relay* relay)
{
    if (ble.getGapState().connected) 
    {   
        relayValue.updateData(relay);
        ble.updateCharacteristicValue(relayChar.getValueHandle(),relayValue.getDataPointer(),
        relayValue.getLenBytes());
    }
}

void SmartPlugService::setupService(void) 
{

    static bool serviceAdded = false; /* We should only ever need to add the service once. */
    if (serviceAdded)
    {
        return;
    }

        
    GattCharacteristic *charTable[] = {&voltageChar,&currentChar,&powerChar,
                                        &powerFactorChar,&energyChar,&relayChar,
                                        &updateChar
                                        };
    GattService smartPlugService = GattService(SPS_UUID_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
    ble.addService(smartPlugService);

}