123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573 |
- #pragma once
- #include "uart/ProtocolSender.h"
- #include "utils/TimeHelper.h"
- #include "net/NetManager.h"
- #include "net/DhcpClient.h"
- #include "manager/LanguageManager.h"
- #include "core/utilities.h"
- #include "core/sip_config.h"
- #include "edge/call_log.h"
- #include "edge/popup_service.h"
- #include "base/base.hpp"
- #include "service/BusinessConfig.h"
- #include "net/tcp_client.h"
- #include <sys/reboot.h>
- #include <net/NetUtils.h>
- /*
- *此文件由GUI工具生成
- *文件功能:用于处理用户的逻辑相应代码
- *功能说明:
- *========================onButtonClick_XXXX
- 当页面中的按键按下后系统会调用对应的函数,XXX代表GUI工具里面的[标识]名称,
- 如Button1,当返回值为false的时候系统将不再处理这个按键,返回true的时候系统将会继续处理此按键。比如SYS_BACK.
- *========================onSlideWindowItemClick_XXXX(int index)
- 当页面中存在滑动窗口并且用户点击了滑动窗口的图标后系统会调用此函数,XXX代表GUI工具里面的[标识]名称,
- 如slideWindow1;index 代表按下图标的偏移值
- *========================onSeekBarChange_XXXX(int progress)
- 当页面中存在滑动条并且用户改变了进度后系统会调用此函数,XXX代表GUI工具里面的[标识]名称,
- 如SeekBar1;progress 代表当前的进度值
- *========================ogetListItemCount_XXXX()
- 当页面中存在滑动列表的时候,更新的时候系统会调用此接口获取列表的总数目,XXX代表GUI工具里面的[标识]名称,
- 如List1;返回值为当前列表的总条数
- *========================oobtainListItemData_XXXX(ZKListView::ZKListItem *pListItem, int index)
- 当页面中存在滑动列表的时候,更新的时候系统会调用此接口获取列表当前条目下的内容信息,XXX代表GUI工具里面的[标识]名称,
- 如List1;pListItem 是贴图中的单条目对象,index是列表总目的偏移量。具体见函数说明
- *========================常用接口===============
- *LOGD(...) 打印调试信息的接口
- *mTextXXX->setText("****") 在控件TextXXX上显示文字****
- *mButton1->setSelected(true); 将控件mButton1设置为选中模式,图片会切换成选中图片,按钮文字会切换为选中后的颜色
- *mSeekBar->setProgress(12) 在控件mSeekBar上将进度调整到12
- *mListView1->refreshListView() 让mListView1 重新刷新,当列表数据变化后调用
- *mDashbroadView1->setTargetAngle(120) 在控件mDashbroadView1上指针显示角度调整到120度
- */
- #define ETHERNETMANAGER NETMANAGER->getEthernetManager()
- #define WIFIMANAGER NETMANAGER->getWifiManager()
- #define SYS_RESTART 10 // 重启
- static bool isLight = false;
- static net::DhcpClient _s_wifi_dhcp_client;
- static net::DhcpClient _s_eth_dhcp_client;
- static bool _start_wifi_dhcp_client() {
- // 静态ip模式不做处理
- if (!WIFIMANAGER->isAutoMode()) {
- return false;
- }
- if (!WIFIMANAGER->isConnected()) {
- return false;
- }
- char ip[32], gw[32];
- if (!WIFIMANAGER->getConfigureInfo(ip, NULL, gw, NULL, NULL)) {
- return false;
- }
- return _s_wifi_dhcp_client.start(ip, WIFIMANAGER->getMacAddr(), gw);
- }
- static void _stop_wifi_dhcp_client() {
- _s_wifi_dhcp_client.stop();
- }
- static bool _start_eth_dhcp_client() {
- // 静态ip模式不做处理
- if (!ETHERNETMANAGER->isAutoMode()) {
- LOGD("以太网静态");
- return false;
- }
- if (!ETHERNETMANAGER->isConnected()) {
- LOGD("以太网连接状态");
- return false;
- }
- char ip[32], gw[32];
- if (!ETHERNETMANAGER->getConfigureInfo(ip, NULL, gw, NULL, NULL)) {
- return false;
- }
- LOGD("正在重新请求dhcp");
- return _s_eth_dhcp_client.start(ip, ETHERNETMANAGER->getMacAddr(), gw);
- }
- static void _stop_eth_dhcp_client() {
- _s_eth_dhcp_client.stop();
- }
- static void _dhcp_lease_cb(dhcp_lease_type_e type, void *data) {
- LOGD("_dhcp_lease_cb type %d\n", type);
- switch (type) {
- case E_DHCP_LEASE_TYPE_SUCCESS:
- LOGD("DHCP连接成功");
- break;
- case E_DHCP_LEASE_TYPE_FAIL:
- LOGD("DHCP连接失败,重连");
- _start_eth_dhcp_client();
- break;
- case E_DHCP_LEASE_TYPE_TIMEOUT:
- // 续租超时,可以断开再重连尝试
- LOGD("DHCP连接超时,断开重连");
- _stop_eth_dhcp_client();
- _start_eth_dhcp_client();
- break;
- }
- }
- void NavibarSetPartName(const std::string& partName){
- mPartNamePtr->setText(partName);
- }
- static int netOffCount = 0; //断网计数,每秒+1
- static int netOffMax = 300; //断网300秒,则重启
- static bool isDataRefresh = false;
- static void updateNetState(){
- if (netOffCount == 0) {
- mRestartTimeTextViewPtr->setText("");
- }
- else {
- int restartTime = netOffMax - netOffCount;
- string restartTimeStr = LANGUAGEMANAGER->getValue("RestartTime") + to_string(restartTime);
- mRestartTimeTextViewPtr->setText(restartTimeStr);
- }
- if (ETHERNETMANAGER->isConnected() || WIFIMANAGER->isConnected()){
- netOffCount = 0;
- if (isDataRefresh) {
- dataRefresh();
- isDataRefresh = false;
- }
- } else {
- netOffCount++;
- isDataRefresh = true;
- //5分钟仍然断网,则重启
- if (netOffCount > netOffMax){
- netOffCount = 0;
- // 网卡重启
- LOGD("断网300秒,网卡断电重启");
- std::string heartStr = "NETRESET,1F";
- LOGD("=====> the heartStr == %s", heartStr.c_str());
- const char* sendMsg = heartStr.c_str();
- sendProtocolTo(UART_TTYS2, (byte*)(sendMsg), strlen(sendMsg));
- //重启
- sleep(3);
- LOGD("网卡重启无效,程序内部重启");
- sync();
- reboot(RB_AUTOBOOT);
- }
- }
- ENetChannel channel = NETMANAGER->getConnChannel();
- switch (channel) {
- case E_NET_CHANNEL_ETHERNET:
- if (ETHERNETMANAGER->isConnected()) {
- mPainter1Ptr->setBackgroundColor(0xFF37C127);
- }
- else {
- mPainter1Ptr->setBackgroundColor(0xFF949494);
- }
- break;
- case E_NET_CHANNEL_WIFI:
- if (WIFIMANAGER->isConnected()) {
- mPainter1Ptr->setBackgroundColor(0xFF37C127);
- }
- else {
- mPainter1Ptr->setBackgroundColor(0xFF949494);
- }
- break;
- }
- // EEthConnState connState = ETHERNETMANAGER->getConnState();
- // switch (connState){
- // case E_ETH_DISCONNECTED: // 以太网断开
- // mPainter1Ptr->setBackgroundColor(0xFF949494);
- // break;
- // case E_ETH_CONNECTING: // 以太网连接中
- // mPainter1Ptr->setBackgroundColor(0xFF2F9DF1);
- // break;
- // case E_ETH_CONNECTED: // 以太网连接
- // mPainter1Ptr->setBackgroundColor(0xFF37C127);
- // break;
- // case E_ETH_DISCONNECTING: // 以太网断开连接中
- // mPainter1Ptr->setBackgroundColor(0xFF949494);
- // break;
- // case E_ETH_CONN_UNKNOWN: // 以太网未知
- // mPainter1Ptr->setBackgroundColor(0xFF949494);
- // break;
- // }
- }
- void OnRegisterStateChanged(voip::Telephone* telephone, int code){
- code = GetTelephone()->GetRegistrationStatusCode();
- LOGD("SIP STATUS : %d", code);
- if (code == voip::SIP_STATUS_CODE_PROGRESS){
- //mTextViewRegPtr->setText("CONNECTING");
- mTextViewRegPtr->setBackgroundColor(0xFF2F9DF1);
- } else if (code == voip::SIP_STATUS_CODE_OK){
- //mTextViewRegPtr->setText("OK");
- mTextViewRegPtr->setBackgroundColor(0xFF37C127);
- } else {
- //mTextViewRegPtr->setText("ERROR");
- mTextViewRegPtr->setBackgroundColor(0xFF949494);
- }
- }
- void tcpStatus(){
- if (TcpClient::instance()->connected()){
- mTextViewTCPPtr->setBackgroundColor(0xFF37C127);
- } else {
- mTextViewTCPPtr->setBackgroundColor(0xFF949494);
- }
- }
- int pingCount = 0;
- class PingThread: public Thread {
- public:
- /**
- * 线程创建成功后会调用该函数,可以在该函数中做一些初始化操作
- * return true 继续线程
- * false 退出线程
- */
- virtual bool readyToRun() {
- LOGD("Ping Thread 已经创建完成");
- return true;
- }
- /**
- * 线程循环函数
- *
- * return true 继续线程循环
- * false 推出线程
- */
- virtual bool threadLoop() {
- //检查是否有退出线程的请求,如果有,则返回false,立即退出线程
- if (exitPending()) {
- return false;
- }
- //为了方便观察,这里添加休眠500ms
- // usleep(1000 * 1000);
- usleep(1 * 60 * 1000 * 1000);
- LOGD("Ping 线程循环函数");
- if (ETHERNETMANAGER->isAutoMode()) {
- string serverIp = StoragePreferences::getString(STORE_GATEWAY, serverIP);
- LOGD("===================> ping %s", serverIp.c_str());
- string pingStr = "ping " + serverIp + " -c 3";
- int result = system(pingStr.c_str());
- LOGD("===================> ping 服务器返回的结果: %d", result);
- if (result != 0) { // 如果不是0,就代表ping不通,需要重新请求dhcp
- LOGD("无法ping通服务器地址:%s", serverIp.c_str());
- pingCount += 1;
- LOGD("pingCount计数 == %d", pingCount);
- if (pingCount == 3) {
- NetUtils::dhcpExit();
- usleep(20 * 1000);
- NetUtils::enableIfc("eth0", false);
- usleep(100 * 1000);
- NetUtils::enableIfc("eth0", true);
- pingCount = 0;
- }
- }
- else {
- pingCount = 0;
- }
- }
- //返回真,继续下次线程循环
- return true;
- }
- };
- static PingThread ping_thread;
- void pingThreadExitPending() {
- bool result = ping_thread.isRunning();
- if (result) {
- ping_thread.requestExitAndWait();
- LOGD("ping_thread已关闭");
- }
- }
- void setIsLight(bool _isLight) {
- LOGD("设置是否常亮:%d", _isLight);
- isLight = _isLight;
- if (isLight) { // 常亮的时候关闭息屏
- setSleepTimerRegistered(false); // 关闭息屏功能
- }
- }
- int getTime(char* timeStr) {
- int hour, minute, second;// 定义时间的各个int临时变量。
- sscanf(timeStr, "%d:%d:%d", &hour, &minute, &second);
- int time = hour * 60 * 60 + minute * 60 + second;
- return time;
- }
- int dayLight = 100;
- int nightLight = 100;
- void isTimeScale(char* timeStr) {
- std::string dayTime = StoragePreferences::getString(STORE_DAY_START, "07:00:00");
- std::string nightTime = StoragePreferences::getString(STORE_NIGHT_START, "18:00:00");
- int day = getTime((char*) dayTime.data());
- int night = getTime((char*) nightTime.data());
- int now = getTime(timeStr);
- bool isSleepTime = getSleepTimerRegistered();
- if (day <= night) { // 在白天时间小于晚上时间的时候
- if (day <= now && now <= night) { // 这个时候,时间处于白天
- BRIGHTNESSHELPER->setBrightness(dayLight);
- // if (isSleepTime) { // isSleepTime就是定时任务开启,需要关闭
- // setSleepTimerRegistered(false); // 关闭息屏功能
- // dayLight = StoragePreferences::getInt(STORE_DAY_LIGHT, 100);
- // if (dayLight > 100) {
- // dayLight = 100;
- // }
- // LOGD("dayLight == %d", dayLight);
- // LOGD("白天开始时间小于晚上开始时间,切换至白天");
- // }
- }
- else { // 这个时间处于晚上
- BRIGHTNESSHELPER->setBrightness(nightLight);
- // if (!isSleepTime) { // isSleepTime就是定时任务关闭,需要开启
- // setSleepTimerRegistered(true); // 开启息屏功能
- // nightLight = StoragePreferences::getInt(STORE_NIGHT_LIGHT, 100);
- // if (nightLight > 100) {
- // nightLight = 100;
- // }
- // LOGD("nightLight == %d", nightLight);
- // LOGD("白天开始时间小于晚上开始时间,切换至晚上");
- // }
- }
- } else { // 这里是day > night时间的时候
- if (night <= now && now < day) { // 这个其实是晚上时间
- BRIGHTNESSHELPER->setBrightness(nightLight);
- // if (!isSleepTime) { // isSleepTime就是定时任务关闭,需要开启
- // setSleepTimerRegistered(true); // 开启息屏功能
- // nightLight = StoragePreferences::getInt(STORE_NIGHT_LIGHT, 100);
- // if (nightLight > 100) {
- // nightLight = 100;
- // }
- // LOGD("nightLight == %d", nightLight);
- // LOGD("白天开始时间大于晚上开始时间,切换至晚上");
- // }
- } else {
- BRIGHTNESSHELPER->setBrightness(dayLight);
- // if (isSleepTime) { // isSleepTime就是定时任务开启,需要关闭
- // setSleepTimerRegistered(false); // 关闭息屏功能
- // dayLight = StoragePreferences::getInt(STORE_DAY_LIGHT, 100);
- // if (dayLight > 100) {
- // dayLight = 100;
- // }
- // LOGD("dayLight == %d", dayLight);
- // LOGD("白天开始时间大于晚上开始时间,切换至白天");
- // }
- }
- }
- }
- namespace {
- // 以太网状态监听
- class EthernetConnStateListener : public EthernetManager::IEthernetConnStateListener {
- public:
- virtual void handleEthernetConnState(EEthConnState state) {
- LOGD("[net] handleEthernetConnState state: %d\n", state);
- switch (state) {
- case E_ETH_CONNECTED:
- LOGD("以太网连接");
- _start_eth_dhcp_client();
- break;
- case E_ETH_DISCONNECTED:
- LOGD("以太网断开");
- _stop_eth_dhcp_client();
- break;
- default:
- break;
- }
- }
- };
- // wifi状态监听
- class WifiListener: public WifiManager::IWifiListener {
- public:
- virtual void handleWifiConnect(E_WIFI_CONNECT event, int args) {
- LOGD("[net] handleWifiConnect event: %d\n", event);
- switch (event) {
- case E_WIFI_CONNECT_CONNECTED:
- _start_wifi_dhcp_client();
- break;
- case E_WIFI_CONNECT_DISCONNECT:
- _stop_wifi_dhcp_client();
- break;
- default:
- break;
- }
- }
- };
- }
- /**
- * 注册定时器
- * 填充数组用于注册定时器
- * 注意:id不能重复
- */
- static S_ACTIVITY_TIMEER REGISTER_ACTIVITY_TIMER_TAB[] = {
- {1, 1000},
- {2, 5000}
- };
- static void updateUI_time() {
- struct tm *t = TimeHelper::getDateTime();
- char timeStr[50];
- string day[] = {
- LANGUAGEMANAGER->getValue("Sunday"),
- LANGUAGEMANAGER->getValue("Monday"),
- LANGUAGEMANAGER->getValue("Tuesday"),
- LANGUAGEMANAGER->getValue("Wednesday"),
- LANGUAGEMANAGER->getValue("Thursday"),
- LANGUAGEMANAGER->getValue("Friday"),
- LANGUAGEMANAGER->getValue("Saturday")};
- string formatStr = "%s %d"+LANGUAGEMANAGER->getValue("Year")+"%02d"+LANGUAGEMANAGER->getValue("Month")+"%02d"+LANGUAGEMANAGER->getValue("Day")+" %02d:%02d:%02d";
- //string formatStr = "%s %d-%02d-%02d %02d:%02d:%02d";
- sprintf(timeStr, formatStr.c_str(), day[t->tm_wday].c_str(),1900 + t->tm_year, t->tm_mon + 1, t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);
- mDateViewPtr->setText(timeStr); // 注意修改控件名称
- // 如果不是常亮,就能需要去判断
- if (!isLight) {
- char timeStr2[50];
- string formatStr2 = "%02d:%02d:%02d";
- sprintf(timeStr2, formatStr2.c_str(), t->tm_hour,t->tm_min,t->tm_sec);
- isTimeScale(timeStr2);
- }
- }
- static EthernetConnStateListener _s_eth_state_listener;
- static WifiListener _s_wifi_listener;
- /**
- * 当界面构造时触发
- */
- static void onUI_init(){
- //SIP
- GetTelephone()->AddRegisteredStateListener(OnRegisterStateChanged);
- mPartNamePtr->setText(StoragePreferences::getString(STORE_PARTNAME,""));
- updateUI_time();
- //网络判断
- updateNetState();
- tcpStatus();
- //Tips :添加 UI初始化的显示代码到这里,如:mText1Ptr->setText("123");
- ETHERNETMANAGER->addEthernetConnStateListener(&_s_eth_state_listener);
- WIFIMANAGER->addWifiListener(&_s_wifi_listener);
- StoragePreferences::putString(STORE_MAC_ADDR, std::string(ETHERNETMANAGER->getMacAddr()));
- _s_wifi_dhcp_client.set_lease_cb(_dhcp_lease_cb);
- _s_eth_dhcp_client.set_lease_cb(_dhcp_lease_cb);
- // 这里也检测网络是否连接上 (有可能启动前网络已经连接触发不到回调),启动dhcp租赁检测,之后都是根据网络的连接断开执行相应的操作
- _start_wifi_dhcp_client();
- _start_eth_dhcp_client();
- ping_thread.run("ping");
- StoragePreferences::putBool(STORE_LIGHT, true);
- isLight = StoragePreferences::getBool(STORE_LIGHT, false);
- }
- /*
- * 当界面完全退出时触发
- */
- static void onUI_quit() {
- GetTelephone()->RemoveRegisteredStateListener(OnRegisterStateChanged);
- }
- /**
- * 串口数据回调接口
- */
- static void onProtocolDataUpdate(const SProtocolData &data) {
- //串口数据回调接口
- }
- /**
- * 定时器触发函数
- * 不建议在此函数中写耗时操作,否则将影响UI刷新
- * 参数: id
- * 当前所触发定时器的id,与注册时的id相同
- * 返回值: true
- * 继续运行当前定时器
- * false
- * 停止运行当前定时器
- */
- static bool onUI_Timer(int id){
- switch (id) {
- case 1:
- updateUI_time();
- updateNetState();
- break;
- case 2:
- tcpStatus();
- break;
- default:
- break;
- }
- return true;
- }
- /**
- * 有新的触摸事件时触发
- * 参数:ev
- * 新的触摸事件
- * 返回值:true
- * 表示该触摸事件在此被拦截,系统不再将此触摸事件传递到控件上
- * false
- * 触摸事件将继续传递到控件上
- */
- static bool onstatusbarActivityTouchEvent(const MotionEvent &ev) {
- switch (ev.mActionStatus) {
- case MotionEvent::E_ACTION_DOWN://触摸按下
- //LOGD("时刻 = %ld 坐标 x = %d, y = %d", ev.mEventTime, ev.mX, ev.mY);
- break;
- case MotionEvent::E_ACTION_MOVE://触摸滑动
- break;
- case MotionEvent::E_ACTION_UP: //触摸抬起
- break;
- default:
- break;
- }
- return false;
- }
|