CGP协议
物联网应用中除了通信层技术提供高并发高连接通信技术外,还需要一种可以跨技术语言的通信控制协议。例如某个传感器要上传一次原信息到物联网云平台中记录,或者针对某个终端设备(灯、门等)发送一个控制开关指令操作。CGP协议全称:Coolpy General Protocol (中文: Coolpy通用协议)

数据库操作篇

传感器应用一般会从传感器终端产生(定时或实时)传感器数据,然后通过本协议把数据发送到CP7并保存到数据库或进行其他数据库相关的操作可以通过本协议进行操作,本协议支持Nosql数据库 (Mongodb)、Sql数据库(MsSql、MySql、PostgresSql)。
如果使用Mongodb数据库还可以通过本协议让Mongodb成为一个图片或文件存储服务器,通过Mongodb集群可以组建高性能高可用的OSS系统。使用Mongodb的GridFS技术。

Mongodb操作

插入一条数据

1
{
2
"db_name":"DbPoxyTestDB",
3
"table_name":"test",
4
"op_name":"insert",
5
"token":"password",
6
"msg_id":1,
7
"ref_topic":"dbpoxy/mongodb/result",
8
"ref_qos":0,
9
"value":{"datetime":"2018-10-12T10:10:12+08:00","number":12345.12345655889977,"string":"djkfjdkfj" }
10
}
Copied!

修改指定id记录内容

1
{
2
"db_name":"DbPoxyTestDB",
3
"table_name":"test",
4
"op_name":"update",
5
"token":"password",
6
"msg_id":1,
7
"ref_topic":"dbpoxy/mongodb/result",
8
"ref_qos":0,
9
"filter_id":"5c106004536ad2a1ddfb0106",
10
"value":{"string":"333333333" }
11
}
Copied!

按条件更新信息

1
{
2
"db_name":"DbPoxyTestDB",
3
"table_name":"test",
4
"op_name":"update",
5
"token":"password",
6
"msg_id":1,
7
"ref_topic":"dbpoxy/mongodb/result",
8
"ref_qos":0,
9
"filter":{"datetime":"2018-10-12T10:10:12+08:00"},
10
"value":{"string":"444444" }
11
}
Copied!

删除指定ID数据

1
{
2
"db_name":"DbPoxyTestDB",
3
"table_name":"test",
4
"op_name":"delete",
5
"token":"password",
6
"msg_id":2,
7
"ref_topic":"dbpoxy/mongodb/result",
8
"ref_qos":0,
9
"filter_id":"5be3a553572c066aa453f8c6"
10
}
Copied!

通过查询条件(datatime字段等于指定值)删除指定数据

1
{
2
"db_name":"DbPoxyTestDB",
3
"table_name":"test",
4
"op_name":"delete",
5
"token":"password",
6
"msg_id":3,
7
"ref_topic":"dbpoxy/mongodb/result",
8
"ref_qos":0,
9
"filter":{"datetime":"2018-10-12T10:10:12+08:00"}
10
}
Copied!

按条件(指定时间范围)查询数据

filter_pipe值支持Mongodb原生的Aggregate聚合操作,祥细说明说参阅:https://docs.mongodb.com/manual/core/aggregation-pipeline/index.html

1
{
2
"db_name":"DbPoxyTestDB",
3
"table_name":"test",
4
"op_name":"query",
5
"token":"password",
6
"msg_id":4,
7
"ref_topic":"dbpoxy/mongodb/result",
8
"ref_qos":0,
9
"filter_pipe":[
10
{
11
"$match": {
12
"$and": [
13
{ "datetime": { "$gte": "2018-10-16T10:10:12+08:00" } },
14
{ "datetime": { "$lte": "2018-10-18T10:10:12+08:00" } }
15
]
16
}
17
},
18
{"$skip":1},
19
{"$limit":2}
20
]}
Copied!

按条件(指定Id)查询数据

1
{
2
"db_name":"DbPoxyTestDB",
3
"table_name":"test",
4
"op_name":"query",
5
"token":"password",
6
"msg_id":5,
7
"ref_topic":"dbpoxy/mongodb/result",
8
"ref_qos":0,
9
"filter_id":"5b03e04250d8e8bc694a3b2f"
10
}
Copied!

事务(事务执行插入,更新,删除操作)

1
{
2
"db_name": "DbPoxyTestDB",
3
"op_name": "work",
4
"token": "password",
5
"msg_id": 1,
6
"ref_topic": "dbpoxy/mongodb/result",
7
"ref_qos": 0,
8
"mongo_works": [{
9
"table_name": "users",
10
"op_name": "insert",
11
"work": {
12
"datetime": "2018-10-12T10:10:12+08:00",
13
"number": 12345.12345655889977,
14
"string": "djkfjdkfj"
15
},
16
"id_alias": "insertid"
17
},
18
{
19
"table_name": "users",
20
"op_name": "update",
21
"work": {
22
"filter": {
23
"_id": "insertid"
24
},
25
"value": {
26
"$set": {
27
"string": "333333333"
28
}
29
}
30
}
31
},
32
{
33
"table_name": "users",
34
"op_name": "query",
35
"work": {
36
"filter": {
37
"_id": "insertid"
38
}
39
}
40
},
41
{
42
"table_name": "users",
43
"op_name": "delete",
44
"work": {
45
"filter": {
46
"_id": "insertid"
47
}
48
}
49
}
50
]
51
}
Copied!

SQL数据库操作(mysql mssql pgsql等)

DbPoxy配置文件启用sql数据库连接
1
#postgres
2
DatabaseType: postgres #mssql mysql postgres
3
DatabaseConnectionString: host=192.168.100.187 port=13246 user=postgres dbname=ldh sslmode=disable password=123456
Copied!

插入数据

1
{
2
"table_name":"users",
3
"op_name":"insert",
4
"token":"password",
5
"msg_id":1,
6
"ref_topic":"dbpoxy/mongodb/result",
7
"ref_qos":0,
8
"sql_exec":"INSERT INTO users(age, name, num) VALUES (100, 'ccc', 77)"
9
}
Copied!

删除数据

1
{
2
"table_name":"users",
3
"op_name":"delete",
4
"token":"password",
5
"msg_id":1,
6
"ref_topic":"dbpoxy/mongodb/result",
7
"ref_qos":0,
8
"sql_exec":"DELETE FROM users WHERE id=3;"
9
}
Copied!

更新数据

1
{
2
"table_name":"users",
3
"op_name":"update",
4
"token":"password",
5
"msg_id":1,
6
"ref_topic":"dbpoxy/mongodb/result",
7
"ref_qos":0,
8
"sql_exec":"UPDATE users SET age=8, name='coolpy7' WHERE id=2;"
9
}
Copied!
查询数据
1
{
2
"table_name":"users",
3
"op_name":"query",
4
"token":"password",
5
"msg_id":1,
6
"ref_topic":"dbpoxy/mongodb/result",
7
"ref_qos":0,
8
"sql_query":"SELECT * FROM users;"
9
}
Copied!
事务插入数据,通过id_alias记录插入成功后新数据结点ID
1
{
2
"table_name":"users",
3
"op_name":"work",
4
"token":"password",
5
"msg_id":1,
6
"ref_topic":"dbpoxy/mongodb/result",
7
"ref_qos":0,
8
"sql_works":[
9
{"table_name":"users", "work":"INSERT INTO users(age, name, num) VALUES (100, 'ccc', 77)","id_alias":"insertid"},
10
{"table_name":"users", "work":"UPDATE users SET age=8, name='coolpy7' WHERE id=insertid"},
11
{"table_name":"users", "work":"DELETE FROM users WHERE id=insertid"}
12
]}
Copied!

OSS( Object Storage Service)对象存储服务

此功能实现像阿里云OSS对象存储系统类似功能,可以支持直接保存文件或图片等流式文件,以流式格式保存到Mongodb的Gridfs特殊的对象存储数据结构,提供了高性能和可扩展的文件存储服务功能。
上传及删除文件示例开源地址:https://github.com/Coolpy7/dbpoxyclient
DbPoxy配置文件启用OSS连接,基于Mongodb
1
#oss
2
DatabaseType: oss-gridfs
3
DatabaseConnectionString: mongodb://localhost:27017
Copied!
上传文件
1
{
2
"db_name":"gridfs",
3
"table_name":"fs",
4
"op_name":"insert",
5
"token":"password",
6
"msg_id":1,
7
"ref_topic":"dbpoxy/mongodb/result",
8
"ref_qos":0,
9
"oss_file_name":文件名,
10
"oss_file_base64":以base64形式的文件流
11
// "oss_file_hex":以hex形式的文件流、此操作符不能与oss_file_base64同时使用
12
}
Copied!
删除文件
1
{
2
"db_name":"gridfs",
3
"table_name":"fs",
4
"op_name":"delete",
5
"token":"password",
6
"msg_id":1,
7
"ref_topic":"dbpoxy/mongodb/result",
8
"ref_qos":0,
9
"filter_id":"5b0a610853c613cd1c4d9840"
10
}
Copied!
Last modified 2yr ago