基於ARM的智能燈光控制系統(8)設備添加

基於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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章