def POST(self): logger.debug('POST ' + self.__class__.__name__) action = web.input().get('do', '').lower() if action == 'reboot': logger.info('System reboot requested via API') helpers.reboot() elif action == 'restart': logger.info('OSPy service restart requested via API') helpers.restart() elif action == 'poweroff': logger.info('System poweroff requested via API') helpers.poweroff() else: logger.error('Unknown system action: "%s"', action) raise badrequest()
def run(self): log.clear(NAME) while not self._stop.is_set(): try: if plugin_options['use_button']: # if button plugin is enabled actual_buttons = read_buttons() #led_outputs(actual_buttons) for i in range(8): tb = "button" + str(i) if actual_buttons == i and plugin_options[tb]== "reboot": log.info(NAME, datetime_string() + ': ' + _('System reboot')) stations.clear() reboot() self._sleep(2) if actual_buttons == i and plugin_options[tb]== "pwrOff": log.info(NAME, datetime_string() + ': ' + _('System shutdown')) stations.clear() poweroff() self._sleep(2) if actual_buttons == i and plugin_options[tb]== "stopAll": log.info(NAME, datetime_string() + ': ' + _('All stations now stopped')) log.finish_run(None) # save log stations.clear() # set all station to off self._sleep(2) if actual_buttons == i and plugin_options[tb]== "schedEn": log.info(NAME, datetime_string() + ': ' + _('Scheduler is now enabled')) options.scheduler_enabled = True self._sleep(2) if actual_buttons == i and plugin_options[tb]== "schedDis": log.info(NAME, datetime_string() + ': ' + _('Scheduler is now disabled')) options.scheduler_enabled = False self._sleep(2) if actual_buttons == i and plugin_options[tb]== "RunP1": log.info(NAME, datetime_string() + ': ' + _('Run now program 1')) for program in programs.get(): if (program.index == 0): # Run-now program 1 options.manual_mode = False log.finish_run(None) stations.clear() programs.run_now(program.index) program.index+1 self._sleep(2) if actual_buttons == i and plugin_options[tb]== "RunP2": log.info(NAME, datetime_string() + ': ' + _('Run now program 2')) for program in programs.get(): if (program.index == 1): # Run-now program 2 options.manual_mode = False log.finish_run(None) stations.clear() programs.run_now(program.index) program.index+1 self._sleep(2) if actual_buttons == i and plugin_options[tb]== "RunP3": log.info(NAME, datetime_string() + ': ' + _('Run now program 3')) for program in programs.get(): if (program.index == 2): # Run-now program 3 options.manual_mode = False log.finish_run(None) stations.clear() programs.run_now(program.index) program.index+1 self._sleep(2) if actual_buttons == i and plugin_options[tb]== "RunP4": log.info(NAME, datetime_string() + ': ' + _('Run now program 4')) for program in programs.get(): if (program.index == 3): # Run-now program 4 options.manual_mode = False log.finish_run(None) stations.clear() programs.run_now(program.index) program.index+1 self._sleep(2) if actual_buttons == i and plugin_options[tb]== "RunP5": log.info(NAME, datetime_string() + ': ' + _('Run now program 5')) for program in programs.get(): if (program.index == 4): # Run-now program 5 options.manual_mode = False log.finish_run(None) stations.clear() programs.run_now(program.index) program.index+1 self._sleep(2) if actual_buttons == i and plugin_options[tb]== "RunP6": log.info(NAME, datetime_string() + ': ' + _('Run now program 6')) for program in programs.get(): if (program.index == 5): # Run-now program 6 options.manual_mode = False log.finish_run(None) stations.clear() programs.run_now(program.index) program.index+1 self._sleep(2) if actual_buttons == i and plugin_options[tb]== "RunP7": log.info(NAME, datetime_string() + ': ' + _('Run now program 7')) for program in programs.get(): if (program.index == 6): # Run-now program 7 options.manual_mode = False log.finish_run(None) stations.clear() programs.run_now(program.index) program.index+1 self._sleep(2) if actual_buttons == i and plugin_options[tb]== "RunP8": log.info(NAME, datetime_string() + ': ' + _('Run now program 8')) for program in programs.get(): if (program.index == 7): # Run-now program 8 options.manual_mode = False log.finish_run(None) stations.clear() programs.run_now(program.index) program.index+1 self._sleep(2) self._sleep(1) except Exception: log.clear(NAME) log.error(NAME, _('Button plug-in') + ':\n' + traceback.format_exc()) self._sleep(60) pass
def run(self): reboot_time = False once = True once_two = True once_three = True last_time = int(time.time()) while not self._stop.is_set(): try: if ups_options['ups']: # if ups plugin is enabled test = get_check_power() if not test: text = _('OK') else: text = _('FAULT') self.status['power%d'] = text if not test: last_time = int(time.time()) if test: # if power line is not active reboot_time = True # start countdown timer if once: msg = _('UPS plugin detected fault on power line.') # send email with info power line fault log.info(NAME, msg) if ups_options['sendeml']: # if enabled send email send_email(msg) once_three = True once = False if reboot_time and test: count_val = int(ups_options['time']) * 60 # value for countdown actual_time = int(time.time()) log.clear(NAME) log.info(NAME, _('Time to shutdown') + ': ' + str(count_val - (actual_time - last_time)) + ' ' + _('sec')) if ((actual_time - last_time) >= count_val): # if countdown is 0 last_time = actual_time test = get_check_power() if test: # if power line is current not active log.clear(NAME) log.info(NAME, _('Power line is not restore in time -> sends email and shutdown system.')) reboot_time = False if ups_options['sendeml']: # if enabled send email if once_two: msg = _('UPS plugin - power line is not restore in time -> shutdown system!') # send email with info shutdown system send_email(msg) once_two = False GPIO.output(pin_ups_down, GPIO.HIGH) # switch on GPIO fo countdown UPS battery power off self._sleep(4) GPIO.output(pin_ups_down, GPIO.LOW) poweroff(1, True) # shutdown system if not test: if once_three: if ups_options['sendeml']: # if enabled send email msg = _('UPS plugin - power line has restored - OK.') log.clear(NAME) log.info(NAME, msg) send_email(msg) once = True once_two = True once_three = False self._sleep(1) except Exception: log.error(NAME, _('UPS plug-in') + ': \n' + traceback.format_exc()) self._sleep(60)
def POST(self): qdict = web.input() change = False # if change language -> restart ospy if 'lang' in qdict and qdict['lang']: if options.lang != qdict['lang']: change = True qdict['name'] = qdict['name'] qdict['location'] = qdict['location'] qdict['darksky_key'] = qdict['darksky_key'] qdict['HTTP_web_ip'] = qdict['HTTP_web_ip'] save_to_options(qdict) if 'master' in qdict: m = int(qdict['master']) if m < 0: stations.master = None elif m < stations.count(): stations.master = m if 'master_two' in qdict: m = int(qdict['master_two']) if m < 0: stations.master_two = None elif m < stations.count(): stations.master_two = m if 'old_password' in qdict and qdict['old_password'] != "": try: if test_password(qdict['old_password']): if qdict['new_password'] == "": raise web.seeother('/options?errorCode=pw_blank') elif qdict['new_password'] == qdict['check_password']: options.password_salt = password_salt() # Make a new salt options.password_hash = password_hash(qdict['new_password'], options.password_salt) else: raise web.seeother('/options?errorCode=pw_mismatch') else: raise web.seeother('/options?errorCode=pw_wrong') except KeyError: pass if 'rbt' in qdict and qdict['rbt'] == '1': report_rebooted() reboot(True) # Linux HW software return self.core_render.home() if 'rstrt' in qdict and qdict['rstrt'] == '1': report_restarted() restart() # OSPy software return self.core_render.restarting(home_page) if 'pwrdwn' in qdict and qdict['pwrdwn'] == '1': poweroff() # shutdown HW system return self.core_render.restarting(home_page) if 'deldef' in qdict and qdict['deldef'] == '1': OPTIONS_FILE = './ospy/data' try: import shutil, time shutil.rmtree(OPTIONS_FILE) # delete data folder time.sleep(2) os.makedirs(OPTIONS_FILE) # create data folder report_restarted() restart() # restart OSPy software return self.core_render.restarting(home_page) except: pass if change: report_restarted() restart() # OSPy software return self.core_render.restarting(home_page) report_option_change() raise web.seeother('/')
def sms_check(self): """Control and processing SMS""" try: import gammu except Exception: log.error(NAME, _('SMS Modem plug-in') + ':\n' + traceback.format_exc()) tel1 = sms_options['tel1'] tel2 = sms_options['tel2'] comm1 = sms_options['txt1'] comm2 = sms_options['txt2'] comm3 = sms_options['txt3'] comm4 = sms_options['txt4'] comm5 = sms_options['txt5'] comm6 = sms_options['txt6'] comm7 = sms_options['txt7'] comm8 = sms_options['txt8'] comm9 = sms_options['txt9'] sm = gammu.StateMachine() sm.ReadConfig() try: sm.Init() log.debug(NAME, datetime_string() + ': ' + _('Checking SMS...')) except: log.error(NAME, _('SMS Modem plug-in') + ':\n' + traceback.format_exc()) self._sleep(60) if sms_options[ "use_strength"]: # print strength signal in status Window every check SMS signal = sm.GetSignalQuality( ) # list: SignalPercent, SignalStrength, BitErrorRate log.info( NAME, datetime_string() + ': ' + _('Signal') + ': ' + str(signal['SignalPercent']) + '% ' + str(signal['SignalStrength']) + 'dB') status = sm.GetSMSStatus() remain = status['SIMUsed'] + status['PhoneUsed'] + status['TemplatesUsed'] sms = [] start = True while remain > 0: if start: cursms = sm.GetNextSMS(Start=True, Folder=0) start = False else: cursms = sm.GetNextSMS(Location=cursms[0]['Location'], Folder=0) remain = remain - len(cursms) sms.append(cursms) data = gammu.LinkSMS(sms) for x in data: v = gammu.DecodeSMS(x) m = x[0] print '%-15s: %s' % ('Sender', m['Number']) print '%-15s: %s' % ('Date', str(m['DateTime'])) print '%-15s: %s' % ('State', m['State']) print '%-15s: %s' % ('SMS command', m['Text']) if (m['Number'] == tel1) or (m['Number'] == tel2): # If telephone is admin 1 or admin 2 log.info(NAME, datetime_string() + ': ' + _('SMS from admin')) if m['State'] == "UnRead": # If SMS is unread log.clear(NAME) if m['Text'] == comm1: # If command = comm1 (info - send SMS to admin phone1 and phone2) log.info( NAME, _('Command') + ' ' + comm1 + ' ' + _('is processed')) # send 1/2 SMS with text 1 up = helpers.uptime() temp = helpers.get_cpu_temp( options.temp_unit) + ' ' + options.temp_unit ip = str(helpers.get_ip()) ver = version.ver_date datastr = ('SMS 1/2. ' + datetime_string() + ', TEMP: ' + temp + ', IP: ' + ip + ', SW: ' + ver + ', UP: ' + up) message = { 'Text': datastr, 'SMSC': { 'Location': 1 }, 'Number': m['Number'], } sm.SendSMS(message) # send sms 1/2 log.info(NAME, datastr) # send 2/2 SMS with text 2 if inputs.rain_sensed(): rain = "Active" else: rain = "Inactive" try: from plugins import pressure_monitor state_press = pressure_monitor.get_check_pressure() if state_press: press = "High" else: press = "Low" except Exception: press = "N/A" finished = [ run for run in log.finished_runs() if not run['blocked'] ] if finished: last_prog = finished[-1]['start'].strftime( '%H:%M: ') + finished[-1]['program_name'] else: last_prog = 'None' datastr = ('SMS 2/2. ' + 'RAIN: ' + rain + ', PRESS: ' + press + ', LAST: ' + last_prog) message = { 'Text': datastr, 'SMSC': { 'Location': 1 }, 'Number': m['Number'], } sm.SendSMS(message) # send sms 2/2 log.info(NAME, datastr) log.info( NAME, _('Command') + ': ' + comm1 + ' ' + _('was processed and confirmation was sent as SMS to') + ': ' + m['Number']) sm.DeleteSMS(m['Folder'], m['Location']) # SMS deleted log.info(NAME, _('Received SMS was deleted')) elif m['Text'] == comm2: # If command = comm2 (stop - scheduler) log.info( NAME, _('Command') + ' ' + comm2 + ' ' + _('is processed')) options.scheduler_enabled = False log.finish_run(None) stations.clear() message = { 'Text': 'Command: ' + comm2 + ' was processed', 'SMSC': { 'Location': 1 }, 'Number': m['Number'], } sm.SendSMS(message) log.info( NAME, _('Command') + ': ' + comm2 + ' ' + _('was processed and confirmation was sent as SMS to') + ': ' + m['Number']) sm.DeleteSMS(m['Folder'], m['Location']) log.info(NAME, _('Received SMS was deleted')) elif m['Text'] == comm3: # If command = comm3 (start - scheduler) log.info( NAME, _('Command') + ' ' + comm3 + ' ' + _('is processed')) options.scheduler_enabled = True message = { 'Text': 'Command: ' + comm3 + ' was processed', 'SMSC': { 'Location': 1 }, 'Number': m['Number'], } sm.SendSMS(message) log.info( NAME, _('Command') + ': ' + comm3 + ' ' + _('was processed and confirmation was sent as SMS to') + ': ' + m['Number']) sm.DeleteSMS(m['Folder'], m['Location']) log.info(NAME, _('Received SMS was deleted')) elif m['Text'] == comm4: # If command = comm4 (reboot system) log.info( NAME, _('Command') + ' ' + comm4 + ' ' + _('is processed')) message = { 'Text': 'Command: ' + comm4 + ' was processed', 'SMSC': { 'Location': 1 }, 'Number': m['Number'], } sm.SendSMS(message) log.info( NAME, _('Command') + ': ' + comm4 + ' ' + _('was processed and confirmation was sent as SMS to') + ': ' + m['Number']) sm.DeleteSMS(m['Folder'], m['Location']) log.info( NAME, _('Received SMS was deleted and system is now reboot')) reboot() # restart linux system elif m['Text'] == comm5: # If command = comm5 (poweroff system) log.info( NAME, _('Command') + ' ' + comm5 + ' ' + _('is processed')) message = { 'Text': 'Command: ' + comm5 + ' was processed', 'SMSC': { 'Location': 1 }, 'Number': m['Number'], } sm.SendSMS(message) log.info( NAME, _('Command') + ': ' + comm5 + ' ' + _('was processed and confirmation was sent as SMS to') + ': ' + m['Number']) sm.DeleteSMS(m['Folder'], m['Location']) log.info( NAME, _('Received SMS was deleted and system is now poweroff' )) poweroff() # poweroff linux system elif m['Text'] == comm6: # If command = comm6 (update ospi system) log.info( NAME, _('Command') + ' ' + comm6 + ' ' + _('is processed')) message = { 'Text': 'Command: ' + comm6 + ' was processed', 'SMSC': { 'Location': 1 }, 'Number': m['Number'], } sm.SendSMS(message) log.info( NAME, _('Command') + ': ' + comm6 + ' ' + _('was processed and confirmation was sent as SMS to') + ': ' + m['Number']) try: from plugins.system_update import perform_update perform_update() log.info( NAME, _('Received SMS was deleted, update was performed and program will restart' )) except ImportError: log.info( NAME, _('Received SMS was deleted, but could not perform update' )) sm.DeleteSMS(m['Folder'], m['Location']) elif m['Text'] == comm7: # If command = comm7 (send email with foto from webcam) log.info( NAME, _('Command') + ' ' + comm7 + ' ' + _('is processed')) message = { 'Text': 'Command: ' + comm7 + ' was processed', 'SMSC': { 'Location': 1 }, 'Number': m['Number'], } sm.SendSMS(message) log.info( NAME, _('Command') + ': ' + comm7 + ' ' + _('was processed and confirmation was sent as SMS to') + ': ' + m['Number']) try: from plugins.webcam import get_run_cam, get_image_location get_run_cam() # process save foto to ./data/image.jpg msg = _('SMS plug-in send image file from webcam.') send_email(msg, attach=get_image_location()) except ImportError: log.info( NAME, _('Received SMS was deleted, but could not send email with photo from webcam' )) message = { 'Text': 'Error: not send foto from webcam', 'SMSC': { 'Location': 1 }, 'Number': m['Number'], } sm.SendSMS(message) sm.DeleteSMS(m['Folder'], m['Location']) elif m['Text'] == comm8: # If command = comm8 (send SMS with available commands) log.info( NAME, _('Command') + ' ' + comm8 + ' ' + _('is processed')) message = { 'Text': 'Available commands: ' + comm1 + ',' + comm2 + ',' + comm3 + ',' + comm4 + ',' + comm5 + ',' + comm6 + ',' + comm7 + ',' + comm8 + ',' + comm9 + 'xx', 'SMSC': { 'Location': 1 }, 'Number': m['Number'], } sm.SendSMS(message) log.info( NAME, _('Command') + ': ' + comm8 + ' ' + _('was processed and confirmation was sent as SMS to') + ': ' + m['Number']) sm.DeleteSMS(m['Folder'], m['Location']) log.info(NAME, _('Received SMS was deleted')) elif m['Text'][0:len( comm9 )] == comm9: # If command = lenght char comm9 (run now program xx) num = m['Text'][len( comm9 ):] # number from sms text example: run36 -> num=36 log.info( NAME, _('Command') + ' ' + comm9 + ' ' + _('is processed')) index = int(num) if index <= programs.count( ): # if program number from sms text exists in program db log.finish_run(None) stations.clear() prog = int(index - 1) programs.run_now(prog) log.info( NAME, _('Program') + ': ' + str(index) + ' ' + _('now run')) message = { 'Text': 'Program: ' + str(index) + ' now run', 'SMSC': { 'Location': 1 }, 'Number': m['Number'], } else: message = { 'Text': 'Program: ' + str(index) + ' no exists!', 'SMSC': { 'Location': 1 }, 'Number': m['Number'], } sm.SendSMS(message) log.info( NAME, _('Command') + ': ' + str(m['Text']) + ' ' + _('was processed and confirmation was sent as SMS to') + ': ' + m['Number']) sm.DeleteSMS(m['Folder'], m['Location']) log.info(NAME, _('Received SMS was deleted')) else: # If SMS command is not defined sm.DeleteSMS(m['Folder'], m['Location']) log.info( NAME, _('Received command') + ' ' + m['Text'] + ' ' + _('is not defined!')) else: # If SMS was read sm.DeleteSMS(m['Folder'], m['Location']) log.info(NAME, _('Received SMS was deleted - SMS was read')) else: # If telephone number is not admin 1 or admin 2 phone number sm.DeleteSMS(m['Folder'], m['Location']) log.info(NAME, _('Received SMS was deleted - SMS was not from admin'))
def run(self): log.clear(NAME) disable_text = True while not self._stop_event.is_set(): try: if plugin_options['use_button']: # if button plugin is enabled disable_text = True actual_buttons = read_buttons() # led_outputs(actual_buttons) # for test for i in range(8): tb = "button" + str(i) if actual_buttons == i and plugin_options[ tb] == "reboot": log.info( NAME, datetime_string() + ': ' + _(u'System reboot')) stations.clear() report_rebooted() reboot(block=True) # Linux HW software self._sleep(1) if actual_buttons == i and plugin_options[ tb] == "pwrOff": log.info( NAME, datetime_string() + ': ' + _(u'System shutdown')) stations.clear() report_poweroff() poweroff(wait=10, block=True) # shutdown HW system self._sleep(1) if actual_buttons == i and plugin_options[ tb] == "stopAll": log.info( NAME, datetime_string() + ': ' + _(u'Selected stations now stopped')) set_stations_in_scheduler_off() self._sleep(1) if actual_buttons == i and plugin_options[ tb] == "schedEn": log.info( NAME, datetime_string() + ': ' + _(u'Scheduler is now enabled')) options.scheduler_enabled = True self._sleep(1) if actual_buttons == i and plugin_options[ tb] == "schedDis": log.info( NAME, datetime_string() + ': ' + _(u'Scheduler is now disabled')) options.scheduler_enabled = False self._sleep(1) if actual_buttons == i and plugin_options[ tb] == "RunP1": log.info( NAME, datetime_string() + ': ' + _(u'Run now program 1')) for program in programs.get(): if (program.index == 0): # Run-now program 1 options.manual_mode = False if plugin_options['first_stop']: log.finish_run(None) stations.clear() programs.run_now(program.index) program.index + 1 self._sleep(1) if actual_buttons == i and plugin_options[ tb] == "RunP2": log.info( NAME, datetime_string() + ': ' + _(u'Run now program 2')) for program in programs.get(): if (program.index == 1): # Run-now program 2 options.manual_mode = False if plugin_options['first_stop']: log.finish_run(None) stations.clear() programs.run_now(program.index) program.index + 1 self._sleep(1) if actual_buttons == i and plugin_options[ tb] == "RunP3": log.info( NAME, datetime_string() + ': ' + _(u'Run now program 3')) for program in programs.get(): if (program.index == 2): # Run-now program 3 options.manual_mode = False if plugin_options['first_stop']: log.finish_run(None) stations.clear() programs.run_now(program.index) program.index + 1 self._sleep(1) if actual_buttons == i and plugin_options[ tb] == "RunP4": log.info( NAME, datetime_string() + ': ' + _(u'Run now program 4')) for program in programs.get(): if (program.index == 3): # Run-now program 4 options.manual_mode = False if plugin_options['first_stop']: log.finish_run(None) stations.clear() programs.run_now(program.index) program.index + 1 self._sleep(1) if actual_buttons == i and plugin_options[ tb] == "RunP5": log.info( NAME, datetime_string() + ': ' + _('Run now program 5')) for program in programs.get(): if (program.index == 4): # Run-now program 5 options.manual_mode = False if plugin_options['first_stop']: log.finish_run(None) stations.clear() programs.run_now(program.index) program.index + 1 self._sleep(1) if actual_buttons == i and plugin_options[ tb] == "RunP6": log.info( NAME, datetime_string() + ': ' + _(u'Run now program 6')) for program in programs.get(): if (program.index == 5): # Run-now program 6 options.manual_mode = False if plugin_options['first_stop']: log.finish_run(None) stations.clear() programs.run_now(program.index) program.index + 1 self._sleep(1) if actual_buttons == i and plugin_options[ tb] == "RunP7": log.info( NAME, datetime_string() + ': ' + _(u'Run now program 7')) for program in programs.get(): if (program.index == 6): # Run-now program 7 options.manual_mode = False if plugin_options['first_stop']: log.finish_run(None) stations.clear() programs.run_now(program.index) program.index + 1 self._sleep(1) if actual_buttons == i and plugin_options[ tb] == "RunP8": log.info( NAME, datetime_string() + ': ' + _(u'Run now program 8')) for program in programs.get(): if (program.index == 7): # Run-now program 8 options.manual_mode = False if plugin_options['first_stop']: log.finish_run(None) stations.clear() programs.run_now(program.index) program.index + 1 self._sleep(1) self._sleep(1) else: # text on the web if plugin is disabled if disable_text: log.clear(NAME) log.info(NAME, _(u'Button plug-in is disabled.')) disable_text = False self._sleep(1) except Exception: log.clear(NAME) log.error( NAME, _(u'Button plug-in') + ':\n' + traceback.format_exc()) self._sleep(60) pass
def run(self): reboot_time = False once = True once_two = True once_three = True last_time = int(time.time()) ups_mon = None if ups_options['use_footer']: ups_mon = showInFooter() # instantiate class to enable data in footer ups_mon.button = "ups_adj/settings" # button redirect on footer ups_mon.label = _(u'UPS') # label on footer while not self._stop_event.is_set(): try: if ups_options['ups']: # if ups plugin is enabled test = get_check_power() if not test: text = _(u'OK') else: text = _(u'FAULT') self.status['power%d'] = text if ups_options['use_footer']: if ups_mon is not None: ups_mon.val = text.encode('utf8').decode('utf8') # value on footer if not test: last_time = int(time.time()) if test: # if power line is not active reboot_time = True # start countdown timer if once: # send email with info power line fault msg = '<b>' + _(u'UPS plug-in') + '</b> ' + '<br><p style="color:red;">' + _(u'Detected fault on power line.') + '</p>' msglog = _(u'UPS plug-in') + ': ' + _(u'Detected fault on power line.') log.info(NAME, msglog) if ups_options['sendeml']: # if enabled send email try: try_mail = None if ups_options['eplug']==0: # email_notifications from plugins.email_notifications import try_mail if ups_options['eplug']==1: # email_notifications SSL from plugins.email_notifications_ssl import try_mail if try_mail is not None: try_mail(msg, msglog, attachment=None, subject=ups_options['emlsubject']) # try_mail(text, logtext, attachment=None, subject=None) except Exception: log.error(NAME, _(u'UPS plug-in') + ':\n' + traceback.format_exc()) once_three = True if ups_options['enable_log']: update_log(0) once = False if reboot_time and test: count_val = int(ups_options['time']) * 60 # value for countdown actual_time = int(time.time()) log.clear(NAME) log.info(NAME, _(u'Time to shutdown') + ': ' + str(count_val - (actual_time - last_time)) + ' ' + _(u'sec')) if ((actual_time - last_time) >= count_val): # if countdown is 0 last_time = actual_time test = get_check_power() if test: # if power line is current not active log.clear(NAME) log.info(NAME, _(u'Power line is not restore in time -> sends email and shutdown system.')) reboot_time = False if ups_options['sendeml']: # if enabled send email if once_two: # send email with info shutdown system msg = '<b>' + _(u'UPS plug-in') + '</b> ' + '<br><p style="color:red;">' + _(u'Power line is not restore in time -> shutdown system!') + '</p>' msglog = _(u'UPS plug-in') + ': ' + _(u'Power line is not restore in time -> shutdown system!') try: try_mail = None if ups_options['eplug']==0: # email_notifications from plugins.email_notifications import try_mail if ups_options['eplug']==1: # email_notifications SSL from plugins.email_notifications_ssl import try_mail if try_mail is not None: try_mail(msg, msglog, attachment=None, subject=ups_options['emlsubject']) # try_mail(text, logtext, attachment=None, subject=None) except Exception: log.error(NAME, _(u'UPS plug-in') + ':\n' + traceback.format_exc()) once_two = False GPIO.output(pin_ups_down, GPIO.HIGH) # switch on GPIO fo countdown UPS battery power off self._sleep(4) GPIO.output(pin_ups_down, GPIO.LOW) poweroff(1, True) # shutdown system if not test: if once_three: if ups_options['sendeml']: # if enabled send email msg = '<b>' + _(u'UPS plug-in') + '</b> ' + '<br><p style="color:green;">' + _(u'Power line has restored - OK.') + '</p>' msglog = _(u'UPS plug-in') + ': ' + _(u'Power line has restored - OK.') log.clear(NAME) log.info(NAME, msglog) try: try_mail = None if ups_options['eplug']==0: # email_notifications from plugins.email_notifications import try_mail if ups_options['eplug']==1: # email_notifications SSL from plugins.email_notifications_ssl import try_mail if try_mail is not None: try_mail(msg, msglog, attachment=None, subject=ups_options['emlsubject']) # try_mail(text, logtext, attachment=None, subject=None) except Exception: log.error(NAME, _(u'UPS plug-in') + ':\n' + traceback.format_exc()) once = True once_two = True once_three = False if ups_options['enable_log']: update_log(1) self._sleep(2) except Exception: log.error(NAME, _(u'UPS plug-in') + ': \n' + traceback.format_exc()) self._sleep(60)
def POST(self): qdict = web.input() change = False # if change language -> restart ospy if 'lang' in qdict and qdict['lang']: if options.lang != qdict['lang']: change = True newname = qdict['name'] # if name is asci char try: from ospy.helpers import ASCI_convert qdict['name'] = ASCI_convert(newname) except: qdict['name'] = ' ' save_to_options(qdict) if 'master' in qdict: m = int(qdict['master']) if m < 0: stations.master = None elif m < stations.count(): stations.master = m if 'master_two' in qdict: m = int(qdict['master_two']) if m < 0: stations.master_two = None elif m < stations.count(): stations.master_two = m if 'old_password' in qdict and qdict['old_password'] != "": try: if test_password(qdict['old_password']): if qdict['new_password'] == "": raise web.seeother('/options?errorCode=pw_blank') elif qdict['new_password'] == qdict['check_password']: options.password_salt = password_salt( ) # Make a new salt options.password_hash = password_hash( qdict['new_password'], options.password_salt) else: raise web.seeother('/options?errorCode=pw_mismatch') else: raise web.seeother('/options?errorCode=pw_wrong') except KeyError: pass if 'rbt' in qdict and qdict['rbt'] == '1': reboot(True) # Linux HW software return self.core_render.home() if 'rstrt' in qdict and qdict['rstrt'] == '1': restart() # OSPy software return self.core_render.restarting(home_page) if 'pwrdwn' in qdict and qdict['pwrdwn'] == '1': poweroff() # shutdown HW system return self.core_render.restarting(home_page) if 'deldef' in qdict and qdict['deldef'] == '1': OPTIONS_FILE = './ospy/data' try: import shutil, time shutil.rmtree(OPTIONS_FILE) # delete data folder time.sleep(2) os.makedirs(OPTIONS_FILE) # create data folder restart() # restart OSPy software return self.core_render.restarting(home_page) except: pass if change: restart() # OSPy software return self.core_render.restarting(home_page) raise web.seeother('/')