Skip to content

Commit

Permalink
Merge pull request #10 from mhzawadi/dev-php
Browse files Browse the repository at this point in the history
GH7/Make logging levels
  • Loading branch information
mhzawadi authored Jun 9, 2023
2 parents d7ca135 + 1667332 commit 2c01239
Show file tree
Hide file tree
Showing 10 changed files with 268 additions and 103 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ The json for the config.json file
"mqtt_host": "mosquitto",
"mqtt_port": "1883",
"mqtt_user": "foxess",
"mqtt_pass": "foxess"
"mqtt_pass": "foxess",
"log_level": 2
}
```

- log_level: is how much you want in the console, 1 is minimal, 2 is basic, 3 is everything
4 changes: 2 additions & 2 deletions src/classes/json.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ protected function load_from_file($filename, $array = true){
fclose($handle);
return $json;
} catch (Exception $e) {
echo 'Issues opening file: ', $e->getMessage(), "\n";
$this->log('Issues opening file: '.$e->getMessage(), 1);
return false;
}

Expand All @@ -32,7 +32,7 @@ protected function save_to_file($filename, $json){
sleep(1);
return true;
} catch (\Exception $e) {
echo 'that didnt work';
$this->log('Issues saving file: '.$e->getMessage(), 1);
return false;
}
}
Expand Down
11 changes: 9 additions & 2 deletions src/classes/logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@ class logger {
/**
* Log function
**/
protected function log($text, $level = 1){
echo date('Y-m-d H:i:s').' - '.$text."\n";
protected function log($text, $level = 2){

if(!defined('log_level')){
define('log_level', 2);
}

if($level <= constant('log_level')){
echo date('Y-m-d H:i:s').' - '.$text."\n";
}
}

}
31 changes: 18 additions & 13 deletions src/controller/foxess_data.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,17 @@ public function __construct(){
try {
$this->config = new config();
} catch (Exception $e) {
$this->log('Missing config: '.$e->getMessage());
$this->log('Missing config: '.$e->getMessage(), 1);
}

$this->log('Start of work');
$this->log('Start of work', 2);
# load the json data from file
$this->foxess_data = $this->load_from_file('data/foxess_data.json');
if(count($this->foxess_data['variables']) < 63){
$this->foxess_data['setup'] = '0';
$new_variables = $this->load_from_file('template/foxess_data.json');
$this->foxess_data['variables'] = $new_variables['variables'];
}
if($this->foxess_data['setup'] < time()){
$this->foxess_data['setup'] = $this->mqtt->setup_mqtt($this->foxess_data);
}
Expand All @@ -42,7 +47,7 @@ public function __construct(){
$this->data->process_data($this->foxess_data, $this->collected_data);
}

$this->log("Work complete");
$this->log("Work complete", 2);
}


Expand All @@ -54,7 +59,7 @@ public function __construct(){
*
*/
protected function collect_data() {
$this->log('Collect data from the cloud');
$this->log('Collect data from the cloud', 3);
$data = '{
"deviceID": "'.$this->config->device_id.'",
"variables": '.json_encode($this->foxess_data['variables']).',
Expand Down Expand Up @@ -96,31 +101,31 @@ protected function collect_data() {
$this->save_to_file('data/collected.json', $return_data);
if(is_null($return_data) === false){
if($return_data['errno'] == 40401){
$this->log('Too many logins');
$this->log('Too many logins', 2);
return 1;
}elseif($return_data['errno'] == 41809 ||
$return_data['errno'] == 41808){
$this->log('we need to login again');
$this->log('we need to login again', 3);
if($this->foxess_data['token'] = $this->login->login()){
$this->log('login complte');
$this->log('login complte', 3);
return 2;
}else{
$this->log('login error');
$this->log('login error', 2);
return 1;
}
}elseif($return_data['errno'] > 0){
$this->log('We have an error getting data, we have logged in fine');
$this->log('Error: '.$return_data['errno']);
$this->log('We have an error getting data, we have logged in fine', 3);
$this->log('Error: '.$return_data['errno'], 2);
return 1;
}else{
$this->log('We have the data, ready to process');
$this->log('We have the data, ready to process', 3);
}
}else{
$this->log('We have an error getting data, the file is empty');
$this->log('We have an error getting data, the file is empty', 3);
return 1;
}
$this->collected_data = $return_data;
$this->log('Data collected');
$this->log('Data collected', 3);
return 0;
}
}
9 changes: 8 additions & 1 deletion src/model/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,15 @@ public function __construct(){
$this->mqtt_port = $config['mqtt_port'];
$this->mqtt_user = $config['mqtt_user'];
$this->mqtt_pass = $config['mqtt_pass'];
if(!defined('log_level') && isset($config['log_level'])){
define('log_level', $config['log_level']);
}elseif(!defined('log_level') && getenv('LOG_LEVEL') !== false){
define('log_level', getenv('LOG_LEVEL'));
}elseif(!defined('log_level')){
define('log_level', 2);
}
} catch (Exception $e) {
$this->log('Missing config: '. $e->getMessage());
$this->log('Missing config: '. $e->getMessage(), 1);
}


Expand Down
63 changes: 43 additions & 20 deletions src/model/data.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,56 @@ class data extends json {
*/
public function process_data($foxess_data, $collected_data) {
$this->mqtt = new mqtt();
$this->log('Start of processing the data');
$this->log('Start of processing the data', 3);
$options_count = count($collected_data['result']);
for( $i = 0 ; $i < $options_count; $i++ ){
$option = $collected_data['result'][$i]['variable'];
if($collected_data['result'] == 'null'){
$value_kw = 0;
$value_kwh = $foxess_data['result'][$i];
}else{
$data = end($collected_data['result'][$i]['data']);
$name = $collected_data['result'][$i]['variable'];
if(is_array($data) && substr($data['time'], 0, 13) == date('Y-m-d H')){
$value_kw = round($data['value'], 2, PHP_ROUND_HALF_DOWN);
$sum = round(($data['value']*0.08), 2, PHP_ROUND_HALF_DOWN);
$value_kwh = round(($foxess_data['result'][$name] + $sum), 2, PHP_ROUND_HALF_DOWN);
$name = $collected_data['result'][$i]['variable'];
if(strstr($option, 'Temperature') !== false || strstr($option, 'SoC') !== false
|| strstr($option, 'Volt') !== false || strstr($option, 'Current') !== false ||
strstr($option, 'Temperation') !== false
){
$this->log($name);
if($collected_data['result'] == 'null'){
$value = 0;
}else{
$data = end($collected_data['result'][$i]['data']);
if(is_array($data) && substr($data['time'], 0, 13) == date('Y-m-d H')){
$value = abs($data['value']);
}else{
$value = 0;
}
$this->mqtt->post_mqtt('foxesscloud/'.$name, $value);
$foxess_data['result'][$name] = $value;
$this->save_to_file('data/foxess_data.json', $foxess_data);
$this->log('Post '.$value.' of '.$name.' to MQTT', 3);

}
}else{
if($collected_data['result'] == 'null'){
$value_kw = 0;
$value_kwh = $foxess_data['result'][$name];
$value_kwh = $foxess_data['result'][$i];
}else{
$data = end($collected_data['result'][$i]['data']);
$name = $collected_data['result'][$i]['variable'];
if(is_array($data) && substr($data['time'], 0, 13) == date('Y-m-d H')){
$value_kw = abs(round($data['value'], 2, PHP_ROUND_HALF_DOWN));
$sum = round(($data['value']*0.08), 2, PHP_ROUND_HALF_DOWN);
$value_kwh = abs(round(($foxess_data['result'][$name] + $sum), 2, PHP_ROUND_HALF_DOWN));
}else{
$value_kw = 0;
$value_kwh = abs($foxess_data['result'][$name]);
}
}
}
$this->mqtt->post_mqtt('foxesscloud/'.$name, $value_kw);
$this->log('Post '.$value_kw.'kw of '.$name.' to MQTT');
$this->mqtt->post_mqtt('foxesscloud/'.$name, $value_kw);
$this->log('Post '.$value_kw.'kw of '.$name.' to MQTT', 3);

$foxess_data['result'][$name] = $value_kwh;
$this->save_to_file('data/foxess_data.json', $foxess_data);
$this->mqtt->post_mqtt('foxesscloud/'.$name.'_kwh', $value_kwh);
$this->log('Post '.$value_kwh.'kwh of '.$name.' to MQTT');
$foxess_data['result'][$name] = $value_kwh;
$this->save_to_file('data/foxess_data.json', $foxess_data);
$this->mqtt->post_mqtt('foxesscloud/'.$name.'_kwh', $value_kwh);
$this->log('Post '.$value_kwh.'kwh of '.$name.' to MQTT', 3);
}
}
$this->log('Data procssed and posted to MQTT');
$this->log('Data procssed and posted to MQTT', 3);
}
}
8 changes: 4 additions & 4 deletions src/model/login.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function __construct(){
try {
$this->config = new config();
} catch (Exception $e) {
echo 'Missing config: ', $e->getMessage(), "\n";
$this->log('Missing config: '. $e->getMessage(), 1);
}
}

Expand All @@ -25,7 +25,7 @@ public function __construct(){
* @return return type
*/
public function login() {
$this->log('start of login');
$this->log('start of login', 3);
$data = '{
"user": "'.$this->config->foxess_username.'",
"password": "'.md5($this->config->foxess_password).'"
Expand Down Expand Up @@ -58,9 +58,9 @@ public function login() {
curl_close($curl);
if($return_data['errno'] > 0){
if($return_data['errno'] == 41807){
$this->log('Error: '.$return_data['errno'].', maybe check your username and password');
$this->log('Error: '.$return_data['errno'].', maybe check your username and password', 2);
}else{
$this->log('We got an error, '.$return_data['errno'].'. Dropping out of run');
$this->log('We got an error, '.$return_data['errno'].'. Dropping out of run', 2);
}
return false;
}else{
Expand Down
75 changes: 46 additions & 29 deletions src/model/mqtt.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function __construct(){
try {
$this->config = new config();
} catch (Exception $e) {
$this->log('Missing config: '. $e->getMessage());
$this->log('Missing config: '. $e->getMessage(), 1);
}


Expand All @@ -26,8 +26,45 @@ public function __construct(){
* @return return type
*/
public function setup_mqtt($foxess_data) {
$this->log('Start of MQTT setup for HA');
$this->log('Start of MQTT setup for HA', 3);
foreach($foxess_data['result'] as $name => $value){
if(strstr($name, 'Temperature') !== false || strstr($name, 'Soc') !== false ||
strstr($name, 'Temperation') !== false
){
$dev_cla = 'temperature';
$unit = '°C';
}elseif(strstr($name, 'Volt') !== false){
$dev_cla = 'voltage';
$unit = 'V';
}elseif(strstr($name, 'Current') !== false){
$dev_cla = 'current';
$unit = 'A';
}else{
$dev_cla = 'power';
$unit = 'kW';
$data_kwh = '{
"name": "foxesscloud '.$name.'_kwh",
"device": {
"identifiers": "foxesscloud",
"name": "foxesscloud",
"model": "F5000",
"manufacturer": "FoxEss"
},
"stat_t": "~'.$name.'_kwh",
"uniq_id": "foxesscloud-'.$name.'_kwh",
"~": "foxesscloud/",
"unit_of_measurement": "kWh",
"dev_cla": "energy",
"state_class": "total_increasing",
"exp_aft": 86400
}';
$this->log('Post to MQTT foxesscloud-'.$name.'_kwh', 3);
try {
$this->post_mqtt('homeassistant/sensor/foxesscloud-'.$name.'_kwh/config', $data_kwh);
} catch (\Exception $e) {
$this->log('MQTT not yet ready, need to sleep on first run maybe', 1);
}
}
$data = '{
"name": "foxesscloud '.$name.'",
"device": {
Expand All @@ -39,40 +76,20 @@ public function setup_mqtt($foxess_data) {
"stat_t": "~'.$name.'",
"uniq_id": "foxesscloud-'.$name.'",
"~": "foxesscloud/",
"unit_of_measurement": "KW",
"dev_cla": "power",
"unit_of_measurement": "'.$unit.'",
"dev_cla": "'.$dev_cla.'",
"exp_aft": 86400
}';
$this->log('Post to MQTT foxesscloud-'.$name);
$this->post_mqtt('homeassistant/sensor/foxesscloud-'.$name.'/config', $data);
$data = '{
"name": "foxesscloud '.$name.'_kwh",
"device": {
"identifiers": "foxesscloud",
"name": "foxesscloud",
"model": "F5000",
"manufacturer": "FoxEss"
},
"stat_t": "~'.$name.'_kwh",
"uniq_id": "foxesscloud-'.$name.'_kwh",
"~": "foxesscloud/",
"unit_of_measurement": "kWh",
"dev_cla": "energy",
"state_class": "total_increasing",
"exp_aft": 86400
}';
$this->log('Post to MQTT foxesscloud-'.$name.'_kwh');
$this->log('Post to MQTT foxesscloud-'.$name, 3);
try {
$this->post_mqtt('homeassistant/sensor/foxesscloud-'.$name.'_kwh/config', $data);
} catch (\Exception $e) {
echo "MQTT not yet ready, need to sleep on first run maybe";
$this->post_mqtt('homeassistant/sensor/foxesscloud-'.$name.'/config', $data);
} catch (Exception $e) {
$this->log('MQTT not yet ready, need to sleep on first run maybe', 1);
}
}
$date = new \DateTimeImmutable;
$time = $date->add(new \DateInterval("PT1H"));
// $foxess_data['setup'] = $time->format('U');
// $this->save_to_file('data/foxess_data.json', $foxess_data);
$this->log('Setup complete');
$this->log('Setup complete', 3);
return $time->format('U');
}

Expand Down
3 changes: 2 additions & 1 deletion template/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"mqtt_host": "changeme",
"mqtt_port": "changeme",
"mqtt_user": "changeme",
"mqtt_pass": "changeme"
"mqtt_pass": "changeme",
"log_level": 2
}
Loading

0 comments on commit 2c01239

Please sign in to comment.