# Path: config.py
# def editbatconfig(section,item,itemvalue):
# def loadconfig():


from paho.mqtt import client as mqtt_client
from time import sleep
from config import editbatconfig, config
import logger
# *****BatteryMonitor MQTT client*****
# Copyright (C) 2021 Simon Richard Matthews
# Project loaction https://github.com/simat/BatteryMonitor
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

log = logger.logging.getLogger(__name__)
log.setLevel(logger.logging.DEBUG)
log.addHandler(logger.errfile)


def on_message(client,userata,message):
  """Executed when any subscribed MQTT message arives
  At present assumes payload is item to change in battery.config
  makes change and rewrites config file"""

  payload=eval(str(message.payload.decode("utf-8")))
  print (payload)
  for section in payload:
    for item in payload[section]:
#      print (section,item,payload[section][item])

# Path: config.py
# def editbatconfig(section,item,itemvalue):
# def loadconfig():


# Path: mqtt.py
from paho.mqtt import client as mqtt_client
from time import sleep
from config import editbatconfig, config
import logger

# *****BatteryMonitor MQTT client*****
# Copyright (C) 2021 Simon Richard Matthews
# Project loaction https://github.com/simat/BatteryMonitor
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

log = logger.logging.getLogger(__name__)
log.setLevel(logger.logging.DEBUG)
log.addHandler(logger.errfile)


def on_message(client,userata,message):
  """Executed when any subscribed MQTT message arives
  At present assumes payload is item to change in battery.config
  makes change and rewrites config file"""

  payload=eval(str(message.payload.decode("utf-8")))
  print (payload)
  for section in payload:
    for item in payload[section]:
#      print (section,item,payload[section][item])

      editbatconfig(section,item,payload[section][item])