PageRenderTime 45ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/backend/components/TemperatureComponent.php

https://bitbucket.org/gndiot/gnd-iot
PHP | 411 lines | 295 code | 112 blank | 4 comment | 33 complexity | 8c10bfc748528514675fe2ee05040a78 MD5 | raw file
  1. <?php
  2. namespace backend\components;
  3. use yii\base\Component;
  4. class TemperatureComponent extends Component{
  5. private $input;
  6. public function init(){
  7. parent::init();
  8. }
  9. private function base64strToHex($string){
  10. $hex = '';
  11. for ($i=0; $i<strlen($string); $i++){
  12. $ord = ord($string[$i]);
  13. $hexCode = dechex($ord);
  14. $hex .= substr('0'.$hexCode, -2);
  15. }
  16. return strToUpper($hex);
  17. }
  18. private function hexToStr($hex){
  19. $string='';
  20. for ($i=0; $i < strlen($hex)-1; $i+=2){
  21. $string .= chr(hexdec($hex[$i].$hex[$i+1]));
  22. }
  23. return $string;
  24. }
  25. public function convertHex(){
  26. }
  27. public function getHexDownlinkConfig($rp_interval,$ss_interval,$tht,$tlt,$hh,$hl,$mod=2,$is_base64=false){
  28. $str1='b1'.'00'.$this->decimalToHexBattery($rp_interval).'00'.$this->decimalToHexBattery($ss_interval).$this->getTemperatureHex($tht).$this->getTemperatureHex($tlt).$this->decimalToHexBattery($hh).$this->decimalToHexBattery($hl).$this->decimalToHexBattery($mod);
  29. if($is_base64)
  30. {
  31. return $this->hexToStr($str1);
  32. }
  33. return $str1;
  34. }
  35. public function decodeDownlinkConfig($hex){
  36. return base64_encode($this->hexToStr($hex));
  37. }
  38. public function periodicMessage(){
  39. $result=[];
  40. $strarr=str_split($this->input,2);
  41. if($strarr[0]!='A4')
  42. {
  43. return [];
  44. }
  45. $result["temperature"]=$this->convertHexToDecimalForTemp($strarr[1].$strarr[2]);
  46. $result["humidity"]=$this->getHexToDecimal($strarr[3]);
  47. $result["battery"]=$this->getHexToDecimal($strarr[4]);
  48. return $result;
  49. }
  50. public function alertMessage() {
  51. $result=[];
  52. $strarr=str_split($this->input,2);
  53. if($strarr[0]!='A3')
  54. {
  55. return [];
  56. }
  57. $result["alert_type"]=$this->getHexToDecimal($strarr[1]);
  58. $result["alarm_type"]=$this->getHexToDecimal($strarr[2]);
  59. $result["temperature"]=$this->convertHexToDecimalForTemp($strarr[3].$strarr[4]);
  60. $result["humidity"]=$this->getHexToDecimal($strarr[5]);
  61. $result["battery"]=$this->getHexToDecimal($strarr[6]);
  62. return $result;
  63. }
  64. //this function is used to convert decimal to hex for only battery and humdity
  65. private function decimalToHexBattery($value){
  66. $hexString=dechex($value);
  67. if(strlen($hexString)==1)
  68. {
  69. $hex='0'.$hexString;
  70. return $hex;
  71. }else{
  72. return $hexString;
  73. }
  74. }
  75. private function getTemperatureHex($value,$multiply=10){
  76. if($value > 0)
  77. {
  78. $r1=$value*$multiply;
  79. $hexString=dechex($r1);
  80. //check value is 20
  81. if(strlen($hexString) > 2)
  82. {
  83. if(strlen($hexString)==4)
  84. {
  85. $hex=$hexString;
  86. }else{
  87. $hex='0'.$hexString;
  88. }
  89. }else{
  90. $hex='00'.$hexString;
  91. }
  92. return $hex;
  93. }else{
  94. $r1=abs($value)*$multiply;
  95. $hexString=dechex($r1);
  96. if(strlen($hexString) > 2)
  97. {
  98. $hex='8'.$hexString;
  99. }else{
  100. $hex='80'.$hexString;
  101. }
  102. return $hex;
  103. }
  104. }
  105. public function getDecodeHealthCheckMsg($hex){
  106. $result=[];
  107. $strarr=str_split($hex,2);
  108. if($strarr[0]!='A1')
  109. {
  110. return [];
  111. }
  112. $result['model_number']=$this->getVersionNumber($strarr[7].$strarr[8]);
  113. $result['firmware_version']=$this->getVersionNumber($strarr[9].$strarr[10]);
  114. $result['product_version']=$this->getVersionNumber($strarr[11].$strarr[12]);
  115. $result['reporting_interval']=$this->getHexToDecimal($strarr[13].$strarr[14]);
  116. $result['sesnsor_sampling_interval']=$this->getHexToDecimal($strarr[15].$strarr[16]);
  117. $result['temperature_high_threshold']=$this->convertHexToDecimalForTemp($strarr[17].$strarr[18]);
  118. $result['temperature_low_threshold']=$this->convertHexToDecimalForTemp($strarr[19].$strarr[20]);
  119. $result['humidity_high_threshold']=$this->getHexToDecimal($strarr[21]);
  120. $result['humidity_low_threshold']=$this->getHexToDecimal($strarr[22]);
  121. $result['battery_percentage']=$this->getHexToDecimal($strarr[23]);
  122. $result['mode']=$this->getHexToDecimal($strarr[24]);
  123. return $result;
  124. }
  125. public function loadBase64Hex($str){
  126. $str= base64_decode($str);
  127. $this->input=$this->base64strToHex($str);
  128. }
  129. public function loadHexStr($str){
  130. $this->input=$str;
  131. }
  132. public function getHextoString(){
  133. return $this->input;
  134. }
  135. public function output(){
  136. }
  137. private function convertDecimalToHexForTemp($value,$multiply=10){
  138. if($value > 0)
  139. {
  140. $r1=$value*$multiply;
  141. $hexString=dechex($r1);
  142. //check value is 20
  143. if(strlen($hexString) > 2)
  144. {
  145. if(strlen($hexString)==4)
  146. {
  147. $hex=$hexString;
  148. }else{
  149. $hex='0'.$hexString;
  150. }
  151. }else{
  152. $hex='00'.$hexString;
  153. }
  154. return $hex;
  155. }else{
  156. $r1=abs($value)*$multiply;
  157. $hexString=dechex($r1);
  158. if(strlen($hexString) > 2)
  159. {
  160. $hex='8'.$hexString;
  161. }else{
  162. $hex='80'.$hexString;
  163. }
  164. return $hex;
  165. }
  166. }
  167. private function convertHexToDecimalForTemp($hexString,$divided=10){
  168. $strarr=str_split($hexString,2);
  169. if($strarr[0]=='01')
  170. {
  171. $value=hexdec($hexString);
  172. return (intval($value) / $divided);
  173. }
  174. if($strarr[0]=='02')
  175. {
  176. $value=hexdec($hexString);
  177. $value=hexdec('2'.$strarr[1]);
  178. return (intval($value) / $divided);
  179. }
  180. if($strarr[0]=='03')
  181. {
  182. $value=hexdec('3'.$strarr[1]);
  183. return (intval($value) / $divided);
  184. }
  185. if($strarr[0]=='81')
  186. {
  187. $value=hexdec('01'.$strarr[1]);
  188. return '-'.(intval($value) / $divided);
  189. }
  190. if($strarr[0]=='00')
  191. {
  192. $value=hexdec($hexString);
  193. return floatval((intval($value) / $divided));
  194. }
  195. if($strarr[0]=='80')
  196. {
  197. if($strarr[1]=='0')
  198. {
  199. return 0;
  200. }
  201. $value=hexdec('00'.$strarr[1]);
  202. $result='-'.(intval($value) / $divided);
  203. return floatval($result);
  204. }
  205. }
  206. private function getHexToDecimal($value){
  207. return hexdec($value);
  208. }
  209. private function getVersionNumber($hex){
  210. if(strlen($hex)==4)
  211. {
  212. $strarr=str_split($hex,2);
  213. $one=hexdec($strarr[0]);
  214. $two=hexdec($strarr[1]);
  215. return $one.'.'.$two;
  216. }else{
  217. return "0.00";
  218. }
  219. }
  220. public function isPeriodicMsg(){
  221. $arr=str_split($this->input,2);
  222. if(!empty($arr))
  223. {
  224. if($arr[0]=='A4')
  225. {
  226. return true;
  227. }else{
  228. return false;
  229. }
  230. }else{
  231. return false;
  232. }
  233. }
  234. public function isAlertMsg(){
  235. $arr=str_split($this->input,2);
  236. if(!empty($arr))
  237. {
  238. if($arr[0]=='A3')
  239. {
  240. return true;
  241. }else{
  242. return false;
  243. }
  244. }else{
  245. return false;
  246. }
  247. }
  248. public function isBatteryCalibration(){
  249. $arr=str_split($this->input,2);
  250. if(!empty($arr))
  251. {
  252. if($arr[0]=='A5')
  253. {
  254. return true;
  255. }else{
  256. return false;
  257. }
  258. }else{
  259. return false;
  260. }
  261. }
  262. public function isHealthMsg(){
  263. $arr=str_split($this->input,2);
  264. if(!empty($arr))
  265. {
  266. if($arr[0]=='A1')
  267. {
  268. return true;
  269. }else{
  270. return false;
  271. }
  272. }else{
  273. return false;
  274. }
  275. }
  276. public function isConfigurationRequest(){
  277. $arr=str_split($this->input,2);
  278. if(!empty($arr))
  279. {
  280. if($arr[0]=='A2')
  281. {
  282. return true;
  283. }else{
  284. return false;
  285. }
  286. }else{
  287. return false;
  288. }
  289. }
  290. //test code
  291. public function _periodicMessage($temperature,$humidity,$battery){
  292. $str1='a4'.$this->getTemperatureHex($temperature).$this->decimalToHexBattery($humidity).$this->decimalToHexBattery($battery);
  293. return $str1;
  294. }
  295. public function _alertMessage($alertType,$alarm_type,$temperature,$humidity,$battery){
  296. $str1='a3'.$this->decimalToHexBattery($alertType).$this->decimalToHexBattery($alarm_type).$this->getTemperatureHex($temperature).$this->decimalToHexBattery($humidity).$this->decimalToHexBattery($battery);
  297. return $str1;
  298. }
  299. }