Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
K
k-sdk
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
陈伟灿
k-sdk
Commits
02c29df5
Commit
02c29df5
authored
Aug 31, 2020
by
chen.weican
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
【修改内容】增加设备属性数据表的建立和更新
【提交人】陈伟灿
parent
7d285570
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
335 additions
and
6 deletions
+335
-6
midware/midware/dm/kk_dm_mng.c
midware/midware/dm/kk_dm_mng.c
+25
-1
midware/midware/dm/kk_linkkit.c
midware/midware/dm/kk_linkkit.c
+1
-0
midware/midware/dm/kk_property_db.c
midware/midware/dm/kk_property_db.c
+293
-0
midware/midware/dm/kk_property_db.h
midware/midware/dm/kk_property_db.h
+8
-0
midware/midware/dm/kk_sub_db.c
midware/midware/dm/kk_sub_db.c
+5
-1
midware/midware/midware.c
midware/midware/midware.c
+3
-1
midware/tsl/tsl_handle/kk_tsl_func.c
midware/tsl/tsl_handle/kk_tsl_func.c
+0
-3
No files found.
midware/midware/dm/kk_dm_mng.c
View file @
02c29df5
...
...
@@ -8,6 +8,8 @@
#include "iot_export_linkkit.h"
#include "kk_product.h"
#include "kk_log.h"
#include "kk_property_db.h"
...
...
@@ -146,6 +148,25 @@ static int _dm_init_tsl_params(int devId)
}
return
res
;
}
int
dm_mgr_properities_db_create
(
kk_tsl_t
*
dev_shadow
,
char
*
deviceCode
)
{
int
num
=
0
,
idx
=
0
;
kk_tsl_data_t
*
pProperty
=
NULL
;
if
(
dev_shadow
==
NULL
||
deviceCode
==
NULL
){
ERROR_PRINT
(
"dm_mgr_properities_db_create fail!!
\n
"
);
return
INVALID_PARAMETER
;
}
num
=
dev_shadow
->
property_number
;
INFO_PRINT
(
"dm_mgr_properities_db_create num:%d!!
\n
"
,
num
);
for
(
idx
=
0
;
idx
<
num
;
idx
++
){
pProperty
=
dev_shadow
->
properties
+
idx
;
INFO_PRINT
(
"dm_mgr_properities_db_create pProperty->identifier:%s!!
\n
"
,
pProperty
->
identifier
);
if
(
pProperty
){
kk_property_db_insert
(
deviceCode
,
pProperty
->
identifier
,
pProperty
->
data_value
.
type
);
}
}
}
int
dm_mgr_device_create
(
_IN_
int
dev_type
,
_IN_
char
productCode
[
PRODUCT_CODE_MAXLEN
],
_IN_
char
deviceCode
[
DEVICE_CODE_MAXLEN
],
_IN_
char
mac
[
DEVICE_MAC_MAXLEN
],
_IN_
char
fatherDeviceCode
[
DEVICE_CODE_MAXLEN
],
_OU_
int
*
devid
)
{
...
...
@@ -203,6 +224,7 @@ int dm_mgr_device_create(_IN_ int dev_type,_IN_ char productCode[PRODUCT_CODE_MA
return
FAIL_RETURN
;
}
}
dm_mgr_properities_db_create
(
node
->
dev_shadow
,
deviceCode
);
INIT_LIST_HEAD
(
&
node
->
linked_list
);
list_add_tail
(
&
node
->
linked_list
,
&
ctx
->
dev_list
);
...
...
@@ -387,7 +409,7 @@ int dm_mgr_init(void)
}
/* Init Device Id*/
ctx
->
global_devid
=
1
;
kk_property_db_init
();
/* Init Device List */
INIT_LIST_HEAD
(
&
ctx
->
dev_list
);
...
...
@@ -400,6 +422,8 @@ int dm_mgr_init(void)
}
else
{
_dm_init_tsl_params
(
devId
);
kk_property_db_update
(
"CCU_66666"
);
}
return
SUCCESS_RETURN
;
...
...
midware/midware/dm/kk_linkkit.c
View file @
02c29df5
...
...
@@ -308,6 +308,7 @@ static void _iotx_linkkit_event_callback(iotx_dm_event_types_t type, char *data)
{
ERROR_PRINT
(
"[%s][%d] res:%d
\n
"
,
__FUNCTION__
,
__LINE__
,
res
);
}
kk_property_db_update
(
"CCU_66666"
);
if
(
s_CloudStatus
){
iotx_dm_dev_online
(
0
);
//first online,report the online status
usleep
(
200000
);
...
...
midware/midware/dm/kk_property_db.c
0 → 100644
View file @
02c29df5
#include <stdio.h>
#include "kk_tsl_api.h"
#include "sqlite3.h"
#include "kk_log.h"
#include "kk_dm_mng.h"
#define KK_PROPERTIES_DB_FILE "kk_properties.db"
typedef
struct
{
void
*
mutex
;
sqlite3
*
pDb
;
}
kk_property_db_ctx_t
;
typedef
enum
{
DB_IDX
=
0
,
DB_DEVICECODE
,
DB_IDENTIFITER
,
DB_VALUE
,
DB_VALUETYPE
,
};
static
kk_property_db_ctx_t
s_kk_property_db_ctx
=
{
0
};
static
kk_property_db_ctx_t
*
_kk_property_db_get_ctx
(
void
)
{
return
&
s_kk_property_db_ctx
;
}
static
void
_kk_property_db_lock
(
void
)
{
kk_property_db_ctx_t
*
ctx
=
_kk_property_db_get_ctx
();
if
(
ctx
->
mutex
)
{
HAL_MutexLock
(
ctx
->
mutex
);
}
}
static
void
_kk_property_db_unlock
(
void
)
{
kk_property_db_ctx_t
*
ctx
=
_kk_property_db_get_ctx
();
if
(
ctx
->
mutex
)
{
HAL_MutexUnlock
(
ctx
->
mutex
);
}
}
static
int
kk_property_db_Init
(
void
)
{
kk_property_db_ctx_t
*
ctx
=
_kk_property_db_get_ctx
();
//eUtils_LockLock(&sLock);
_kk_property_db_lock
();
if
(
sqlite3_open_v2
(
KK_PROPERTIES_DB_FILE
,
&
ctx
->
pDb
,
SQLITE_OPEN_READWRITE
|
SQLITE_OPEN_CREATE
|
SQLITE_OPEN_FULLMUTEX
,
NULL
)
!=
SQLITE_OK
)
{
ERROR_PRINT
(
"Error initialising linkage database (%s)"
,
sqlite3_errmsg
(
ctx
->
pDb
));
_kk_property_db_unlock
();
return
FAIL_RETURN
;
}
INFO_PRINT
(
"property db Database opened
\n
"
);
{
const
char
*
pPrpertyTable
=
"CREATE TABLE IF NOT EXISTS PropertiesInfo( \
idx INTEGER PRIMARY KEY, \
deviceCode varchar(33), \
identifier varchar(33), \
value varchar(33), \
valueType INTEGER)"
;
char
*
pcErr
;
// DBG_vPrintf(DBG_SQL, "Execute SQL: '%s'\n", pConditionTableDef);
if
(
sqlite3_exec
(
ctx
->
pDb
,
pPrpertyTable
,
NULL
,
NULL
,
&
pcErr
)
!=
SQLITE_OK
)
{
ERROR_PRINT
(
"Error creating table (%s)
\n
"
,
pcErr
);
sqlite3_free
(
pcErr
);
//eUtils_LockUnlock(&sLock);
_kk_property_db_unlock
();
return
FAIL_RETURN
;
}
}
//eUtils_LockUnlock(&sLock);
_kk_property_db_unlock
();
return
SUCCESS_RETURN
;
}
int
kk_property_db_init
(
void
)
{
int
res
=
0
;
kk_property_db_ctx_t
*
ctx
=
_kk_property_db_get_ctx
();
/* Create Mutex */
ctx
->
mutex
=
HAL_MutexCreate
();
if
(
ctx
->
mutex
==
NULL
)
{
return
FAIL_RETURN
;
}
res
=
kk_property_db_Init
();
if
(
res
!=
SUCCESS_RETURN
){
ERROR_PRINT
(
"[%s][%d]kk_wlist_db_Init FAIL!!!
\n
"
,
__FUNCTION__
,
__LINE__
);
}
//_kk_load_subDevice();
return
SUCCESS_RETURN
;
}
static
int
_kk_check_property_exist
(
const
char
*
deviceCode
,
const
char
*
identifier
)
{
int
isExist
=
0
;
sqlite3_stmt
*
stmt
;
char
*
pDeviceCode
=
NULL
;
char
*
pIdentifier
=
NULL
;
kk_property_db_ctx_t
*
ctx
=
_kk_property_db_get_ctx
();
const
char
*
searchCmd
=
"select * from PropertiesInfo;"
;
_kk_property_db_lock
();
sqlite3_prepare_v2
(
ctx
->
pDb
,
searchCmd
,
strlen
(
searchCmd
),
&
stmt
,
NULL
);
INFO_PRINT
(
"total_column = %d
\n
"
,
sqlite3_column_count
(
stmt
));
while
(
sqlite3_step
(
stmt
)
==
SQLITE_ROW
){
pDeviceCode
=
sqlite3_column_text
(
stmt
,
DB_DEVICECODE
);
pIdentifier
=
sqlite3_column_text
(
stmt
,
DB_IDENTIFITER
);
if
(
!
strcmp
(
deviceCode
,
pDeviceCode
)
&&!
strcmp
(
identifier
,
pIdentifier
))
{
isExist
=
1
;
break
;
}
}
INFO_PRINT
(
"
\n
"
);
sqlite3_finalize
(
stmt
);
_kk_property_db_unlock
();
return
isExist
;
}
int
kk_property_db_insert
(
const
char
*
deviceCode
,
const
char
*
identifier
,
kk_tsl_data_type_e
valuetype
)
{
const
char
*
insertCmd
=
"insert into PropertiesInfo (deviceCode,identifier,value,valueType) \
values ('%s','%s','%s','%d');"
;
char
*
sqlCmd
=
NULL
;
int
rc
=
0
;
char
*
zErrMsg
=
0
;
kk_property_db_ctx_t
*
ctx
=
_kk_property_db_get_ctx
();
if
(
_kk_check_property_exist
(
deviceCode
,
identifier
)
==
1
)
{
WARNING_PRINT
(
"[%s][%d] DATA ALREADY EXIST!!!
\n
"
,
__FUNCTION__
,
__LINE__
);
return
SUCCESS_RETURN
;
}
_kk_property_db_lock
();
sqlCmd
=
sqlite3_mprintf
(
insertCmd
,
deviceCode
,
identifier
,
""
,
valuetype
);
rc
=
sqlite3_exec
(
ctx
->
pDb
,
sqlCmd
,
NULL
,
NULL
,
&
zErrMsg
);
if
(
rc
!=
SQLITE_OK
){
ERROR_PRINT
(
"SQL error: %s
\n
"
,
zErrMsg
);
sqlite3_free
(
zErrMsg
);
}
else
{
INFO_PRINT
(
"sub device insert data successfully
\n
"
);
}
sqlite3_free
(
sqlCmd
);
_kk_property_db_unlock
();
return
SUCCESS_RETURN
;
}
int
kk_property_db_update_value
(
const
char
*
deviceCode
,
const
char
*
identifier
,
const
char
*
value
)
{
char
*
sqlCmd
=
NULL
;
int
rc
=
0
;
char
*
zErrMsg
=
0
;
kk_property_db_ctx_t
*
ctx
=
_kk_property_db_get_ctx
();
_kk_property_db_lock
();
sqlCmd
=
sqlite3_mprintf
(
"UPDATE PropertiesInfo SET value='%s' WHERE deviceCode= '%s' and identifier = '%s'"
,
value
,
deviceCode
,
identifier
);
DEBUG_PRINT
(
"kk_property_db_update_value sqlCmd:%s
\n
"
,
sqlCmd
);
rc
=
sqlite3_exec
(
ctx
->
pDb
,
sqlCmd
,
NULL
,
NULL
,
&
zErrMsg
);
if
(
rc
!=
SQLITE_OK
){
ERROR_PRINT
(
"SQL error: %s
\n
"
,
zErrMsg
);
sqlite3_free
(
zErrMsg
);
}
else
{
DEBUG_PRINT
(
"kk_property_db_update_value successfully
\n
"
);
}
sqlite3_free
(
sqlCmd
);
_kk_property_db_unlock
();
return
SUCCESS_RETURN
;
}
int
kk_property_db_update
(
const
char
*
deviceCode
)
{
int
res
=
0
;
int
idx
=
0
;
int
num
=
0
;
char
tmpValue
[
20
]
=
{
0
};
dm_mgr_dev_node_t
*
node
=
NULL
;
kk_tsl_data_t
*
property
=
NULL
;
res
=
dm_mgr_get_device_by_devicecode
(
deviceCode
,
&
node
);
if
(
res
!=
SUCCESS_RETURN
)
{
return
FAIL_RETURN
;
}
num
=
node
->
dev_shadow
->
property_number
;
for
(
idx
=
0
;
idx
<
num
;
idx
++
){
property
=
node
->
dev_shadow
->
properties
+
idx
;
if
(
property
->
data_value
.
type
==
KK_TSL_DATA_TYPE_INT
||
property
->
data_value
.
type
==
KK_TSL_DATA_TYPE_ENUM
||
property
->
data_value
.
type
==
KK_TSL_DATA_TYPE_BOOL
){
sprintf
(
tmpValue
,
"%d"
,
property
->
data_value
.
value_int
);
kk_property_db_update_value
(
deviceCode
,
property
->
identifier
,
tmpValue
);
}
else
if
(
property
->
data_value
.
type
==
KK_TSL_DATA_TYPE_FLOAT
){
sprintf
(
tmpValue
,
"%d"
,
property
->
data_value
.
value_float
);
kk_property_db_update_value
(
deviceCode
,
property
->
identifier
,
tmpValue
);
}
else
if
(
property
->
data_value
.
type
==
KK_TSL_DATA_TYPE_DOUBLE
){
sprintf
(
tmpValue
,
"%d"
,
property
->
data_value
.
value_double
);
kk_property_db_update_value
(
deviceCode
,
property
->
identifier
,
tmpValue
);
}
else
if
(
property
->
data_value
.
type
==
KK_TSL_DATA_TYPE_TEXT
||
property
->
data_value
.
type
==
KK_TSL_DATA_TYPE_DATE
){
kk_property_db_update_value
(
deviceCode
,
property
->
identifier
,
property
->
data_value
.
value
);
}
else
{
ERROR_PRINT
(
"Unkonwn Number Type"
);
}
}
return
SUCCESS_RETURN
;
}
int
kk_property_sync_values
(
const
char
*
deviceCode
)
{
char
*
sqlCmd
=
NULL
;
int
rc
=
0
;
int
res
=
0
;
char
*
zErrMsg
=
0
;
sqlite3_stmt
*
stmt
;
char
*
pIdentifier
=
NULL
;
char
*
valueStr
=
NULL
;
int
valueType
=
0
;
int
devId
=
0
;
kk_property_db_ctx_t
*
ctx
=
_kk_property_db_get_ctx
();
char
*
searchCmd
=
"select * from PropertiesInfo;"
;
dm_mgr_get_devId_by_devicecode
(
deviceCode
,
&
devId
);
sqlCmd
=
sqlite3_mprintf
(
searchCmd
,
deviceCode
);
_kk_property_db_lock
();
sqlite3_prepare_v2
(
ctx
->
pDb
,
searchCmd
,
strlen
(
searchCmd
),
&
stmt
,
NULL
);
while
(
sqlite3_step
(
stmt
)
==
SQLITE_ROW
){
if
(
!
strcmp
(
deviceCode
,
sqlite3_column_text
(
stmt
,
DB_DEVICECODE
)))
{
pIdentifier
=
sqlite3_column_text
(
stmt
,
DB_IDENTIFITER
);
valueStr
=
sqlite3_column_text
(
stmt
,
DB_VALUE
);
valueType
=
sqlite3_column_int
(
stmt
,
DB_VALUETYPE
);
if
(
valueType
==
KK_TSL_DATA_TYPE_INT
||
valueType
==
KK_TSL_DATA_TYPE_ENUM
||
valueType
==
KK_TSL_DATA_TYPE_BOOL
){
int
value_int
=
atoi
(
valueStr
);
res
=
kk_tsl_set_value
(
kk_tsl_set_property_value
,
devId
,
pIdentifier
,
&
value_int
,
NULL
);
}
else
if
(
valueType
==
KK_TSL_DATA_TYPE_FLOAT
){
float
value_float
=
atoi
(
valueStr
);
res
=
kk_tsl_set_value
(
kk_tsl_set_property_value
,
devId
,
pIdentifier
,
&
value_float
,
NULL
);
}
else
if
(
valueType
==
KK_TSL_DATA_TYPE_DOUBLE
){
double
value_double
=
atoi
(
valueStr
);
res
=
kk_tsl_set_value
(
kk_tsl_set_property_value
,
devId
,
pIdentifier
,
&
value_double
,
NULL
);
}
else
{
res
=
kk_tsl_set_value
(
kk_tsl_set_property_value
,
devId
,
pIdentifier
,
NULL
,
valueStr
);
}
if
(
res
!=
SUCCESS_RETURN
)
{
ERROR_PRINT
(
"[%s][%d] res:%d
\n
"
,
__FUNCTION__
,
__LINE__
,
res
);
}
}
}
_kk_property_db_unlock
();
sqlite3_finalize
(
stmt
);
return
SUCCESS_RETURN
;
}
midware/midware/dm/kk_property_db.h
0 → 100644
View file @
02c29df5
#ifndef _KK_PROPERTY_DB_H_
#define _KK_PROPERTY_DB_H_
#include "kk_tsl_common.h"
int
kk_property_db_init
(
void
);
#endif
midware/midware/dm/kk_sub_db.c
View file @
02c29df5
...
...
@@ -2,6 +2,8 @@
#include <stdio.h>
#include "kk_tsl_api.h"
#include "kk_sub_db.h"
#include "kk_dm_mng.h"
#include "sqlite3.h"
#include "kk_log.h"
...
...
@@ -100,7 +102,6 @@ static int _kk_load_subDevice(void)
int
res
=
0
;
_kk_subDb_lock
();
sqlite3_prepare_v2
(
ctx
->
pDb
,
searchCmd
,
strlen
(
searchCmd
),
&
stmt
,
NULL
);
INFO_PRINT
(
"_kk_load_subDevice total_column = %d
\n
"
,
sqlite3_column_count
(
stmt
));
while
(
sqlite3_step
(
stmt
)
==
SQLITE_ROW
){
res
=
dm_mgr_subdev_create
(
sqlite3_column_int
(
stmt
,
DB_DEVTYPE
),
sqlite3_column_text
(
stmt
,
DB_PRODUCTCODE
),
...
...
@@ -118,6 +119,9 @@ static int _kk_load_subDevice(void)
//再上线
iotx_dm_subscribe
(
devId
);
if
(
sqlite3_column_int
(
stmt
,
DB_DEVTYPE
)
==
KK_DM_DEVICE_SUBDEV
){
kk_property_sync_values
(
sqlite3_column_text
(
stmt
,
DB_DEVICECODE
));
}
kk_dm_ota_report_version
(
devId
,
sqlite3_column_text
(
stmt
,
DB_VERSION
));
//version
//usleep(100000);
...
...
midware/midware/midware.c
View file @
02c29df5
...
...
@@ -217,6 +217,7 @@ void kk_platMsg_handle(void* data, char* chalMark){
INFO_PRINT
(
"save property and send to cloud
\n
"
);
char
*
outstr
=
cJSON_Print
(
payload
);
kk_tsl_property_set_by_devicecode
(
info_dcode
->
valuestring
,
outstr
,
strlen
(
outstr
)
+
1
);
kk_property_db_update
(
info_dcode
->
valuestring
);
free
(
outstr
);
}
else
if
(
strstr
(
msgType
->
valuestring
,
"/thing/topo/delete"
)
!=
NULL
){
...
...
@@ -574,7 +575,8 @@ void *ccu_property_monitor(void *args)
else
{
if
(
strcmp
(
s_IP
,
s_IP_TSL
)){
kk_tsl_set_value
(
kk_tsl_set_property_value
,
0
,
KK_TSL_CCU_WANIP_IDENTIFIER
,
NULL
,
s_IP
);
INFO_PRINT
(
"current ip:%s,db ip:%s
\n
"
,
s_IP
,
s_IP_TSL
);
INFO_PRINT
(
"current ip:%s,before ip:%s
\n
"
,
s_IP
,
s_IP_TSL
);
kk_property_db_update
(
"CCU_66666"
);
needReport
=
1
;
}
}
...
...
midware/tsl/tsl_handle/kk_tsl_func.c
View file @
02c29df5
...
...
@@ -670,15 +670,12 @@ int dm_tsl_set_property_value(_IN_ kk_tsl_t *shadow, _IN_ char *key, _IN_ int ke
if
(
shadow
==
NULL
||
key
==
NULL
||
key_len
<=
0
)
{
return
FAIL_RETURN
;
}
printf
(
"Key:%d %s"
,
key_len
,
key
);
res
=
_kk_tsl_property_search
(
shadow
,
key
,
key_len
,
&
data
,
&
array_index
);
if
(
res
!=
SUCCESS_RETURN
)
{
return
FAIL_RETURN
;
}
if
(
data
->
data_value
.
type
==
KK_TSL_DATA_TYPE_ARRAY
)
{
printf
(
"Current Found Data Index: %d"
,
array_index
);
res
=
_kk_tsl_data_array_set
(
&
data
->
data_value
,
value
,
value_len
,
array_index
);
if
(
res
!=
SUCCESS_RETURN
)
{
return
TSL_PROPERTY_SET_FAILED
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment