def arm(armIt): ''' Turn on and of the alarm ''' if armIt==True: steelsquid_utils.set_flag("alarm_security") else: steelsquid_utils.del_flag("alarm_security") GLOBAL.turn_off_alarm()
def enable(argument=None): ''' When this module is enabled what needs to be done (execute: steelsquid module XXX on) argument: Send data to the enable or disable method in the module Usually a string to tell the start/stop something Maybe you need create some files or enable other stuff. ''' steelsquid_utils.set_flag("auto") steelsquid_utils.del_flag("nokia") steelsquid_utils.del_flag("hdd") steelsquid_utils.del_flag("ssd")
def enable(argument=None): ''' When this module is enabled what needs to be done (execute: steelsquid module XXX on) argument: Send data to the enable or disable method in the module Usually a string to tell the start/stop something Maybe you need create some files or enable other stuff. ''' steelsquid_utils.set_flag("auto") steelsquid_utils.del_flag("nokia") steelsquid_utils.del_flag("hdd") steelsquid_utils.del_flag("ssd")
def enable(argument=None): ''' When this module is enabled what needs to be done (execute: steelsquid module XXX on) Maybe you need create some files or enable other stuff. argument: Send data to the enable or disable method in the module Usually a string to tell the start/stop something ''' # Clear any saved settings for this module steelsquid_kiss_global.clear_modules_settings("kiss_squidrover") # Enable transeiver as client steelsquid_kiss_global.hmtrlrs_status("server") # Disable the automatic print if IP to LCD...this module will do it steelsquid_utils.set_flag("no_net_to_lcd")
def nrf24_status(status): ''' Enable nrf24 transmitter on this device Must reboot to implement status: server=Enable as server client=Enable as client master=Enable as master slave=Enable as slave None=Disable ''' if status == None: steelsquid_utils.del_flag("nrf24_server") steelsquid_utils.del_flag("nrf24_client") steelsquid_utils.del_flag("nrf24_master") steelsquid_utils.del_flag("nrf24_slave") elif status == "server": steelsquid_utils.set_flag("nrf24_server") steelsquid_utils.del_flag("nrf24_client") steelsquid_utils.del_flag("nrf24_master") steelsquid_utils.del_flag("nrf24_slave") elif status == "client": steelsquid_utils.del_flag("nrf24_server") steelsquid_utils.set_flag("nrf24_client") steelsquid_utils.del_flag("nrf24_master") steelsquid_utils.del_flag("nrf24_slave") elif status == "master": steelsquid_utils.del_flag("nrf24_server") steelsquid_utils.del_flag("nrf24_client") steelsquid_utils.set_flag("nrf24_master") steelsquid_utils.del_flag("nrf24_slave") elif status == "slave": steelsquid_utils.del_flag("nrf24_server") steelsquid_utils.del_flag("nrf24_client") steelsquid_utils.del_flag("nrf24_master") steelsquid_utils.set_flag("nrf24_slave")
def module_status(name, status, argument=None, restart=True): ''' Enable or disable a module name: Name of the mopule status: True/False argument: Send data to the enable or disable method in the module Usually a string to tell the start/stop something restart: restart the steelsquid daemon Return: Is it found ''' try: pkgpath = os.path.dirname(modules.__file__) doit = False for f in pkgutil.iter_modules([pkgpath]): if f[1] == name: doit = True if doit: try: mod = import_module('modules.' + name) if status: steelsquid_utils.set_flag("module_" + name) try: mod.enable(argument) except: steelsquid_utils.del_flag("module_" + name) steelsquid_utils.shout(string="Enable module error", is_error=True) return False else: steelsquid_utils.del_flag("module_" + name) try: mod.disable(argument) except: steelsquid_utils.set_flag("module_" + name) steelsquid_utils.shout(string="Disable module error", is_error=True) return False if restart: os.system('systemctl restart steelsquid') return True except: steelsquid_utils.shout(string="Import module error", is_error=True) return False else: return False steelsquid_utils.shout(string="Module not found") except: steelsquid_utils.shout() return False
def radio_tcp(is_the_remote, host=None): ''' Send and reseive messages with TCP (same way is with HM-TRLR-S transmitter) is_the_remote = this one send the push and request to the other is_the_remote = none, disable tcp server if host==None (this is a server, listen for connections) If host!=None this is the client (connect to server) ''' # Disable if is_the_remote == None: steelsquid_utils.del_flag("radio_tcp_server") steelsquid_utils.del_flag("radio_tcp_client") steelsquid_utils.del_flag("radio_tcp_remote") steelsquid_utils.del_parameter("radio_tcp_host") # Server elif host == None: steelsquid_utils.set_flag("radio_tcp_server") steelsquid_utils.del_flag("radio_tcp_client") if is_the_remote: steelsquid_utils.set_flag("radio_tcp_remote") else: steelsquid_utils.del_flag("radio_tcp_remote") steelsquid_utils.del_parameter("radio_tcp_host") # Client else: steelsquid_utils.del_flag("radio_tcp_server") steelsquid_utils.set_flag("radio_tcp_client") if is_the_remote: steelsquid_utils.set_flag("radio_tcp_remote") else: steelsquid_utils.del_flag("radio_tcp_remote") steelsquid_utils.set_parameter("radio_tcp_host", host)
def nrf24_status(status): ''' Enable nrf24 transmitter on this device Must reboot to implement status: server=Enable as server client=Enable as client master=Enable as master slave=Enable as slave None=Disable ''' if status==None: steelsquid_utils.del_flag("nrf24_server") steelsquid_utils.del_flag("nrf24_client") steelsquid_utils.del_flag("nrf24_master") steelsquid_utils.del_flag("nrf24_slave") elif status=="server": steelsquid_utils.set_flag("nrf24_server") steelsquid_utils.del_flag("nrf24_client") steelsquid_utils.del_flag("nrf24_master") steelsquid_utils.del_flag("nrf24_slave") elif status=="client": steelsquid_utils.del_flag("nrf24_server") steelsquid_utils.set_flag("nrf24_client") steelsquid_utils.del_flag("nrf24_master") steelsquid_utils.del_flag("nrf24_slave") elif status=="master": steelsquid_utils.del_flag("nrf24_server") steelsquid_utils.del_flag("nrf24_client") steelsquid_utils.set_flag("nrf24_master") steelsquid_utils.del_flag("nrf24_slave") elif status=="slave": steelsquid_utils.del_flag("nrf24_server") steelsquid_utils.del_flag("nrf24_client") steelsquid_utils.del_flag("nrf24_master") steelsquid_utils.set_flag("nrf24_slave")
def module_status(name, status, argument = None, restart=True): ''' Enable or disable a module name: Name of the mopule status: True/False argument: Send data to the enable or disable method in the module Usually a string to tell the start/stop something restart: restart the steelsquid daemon Return: Is it found ''' try: pkgpath = os.path.dirname(modules.__file__) doit=False for f in pkgutil.iter_modules([pkgpath]): if f[1]==name: doit=True if doit: try: mod = import_module('modules.'+name) if status: steelsquid_utils.set_flag("module_"+name) try: mod.enable(argument) except: steelsquid_utils.del_flag("module_"+name) steelsquid_utils.shout(string="Enable module error", is_error=True) return False else: steelsquid_utils.del_flag("module_"+name) try: mod.disable(argument) except: steelsquid_utils.set_flag("module_"+name) steelsquid_utils.shout(string="Disable module error", is_error=True) return False if restart: os.system('systemctl restart steelsquid') return True except: steelsquid_utils.shout(string="Import module error", is_error=True) return False else: return False steelsquid_utils.shout(string="Module not found") except: steelsquid_utils.shout() return False
def radio_hmtrlrs(is_server): ''' Send and reseive messages with HM-TRLR-S transmitter http://www.digikey.com/product-detail/en/HM-TRLR-S-868/HM-TRLR-S-868-ND/5051756 Must restart to implement is_server: None = Disable True=Enable as server False=Enable as client ''' if is_server == None: steelsquid_utils.del_flag("radio_hmtrlrs_server") steelsquid_utils.del_flag("radio_hmtrlrs_client") elif is_server: steelsquid_utils.set_flag("radio_hmtrlrs_server") steelsquid_utils.del_flag("radio_hmtrlrs_client") else: steelsquid_utils.del_flag("radio_hmtrlrs_server") steelsquid_utils.set_flag("radio_hmtrlrs_client")
def hmtrlrs_status(status): ''' Send and reseive messages with HM-TRLR-S transmitter http://www.digikey.com/product-detail/en/HM-TRLR-S-868/HM-TRLR-S-868-ND/5051756 Must reboot to implement status: server=Enable as server client=Enable as client None=Disable ''' if status==None: steelsquid_utils.del_flag("hmtrlrs_server") steelsquid_utils.del_flag("hmtrlrs_client") elif status=="server": steelsquid_utils.set_flag("hmtrlrs_server") steelsquid_utils.del_flag("hmtrlrs_client") elif status=="client": steelsquid_utils.del_flag("hmtrlrs_server") steelsquid_utils.set_flag("hmtrlrs_client")
def alarm_settings(session_id, parameters): ''' Settings of alarm ''' is_saved=False if len(parameters) > 0: if int(parameters[2]) >= int(parameters[3]): raise Exception("Alarm time must be smaller than alarm wait!") steelsquid_utils.set_parameter("alarm_security_movments", parameters[0]) steelsquid_utils.set_parameter("alarm_security_movments_seconds", parameters[1]) steelsquid_utils.set_parameter("alarm_security_seconds", parameters[2]) steelsquid_utils.set_parameter("alarm_security_wait", parameters[3]) if parameters[4]=="True": steelsquid_utils.set_flag("alarm_security_activate_siren") else: steelsquid_utils.del_flag("alarm_security_activate_siren") if parameters[5]=="True": steelsquid_utils.set_flag("alarm_security_send_mail") else: steelsquid_utils.del_flag("alarm_security_send_mail") steelsquid_utils.set_parameter("alarm_light_acivate", parameters[6]) if parameters[7]=="True": steelsquid_utils.set_flag("alarm_remote_siren") else: steelsquid_utils.del_flag("alarm_remote_siren") is_saved=True movments = steelsquid_utils.get_parameter("alarm_security_movments") movments_seconds = steelsquid_utils.get_parameter("alarm_security_movments_seconds") seconds = steelsquid_utils.get_parameter("alarm_security_seconds") wait_ = steelsquid_utils.get_parameter("alarm_security_wait") alarm_activate_siren = steelsquid_utils.get_flag("alarm_security_activate_siren") alarm_mail = steelsquid_utils.get_flag("alarm_security_send_mail") alarm_light_a = steelsquid_utils.get_parameter("alarm_light_acivate") alarm_remote_siren = steelsquid_utils.get_flag("alarm_remote_siren") return [is_saved, movments, movments_seconds, seconds, wait_, alarm_activate_siren, alarm_mail, alarm_light_a, alarm_remote_siren]
def task_event_thread(command, parameters=None): ''' execute a event ''' try: # Shutdown the system if command == "shutdown" or command == "stop": global running running = False event.set() # Enable a module elif command == "module_on": if len(parameters)>1: steelsquid_kiss_global.module_status(parameters[0], True, argument=parameters[1], restart=True) else: steelsquid_kiss_global.module_status(parameters[0], True, restart=True) # Disable a module elif command == "module_off": if len(parameters)>1: steelsquid_kiss_global.module_status(parameters[0], False, argument=parameters[1], restart=True) else: steelsquid_kiss_global.module_status(parameters[0], False, restart=True) # Set or del flag elif command == "flag": if parameters[0]=="set": steelsquid_utils.set_flag(parameters[1]) elif parameters[0]=="del": steelsquid_utils.del_flag(parameters[1]) # Set or del parameter elif command == "parameter": if parameters[0]=="set": steelsquid_utils.set_parameter(parameters[1], parameters[2]) elif parameters[0]=="del": steelsquid_utils.del_parameter(parameters[1]) # Reload modules elif command == "reload": if parameters!=None: for name, obj in steelsquid_kiss_global.loaded_modules.iteritems(): if name == parameters[0]: thread.start_new_thread(reload_file_dyn, (obj,)) else: for obj in steelsquid_kiss_global.loaded_modules.itervalues(): thread.start_new_thread(reload_file_dyn, (obj,)) # Execute a network event so the IP is shown execute_task_event("network") # Shout elif command == "shout": steelsquid_utils.shout(" ".join(parameters)) # VPN event elif command == "vpn": if parameters[0] == "up": name = "unknown" try: name = steelsquid_nm.vpn_configured() except: pass ip = steelsquid_utils.network_ip_vpn() steelsquid_kiss_global._execute_all_modules("SYSTEM", "on_vpn", (True, name, ip,)) steelsquid_utils.shout("VPN ENABLED\n"+name + "\nIP\n" + ip) else: steelsquid_utils.shout("VPN DISABLED") steelsquid_kiss_global._execute_all_modules("SYSTEM", "on_vpn", (False, None, None,)) # Network event elif command == "network": wired = steelsquid_utils.network_ip_wired() wifi = steelsquid_utils.network_ip_wifi() wan = "---" net = "---" access = "---" if wired == "---" and wifi == "---": net = "down" else: net = "up" wan = steelsquid_utils.network_ip_wan() if wifi != "---": try: access = steelsquid_nm.get_connected_access_point_name() except: pass on_network(net, wired, wifi, access, wan) elif command == "mount": the_type = parameters[0] the_remote = parameters[1] the_local = parameters[2] steelsquid_utils.shout("MOUNT %s\n%s\nTO\n%s" %(the_type, the_remote, the_local)) steelsquid_kiss_global._execute_all_modules("SYSTEM", "on_mount", (the_type, the_remote, the_local)) elif command == "umount": the_type = parameters[0] the_remote = parameters[1] the_local = parameters[2] steelsquid_utils.shout("UMOUNT %s\n%s\nFROM\n%s" %(the_type, the_remote, the_local)) steelsquid_kiss_global._execute_all_modules("SYSTEM", "on_umount", (the_type, the_remote, the_local)) # Try to broadcast the event to alla modules else: steelsquid_kiss_global._execute_all_modules("EVENTS", command, parameters) except: steelsquid_utils.shout()
def _cleanup(): ''' Clean ''' global running running = False event.set() try: for obj in steelsquid_kiss_global.loaded_modules.itervalues(): class_system = steelsquid_kiss_global._get_object(obj, "SYSTEM") if class_system!=None: method_event = steelsquid_kiss_global._get_object(class_system, "on_event_data") if method_event!=None: steelsquid_kiss_global.remove_event_data_callback(method_event) class_events = steelsquid_kiss_global._get_object(obj, "EVENTS") if class_events!=None: steelsquid_kiss_global.remove_broadcast_event_callback(class_events) if class_system!=None: steelsquid_kiss_global._exec_method_set_started(class_system, "on_stop", is_started=False) class_settings = steelsquid_kiss_global._get_object(obj, "SETTINGS") if class_settings!=None: persistent_off=False try: persistent_off = getattr(class_settings, "_persistent_off")==True except: pass if not persistent_off: members = [attr for attr in dir(class_settings) if not callable(getattr(class_settings, attr)) and not attr.startswith("_")] for var_name in members: the_var = getattr(class_settings, var_name, None) if isinstance(the_var, (bool)): if the_var: steelsquid_utils.set_flag(var_name) else: steelsquid_utils.del_flag(var_name) elif isinstance(the_var, list): steelsquid_utils.set_list(var_name, the_var) else: steelsquid_utils.set_parameter(var_name, str(the_var)) except: steelsquid_utils.shout() if steelsquid_utils.get_flag("nrf24_master") or steelsquid_utils.get_flag("nrf24_slave"): try: steelsquid_nrf24.stop() except: steelsquid_utils.shout() if steelsquid_utils.get_flag("hmtrlrs_server") or steelsquid_utils.get_flag("hmtrlrs_client"): try: steelsquid_hmtrlrs.stop() except: steelsquid_utils.shout() if steelsquid_utils.get_flag("socket_connection"): try: steelsquid_kiss_global.socket_connection.stop() except: pass if steelsquid_utils.get_flag("web"): try: steelsquid_kiss_global.http_server.stop_server() except: pass steelsquid_utils.execute_system_command_blind(['killall', 'aria2c']) steelsquid_utils.shout("Goodbye :-(")
def reload_file_dyn(obj): ''' Reload custom module ''' try: event.set() steelsquid_utils.shout("Reload module: " + obj.__name__) class_system = steelsquid_kiss_global._get_object(obj, "SYSTEM") if class_system!=None: method_event = steelsquid_kiss_global._get_object(class_system, "on_event_data") if method_event!=None: steelsquid_kiss_global.remove_event_data_callback(method_event) class_events = steelsquid_kiss_global._get_object(obj, "EVENTS") if class_events!=None: steelsquid_kiss_global.remove_broadcast_event_callback(class_events) if class_system!=None: steelsquid_kiss_global._exec_method_set_started(class_system, "on_stop", is_started=False) class_settings = steelsquid_kiss_global._get_object(obj, "SETTINGS") if class_settings!=None: persistent_off=False try: persistent_off = getattr(class_settings, "_persistent_off")==True except: pass if not persistent_off: members = [attr for attr in dir(class_settings) if not callable(getattr(class_settings, attr)) and not attr.startswith("_")] for var_name in members: the_var = getattr(class_settings, var_name, None) if isinstance(the_var, (bool)): if the_var: steelsquid_utils.set_flag(var_name) else: steelsquid_utils.del_flag(var_name) elif isinstance(the_var, list): steelsquid_utils.set_list(var_name, the_var) else: steelsquid_utils.set_parameter(var_name, str(the_var)) reload(obj) class_settings = steelsquid_kiss_global._get_object(obj, "SETTINGS") if class_settings!=None: persistent_off=False try: persistent_off = getattr(class_settings, "_persistent_off")==True except: pass if not persistent_off: members = [attr for attr in dir(class_settings) if not callable(getattr(class_settings, attr)) and not attr.startswith("_")] for var_name in members: the_var = getattr(class_settings, var_name, None) if isinstance(the_var, (bool)): the_var = steelsquid_utils.get_flag(var_name) setattr(class_settings, var_name, the_var) elif isinstance(the_var, list): the_var = steelsquid_utils.get_list(var_name, the_var) setattr(class_settings, var_name, the_var) elif isinstance(the_var, int): the_var = steelsquid_utils.get_parameter(var_name, str(the_var)) setattr(class_settings, var_name, int(the_var)) elif isinstance(the_var, float): the_var = steelsquid_utils.get_parameter(var_name, str(the_var)) setattr(class_settings, var_name, float(the_var)) else: the_var = steelsquid_utils.get_parameter(var_name, str(the_var)) setattr(class_settings, var_name, the_var) class_system = steelsquid_kiss_global._get_object(obj, "SYSTEM") if class_system!=None: steelsquid_kiss_global._exec_method_set_started(class_system, "on_start", is_started=True) method_event = steelsquid_kiss_global._get_object(class_system, "on_event_data") if method_event!=None: steelsquid_kiss_global.add_event_data_callback(method_event) class_events = steelsquid_kiss_global._get_object(obj, "EVENTS") if class_events!=None: steelsquid_kiss_global.add_broadcast_event_callback(class_events) class_loop = steelsquid_kiss_global._get_object(obj, "LOOP") event.clear() if class_loop!=None: for ob in vars(class_loop): m = getattr(class_loop, ob, None) if callable(m): thread.start_new_thread(do_on_loop, (m,)) except: steelsquid_utils.shout("Fatal error when reload module: " + obj.__name__, is_error=True)