智能家居 (5) ——智能家居项目整合(语音控制线程,网络控制线程、烟雾报警线程)

本文介绍了智能家居项目整合过程,包括语音控制线程、网络控制线程和烟雾报警线程的实现。在三线程运行中遇到的问题,如线程同步、初始化操作和设备状态管理进行了探讨,并给出了相应的解决方案。
摘要由CSDN通过智能技术生成

 

目录

一、主函数

mianPro.c

二、指令工厂

voiceControl.c

socketControl.c

inputCommand.h

三、设备工厂

smokeAlarm.c

buzzer.c

livingroomLight.c

restaurantLight.c

bedroomLight.c

bathroomLight.c

controlDevice.h

四、总结

问题1:三线程同时运行,有时候无法正常进行开关灯操作。

问题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
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值