2020-10-13 17:01:29 +02:00
# include "OmobiLedDisplay.h"
2020-10-17 23:06:55 +02:00
//OmobiLedDisplay::OmobiLedDisplay(String deviceName, Adafruit_NeoMatrix *ledDisplayMatrix)
2020-10-13 17:01:29 +02:00
2020-10-15 22:54:20 +02:00
void OmobiLedDisplay : : loop ( )
{
if ( millis ( ) - lastKeepAlive > this - > maximumKeepAliveDelay )
{
2020-10-15 17:21:44 +02:00
this - > lastKeepAlive = millis ( ) ;
2020-10-15 16:58:06 +02:00
this - > bleServer - > disconnectCurrentDevice ( ) ;
2020-10-15 17:21:44 +02:00
}
2020-10-15 16:58:06 +02:00
}
2020-10-13 17:01:29 +02:00
void OmobiLedDisplay : : onDeviceConnectedChanged ( bool deviceConnected )
{
2020-10-15 22:54:20 +02:00
if ( deviceConnected )
{
2020-10-13 17:01:29 +02:00
Serial . println ( " Device connected " ) ;
2020-10-15 16:58:06 +02:00
this - > lastKeepAlive = millis ( ) ;
}
2020-10-15 22:54:20 +02:00
else
{
2020-10-14 23:54:54 +02:00
Serial . println ( " Device disconnected " ) ;
2020-10-15 16:58:06 +02:00
this - > lastKeepAlive = - 1 ;
2020-10-15 22:54:20 +02:00
this - > sessionAuthorized = false ;
2020-10-15 16:58:06 +02:00
}
2020-10-13 17:01:29 +02:00
}
2020-10-14 23:54:54 +02:00
void OmobiLedDisplay : : onDataReceived ( String dataString )
2020-10-13 17:01:29 +02:00
{
2020-10-14 23:54:54 +02:00
// process JSON
const size_t capacity = JSON_OBJECT_SIZE ( 2 ) + JSON_OBJECT_SIZE ( 3 ) + 200 ;
DynamicJsonDocument requestDoc ( capacity ) ;
DeserializationError error = deserializeJson ( requestDoc , dataString ) ;
// return on error
2020-10-15 22:54:20 +02:00
if ( error ! = DeserializationError : : Ok )
2020-10-14 23:54:54 +02:00
return ;
// get reuqest data
OmobiDisplayCommand requestHeader = requestDoc [ " header " ] ;
JsonObject requestData = requestDoc [ " data " ] ;
// prepare reply data
DynamicJsonDocument replyDoc ( capacity ) ;
replyDoc [ " header " ] = requestHeader ;
OmobiDisplayStatusCode replyStatus = InternalError ;
JsonObject replyData = replyDoc . createNestedObject ( " data " ) ;
2020-10-17 01:08:43 +02:00
if ( requestHeader > KeepAliveCommand & & ! this - > sessionAuthorized )
2020-10-15 22:54:20 +02:00
replyStatus = Unauthorized ;
else
switch ( requestHeader )
{
2020-10-18 01:40:24 +02:00
case AuthenticateCommand :
2020-10-15 22:54:20 +02:00
{
2020-10-18 19:16:27 +02:00
String combinedCode = String ( this - > properties . deviceCode ) ; //this->bleServer->getDeviceAddress() + String(this->properties.deviceCode);
2020-10-17 01:08:43 +02:00
String secret = this - > sha256 ( combinedCode ) ;
2020-10-15 22:54:20 +02:00
if ( this - > sessionAuthorized )
{
replyStatus = Success ;
}
2020-10-17 01:08:43 +02:00
else if ( requestData [ " secret " ] = = secret )
2020-10-15 22:54:20 +02:00
{
replyStatus = Success ;
this - > sessionAuthorized = true ;
}
else
{
replyStatus = Unauthorized ;
this - > sessionAuthorized = false ;
break ;
}
2020-10-15 16:58:06 +02:00
2020-10-15 22:54:20 +02:00
// for future use: add some variables of the display
}
case KeepAliveCommand :
{
replyStatus = Success ;
this - > lastKeepAlive = millis ( ) ;
break ;
}
case GetAllTextSetsCommand :
{
// cycle through all text sets
for ( int textSetIndex = 0 ; textSetIndex < LedDisplayController : : maximumTextSets ; textSetIndex + + )
{
if ( this - > ledDisplayController - > getTextSetParameter ( textSetIndex , LedDisplayController : : TextParameter ) = = " " )
continue ;
2022-07-28 10:11:12 +02:00
2020-10-15 22:54:20 +02:00
Serial . println ( " Adding index " + String ( textSetIndex ) + " with text: " + this - > ledDisplayController - > getTextSetParameter ( textSetIndex , LedDisplayController : : TextParameter ) ) ;
// if a set isn't empty, go through all parameters
for ( int textSetParameter = 0 ; textSetParameter < LedDisplayController : : DisplayTextSetParameterCount ; textSetParameter + + )
{
// send each parameter to the client
2022-07-28 10:11:12 +02:00
const size_t capacity = JSON_OBJECT_SIZE ( 2 ) + JSON_OBJECT_SIZE ( 3 ) + LedDisplayController : : maximumTextLength ; //200;
2020-10-15 22:54:20 +02:00
DynamicJsonDocument doc ( capacity ) ;
doc [ " header " ] = GetTextSetParameterCommand ;
JsonObject data = doc . createNestedObject ( " data " ) ;
data [ " index " ] = textSetIndex ;
data [ " parameter " ] = textSetParameter ;
2022-07-28 10:11:12 +02:00
2020-10-15 22:54:20 +02:00
data [ " value " ] = this - > ledDisplayController - > getTextSetParameter (
textSetIndex ,
LedDisplayController : : DisplayTextSetParameter ( textSetParameter ) ) ;
2022-07-28 10:11:12 +02:00
Serial . println ( " sending parameter: " + String ( textSetParameter ) + " with value: ' " + this - > ledDisplayController - > getTextSetParameter (
2020-10-18 15:44:18 +02:00
textSetIndex ,
2022-07-28 10:11:12 +02:00
LedDisplayController : : DisplayTextSetParameter ( textSetParameter ) ) + " ' " ) ;
2020-10-18 15:44:18 +02:00
2020-10-15 22:54:20 +02:00
String json ;
serializeJson ( doc , json ) ;
2022-07-28 10:11:12 +02:00
Serial . println ( " JSON: ' " + json + " ' " ) ;
2020-10-15 22:54:20 +02:00
this - > bleServer - > sendData ( json ) ;
2022-07-28 10:11:12 +02:00
2020-10-15 22:54:20 +02:00
}
}
replyStatus = Success ;
replyData [ " maximumTextSets " ] = LedDisplayController : : maximumTextSets ;
replyData [ " maximumTextLength " ] = LedDisplayController : : maximumTextLength ;
break ;
}
case GetDisplayBrightnessCommand :
{
replyData [ " displayBrightness " ] = this - > ledDisplayController - > getBrightness ( ) ;
2020-10-18 16:26:24 +02:00
replyData [ " automaticBrightnessAdjustment " ] = this - > ledDisplayController - > getAutomaticBrightnessAdjustment ( ) ;
2020-10-15 22:54:20 +02:00
replyStatus = Success ;
break ;
}
case SetTextSetParameterCommand :
2020-10-13 17:01:29 +02:00
{
2020-10-15 22:54:20 +02:00
int index = requestData [ " index " ] ;
int parameter = requestData [ " parameter " ] ;
String value = requestData [ " value " ] ;
2020-10-13 17:01:29 +02:00
2020-10-15 22:54:20 +02:00
LedDisplayController : : GetSetTextSetParameterExitCode res = this - > ledDisplayController - > setTextSetParameter ( index , LedDisplayController : : DisplayTextSetParameter ( parameter ) , value ) ;
2020-10-13 17:01:29 +02:00
2020-10-15 22:54:20 +02:00
if ( res = = LedDisplayController : : Success )
replyStatus = Success ;
else
2020-10-14 23:54:54 +02:00
{
2020-10-15 22:54:20 +02:00
replyStatus = DisplayControllerError ;
replyData [ " displayControllerError " ] = res ;
2020-10-14 23:54:54 +02:00
}
2020-10-15 22:54:20 +02:00
break ;
2020-10-14 23:54:54 +02:00
}
2020-10-15 22:54:20 +02:00
case SetDisplayBrightnessCommand :
{
this - > ledDisplayController - > setBrightness ( requestData [ " displayBrightness " ] ) ;
2020-10-18 16:26:24 +02:00
this - > ledDisplayController - > setAutomaticBrightnessAdjustment ( requestData [ " automaticBrightnessAdjustment " ] ) ;
2020-10-15 22:54:20 +02:00
replyStatus = Success ;
break ;
}
2020-10-17 03:22:37 +02:00
case SetDisplayCodeCommand : {
String code = requestData [ " displayCode " ] ;
if ( code . length ( ) ! = 4 ) {
replyStatus = BadRequestError ;
break ;
}
strncpy ( this - > properties . deviceCode , code . c_str ( ) , sizeof ( this - > properties . deviceCode ) ) ;
this - > storeProperties ( ) ;
replyStatus = Success ;
break ;
}
case SetDisplayNameCommand : {
String name = requestData [ " displayName " ] ;
if ( name . length ( ) < = 0 ) {
replyStatus = BadRequestError ;
break ;
}
strncpy ( this - > properties . deviceName , name . c_str ( ) , sizeof ( this - > properties . deviceName ) ) ;
this - > storeProperties ( ) ;
replyStatus = Success ;
break ;
}
2020-10-15 22:54:20 +02:00
default :
break ;
}
// reply to the client
replyDoc [ " status " ] = replyStatus ;
String json ;
serializeJson ( replyDoc , json ) ;
this - > bleServer - > sendData ( json ) ;
2022-07-28 10:11:12 +02:00
2020-10-15 22:54:20 +02:00
}
bool OmobiLedDisplay : : loadProperties ( )
{
if ( this - > eepromUnit = = nullptr )
return false ;
DisplayProperties buf = { } ;
2020-10-13 17:01:29 +02:00
2020-10-15 22:54:20 +02:00
// read conf from EEPROM
this - > eepromUnit - > read ( buf ) ;
2020-10-13 17:01:29 +02:00
2020-10-15 22:54:20 +02:00
if ( strcmp ( buf . valid , " OK " ) = = 0 )
{
memcpy ( & this - > properties , & buf , sizeof ( DisplayProperties ) ) ;
2020-10-14 23:54:54 +02:00
}
2020-10-15 22:54:20 +02:00
else
2020-10-14 23:54:54 +02:00
{
2020-10-15 22:54:20 +02:00
// There was an error reading the properties -> rebuild with default values!
DisplayProperties defaultProperties {
" Omobi Led Display " ,
" 1234 " ,
" OK " } ;
memcpy ( & this - > properties , & defaultProperties , sizeof ( DisplayProperties ) ) ;
this - > storeProperties ( ) ;
2020-10-14 23:54:54 +02:00
}
2020-10-13 17:01:29 +02:00
2020-10-15 22:54:20 +02:00
return true ;
}
2020-10-13 17:01:29 +02:00
2020-10-15 22:54:20 +02:00
bool OmobiLedDisplay : : storeProperties ( )
{
if ( this - > eepromUnit = = nullptr )
return false ;
2020-10-13 17:01:29 +02:00
2020-10-15 22:54:20 +02:00
strncpy ( this - > properties . valid , " OK " , sizeof ( this - > properties . valid ) ) ;
this - > eepromUnit - > write ( this - > properties ) ;
return true ;
}
2020-10-13 17:01:29 +02:00
2020-10-15 22:54:20 +02:00
String OmobiLedDisplay : : sha256 ( String payload )
{
byte shaResult [ 32 ] ;
mbedtls_md_context_t ctx ;
mbedtls_md_type_t md_type = MBEDTLS_MD_SHA256 ;
const size_t payloadLength = strlen ( payload . c_str ( ) ) ;
mbedtls_md_init ( & ctx ) ;
mbedtls_md_setup ( & ctx , mbedtls_md_info_from_type ( md_type ) , 0 ) ;
mbedtls_md_starts ( & ctx ) ;
mbedtls_md_update ( & ctx , ( const unsigned char * ) payload . c_str ( ) , payloadLength ) ;
mbedtls_md_finish ( & ctx , shaResult ) ;
mbedtls_md_free ( & ctx ) ;
String resultString = " " ;
for ( int i = 0 ; i < sizeof ( shaResult ) ; i + + )
2020-10-14 23:54:54 +02:00
{
2020-10-15 22:54:20 +02:00
char str [ 3 ] ;
sprintf ( str , " %02x " , ( int ) shaResult [ i ] ) ;
resultString + = str ;
2020-10-13 17:01:29 +02:00
}
2020-10-14 23:54:54 +02:00
2020-10-15 22:54:20 +02:00
return resultString ;
2020-10-13 17:01:29 +02:00
}