#include "LED.h"#include <QFile>#include "Json.h"#include "Config.h"#include "ChannelVI.h"#include "ChannelUSB.h"#include <stdio.h>#include <ctype.h>#include <sys/ioctl.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <unistd.h>#include "Record.h"#include "Push.h"#include "Intercom.h"LED::LED(QObject *parent) : QObject(parent){fd=-1;curMode=ModeCut;tallyArbiter=NULL;gpio=NULL;dstR=0;dstG=0;dstB=0;lastR=1;lastG=1;lastB=1;hadMount=false;fac="";connect(&timer,SIGNAL(timeout()),this,SLOT(onTimer()));connect(&mountTimer,SIGNAL(timeout()),this,SLOT(onMountTimer()));}bool LED::hadLedCtrl(){return QFile::exists("/dev/led_ctrl");}bool LED::hadViAvalible(){bool avalible = false;for(int i=0;i<Config::chns.count();i++){if(Config::chns[i]->type=="vi"){ChannelVI *chnVI=(ChannelVI *)Config::chns[i];connect(chnVI->vi,SIGNAL(newEvent(QString,QVariant)),this,SLOT(onNewEvent(QString,QVariant)),Qt::UniqueConnection);if(Config::chns[i]->enable && chnVI->vi->invoke("getReport").toMap()["avalible"].toBool())avalible = true;}}return avalible;}bool LED::hadUsbAvalible(){bool avalible = false;for(int i=0;i<Config::chns.count();i++){if(Config::chns[i]->type=="usb"){ChannelUSB *chn=(ChannelUSB *)Config::chns[i];connect(chn,SIGNAL(newEvent(QString,QVariant)),this,SLOT(onNewEvent(QString,QVariant)),Qt::UniqueConnection);if(Config::chns[i]->enable && chn->camOpen)avalible = true;}}return avalible;}void LED::init(){if(!QFile::exists(LEDCFGFILE))return;if(hadLedCtrl()){fd = open("/dev/led_ctrl", O_RDWR);if (fd < 0){qDebug() << "open /dev/led_ctrl failed";return;}devCfg=Json::loadFile(LEDDEVFILE).toMap();}else{gpio=Link::create("GPIO");gpio->start();}QVariantMap hardwareConf = Json::loadFile(HARDWAREPATH).toMap();fac = hardwareConf["fac"].toString();update(Json::loadFile(LEDCFGFILE).toMap());}bool LED::update(QVariantMap cfg){if(cfg.isEmpty())return false;config=cfg;if(config["enable"].toBool()){funcs.clear();if(config["func"].type() == QVariant::String)funcs << config["func"].toString();if(config["func"].type() == QVariant::List)funcs = config["func"].toStringList();if(funcs.contains("signal")){curInfo = (fac=="UVC2"?hadUsbAvalible():hadViAvalible());onNewInfo("signal");}if(funcs.contains("record") && GRecord!=NULL){connect(GRecord,SIGNAL(newEvent(QString,QVariant)),this,SLOT(onNewEvent(QString,QVariant)),Qt::UniqueConnection);GRecord->newEvent("record",GRecord->isRecordState());}if(funcs.contains("push") && GPush!=NULL){connect(GPush,SIGNAL(newEvent(QString,QVariant)),this,SLOT(onNewEvent(QString,QVariant)),Qt::UniqueConnection);GPush->newEvent("push",GPush->getState()["pushing"].toBool());}if(funcs.contains("tally") && GIntercom!=NULL){connect(GIntercom,SIGNAL(newEvent(QString,QVariant)),this,SLOT(onNewEvent(QString,QVariant)),Qt::UniqueConnection);}if(funcs.contains("mount")){connect(this,SIGNAL(mtEvent(QString,QVariant)),this,SLOT(onNewEvent(QString,QVariant)),Qt::UniqueConnection);if(GRecord != NULL)this->mtEvent("mt_event",GRecord->isMountDisk());if(!mountTimer.isActive())mountTimer.start(1000);}if(funcs.contains("tallyArbiter")){if(tallyArbiter == NULL){tallyArbiter=Link::create("TallyArbiter");connect(tallyArbiter,SIGNAL(newEvent(QString,QVariant)),this,SLOT(onNewEvent(QString,QVariant)),Qt::UniqueConnection);}tallyArbiter->start(config["funcList"].toMap()["tallyArbiter"].toMap());}else{if(tallyArbiter != NULL)tallyArbiter->stop();}}else{timer.stop();mountTimer.stop();fillColor(0,0,0);if(tallyArbiter != NULL)tallyArbiter->stop();}return true;}void LED::onNewEvent(QString type, QVariant info){foreach (QString func, funcs){if(func=="signal" && type=="signal"){curInfo=hadViAvalible();onNewInfo(func);}else if(func=="signal" && type=="CameraOpen" && fac == "UVC2"){curInfo=hadUsbAvalible();onNewInfo(func);}else if(func=="record" && type=="record"){curInfo=info;onNewInfo(func);}else if(func=="push" && type=="push"){curInfo=info;onNewInfo(func);}else if(func=="tally" && type=="TALLY"){curInfo=info;onNewInfo(func);}else if(func=="tallyArbiter" && type=="TA_color"){curInfo=info;onNewInfo(func);}else if(func=="mount" && type=="mt_event"){curInfo=info;onNewInfo(func);}else if(func=="tallyArbiter" && type=="TA_reassign"){QVariantMap cfg=Json::loadFile(LEDCFGFILE).toMap();QVariantMap flist=cfg["funcList"].toMap();QVariantMap taMap=flist["tallyArbiter"].toMap();taMap["deviceId"]=info.toMap()["deviceId"];taMap["deviceName"]=info.toMap()["deviceName"];flist["tallyArbiter"]=taMap;cfg["funcList"]=flist;Json::saveFile(cfg,LEDCFGFILE);}}}void LED::onNewInfo(QString func){if(!config["enable"].toBool())return;if(func=="signal"){if(curInfo.toBool())setLED(func,1);elsesetLED(func,0);}else if(func=="record"){if(curInfo.toBool())setLED(func,1);elsesetLED(func,0);}else if(func=="push"){if(curInfo.toBool())setLED(func,1);elsesetLED(func,0);}else if(func=="mount"){if(curInfo.toBool())setLED(func,1);elsesetLED(func,0);}else if(func=="tally"){int did=Json::loadFile("/link/config/intercom.json").toMap()["intercom"].toMap()["did"].toInt();QVariantList list=curInfo.toList();if(did<=0 || did>list.count())setLED(func,0);else{int c=list[did-1].toInt();if(c<=2)setLED(func,c);elsesetLED(func,0);}}else if(func=="tallyArbiter"){if(curInfo.toString().isEmpty())return;QByteArray color=QByteArray::fromHex(curInfo.toString().mid(1).toLatin1());double brightness=config["brightness"].toDouble();dstR=color[0]*brightness;dstG=color[1]*brightness;dstB=color[2]*brightness;fillColor(dstR,dstG,dstB);}}void LED::setLED(QString func, int state){QVariantMap funcArgs = config["funcList"].toMap()[func].toMap();QString mode = funcArgs["mode"].toString();QVariantList color=funcArgs["color"].toList()[state].toList();double brightness=config["brightness"].toDouble();dstR=color[0].toInt()*brightness;dstG=color[1].toInt()*brightness;dstB=color[2].toInt()*brightness;if((mode == ModeCut && hadLedCtrl()) || mode == ModeGpio){QStringList devlist = QStringList({"led"});if(mode == ModeGpio)devlist = funcArgs["gpio"].toStringList();fillColor(dstR,dstG,dstB,devlist);}else if((mode == ModeBreathe || mode == ModeSlide) && hadLedCtrl()){curMode = mode;if(timer.isActive() && timer.interval()!=20)timer.stop();if(!timer.isActive())timer.start(20);}else if(mode == ModeFlick && hadLedCtrl()){curMode = mode;if(timer.isActive() && timer.interval()!=1000)timer.stop();if(!timer.isActive())timer.start(1000);}}void LED::fillColor(uchar r, uchar g, uchar b, QStringList devlist){foreach (QString dev, devlist){if(dev == "led"){if(fd < 0)return;int count=devCfg["count"].toInt();uchar buf[count*3];for(int i=0;i<count;i++){buf[i*3]=g;buf[i*3+1]=r;buf[i*3+2]=b;}write(fd, (void *)buf, count*3);}else{if(gpio == NULL)return;bool state = false;if(r ==1 && g==1 && b==1)state = true;QVariantList args;args << dev << state;gpio->invoke("setGPIO",args);}}}void LED::onTimer(){if(curMode==ModeSlide){if(fd < 0)return;static int c=0;QVariantList list=devCfg["index"].toList();int count=devCfg["count"].toInt();static uchar *buf=NULL;if(buf==NULL){buf=new uchar[count*3];memset(buf,0,count*3);}int cnt=list.count();int a=40/cnt;c++;if(c>40){c=0;timer.stop();return;}int m=c/a;int n=c%a;for(int i=0;i<m;i++){QVariantList ll=list[i].toList();for(int j=0;j<ll.count();j++){int id=ll[j].toInt();buf[id*3+0]=dstG;buf[id*3+1]=dstR;buf[id*3+2]=dstB;}}if(m<cnt){QVariantList ll=list[m].toList();for(int j=0;j<ll.count();j++){int id=ll[j].toInt();buf[id*3+0]=dstG*n/a;buf[id*3+1]=dstR*n/a;buf[id*3+2]=dstB*n/a;}}write(fd, (void *)buf, count*3);}else if(curMode==ModeBreathe){static int c=0;static int k=1;c+=k;if(c>50){c=50;k=-1;}else if(c<0){c=0;k=1;}uchar r=(int)dstR*c/50;uchar g=(int)dstG*c/50;uchar b=(int)dstB*c/50;if(r!=lastR || g!=lastG || b!=lastB)fillColor(r,g,b);lastR=r;lastG=g;lastB=b;}else if(curMode==ModeFlick){static bool show=false;show=!show;if(show)fillColor(dstR,dstG,dstB);elsefillColor(0,0,0);}}void LED::onMountTimer(){if(GRecord != NULL){bool isMount = GRecord->isMountDisk();if(hadMount != isMount){this->mtEvent("mt_event",GRecord->isMountDisk());hadMount = isMount;}}}QVariantList LED::getTADevices(){QVariantList ret;if(tallyArbiter==NULL)return ret;ret=tallyArbiter->invoke("getDevices").toList();return ret;}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。