您现在的位置是:首页 >技术教程 >智能家居项目整合(网络控制线程、语音控制线程,火灾报警线程)网站首页技术教程
智能家居项目整合(网络控制线程、语音控制线程,火灾报警线程)
简介智能家居项目整合(网络控制线程、语音控制线程,火灾报警线程)
mainPro.c(主函数)
#include <stdio.h>
#include <string.h>
#include "contrlEquipments.h"
#include "inputCommand.h"
#include <pthread.h>
#include <unistd.h>
struct Equipment *findEquipByName(char *name,struct Equipment *phead); //一些函数声明
struct Command *findCommandByName(char *name,struct Command *phead);
void *voiceControlThread(void *data);
void *socketControlThread(void *data);
void *socketReadThread(void *data);
void *fireAlarmThread(void *data);
void *airAlarmThread(void *data);
struct Equipment *equiphead = NULL; //设备工厂链表头节点
struct Command *cmdhead = NULL; //指令控制工厂链表节点头
struct Command *socketHandler = NULL; //“网络控制线程”执行的函数使用到的全局变量
int main()
{
if(wiringPiSetup() == -1){ //使用wiringPi库需要初始化
printf("wiringPiSetup failed!
");
return -1;
}
pthread_t voiceControl_thread;
pthread_t socketControl_thread;
pthread_t fireAlarm_thread;
pthread_t airAlarm_thread;
//1、设备工厂初始化
equiphead = addBathroomLightToEquipmentLink(equiphead); //各设备加入设备工厂
equiphead = addSecondfloorLightToEquipmentLink(equiphead);
equiphead = addLivingroomLightToEquipmentLink(equiphead);
equiphead = addRestaurantLightToEquipmentLink(equiphead);
equiphead = addFireDetectionToEquipmentLink(equiphead);
equiphead = addBuzzerToEquipmentLink(equiphead);
equiphead = addlockToEquipmentLink(equiphead);
equiphead = addAirDetectionToEquipmentLink(equiphead); //煤气传感器(对象)加入设备链表函数
struct Equipment *tmpequiphead = equiphead;
while(tmpequiphead != NULL){ //设备工厂所有设备初始化
tmpequiphead->Init(tmpequiphead->pinNum);
tmpequiphead = tmpequiphead->next;
}
//2、指令工厂初始化
cmdhead = addVoiceControlToCommandLink(cmdhead); //各指令控制加入指令控制工厂
cmdhead = addSocketControlToCommandLink(cmdhead);
//3、线程池建立
//3.1 语音线程
//int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void *), void *restrict arg);
pthread_create(&voiceControl_thread,NULL,voiceControlThread,NULL); //创建线程:语音控制
//3.2 网络线程
pthread_create(&socketControl_thread,NULL,socketControlThread,NULL); //创建线程:网络控制
//3.3 火灾线程
pthread_create(&fireAlarm_thread,NULL,fireAlarmThread,NULL); //创建线程:火灾报警系统
//3.4 摄像头线程
//3.5煤气线程
pthread_create(&airAlarm_thread,NULL,airAlarmThread,NULL); //创建线程:火灾报警系统
pthread_join(voiceControl_thread, NULL); //主函数等待线程退出
pthread_join(socketControl_thread, NULL); //主函数等待线程退出
pthread_join(fireAlarm_thread, NULL); //主函数等待线程退出
pthread_join(airAlarm_thread, NULL); //主函数等待线程退出
return 0;
}
void *voiceControlThread(void *data) //“语音控制线程”执行的函数
{
int nread;
char *temName = NULL;
struct Command *voiceHandler = NULL;
struct Equipment *linkHandler;
voiceHandler = findCommandByName("voiceControl",cmdhead); //寻找“语音控制”所在节点,返回给voiceHandler
if(voiceHandler == NULL){
printf("find voiceHandler error
");
pthread_exit(NULL);
}
if(voiceHandler->Init(voiceHandler) < 0){ //“语音控制”功能初始化
printf("voiceControl init error
");
pthread_exit(NULL);
}
while(1){
nread = voiceHandler->getCommand(voiceHandler); //获取指令
if(nread == 0){ //没有获取到指令
printf("No voiceCommand received
");
}else{ //获取到指令
printf("Get voice command:%s
",voiceHandler->command);
//以下为根据不用指令执行相应操作
//语音模块串口传出来的后面带
,不加对比不出来
if(strcmp("ycdo
",voiceHandler->command) == 0){
linkHandler = findEquipByName("bathroomLight",equiphead);//改为泳池灯
linkHandler->open(linkHandler->pinNum);
//printf("已打开浴室灯
");
printf("已打开泳池灯
");
}
if(strcmp("ycdc
",voiceHandler->command) == 0){
linkHandler = findEquipByName("bathroomLight",equiphead);
linkHandler->close(linkHandler->pinNum);
//printf("已关闭浴室灯
");
printf("已关闭泳池灯
");
}
if(strcmp("eldo
",voiceHandler->command) == 0){
linkHandler = findEquipByName("secondfloorLight",equiphead);
linkHandler->open(linkHandler->pinNum);
}
if(strcmp("eldc
",voiceHandler->command) == 0){
linkHandler = findEquipByName("secondfloorLight",equiphead);
linkHandler->close(linkHandler->pinNum);
}
if(strcmp("ktdo
",voiceHandler->command) == 0){
linkHandler = findEquipByName("livingroomLight",equiphead);
linkHandler->open(linkHandler->pinNum);
}
if(strcmp("ktdc
",voiceHandler->command) == 0){
linkHandler = findEquipByName("livingroomLight",equiphead);
linkHandler->close(linkHandler->pinNum);
}
if(strcmp("wsdo
",voiceHandler->command) == 0){
linkHandler = findEquipByName("restaurantLight",equiphead);//改为卧室灯
linkHandler->open(linkHandler->pinNum);
}
if(strcmp("wsdc
",voiceHandler->command) == 0){
linkHandler = findEquipByName("restaurantLight",equiphead);
linkHandler->close(linkHandler->pinNum);
}
if(strcmp("allo
",voiceHandler->command) == 0){
linkHandler = findEquipByName("bathroomLight",equiphead);//灯全部打开
linkHandler->open(linkHandler->pinNum);
linkHandler = findEquipByName("secondfloorLight",equiphead);
linkHandler->open(linkHandler->pinNum);
linkHandler = findEquipByName("livingroomLight",equiphead);
linkHandler->open(linkHandler->pinNum);
linkHandler = findEquipByName("restaurantLight",equiphead);
linkHandler->open(linkHandler->pinNum);
}
if(strcmp("allc
",voiceHandler->command) == 0){
linkHandler = findEquipByName("bathroomLight",equiphead);//灯全部关闭
linkHandler->close(linkHandler->pinNum);
linkHandler = findEquipByName("secondfloorLight",equiphead);
linkHandler->close(linkHandler->pinNum);
linkHandler = findEquipByName("livingroomLight",equiphead);
linkHandler->close(linkHandler->pinNum);
linkHandler = findEquipByName("restaurantLight",equiphead);
linkHandler->close(linkHandler->pinNum);
}
if(strcmp("dooro
",voiceHandler->command) == 0)
{
system("./smartHomeFaceRec");
}
memset(voiceHandler->command,'