小记
现在在融入到网关项目中,接下来的一段时间应该就可以参与到项目中了,现阶段还是在进行仿写
笔记
MQTT协议
我之前的博客也有写过:
https://blog.csdn.net/lzl980111/article/details/106188393
简单梳理一下:
-
MQTT是干嘛的?
是一个轻量级的,基于代理的”发布/订阅“模式的消息传输协议什么是”发布/订阅“模式?在MQTT协议中,有发布者,订阅者和代理(broker)三种角色,消息由发布者通过topic发布,由订阅者对感兴趣的topic进行订阅,一个发布者可以对应多个订阅者,一个订阅者也可以订阅多个topic,另外,订阅者也可以是发布者,发布者也可以是订阅者,这样就实现了"M2M"(Machine to Machine)的通信。broker类似于邮局或者说信差的角色,它的主要作用就是接收信件并投递给订阅信件的人。如果订阅者不在线,也就是说没有connected to the broker,那么消息会保留,等订阅者在线时投递
发布 - 订阅的消息通信协议,允许一条消息只发布一次,便可被多个消费端(应用程序 / 设备)所接收
cJSON循环在数组中循环创建多个对象
实现的JSON如下:
"datas": [{
"item_id": 0,
"offset": 0,
"dtype": 0,
"endian": 0,
"rwflag": 0,
"mtype": 0,
"radio": 0,
"base": 0,
"topic": "TEMP",
"label": "温湿度传感",
"desc": "1",
"qos": 0
}, {
"item_id": 1,
"offset": 1,
"dtype": 1,
"endian": 1,
"rwflag": 1,
"mtype": 1,
"radio": 1,
"base": 1,
"topic": "TEMP",
"label": "温湿度传感",
"desc": "1",
"qos": 1
}]
c代码段:
int i=0;
for(i;i<2;i++){
cJSON * data=cJSON_CreateObject();
cJSON_AddItemToArray(datas,data);
cJSON_AddNumberToObject(data,"item_id",i);
cJSON_AddNumberToObject(data,"offset",i);
cJSON_AddNumberToObject(data,"dtype",i);
cJSON_AddNumberToObject(data,"endian",i);
cJSON_AddNumberToObject(data,"rwflag",i);
cJSON_AddNumberToObject(data,"mtype",i);
cJSON_AddNumberToObject(data,"radio",i);
cJSON_AddNumberToObject(data,"base",i);
cJSON_AddStringToObject(data,"topic",topic);
cJSON_AddStringToObject(data,"label",label);
cJSON_AddStringToObject(data,"desc",desc);
cJSON_AddNumberToObject(data,"qos",i);
}
cJSON_AddItemToObject(command,"datas",datas);
思路:先向数组中添加Object,再在Object中填充内容
最开始我是想的 每次 new一个新的Object,走了一点小弯路
读json文件
这个是我自己写的
FILE *fp;
char ch;
char result[SIZE];
fp = fopen("target.json", "r");
if(fp==NULL){
printf("open_file_fail\n");
}else
{
printf("open_file_suc\n");
ch=fgetc(fp);
int i=0;
while(ch != EOF){
//putchar(ch);
result[i]=ch;
i++;
ch=fgetc(fp);
}
对照一下:
char* read_json(char str[256]){
FILE *f;
if((f=fopen(str,"r")) == NULL ){
printf("Fail to open file!\n");
exit(0); //退出程序
}
int len;//长度
char *content;//内容
fseek(f,0,SEEK_END);
len=ftell(f);
fseek(f,0,SEEK_SET);
content=(char*)malloc(len+1);
fread(content,1,len,f);
fclose(f);
return content;
}
写了关于对device.json进行编辑和删除的cgi程序
修改:
#include "cgic.h"
#include "cJSON.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "pub_function.h"
void write_json(char *json_str){
FILE *fp;
fp=fopen("../json/new_devices.json","w");
fputs(json_str,fp);
fclose(fp);
}
void edit_json(char *content){
//修改device下的 commands[1]/data[1]下的内容
printf("content:%s",content);
cJSON *device_before_edit=cJSON_Parse(content);
cJSON *commands=cJSON_GetObjectItem(device_before_edit,"commands");
int size=cJSON_GetArraySize(commands);
for(int i = 0 ; i < size ; i++){
cJSON *command=cJSON_GetArrayItem(commands,i);
int command_id = cJSON_GetObjectItem(command,"command_id")->valueint;
//printf("%d",command_id);
if(command_id==1){
//现在拿到了commands[1],下面获取一下 data[1]
cJSON *data=cJSON_GetObjectItem(command,"data");
int data_size=cJSON_GetArraySize(data);
//现在拿到了data
for(int j=0;j<data_size;j++){
//获取每一项object,对每一object的item_id进行比较
//printf("%d",j);
cJSON *data_item=cJSON_GetArrayItem(data,j);
int item_id=cJSON_GetObjectItem(data_item,"item_id")->valueint;
if(item_id==1){
//现在定位到了data[1],下面对data[1]进行修改,并保存到新的new_devices.json文件中
/*
"item_id": 1,
"offset": 1,
"dtype": 1,
"endian": 1,
"rwflag": 1,
"mtype": 1,
"radio": 1,
"base": 1,
"topic": "TEMP",
"label": "温湿度传感",
"desc": "1",
"qos": 1
*/
// cJSON_GetObjectItem(data_item,"offset")->valuedouble=2.2;
// cJSON_GetObjectItem(data_item,"dtype")->valuedouble=2.2;
// cJSON_GetObjectItem(data_item,"endian")->valuedouble=2;
// cJSON_GetObjectItem(data_item,"rwflag")->valuedouble=2;
// cJSON_GetObjectItem(data_item,"mtype")->valuedouble=2;
// cJSON_GetObjectItem(data_item,"radio")->valuedouble=2;
// cJSON_GetObjectItem(data_item,"base")->valuedouble=2;
// cJSON_GetObjectItem(data_item,"topic")->valuestring="修改完成";
cJSON * offset=cJSON_GetObjectItem(data_item,"offset");
cJSON_SetIntValue(offset,"123");
char *json_str=cJSON_Print(device_before_edit);
printf("%s\n",json_str);
//下面把它输出到一个新的文件中
write_json(json_str);
}
}
}
}
}
int main() {
printf("Content type: text/html\n\n");
char *content;
content = read_json("../json/device.json");
//printf("content:%s",content);
edit_json(content);
return 0;
}
删除:
#include "cgic.h"
#include "cJSON.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "pub_function.h"
void write_json(char *json_str){
FILE *fp;
fp=fopen("../json/new_devices_del.json","w");
fputs(json_str,fp);
fclose(fp);
}
void del_json(char *content){
cJSON *device_del=cJSON_Parse(content);
//下面删除device.json中device_name字段
cJSON_DeleteItemFromObject(device_del,"device_name");
char *json_str;
//下面删除commend[0]下的data[0]
cJSON *commands=cJSON_GetObjectItem(device_del,"commands");
cJSON *command=cJSON_GetArrayItem(commands,0);
cJSON *datas=cJSON_GetObjectItem(command,"data");
cJSON_DeleteItemFromArray(datas,1);
//下面删除perset中的第一个数字
cJSON *preset=cJSON_GetObjectItem(command,"preset");
cJSON_DeleteItemFromArray(preset,2);
json_str=cJSON_Print(device_del);
printf("%s",json_str);
write_json(json_str);
}
int main() {
printf("Content type: text/html\n\n");
char *content;
content = read_json("../json/device.json");
del_json(content);
return 0;
}
编译实例:
gcc read_json.c pub_function.c cgic.c cJSON.c -o read_json.cgi