智能家居 —— 网络服务器线程控制

mainPro.c

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "controlDevice.h"
#include "inputCommand.h"

#include <pthread.h>

struct Devices *pdeviceHead  = NULL;
struct InputCommander *socketHandler = NULL;	



struct Devices* findDeviceByName(char *name, struct Devices *phead){   
	struct Devices *tmp =phead;  
	if(phead == NULL){     
		return NULL;    
	}else{     
		while(tmp != NULL){    
			if(strcmp(tmp->deviceName,name)==0){   
				return tmp;        
				}           
			tmp = tmp->next;    
		}        
		return NULL; 
	}
}

struct InputCommander* findCommandByName(char *name, struct InputCommander *phead){   
	struct InputCommander *tmp =phead;  
	if(phead == NULL){     
		return NULL;    
	}else{     
		while(tmp != NULL){    
			if(strcmp(tmp->commandName,name)==0){   
				return tmp;        
				}            
			tmp = tmp->next;    
		}        
		return NULL; 
	}
}



void Command(struct InputCommander *CmdHandler){

	struct Devices *tmp =NULL;

	
	if(strcmp("CSHALL",CmdHandler->command )==0){
		tmp = findDeviceByName("smokeAlarm",pdeviceHead);
		if(tmp!=NULL)tmp->Init(tmp->pinNum);
		tmp = findDeviceByName("buzzer",pdeviceHead);
		if(tmp!=NULL)tmp->Init(tmp->pinNum);
		tmp = findDeviceByName("livingroomLight",pdeviceHead);
		if(tmp!=NULL)tmp->Init(tmp->pinNum);
		tmp = findDeviceByName("restaurantLight",pdeviceHead);
		if(tmp!=NULL)tmp->Init(tmp->pinNum);
		tmp = findDeviceByName("bedroomLight",pdeviceHead);
		if(tmp!=NULL)tmp->Init(tmp->pinNum);
		tmp = findDeviceByName("bathroomLight",pdeviceHead);
		if(tmp!=NULL)tmp->Init(tmp->pinNum);
		printf("设备已全部初始化\n");
	}
	if(strcmp("OL1",CmdHandler->command) == 0){		
		tmp = findDeviceByName("livingroomLight",pdeviceHead);  
		if(tmp != NULL){         
			tmp->open(tmp->pinNum);      
			printf("已打开客厅灯\n");     
		}	
	}   
	if(strcmp("CL1",CmdHandler->command) == 0){	
		tmp = findDeviceByName("livingroomLight",pdeviceHead);   
		if(tmp != NULL){      
			tmp->close(tmp->pinNum);      
			printf("已关闭客厅灯\n");   
		}	
	}    
	if(strcmp("OL2",CmdHandler->command) == 0){	
		tmp = findDeviceByName("restaurantLight",pdeviceHead);   
		if(tmp != NULL){        
			tmp->open(tmp->pinNum);          
			printf("已打开餐厅灯\n");      
		}
	}    
	if(strcmp("CL2",CmdHandler->command) == 0){	
		tmp = findDeviceByName("restaurantLight",pdeviceHead);   
		if(tmp != NULL){          
			tmp->close(tmp->pinNum);        
			printf("已关闭餐厅灯\n");   
		}	
	}   
	if(strcmp("OL3",CmdHandler->command) == 0){		
		tmp = findDeviceByName("bedroomLight",pdeviceHead);  
		if(tmp != NULL){         
			tmp->open(tmp->pinNum);       
			printf("已打开卧室灯\n");      
		}	
	}    
	if(strcmp("CL3",CmdHandler->command) == 0){	
		tmp = findDeviceByName("bedroomLight",pdeviceHead);   
		if(tmp != NULL){      
			tmp->close(tmp->pinNum);     
			printf("已关闭卧室灯\n");    
		}
	}   
	if(strcmp("OL4",CmdHandler->command) == 0){		
		tmp = findDeviceByName("bathroomLight",pdeviceHead);  
		if(tmp != NULL){        
			tmp->open(tmp->pinNum);     
			printf("已打开浴室灯\n");      
		}	
	}   
	if(strcmp("CL4",CmdHandler->command) == 0){		
		tmp = findDeviceByName("bathroomLight",pdeviceHead);  
		if(tmp != NULL){      
			tmp->close(tmp->pinNum);     
			printf("已关闭浴室灯\n");    
		}	
	}
	if(strcmp("OLALL",CmdHandler->command) == 0){	
		tmp = findDeviceByName("livingroomLight",pdeviceHead);		
		if(tmp != NULL)  tmp->open(tmp->pinNum);	
		tmp = findDeviceByName("restaurantLight",pdeviceHead);	
		if(tmp != NULL)  tmp->open(tmp->pinNum);		
		tmp = findDeviceByName("bedroomLight",pdeviceHead);	
		if(tmp != NULL)  tmp->open(tmp->pinNum);		
		tmp = findDeviceByName("bathroomLight",pdeviceHead);	
		if(tmp != NULL)  tmp->open(tmp->pinNum);       
		printf("已打开所有灯\n");    
	}    
	if(strcmp("CLALL",CmdHandler->command) == 0){	
		tmp = findDeviceByName("livingroomLight",pdeviceHead);		
		if(tmp != NULL)  tmp->close(tmp->pinNum);	
		tmp = findDeviceByName("restaurantLight",pdeviceHead);	
		if(tmp != NULL)  tmp->close(tmp->pinNum);	
		tmp = findDeviceByName("bedroomLight",pdeviceHead);	
		if(tmp != NULL)  tmp->close(tmp->pinNum);		
		tmp = findDeviceByName("bathroomLight",pdeviceHead);	
		if(tmp != NULL)  tmp->close(tmp->pinNum);      
		printf("已关闭所有灯\n");	
	}




}




void *socketReadThread(void *arg){
	int n_read;
	printf("Connect success\n");
	while(1){
		memset(socketHandler->command,'\0',sizeof(socketHandler->command));
		n_read = read(socketHandler->fd,socketHandler->command,sizeof(socketHandler->command));
		if(n_read == -1){

			perror("read:");
		}else{
				
			printf("Get SocketCommand-->%s\n",socketHandler->command);
			
			//Command(socketHandler);
			
		}

	}
	
	


	

}
void *socketControlThread(void *arg){
	int c_fd;  

	
	int clen = sizeof(struct sockaddr_in);
	struct sockaddr_in c_addr;
	memset(&c_addr,0,sizeof(struct sockaddr_in));
	pthread_t socketRead_thread;
	socketHandler =  findCommandByName("socket", pcommandHead);

	if(socketHandler == NULL){
		printf("find socketHandler error\n");
        pthread_exit(NULL);

	}else{
		if(socketHandler->Init(socketHandler)<0){
			printf("socketControl init error\n");
        	pthread_exit(NULL);
		}else{
			printf("socketControl init success\n");
			
		}
		while(1){
			c_fd = accept(socketHandler->s_fd,(struct sockaddr*)&c_addr,&clen);
			socketHandler->fd = c_fd;
			pthread_create(&socketRead_thread, NULL, socketReadThread, NULL); 
		}
		
	}
	 	

}





 
int main()
{
    
    if (wiringPiSetup () == -1) { 
        fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ; 
        return 1 ; 
    }

 	//线程的ID
	pthread_t socket_thread;
	

	
    //设备工厂初始化  	
    pdeviceHead = addBathroomLightToDeviceLink(pdeviceHead);            
    pdeviceHead = addBedroomLightToDeviceLink(pdeviceHead);
    pdeviceHead = addRestaurantLightToDeviceLink(pdeviceHead);
    pdeviceHead = addLivingroomLightToDeviceLink(pdeviceHead);
    pdeviceHead = addSmokeAlarmToDeviceLink(pdeviceHead);
    pdeviceHead = addBuzzerToDeviceLink(pdeviceHead);
 	
	//指令工厂初始化
    pcommandHead  = addVoiceControlToInputCommandLink(pcommandHead);
	pcommandHead  = addSocketControlToInputCommandLink(pcommandHead);

	
	//创建socket控制线程
	pthread_create(&socket_thread, NULL, socketControlThread, NULL); 
	

	
	pthread_join(socket_thread, NULL);		
		

 
    return 0;
}

inputCommand.h

#include <wiringPi.h>					
#include <stdio.h>
#include <stdlib.h>
#include <wiringSerial.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>


struct InputCommander{

	char commandName[128];
	char deviceName[128];  
	char command[128];
	int fd;       
	int s_fd;
	char port[12];
	char ipAdress[32];
	int (*Init)(struct InputCommander *voiceControl); 
	int (*getCommand)(struct InputCommander *voiceControl);
	char log[1024];

	
	struct InputCommander *next;

};



struct InputCommander* addSocketControlToInputCommandLink(struct InputCommander *phead)	;

socketControl.c

#include "inputCommand.h"			//自定义设备类的文件
 

int sockInit(struct InputCommander *socketControl){

	int s_fd;                                      
	struct sockaddr_in s_addr;
	memset(&s_addr,0,sizeof(struct sockaddr_in));  
	
	s_fd=socket(AF_INET,SOCK_STREAM,0);            
	if(s_fd == -1){                                 
		perror("socket:");		
		exit(-1);	
	}     
	s_addr.sin_family = AF_INET;                      
	s_addr.sin_port = htons(atoi(socketControl->port)); 
	inet_aton(socketControl->ipAdress,&s_addr.sin_addr);   
	
	bind(s_fd,(struct sockaddr *)&s_addr,sizeof(struct sockaddr_in));

	listen(s_fd,10);
    printf("socket Server listening ...\n");
	
	socketControl->s_fd = s_fd;
	return s_fd;
}
 
struct InputCommander socketControl   = {						
	.commandName = "socket",						
    .port = "8080",
    .ipAdress = "192.168.1.220",
	.command={'\0'},
	.Init=sockInit,
	.log = {'\0'},
	.next = NULL
};
 
 
struct InputCommander* addSocketControlToInputCommandLink(struct InputCommander *phead)		
{
	if(phead == NULL){
		return &socketControl ;
	}else{
		socketControl.next = phead;
		phead = &socketControl ;
		return phead;
	}
}

注意:

  • 在该项目中,定义一个指向socket文件的结构体指针(全局变量),目的是为了传递accept返回的s_fd文件句柄,同时为了存储用户输入的命令在command里面,便于在比较函数中进行使用

测试结果

  • 使用网络调试助手向socket开启的服务器发起请求,socket成功接收到客户端发送的信息并通过Command函数返回该指令的中文意思(目前乱码)
    在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值