|
@@ -810,254 +810,21 @@ class Lampcontrol extends Base_Controller {
|
|
|
$version = $this->session->userdata('version');
|
|
|
|
|
|
$lampArr = explode(',', $lamp_id);
|
|
|
- $lampList = $this->Lamp_model->get_list_in('L.id', $lampArr, 'L.id,N.protocoltype', array());
|
|
|
- $url = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].'/api/device/lampcontrol/turnonoff_asy';
|
|
|
- $time = 0;
|
|
|
- if (count($lampList) > 1 && count($lampList) < 20){
|
|
|
- $time = 60;
|
|
|
- }elseif (count($lampList) >= 20 && count($lampList) < 40) {
|
|
|
- $time = 120;
|
|
|
- }elseif (count($lampList) >= 40 && count($lampList) < 60) {
|
|
|
- $time = 180;
|
|
|
- }elseif (count($lampList) >= 60 && count($lampList) < 80) {
|
|
|
- $time = 240;
|
|
|
- }elseif (count($lampList) >= 80) {
|
|
|
- $time = 300;
|
|
|
- }
|
|
|
- foreach ($lampList as $l) {
|
|
|
- if ($l['protocoltype'] == 0) {
|
|
|
- doAsyncRequest($url,['clientid'=>$clientid,'username'=>$username,'client_key'=>$client_key,'token'=>$token,'version'=>$version,'type'=>$type,'lamp_id'=>$l['id']],30);
|
|
|
- }else{
|
|
|
- doAsyncRequest($url,['clientid'=>$clientid,'username'=>$username,'client_key'=>$client_key,'token'=>$token,'version'=>$version,'type'=>$type,'time'=>$time,'lamp_id'=>$l['id']]);
|
|
|
- usleep(100000);
|
|
|
- }
|
|
|
- // sleep(1);
|
|
|
+
|
|
|
+ $url = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].'/api/device/lampcontrol/dimming_asy';
|
|
|
+ $lightness = empty($type) ? 0 : 100;
|
|
|
+ foreach ($lampArr as $l) {
|
|
|
+ doAsyncRequest($url,['clientid'=>$clientid,'username'=>$username,'client_key'=>$client_key,'token'=>$token,'version'=>$version,'type'=>$type,'lamp_id'=>$l,'lightness'=>$lightness]);
|
|
|
}
|
|
|
|
|
|
exit();
|
|
|
}
|
|
|
|
|
|
- public function turnonoff_asy(){
|
|
|
- set_time_limit(0);
|
|
|
- $role = $this->get_user_info('role');
|
|
|
-
|
|
|
- $lamp_id = intval($this->input->post('lamp_id',true));
|
|
|
- $type = intval($this->input->post('type',true));
|
|
|
- $mode = intval($this->input->post('mode',true));
|
|
|
- $clientid = $this->input->post('clientid',true);
|
|
|
- $version = $this->input->post('version',true);
|
|
|
- $time = $this->input->post('time',true);
|
|
|
-
|
|
|
- $lampData = $this->Lamp_model->getOne($lamp_id,'L.number,N.protocoltype,N.deviceid,L.address,L.protocoltype as lampprotocoltype');
|
|
|
-
|
|
|
- $data['cmdtype'] = 0;
|
|
|
- $data['relateid'] = $lamp_id;
|
|
|
- $data['statuscmd'] = $type;
|
|
|
- $data['updatetime'] = date("Y-m-d H:i:s");
|
|
|
-
|
|
|
- $recordid = $this->Batmanage_model->get_record_id($data, 'batch_switch_cmd');
|
|
|
- if(!$recordid){
|
|
|
- $data = ['code'=>'0009','status'=>2,'msg'=>$this->response['0009'],'data'=>['number'=>$lampData['number'],'type'=>'turnonoff','id'=>$lamp_id]];
|
|
|
- exit(send_websocket($clientid,$data));
|
|
|
- }
|
|
|
- $cmd = '{"cmd_type":"batch_switch_cmd","cmd_id":'.$recordid.',"broadcast":'.$mode.'}';
|
|
|
- if (($lampData['protocoltype'] == 4 || $lampData['protocoltype'] == 6) && $lampData['lampprotocoltype'] == 0) {
|
|
|
- $address = '';
|
|
|
- if (mb_strlen($lampData['address']) >= 8) {
|
|
|
- $address = mb_substr($lampData['address'], -8);
|
|
|
- }else{
|
|
|
- $address = '00000000';
|
|
|
- }
|
|
|
- if ($type == 1) {
|
|
|
- $sendData = $address.'0603000104';
|
|
|
- }else{
|
|
|
- $sendData = $address.'0603000003';
|
|
|
- }
|
|
|
- if ($lampData['protocoltype'] == 4) {
|
|
|
- $res = lampMqttCmd('/WE/TransIn/'.$lampData['address'],'/WE/TransOut/'.$lampData['address'],pack('H*','0001'.$sendData));
|
|
|
- }else{
|
|
|
- device_cmd($lampData['deviceid'],pack('H*',$sendData));
|
|
|
- $cmd = '{"type":"cmd","deviceId":"'.$lampData['address'].'"}';
|
|
|
- $info = send_lorawan_915($cmd);
|
|
|
- $res = array();
|
|
|
- if (!empty($info)) {
|
|
|
- $info = json_decode($info,true);
|
|
|
- if ($info && $info['res'] == 0) $res['msg'] = $info['resInfo'];
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (empty($res)) {
|
|
|
- $data = ['code'=>'0016','status'=>2,'msg'=>$this->response['0016'],'data'=>['number'=>$lampData['number'],'type'=>'turnonoff','id'=>$lamp_id]];
|
|
|
- exit(send_websocket($clientid,$data));
|
|
|
- }else{
|
|
|
- if (strtolower($res['msg']) == strtolower($sendData)) {
|
|
|
- $data = ['code'=>'0000','status'=>2,'msg'=>$this->response['0000'],'data'=>['number'=>$lampData['number'],'type'=>'turnonoff','id'=>$lamp_id]];
|
|
|
- if ($type == 1) {
|
|
|
- $this->Lamp_model->update(array('id'=>$lampData['id']),array('status'=>1,'lighteness'=>100));
|
|
|
- $lampData = $this->Lamp_model->get_project_company(array('L.id'=>$lamp_id),'P.projectname,N.networkname,L.number,L.address');
|
|
|
- $this->add_operation_log('cmd',"\"{$lampData['projectname']}\"项目下的\"{$lampData['networkname']}\"网络里的编号为\"{$lampData['number']}\"、无线模块地址为\"{$lampData['address']}\"的路灯开灯",0);
|
|
|
- $this->add_operation_log('cmd',"\"Turn on the light:\"{$lampData['number']}\".Wireless module address:\"{$lampData['address']}\".Project name:{$lampData['projectname']}\".Network name:\"{$lampData['networkname']}\".",0,1);
|
|
|
- }else{
|
|
|
- $this->Lamp_model->update(array('id'=>$lampData['id']),array('status'=>0,'lighteness'=>0));
|
|
|
- $lampData = $this->Lamp_model->get_project_company(array('L.id'=>$lamp_id),'P.projectname,N.networkname,L.number,L.address');
|
|
|
- $this->add_operation_log('cmd',"\"{$lampData['projectname']}\"项目下的\"{$lampData['networkname']}\"网络里的编号为\"{$lampData['number']}\"、无线模块地址为\"{$lampData['address']}\"的路灯关灯",0);
|
|
|
- $this->add_operation_log('cmd',"Turn off the light:\"{$lampData['number']}\".Wireless module address:\"{$lampData['address']}\".Project name:\"{$lampData['projectname']}\".Network name:\"{$lampData['networkname']}\".",0,1);
|
|
|
- }
|
|
|
- exit(send_websocket($clientid,$data));
|
|
|
- }else{
|
|
|
- $data = ['code'=>'0010','status'=>2,'msg'=>$this->response['0010'],'data'=>['number'=>$lampData['number'],'type'=>'turnonoff','id'=>$lamp_id]];
|
|
|
- exit(send_websocket($clientid,$data));
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- }elseif ($lampData['protocoltype'] == 4 && $lampData['lampprotocoltype'] == 1) {
|
|
|
- if ($type == 1) { // 开灯 0110df0a00020400640258eea0
|
|
|
- $lightness = 100;
|
|
|
- }else{ // 关灯 0110df0a00020400000258af7f
|
|
|
- $lightness = 0;
|
|
|
- }
|
|
|
- $sendData = '0110df0a000204';
|
|
|
- $lightness = empty($lightness) ? '0000' : '0000'.base_convert($lightness, 10, 16);
|
|
|
- $sendData .= mb_strlen($lightness) == 4 ? $lightness : substr($lightness, -4);
|
|
|
- $sendData .= mb_substr('0000'.base_convert(600, 10, 16), -4);
|
|
|
-
|
|
|
- $sendData .= crc16(pack('H*',$sendData));
|
|
|
- $file = fopen('./file/light.txt', 'a+');
|
|
|
- fwrite($file, $sendData.'---'.date('Y-m-d H:i:s').'
|
|
|
-');
|
|
|
- fclose($file);
|
|
|
-
|
|
|
- $res = lampMqttCmd('/WE/TransIn/'.$lampData['address'],'/WE/TransOut/'.$lampData['address'],pack('H*','0001'.$sendData));
|
|
|
- if (empty($res)) {
|
|
|
- $data = ['code'=>'0016','status'=>2,'msg'=>$this->response['0016'],'data'=>['number'=>$lampData['number'],'type'=>'turnonoff','id'=>$lamp_id]];
|
|
|
- exit(send_websocket($clientid,$data));
|
|
|
- }else{
|
|
|
- if (strtolower(crc16(pack('H*',substr($res['msg'], 0,-4)))) == strtolower(substr($res['msg'], -4))) {
|
|
|
- $data = ['code'=>'0000','status'=>2,'msg'=>$this->response['0000'],'data'=>['number'=>$lampData['number'],'type'=>'turnonoff','id'=>$lamp_id]];
|
|
|
- if ($type == 1) {
|
|
|
- $this->Lamp_model->update(array('id'=>$lampData['id']),array('status'=>1,'lighteness'=>100));
|
|
|
- $lampData = $this->Lamp_model->get_project_company(array('L.id'=>$lamp_id),'P.projectname,N.networkname,L.number,L.address');
|
|
|
- $this->add_operation_log('cmd',"\"{$lampData['projectname']}\"项目下的\"{$lampData['networkname']}\"网络里的编号为\"{$lampData['number']}\"、无线模块地址为\"{$lampData['address']}\"的路灯开灯",0);
|
|
|
- $this->add_operation_log('cmd',"\"Turn on the light:\"{$lampData['number']}\".Wireless module address:\"{$lampData['address']}\".Project name:{$lampData['projectname']}\".Network name:\"{$lampData['networkname']}\".",0,1);
|
|
|
- }else{
|
|
|
- $this->Lamp_model->update(array('id'=>$lampData['id']),array('status'=>0,'lighteness'=>0));
|
|
|
- $lampData = $this->Lamp_model->get_project_company(array('L.id'=>$lamp_id),'P.projectname,N.networkname,L.number,L.address');
|
|
|
- $this->add_operation_log('cmd',"\"{$lampData['projectname']}\"项目下的\"{$lampData['networkname']}\"网络里的编号为\"{$lampData['number']}\"、无线模块地址为\"{$lampData['address']}\"的路灯关灯",0);
|
|
|
- $this->add_operation_log('cmd',"Turn off the light:\"{$lampData['number']}\".Wireless module address:\"{$lampData['address']}\".Project name:\"{$lampData['projectname']}\".Network name:\"{$lampData['networkname']}\".",0,1);
|
|
|
- }
|
|
|
- exit(send_websocket($clientid,$data));
|
|
|
- }else{
|
|
|
- $data = ['code'=>'0010','status'=>2,'msg'=>$this->response['0010'],'data'=>['number'=>$lampData['number'],'type'=>'turnonoff','id'=>$lamp_id]];
|
|
|
- exit(send_websocket($clientid,$data));
|
|
|
- }
|
|
|
- }
|
|
|
- }elseif($lampData['protocoltype'] == 6 && $lampData['lampprotocoltype'] == 1){
|
|
|
- if ($type == 1) { // 开灯
|
|
|
- $lightness = 100;
|
|
|
- }else{ // 关灯
|
|
|
- $lightness = 0;
|
|
|
- }
|
|
|
- //网络协议类型 loraWAN915 //1 ModBus
|
|
|
- $light_16 = doubBitSubStr(base_convert($lightness, 10, 16));
|
|
|
- $time = doubBitSubStr(base_convert(3600, 10, 16));
|
|
|
- // $cmd='0110DF0A000204'.$light_16.$time;
|
|
|
- $cmd = '0110DF0A000204'.$light_16.$time;
|
|
|
- $cmd .= crc16(pack('H*',$cmd));
|
|
|
- device_cmd($lampData['deviceid'],pack('H*',$cmd));
|
|
|
- $cmd = '{"type":"cmd","deviceId":"'.$lampData['address'].'"}';
|
|
|
- $res=send_lorawan_915($cmd);
|
|
|
- if($res){
|
|
|
- $res=json_decode($res,true);
|
|
|
-
|
|
|
- if($res['info']=='success'){
|
|
|
- $lowanData['lighteness']=$lightness;
|
|
|
- if($lightness > 0){
|
|
|
- $lowanData['status']=1;
|
|
|
- }else{
|
|
|
- $lowanData['status']=0;
|
|
|
- }
|
|
|
- $this->Lamp_model->update(array('id'=>$lampData['id']),$lowanData);
|
|
|
- $dataSocket= ['code'=>'0000','status'=>2,'msg'=>$this->response['0000'],'data'=>['number'=>$lampData['number'],'type'=>'dimming','id'=>$lamp_id]];
|
|
|
- exit(send_websocket($clientid,$dataSocket));
|
|
|
- }else{
|
|
|
- $dataSocket = ['code'=>'0010','status'=>2,'msg'=>$this->response['0010'],'data'=>['number'=>$lampData['number'],'type'=>'dimming','id'=>$lamp_id]];
|
|
|
- exit(send_websocket($clientid,$dataSocket));
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (empty($time)) {
|
|
|
- if (isset($lampData['protocoltype']) && $lampData['protocoltype'] != 0) {
|
|
|
- $cmdret = send_cmd($cmd,1,30,$lampData['protocoltype']);
|
|
|
- if ($cmdret === false) {
|
|
|
- sleep(1);
|
|
|
- $cmdret = send_cmd($cmd,1,30,$lampData['protocoltype']);
|
|
|
- }
|
|
|
- }else{
|
|
|
- $cmdret = send_cmd($cmd,0,30,$lampData['protocoltype']);
|
|
|
- if ($cmdret === false) {
|
|
|
- sleep(1);
|
|
|
- $cmdret = send_cmd($cmd,0,30,$lampData['protocoltype']);
|
|
|
- }
|
|
|
- }
|
|
|
- }else{
|
|
|
- if (isset($lampData['protocoltype']) && $lampData['protocoltype'] != 0) {
|
|
|
- $cmdret = send_cmd($cmd,1,$time,$lampData['protocoltype']);
|
|
|
- if ($cmdret === false) {
|
|
|
- sleep(1);
|
|
|
- $cmdret = send_cmd($cmd,1,$time,$lampData['protocoltype']);
|
|
|
- }
|
|
|
- }else{
|
|
|
- $cmdret = send_cmd($cmd,0,$time,$lampData['protocoltype']);
|
|
|
- if ($cmdret === false) {
|
|
|
- sleep(1);
|
|
|
- $cmdret = send_cmd($cmd,0,$time,$lampData['protocoltype']);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- if (empty($cmdret)) {
|
|
|
- $data = ['code'=>'0016','status'=>2,'msg'=>$this->response['0016'],'data'=>['number'=>$lampData['number'],'type'=>'turnonoff','id'=>$lamp_id]];
|
|
|
- exit(send_websocket($clientid,$data));
|
|
|
- }
|
|
|
- if($cmdret === false){
|
|
|
- $data = ['code'=>'0008','status'=>2,'msg'=>$this->response['0008'],'data'=>['number'=>$lampData['number'],'type'=>'turnonoff','id'=>$lamp_id]];
|
|
|
- exit(send_websocket($clientid,$data));
|
|
|
- } else {
|
|
|
- $res = json_decode($cmdret, true);
|
|
|
- }
|
|
|
-
|
|
|
- if (isset($res['result']) && $res['result'] == true) {
|
|
|
- $lampData = $this->Lamp_model->get_project_company(array('L.id'=>$lamp_id),'P.projectname,N.networkname,L.number,L.address');
|
|
|
- if ($type == 1) { // 开灯
|
|
|
- $this->add_operation_log('cmd',"\"{$lampData['projectname']}\"项目下的\"{$lampData['networkname']}\"网络里的编号为\"{$lampData['number']}\"、无线模块地址为\"{$lampData['address']}\"的路灯开灯",0);
|
|
|
- $this->add_operation_log('cmd',"\"Turn on the light:\"{$lampData['number']}\".Wireless module address:\"{$lampData['address']}\".Project name:{$lampData['projectname']}\".Network name:\"{$lampData['networkname']}\".",0,1);
|
|
|
- }else{ // 关灯
|
|
|
- $this->add_operation_log('cmd',"\"{$lampData['projectname']}\"项目下的\"{$lampData['networkname']}\"网络里的编号为\"{$lampData['number']}\"、无线模块地址为\"{$lampData['address']}\"的路灯关灯",0);
|
|
|
- $this->add_operation_log('cmd',"Turn off the light:\"{$lampData['number']}\".Wireless module address:\"{$lampData['address']}\".Project name:\"{$lampData['projectname']}\".Network name:\"{$lampData['networkname']}\".",0,1);
|
|
|
- }
|
|
|
- $data = ['code'=>'0000','status'=>2,'msg'=>$this->response['0000'],'data'=>['number'=>$lampData['number'],'type'=>'turnonoff','id'=>$lamp_id]];
|
|
|
- exit(send_websocket($clientid,$data));
|
|
|
- } else {
|
|
|
- $version = $this->input->post('version',true);
|
|
|
- if (empty($version)) {
|
|
|
-
|
|
|
- $res['msg'] = empty($res['msg']) ? '未知错误' : transfer_error_tips($res['msg']);
|
|
|
- $data = ['code'=>'0010','status'=>2,'msg'=>$res['msg'],'data'=>['number'=>$lampData['number'],'type'=>'turnonoff','id'=>$lamp_id]];
|
|
|
- exit(send_websocket($clientid,$data));
|
|
|
- }else{
|
|
|
- $res['msg'] = empty($res['msg']) ? 'Unknown error' : $res['msg'];
|
|
|
- $data = ['code'=>'other','status'=>2,'msg'=>$res['msg'],'data'=>['number'=>$lampData['number'],'type'=>'turnonoff','id'=>$lamp_id]];
|
|
|
- exit(send_websocket($clientid,$data));
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
// 灯控调光
|
|
|
public function dimming(){
|
|
|
$username = $this->input->post('username',true);
|
|
|
$client_key = $this->input->post('client_key',true);
|
|
|
$token = $this->input->post('token',true);
|
|
|
- $type = intval($this->input->post('type',true));
|
|
|
$lamp_id = $this->input->post('lamp_id',true);
|
|
|
$lightness = $this->input->post('lightness',true);
|
|
|
$clientid = $this->input->post('clientid',true);
|
|
@@ -1065,22 +832,10 @@ class Lampcontrol extends Base_Controller {
|
|
|
|
|
|
$lampArr = explode(',', $lamp_id);
|
|
|
|
|
|
- $lampList = $this->Lamp_model->get_list_in('L.id', $lampArr, 'L.id,N.protocoltype', array());
|
|
|
+ // $lampList = $this->Lamp_model->get_list_in('L.id', $lampArr, 'L.id,N.protocoltype', array());
|
|
|
$url = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].'/api/device/lampcontrol/dimming_asy';
|
|
|
- $time = 0;
|
|
|
- // if (count($lampList) > 1 && count($lampList) < 20){
|
|
|
- // $time = 60;
|
|
|
- // }elseif (count($lampList) >= 20 && count($lampList) < 40) {
|
|
|
- // $time = 120;
|
|
|
- // }elseif (count($lampList) >= 40 && count($lampList) < 60) {
|
|
|
- // $time = 180;
|
|
|
- // }elseif (count($lampList) >= 60 && count($lampList) < 80) {
|
|
|
- // $time = 240;
|
|
|
- // }elseif (count($lampList) >= 80) {
|
|
|
- // $time = 300;
|
|
|
- // }
|
|
|
- foreach ($lampList as $l) {
|
|
|
- doAsyncRequest($url,['clientid'=>$clientid,'username'=>$username,'client_key'=>$client_key,'token'=>$token,'version'=>$version,'type'=>$type,'lamp_id'=>$l['id'],'lightness'=>$lightness,'time'=>$time]);
|
|
|
+ foreach ($lampArr as $l) {
|
|
|
+ doAsyncRequest($url,['clientid'=>$clientid,'username'=>$username,'client_key'=>$client_key,'token'=>$token,'version'=>$version,'lamp_id'=>$l,'lightness'=>$lightness]);
|
|
|
}
|
|
|
|
|
|
exit();
|
|
@@ -1088,26 +843,22 @@ class Lampcontrol extends Base_Controller {
|
|
|
|
|
|
public function dimming_asy(){
|
|
|
set_time_limit(0);
|
|
|
- $role = $this->get_user_info('role');
|
|
|
-
|
|
|
$lamp_id = intval($this->input->post('lamp_id',true));
|
|
|
- $type = intval($this->input->post('type',true));
|
|
|
$lightness = intval($this->input->post('lightness',true));
|
|
|
- $mode = intval($this->input->post('mode',true));
|
|
|
$clientid = $this->input->post('clientid',true);
|
|
|
$version = $this->input->post('version',true);
|
|
|
- $time = $this->input->post('time',true);
|
|
|
if (empty($lamp_id)) {
|
|
|
exit(json_result('0400', $this->response['0400'], array()));
|
|
|
}
|
|
|
- $lampData = $this->Lamp_model->getOne($lamp_id,'L.number,N.protocoltype,N.deviceid,L.address,L.protocoltype as lampprotocoltype');
|
|
|
+ $lampData = $this->Lamp_model->get_one(['id'=>$lamp_id],'L.number,L.address');
|
|
|
|
|
|
$seq = get_seq();
|
|
|
|
|
|
- $sendData = '12'.PROTOCOL_VERSION.$seq;
|
|
|
+ $sendData = '12'.PROTOCOL_VERSION.$seq.'0000000000';
|
|
|
$lightness2 = empty($lightness) ? '00' : '00'.base_convert($lightness, 10, 16);
|
|
|
$sendData .= strlen($lightness2) == 2 ? $lightness2 : substr($lightness2, -2);
|
|
|
-
|
|
|
+ $sendData .= '00';
|
|
|
+
|
|
|
$sendData .= crc16(pack('H*',$sendData));
|
|
|
|
|
|
$res = lampMqttCmd('/WE/TransIn/'.$lampData['address'],'/WE/TransOut/'.$lampData['address'],pack('H*','0001'.$sendData));
|