#include "controlDevices.h"
#include "inputCmd.h"
struct Devices *pDevicesHead = NULL;
struct InputCmd *pCmdHead = NULL;
struct Devices *Devicetmp = NULL;
struct InputCmd *Cmdtmp = NULL;
struct InputCmd *serverHandle = NULL;
int c_fd;
pthread_t sendData_pid;
int firetype = 0; //火灾发生标志,1为发生
int shocktype = 0; //地震发生标志,1为发生
char sensorToStr[128]; //空气的温湿度数据存储地址
char sensorToStr1[128];
char sensorToStr2[128];
//遍历产品工厂的设备,找到对应名字的设备
struct Devices* findDeviceByName(char *name,struct Devices *phead) {
struct Devices *tmp = phead;
if (phead == NULL) {
return NULL;
} else {
while(tmp != NULL) {
if (strcmp(tmp->devicesName, name) == 0) {
return tmp;
}
tmp = tmp->next;
}
return NULL;
}
}
//遍历指令工厂的设备,找到对应名字的设备
struct InputCmd* findCmdByName(char *name,struct InputCmd *phead)
{
struct InputCmd *tmp = phead;
if(phead == NULL){
return NULL;
}else{
while(tmp != NULL){
if(strcmp(tmp->cmdName,name) == 0){
return tmp;
}
tmp = tmp->next;
}
return NULL;
}
}
/* 发送数据线程执行函数 */
void *sendData_thread(void *data){
while(1){
write(c_fd,"1",1); //随便发个数据与客户端进行响应
delay(1000);
write(c_fd,sensorToStr,strlen(sensorToStr));
//write(c_fd,sensorHStr,strlen(sensorHStr));
}
}
// 语音串口接收
void *voice_thread(void *data)
{
struct InputCmd *voiceHandle = NULL;
struct Devices *devicesHandle = NULL;
char behavior[32];
char room[32];
char *tmp;
int n_read = 0;
voiceHandle = findCmdByName("voice",pCmdHead); //找到指令工厂的语音控制设备
if(voiceHandle == NULL){
printf("find voiceHandle error\n");
pthread_exit(NULL);
}else{
if(voiceHandle->Init(voiceHandle,NULL,NULL) < 0){
printf("voice Init error\n");
pthread_exit(NULL);
}
while(1){
n_read = voiceHandle->getCmd(voiceHandle);
if(n_read == 0){
printf("no data from voice\n");
}else{
/* 获取命令和设备名称 */
memset(behavior,'\0',sizeof(behavior));
tmp = strtok(voiceHandle->cmd," ");
strcat(behavior,tmp); //命令:open or close
printf("behavior:%s\n",behavior);
memset(room,'\0',sizeof(room));
tmp = strtok(NULL," ");
strcat(room,tmp); //产品工厂的设备名称
printf("room:%s\n",room);
devicesHandle = findDeviceByName(room,pDevicesHead); //找到产品工厂的设备
if(devicesHandle != NULL && strcmp(behavior,"open") == 0){
devicesHandle->devicesInit(devicesHandle->pinNum);
devicesHandle->open(devicesHandle->pinNum);
printf("open success\n");
}
else if(devicesHandle != NULL && strcmp(behavior,"close") == 0){
devicesHandle->devicesInit(devicesHandle->pinNum);
devicesHandle->close(devicesHandle->pinNum);
}
else {
printf("error\n");
}
}
}
}
}
// 串口发送数据
void *voiceToUart_thread(void *data)
{
struct InputCmd *voiceHandle = NULL;
struct Devices *devicesHandle = NULL;
voiceHandle = findCmdByName("voice",pCmdHead); //找到指令工厂的语音控制设备
if(voiceHandle == NULL){
printf("find voiceHandle error\n");
pthread_exit(NULL);
}else{
if(voiceHandle->Init(voiceHandle,NULL,NULL) < 0){
printf("receive msg Init error\n");
pthread_exit(NULL);
}
while(1){
printf("receive msg success\n");
write(voiceHandle->fd,sensorToStr1,strlen(sensorToStr1));
serialPrintf(voiceHandle->fd,"\r\n");
if(firetype == 1 || shocktype == 1) {
serialPrintf(voiceHandle->fd,"buzzer_On\r\n");
} else if (firetype == 0 || shocktype == 0) {
serialPrintf(voiceHandle->fd,"buzzer_Off\r\n");
}
delay(500);
// printf("RH:%d.%d\n", (sensorHandle->databuf >> 24) & 0xff, (sensorHandle->databuf >> 16) & 0xff);
// printf("TMP:%d.%d\n", (sensorHandle->databuf >> 8) & 0xff, sensorHandle->databuf & 0xff);
}
}
}
/* 客户端连接后处理线程执行函数 */
void *connect_thread(void *data)
{
int n_read;
struct Devices *devicesHandle = NULL;
char behavior[32];
char room[32];
char *tmp;
pthread_create(&sendData_pid,NULL,sendData_thread,NULL); //建立发送数据线程
n_read = read(c_fd,serverHandle->cmd,sizeof(serverHandle->cmd)); //读取客户端的数据
if(n_read == -1){
perror("read");
}else if(n_read > 0){
printf("cmd: %s\n",serverHandle->cmd);
/* 获取命令和设备名称 */
memset(behavior,'\0',sizeof(behavior));
tmp = strtok(serverHandle->cmd," "); //命令:open or close
strcat(behavior,tmp);
printf("behavior:%s\n",behavior);
memset(room,'\0',sizeof(room));
tmp = strtok(NULL," ");
strcat(room,tmp); //产品工厂的设备名称
printf("room:%s\n",room);
devicesHandle = findDeviceByName(room,pDevicesHead); //找到产品工厂的设备
if(devicesHandle != NULL && strcmp(behavior,"open") == 0){
devicesHandle->devicesInit(devicesHandle->pinNum);
devicesHandle->open(devicesHandle->pinNum);
printf("open success\n");
}
else if(devicesHandle != NULL && strcmp(behavior,"close") == 0){
devicesHandle->devicesInit(devicesHandle->pinNum);
devicesHandle->close(devicesHandle->pinNum);
printf("close success\n");
}
else {
printf("error\n");
}
}else{
printf("client is quit\n");
}
}
/* sever控制线程执行函数 */
void *server_thread(void *data)
{
int clen = sizeof(struct sockaddr_in);
struct sockaddr_in c_addr;
memset(&c_addr,0,sizeof(struct sockaddr_in));
pthread_t connect_pid;
serverHandle = findCmdByName("server",pCmdHead); //找到指令工厂的server控制设备
if(serverHandle == NULL){
printf("find serverHandle error\n");
pthread_exit(NULL);
}else{
if(serverHandle->Init(serverHandle,NULL,NULL) < 0){
printf("server Init error\n");
pthread_exit(NULL);
}
while(1){
c_fd = accept(serverHandle->sfd,(struct sockaddr *)&c_addr,&clen);
printf("have people\n");
pthread_create(&connect_pid,NULL,connect_thread,NULL); //建立客户端连接后处理线程
}
}
}
/* 火灾监测线程执行函数 */
void *fire_thread(void *data)
{
struct Devices *fireHandle = NULL;
struct Devices *devicesHandle = NULL;
int fireStatus = 1;
fireHandle = findDeviceByName("fire",pDevicesHead); //找到产品工厂的火灾设备
if(fireHandle == NULL){
printf("find fireHandle error\n");
pthread_exit(NULL);
}else{
fireHandle->devicesInit(fireHandle->pinNum);
while(1){
fireStatus = fireHandle->readStatus(fireHandle->pinNum);
//printf("fireStatus:%d\n",fireStatus);
delay(125);
if((fireStatus) == 0){
firetype = 1;
/* 开启警报,调试时嫌吵我就把他关了
devicesHandle = findDeviceByName("buzzer",pDevicesHead); //找到指令工厂的报警器设备
if(devicesHandle != NULL){
devicesHandle->open(devicesHandle->pinNum);
}
*/
}else if(fireStatus == 1){
firetype = 0;
/*
devicesHandle = findDeviceByName("buzzer",pDevicesHead); //找到指令工厂的报警器设备
if(devicesHandle != NULL){
devicesHandle->close(devicesHandle->pinNum);
}
*/
}
}
}
}
/* 地震监测线程执行函数 */
void *shock_thread(void *data)
{
struct Devices *shockHandle = NULL;
struct Devices *devicesHandle = NULL;
int shockStatus = 1;
shockHandle = findDeviceByName("shock",pDevicesHead); //找到产品工厂的地震监测设备
if(shockHandle == NULL){
printf("find shockHandle error\n");
pthread_exit(NULL);
}else{
shockHandle->devicesInit(shockHandle->pinNum);
while(1){
shockStatus = shockHandle->readStatus(shockHandle->pinNum);
//printf("sh
没有合适的资源?快使用搜索试试~ 我知道了~
智能家居(smarthome)调试代码(未完全)
共65个文件
c:28个
sisc:17个
h:5个
需积分: 5 0 下载量 116 浏览量
2024-05-12
20:00:48
上传
评论
收藏 76KB ZIP 举报
温馨提示
智能家居(smarthome)调试代码(未完全)
资源推荐
资源详情
资源评论
收起资源包目录
smarthome01.zip (65个子文件)
smarthome01
bathroomLight.c 790B
camera.c 0B
fan.c 761B
buzzer.c 752B
mainPro.c 13KB
si
smarthome01.si4project
smarthome01.siproj 6KB
smarthome01.siproj_settings.xml 509B
smarthome01.sip_xr 34KB
Backup
controlDevices(6535).h 2KB
controlDevices(2571).h 1KB
fire(7932).c 652B
mainPro(4458).c 13KB
buzzer(2532).c 752B
shock(6185).c 2B
mainPro(1020).c 10KB
voiceControl(1365).c 3KB
sensor(7932).c 2KB
controlDevices(236).h 1KB
mainPro(332).c 10KB
fire(952).c 0B
buzzer(5741).c 0B
serverControl(2375).c 1KB
mainPro(6273).c 10KB
mainPro(4287).c 8KB
smarthome01.sip_xsd 368B
smarthome01.sip_xsb 4KB
smarthome01.sip_xm 68B
cache
parse
.._livingroomLight.c.sisc 4KB
.._controlDevices.h.sisc 11KB
.._bathroomLight.c.sisc 4KB
.._sensor.c.sisc 6KB
.._swimmingLight.c.sisc 4KB
.._fan.c.sisc 4KB
.._camera.c.sisc 820B
.._serverControl.c.sisc 4KB
.._mainPro.c.sisc 29KB
.._buzzer.c.sisc 5KB
.._shock.c.sisc 5KB
.._lock.c.sisc 820B
.._fire.c.sisc 4KB
.._secondfloorLight.c.sisc 4KB
.._inputCmd.h.sisc 5KB
.._restaurantLight.c.sisc 4KB
.._voiceControl.c.sisc 14KB
smarthome01.sip_xad 408B
smarthome01.siwork 10KB
smarthome01.sip_sym 150KB
smarthome01.SearchResults 43B
smarthome01.bookmarks.xml 162B
smarthome01.sip_xc 34KB
smarthome01.sip_xf 34KB
smarthome01.sip_xab 8KB
smarthome01.snippets.xml 170B
controlDevices.h 2KB
voiceControl.c 3KB
livingroomLight.c 816B
shock.c 661B
secondfloorLight.c 828B
inputCmd.h 1KB
fire.c 644B
sensor.c 2KB
swimmingLight.c 792B
serverControl.c 1KB
restaurantLight.c 811B
lock.c 0B
共 65 条
- 1
资源评论
热爱嵌入式的小佳同学
- 粉丝: 6934
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- n.cpp
- jdk-8u411-windows-x64下载安装可用
- vgg模型-图像分类算法对水果识别-不含数据集图片-含逐行注释和说明文档.zip
- KMP算法(Knuth-Morris-Pratt算法
- vgg模型-python语言pytorch框架训练识别化妆品分类-不含数据集图片-含逐行注释和说明文档.zip
- KMP算法(Knuth-Morris-Pratt算法
- shufflenet模型-基于人工智能的卷积网络训练识别狗的表情-不含数据集图片-含逐行注释和说明文档.zip
- shufflenet模型-python语言pytorch框架训练识别张嘴闭嘴-不含数据集图片-含逐行注释和说明文档.zip
- resnet模型-基于人工智能的卷积网络训练识别面部表情识别-不含数据集图片-含逐行注释和说明文档
- resnet模型-python语言pytorch框架训练识别香蕉品质-不含数据集图片-含逐行注释和说明文档.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功