基于ARM的智能灯光控制系统总结(15-网页CGI进程之设备添加)

在欢迎界面导航栏中点击设备设置->设备添加,则打开dev_add.cgi程序,

一、该进程首先通过进程间通信函数向主控进程发送查询消息,主控进程将主数据结构内存中的数据加载至共享内存,cgi进程然后将共享内存的数据显示到html中。

二、在html中点击表单的submit键时,cgi程序将表单中的数据提取出,修改共享内存数据,然后向主控进程发送更新消息,主控进程则将共享内存中的数据更新至主数据结构内存。

基于ARM的智能灯光控制系统总结(15-网页CGI进程之设备添加)_第1张图片

dev_add.c如下:

#include 
#include 
#include 
#include 

#include "html.h"
#include "config.h"
#include "ipc.h"

void table_tr(char * name,char type,int checked)
{
	char type_id[2]="0";
	type_id[0]=type+'0';

	printf("");
	printf("",type_id,name);
	printf("",type_id,type_id);
	switch(type){
		case 1:
			printf("主控制器");
			break;
		case 2:
			printf("分控制器");
			break;
		case 3:
			printf("光线感应");
			break;
		case 4:
			printf("人体感应");
			break;
		case 5:
			printf("声音感应");
			break;
		case 6:			
		case 7:
			printf("灯光设备");
			break;
		case 8:
			printf("网络设备");
			break;
		default:
			break;
	}

	if(checked==1){
		printf("连通断开",type_id,type_id);
	}else{
		printf("连通断开",type_id,type_id);
	}

	printf("");
}

int main(int argc,char * argv[])
{
	int ret=0;
	int i,msgid;
	struct sys_all * shm_dev;
	char item_name[5][16];

	if((msgid=get_msgid())<0){
		ret=ERR_MSG;
	}

	if(msg_send(msgid,CMD_GET)==0){
		if((shm_dev=(struct sys_all *)set_web_shm())==NULL){
		ret=ERR_SHM;
		}
	}

	html_head();
	html_title();
	html_nav();
	html_table_title("设备添加","设备设置","设备添加");

	if(ret!=0){
		html_return_show(ret);
		html_end();
		return 0;
	}

	strcpy(item_name[0],"设备名称");
	strcpy(item_name[1],"节点信息");
	strcpy(item_name[2],"设备类型");
	strcpy(item_name[3],"连接状态");
	strcpy(item_name[4],"提交操作");
	
	printf("
"); html_table_head(5,item_name,"设备列表"); for(i=0;icount_dev;i++){ table_tr(shm_dev->sys_dev[i].name,shm_dev->sys_dev[i].node.type,shm_dev->sys_dev[i].join_sta); } html_table_end(); html_end(); return 0; }

在html的表单中修改属性后submit按键后,cgi程序向环境变量提交了键值对字符串,设备添加页面处理程序(dev_add_post.c)提取出各键对应的值,更新共享内存,然后向主控进程发送更新信息

#include 
#include 
#include 
#include 
#include "getvalue.h"
#include "html.h"
#include "config.h"
#include "ipc.h"

int main(int argc,char * argv[])
{
	int ret=0;
	char * val=NULL;
	char val_name[16];
	char type_id[2]="0";
	int i,msgid;
	struct sys_all * shm_dev;

	set_env(getenv("REQUEST_METHOD"),getenv("CONTENT_LENGTH"),getenv("QUERY_STRING"));
	html_head();
	html_refresh("3","/cgi-bin/dev_add.cgi");
	html_title();
	html_nav();
	html_table_title("设备添加","设备设置","设备添加");

	if((shm_dev=(struct sys_all *)set_web_shm())==NULL){
		ret=ERR_SHM;
	}else{
		for(i=0;icount_dev;i++){
			//根据共享内存实时数据,动态读取环境变量中变量名为join_sta+id的值 
			type_id[0]=shm_dev->sys_dev[i].node.type +'0';
			strcpy(val_name,"join_sta");
			strcat(val_name,type_id);//组装键
			val=get_value(val_name);//提取键对应的值
			shm_dev->sys_dev[i].join_sta=val[0]-'0';//字符转整型数据

			//根据共享内存实时数据,动态读取环境变量中变量名为name_+id的值
			strcpy(val_name,"name_");
			strcat(val_name,type_id);
			val=get_value(val_name);
			strcpy(shm_dev->sys_dev[i].name,val);
		}
	}

	if(ret==0){
		if((msgid=get_msgid())<0)ret=ERR_MSG;
		if((msg_send(msgid,CMD_SET))<0)ret=ERR_MSG;//发送更新消息给主控进程
	}

	html_return_show(ret);
	html_end();
	return 0;
}

你可能感兴趣的:(基于ARM的智能灯光控制系统总结(15-网页CGI进程之设备添加))