基于ARM的智能灯光控制系统(8)设备添加

设备添加页面

网页显示头文件(html.h)

#include <stdio.h>


#define ERR_SHM 1
#define ERR_MSG 2
#define ERR_REG 3

void html_head()
{
    printf("Content-type:text/html\r\n\r\n");
    printf("<html>");
    printf("<head><title>SL1200智能灯光控制系统</title>");
    printf("<link href=\"../empty_files/bootstrap.css\" rel=\"stylesheet\">");
    printf("<link href=\"../empty_files/font-awesome.css\" rel=\"stylesheet\">");
    printf("<link href=\"../empty_files/custom-styles.css\" rel=\"stylesheet\">");
}

void html_refresh(char* second, char* url)
{
     printf("<META HTTP-EQUIV=\"REFRESH\" CONTENT=\"%s;URL=%s\">",second, url);
}

void html_title()
{   
    printf("</head><body><div id=\"wrapper\">");
    ...
}

void html_nav()
{
    printf("<!--/. NAV TOP  -->");
    ...                                     
}

void html_table_title(char* title,char * title1,char *title2)
{
    ...
}

void html_table_head(int len, char argv[][10],char* title)
{
    ...
}

void html_table_end()
{
    printf("</tbody></table></div></div></div><!--End Advanced Tables --></div></div>");
}

void html_end()
{  
    ...
    printf("</body></html>");
}


void html_return_info(char* info)
{
    printf("<div class=\"row\"><div class=\"col-md-12\">");
    printf("<div class=\"panel panel-default\"><div class=\"panel-heading\">");
    printf("设置结果</div><div class=\"panel-body\"> <div class=\"alert alert-success\">");
    printf("<strong>%s</strong>", info);
    printf("</div></div></div></div></div>");
}

void html_return_show(int ret)
{
    if(ret == 0){
        html_return_info("设置完成,返回界面.");
    }else{
        switch(ret){
            case ERR_SHM:
                html_return_info("共享内存错误.");
                break;
            case ERR_MSG:
                html_return_info("区域记录己满.");
                break;
            case ERR_REG:
                html_return_info("区域记录己满.");
                break;
            default:
                html_return_info("设置失败,未知错误.");
                break;
        }   
    }
}

进程通信头文件(ipc.h)

#ifndef __IPC_H_
#define __IPC_H_

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/shm.h>


#define CMD_GET 1
#define CMD_SET 2

int get_msgid(void)
{
    int id = -1;
    id = msgget((key_t)1234,0666|IPC_CREAT);
    return id;
}

void* set_web_shm(void)
{
    int shmid;
    void* shmaddr=(void*)0;

    if((shmid=shmget((key_t)1122,sizeof(struct sys_all),0666|IPC_CREAT))<0){
        return NULL;
    }else{  
        if((shmaddr=shmat(shmid,(void*)0,0))==(char *)-1){
            return NULL;
        }
    }   

    return shmaddr;
}

int msg_send(int msgid,int cmd_da)
{
    struct st_msg_req cmd;
    cmd.index = WEB_UPDATE_SMG_INDEX;
    cmd.req = cmd_da;
    if(msgsnd(msgid,(void*)&cmd,1,0)==-1)
        return -1;
    return 0;
}

#endif

设备添加页面程序(dev_add.c)

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#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("<tr ><td><input type=\"text\" name=\"name_%s\" value=\"%s\"> </td>",type_id,name);  
    //显示id                                          
    printf("<td><input type=\"text\" name=\"%s\" value=\"SL_M_%s\"></td><td>",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("</td><td class=\"center\"><input type=\"radio\" name=\"join_sta%s\" value=\"1\"  checked=\"checked\">连通<input type=\"radio\" name=\"join_sta%s\" value=\"0\">断开</td>",type_id,type_id);
    }else{
        printf("</td><td class=\"center\"><input type=\"radio\" name=\"join_sta%s\" value=\"1\" >连通<input type=\"radio\" name=\"join_sta%s\" value=\"0\" checked=\"checked\">断开</td>",type_id,type_id);
    }                                           
    printf("<td class=\"center\"><button type=\"submit\" >设置</button>   </td></tr>");

}

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

    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;
    }

    printf("<form method=\"get\" action=\"/cgi-bin/dev_add_post.cgi\">");

    strcpy(item_name[0],"设备名称");
    strcpy(item_name[1],"节点信息");    
    strcpy(item_name[2],"设备类型");
    strcpy(item_name[3],"连接状态");
    strcpy(item_name[4],"提交操作");


    html_table_head(5,item_name,"设备列表");
    for(i=0;i<shm_dev->count_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();
    printf("</form>");
    html_end();
    return 0;
}

页面处理程序(dev_add_post.c)


#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#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;i<shm_dev->count_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';

                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;
}

Copyright © 2016 www.91arm.com 【91创客学堂】