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:
0:25ad6eba7916
Child:
1:e036e77762fa

File content as of revision 0:25ad6eba7916:

#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),
        currentChar(SPS_UUID_CURERNT_CHAR,current),
        powerChar(SPS_UUID_POWER_CHAR,power),
        powerFactorChar(SPS_UUID_POWER_FACTOR_CHAR,powerFactor),
        energyChar(SPS_UUID_ENERGY_CHAR,energy),
        relayChar(SPS_UUID_RELAY_CHAR,relayValue),
        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[0] = v>>24;
        voltage[1] = (v&0x00FFFFFF)>>16;
        voltage[2] = (v&0x0000FFFF)>>8;
        voltage[3] = v&0x000000FF;
        ble.updateCharacteristicValue(voltageChar.getValueHandle(),voltage,4);
    }
}

void SmartPlugService::updateCurrent(unsigned long c)
{
    if (ble.getGapState().connected) 
    {
        current[0] = c>>24;
        current[1] = (c&0x00FFFFFF)>>16;
        current[2] = (c&0x0000FFFF)>>8;
        current[3] = c&0x000000FF;
        ble.updateCharacteristicValue(currentChar.getValueHandle(),voltage,4);
    }
}

void SmartPlugService::updatePower(unsigned long p)
{
    if (ble.getGapState().connected) 
    {
        power[0] = p>>24;
        power[1] = (p&0x00FFFFFF)>>16;
        power[2] = (p&0x0000FFFF)>>8;
        power[3] = p&0x000000FF;
        ble.updateCharacteristicValue(powerChar.getValueHandle(),voltage,4);        
    }
}

void SmartPlugService::updatePowerFactor(unsigned long pf)
{
    if (ble.getGapState().connected) 
    {
        powerFactor[0] = pf>>24;
        powerFactor[1] = (pf&0x00FFFFFF)>>16;
        powerFactor[2] = (pf&0x0000FFFF)>>8;
        powerFactor[3] = pf&0x000000FF;        
        ble.updateCharacteristicValue(powerFactorChar.getValueHandle(),voltage,4);
    }
}

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);

}