目录
问题2:在前几篇文章中,在open\close操作前都有Init操作
问题3: int (*changeStatus)(int status),记住设备的状态
一、主函数
mianPro.c
#include <pthread.h>
#include "controlDevice.h"
#include "inputCommand.h"
struct InputCommand *pcommandHead = NULL; //定义指令工厂初始链表头
struct Devices *pdeviceHead = NULL; //定义设备工厂初始链表头
struct InputCommand *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 InputCommand* findCommandByName(char *name, struct InputCommand *phead)
{
struct InputCommand *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 InputCommand* 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 *voiceControlThread(void *data) //“语音控制线程”执行函数
{
int nread;
struct InputCommand *voiceHandler = NULL;
voiceHandler = findCommandByName("voice", pcommandHead);
if(voiceHandler == NULL){
printf("find voiceHandler error\n");
pthread_exit(NULL);
}else{
if(voiceHandler->Init(voiceHandler) < 0){ //“语音控制”功能初始化
printf("voiceControl init error\n");
pthread_exit(NULL);
}else{
printf("voic