def test(self): from tentsensord.cli import load_logic_module from tentsensord.common import load_config import json load_config(json.loads(config_content)) self.assertRaises(FileNotFoundError, load_logic_module)
def sighup_handler(signum, frame): """ reload configuration file and logic module """ try: common.load_config() load_logic_module() print('SIGHUP: Reloading config from', common.config_file) except Exception: common.gw_thread.stop() raise
def test_get_child_name_by_id(self): from tentsensord.common import load_config from tentsensord.cli import child_name_by_id from tentsensord import common import json load_config(json.loads(config_content)) self.assertEqual(child_name_by_id(common.config['child_map']["Hum1"]), "Hum1")
def test(self): from tentsensord.cli import update_child_state from tentsensord.common import load_config from tentsensord import common import json load_config(json.loads(config_content)) update_child_state(common.config['child_map']["Hum1"], 1) self.assertEqual(len(common.current_state), 1) self.assertTrue("Hum1" in common.current_state) self.assertEqual(common.current_state["Hum1"]["value"], '1')
def main(config_dict=None): if len(sys.argv) != 2: print("Usage: %s <config file>" % (sys.argv[0], )) sys.exit(2) """ load common.config_file """ common.config_file = sys.argv[1] common.load_config() """ setup posix signal handling """ signal.signal(signal.SIGHUP, sighup_handler) signal.signal(signal.SIGTERM, sigterm_handler) print("Registered signal handlers") load_logic_module() """ open the serial interface and start gw thread """ common.init_gw_thread(message_handler) common.gw_thread.start() print("Mysensors gateway started") """ give a few seconds for gw to startup """ print("Sleeping for a few seconds") time.sleep(5) """ set children initial state """ for device in common.config['child_map'].keys(): if device not in common.current_state: operations.turn_off(device) else: value = common.current_state[device]['value'] if value == '1': operations.turn_on(device) if value == '0': operations.turn_off(device) """ start http interface """ conf = {'/': {'request.dispatch': cherrypy.dispatch.MethodDispatcher()}} cherrypy.tree.mount(DeviceWebService(), '/device', conf) cherrypy.tree.mount(ControlWebService(), '/control', conf) cherrypy.engine.start() print("Started HTTP interface") """ run logic while gw is active """ while common.gw_thread.is_alive(): if common.logic_enabled: logic.run() if 'influxdb_uri' in common.config: data = '' for k, v in common.current_state.items(): data += "%s,host=localhost value=%s\n" % (k, v['value']) url = "%(influxdb_uri)s/write?db=%(influxdb_db)s" % common.config requests.post(url, data=data) time.sleep(common.config['loop_sleep']) """ clean up """ print("Cleaning up") cherrypy.engine.stop()
def test_device_toggle(self): from tentsensord.common import load_config from tentsensord.cli import child_name_by_id from tentsensord import common from tentsensord import operations import json load_config(json.loads(config_content)) self.assertIsNone(operations.device_value("Extractor")) operations.turn_on("Extractor") self.assertEqual(operations.device_value("Extractor"), '1') operations.turn_off("Extractor") self.assertEqual(operations.device_value("Extractor"), '0') operations.toggle("Extractor") self.assertEqual(operations.device_value("Extractor"), '1')