def restart(wait=1, block=False): """Provides orderly restart of the OSPy server. Module first spawns a new thread which calls this module with the the 'block' flag set. When block=True the server is shut down, stations cleared, and current process is replaced. wait Time to wait after clearing stations before reboot process. block Flag used to initiate reboot.""" if block: # Stop the web server first: from ospy import server server.stop() from ospy.stations import stations stations.clear() time.sleep(wait) logging.info("Restarting...") import sys if determine_platform() == 'nt': import subprocess # Use this weird construction to start a separate process that is not killed when we stop the current one subprocess.Popen(['cmd.exe', '/c', 'start', sys.executable] + sys.argv) else: import os os.execl(sys.executable, sys.executable, *sys.argv) else: from threading import Thread t = Thread(target=restart, args=(wait, True)) t.daemon = False t.start()
def poweroff(wait=1, block=False): """Provides orderly power off of the OSPy machine. Module first spawns a new thread which calls this module with the the 'block' flag set. When block=True the server is shut down, stations cleared, and power off begins via a shell command. wait Time to wait after clearing stations before power off process. block Flag used to initiate power off.""" if block: # Stop the web server first: from ospy import server server.stop() from ospy.stations import stations stations.clear() time.sleep(wait) logging.info("Powering off...") import subprocess if determine_platform() == 'nt': subprocess.Popen('shutdown /t 0'.split()) else: subprocess.Popen(['poweroff']) else: from threading import Thread t = Thread(target=poweroff, args=(wait, True)) t.daemon = False t.start()
def restart(wait=1, block=False): if block: # Stop the web server first: from ospy import server server.stop() from ospy.stations import stations stations.clear() time.sleep(wait) logging.info("Restarting...") import sys if determine_platform() == 'nt': import subprocess # Use this weird construction to start a separate process that is not killed when we stop the current one subprocess.Popen(['cmd.exe', '/c', 'start', sys.executable] + sys.argv) else: import os os.execl(sys.executable, sys.executable, *sys.argv) else: from threading import Thread t = Thread(target=restart, args=(wait, True)) t.daemon = False t.start()
def _option_cb(self, key, old, new): # Clear if manual mode changed: if key == 'manual_mode': programs.run_now_program = None run_once.clear() log.finish_run(None) stations.clear() # Stop relay if not used anymore: if key == 'master_relay' and not new and outputs.relay_output: outputs.relay_output = False
def _botCmd_stop(self, bot, update): # msg for stop all station and disable scheduler chat_id = update.message.chat.id if chat_id in list(self._currentChats): txt = _(u'{} System - scheduler OFF. All stations OFF.').format(options.name) programs.run_now_program = None run_once.clear() log.finish_run(None) stations.clear() else: txt = _(u'Sorry I can not do that.') if is_python2(): bot.sendMessage(chat_id, text=txt.encode('utf-8')) else: bot.sendMessage(chat_id, text=txt)
def _botCmd_runOnce(self, bot, update, args): # run-now program xx chat_id = update.message.chat.id if chat_id in list(self._currentChats): txt = _(u'{} RunOnce: program {}.').format(options.name, args) for program in programs.get(): if (program.index == int(args-1)): options.manual_mode = False log.finish_run(None) stations.clear() programs.run_now(program.index) break program.index+1 else: txt = _(u'Sorry I can not do that.') if is_python2(): bot.sendMessage(chat_id, text=txt.encode('utf-8')) else: bot.sendMessage(chat_id, text=txt)
def poweroff(wait=1, block=False): if block: # Stop the web server first: from ospy import server server.stop() from ospy.stations import stations stations.clear() time.sleep(wait) logging.info("Powering off...") import subprocess if determine_platform() == 'nt': subprocess.Popen('shutdown /t 0'.split()) else: subprocess.Popen(['poweroff']) else: from threading import Thread t = Thread(target=poweroff, args=(wait, True)) t.daemon = False t.start()
def run(self): once_text = True two_text = True send = False mini = True while not self._stop.is_set(): try: if tank_options['use_sonic']: if two_text: log.clear(NAME) log.info(NAME, _('Water tank monitor is enabled.')) once_text = True two_text = False level_in_tank = get_sonic_tank_cm() if level_in_tank >= 0: # if I2C device exists log.info( NAME, datetime_string() + ' ' + _('Water level') + ': ' + str(level_in_tank) + ' ' + _('cm') + '.') if level_in_tank <= int( tank_options['water_minimum'] ) and mini and not options.manual_mode and level_in_tank > -1: if tank_options['use_send_email']: send = True mini = False log.info( NAME, datetime_string() + ' ' + _('ERROR: Water in Tank') + ' < ' + str(tank_options['water_minimum']) + ' ' + _('cm') + '!') options.scheduler_enabled = False # disable scheduler log.finish_run(None) # save log stations.clear() # set all station to off if level_in_tank > int(tank_options['water_minimum'] ) + 5 and not mini: mini = True else: log.info( NAME, datetime_string() + ' ' + _('Water level: Error I2C device not found.')) else: if once_text: log.info(NAME, 'Water tank monitor is disabled.') once_text = False two_text = True last_level = 0 if tank_options['use_freq_1']: humi1 = get_freq(1) if humi1 >= 0: humi1_lvl = maping(humi1, int(tank_options['minimum_freq']), int(tank_options['maximum_freq']), 0, 100) if humi1_lvl >= 100: humi1_lvl = 100 log.info( NAME, datetime_string() + ' F1: ' + str(humi1) + 'Hz H: ' + str(humi1_lvl) + '%.') else: log.info( NAME, datetime_string() + ' F1: ' + _('Error I2C device not found.')) if tank_options['use_freq_2']: humi2 = get_freq(2) if humi2 >= 0: humi2_lvl = maping(humi2, int(tank_options['minimum_freq']), int(tank_options['maximum_freq']), 0, 100) if humi2_lvl >= 100: humi2_lvl = 100 log.info( NAME, datetime_string() + ' F2: ' + str(humi2) + 'Hz H: ' + str(humi2_lvl) + '%.') else: log.info( NAME, datetime_string() + ' F2: ' + _('Error I2C device not found.')) if tank_options['use_freq_3']: humi3 = get_freq(3) if humi3 >= 0: humi3_lvl = maping(humi3, int(tank_options['minimum_freq']), int(tank_options['maximum_freq']), 0, 100) if humi3_lvl >= 100: humi3_lvl = 100 log.info( NAME, datetime_string() + ' F3: ' + str(humi3) + 'Hz H: ' + str(humi3_lvl) + '%.') else: log.info( NAME, datetime_string() + ' F3: ' + _('Error I2C device not found.')) if tank_options['use_freq_4']: humi4 = get_freq(4) if humi4 >= 0: humi4_lvl = maping(humi4, int(tank_options['minimum_freq']), int(tank_options['maximum_freq']), 0, 100) if humi4_lvl >= 100: humi4_lvl = 100 log.info( NAME, datetime_string() + ' F4: ' + str(humi4) + 'Hz H: ' + str(humi4_lvl) + '%.') else: log.info( NAME, datetime_string() + ' F4: ' + _('Error I2C device not found.')) if tank_options['use_freq_5']: humi5 = get_freq(5) if humi5 >= 0: humi5_lvl = maping(humi5, int(tank_options['minimum_freq']), int(tank_options['maximum_freq']), 0, 100) if humi5_lvl >= 100: humi5_lvl = 100 log.info( NAME, datetime_string() + ' F5: ' + str(humi5) + 'Hz H: ' + str(humi5_lvl) + '%.') else: log.info( NAME, datetime_string() + ' F5: ' + _('Error I2C device not found.')) if tank_options['use_freq_6']: humi6 = get_freq(6) if humi6 >= 0: humi6_lvl = maping(humi6, int(tank_options['minimum_freq']), int(tank_options['maximum_freq']), 0, 100) if humi6_lvl >= 100: humi6_lvl = 100 log.info( NAME, datetime_string() + ' F6: ' + str(humi6) + 'Hz H: ' + str(humi6_lvl) + '%.') else: log.info( NAME, datetime_string() + ' F6: ' + _('Error I2C device not found.')) if tank_options['use_freq_7']: humi7 = get_freq(7) if humi7 >= 0: humi7_lvl = maping(humi7, int(tank_options['minimum_freq']), int(tank_options['maximum_freq']), 0, 100) if humi7_lvl >= 100: humi7_lvl = 100 log.info( NAME, datetime_string() + ' F7: ' + str(humi7) + 'Hz H: ' + str(humi7_lvl) + '%.') else: log.info( NAME, datetime_string() + ' F7: ' + _('Error I2C device not found.')) if tank_options['use_freq_8']: humi8 = get_freq(8) if humi8 >= 0: humi8_lvl = maping(humi8, int(tank_options['minimum_freq']), int(tank_options['maximum_freq']), 0, 100) if humi8_lvl >= 100: humi8_lvl = 100 log.info( NAME, datetime_string() + ' F8: ' + str(humi8) + 'Hz H: ' + str(humi8_lvl) + '%.') else: log.info( NAME, datetime_string() + ' F8: ' + _('Error I2C device not found.')) if send: msg = (datetime_string() + '\n' + _( 'System detected error: Water Tank has minimum Water Level' ) + ': ' + str( tank_options['water_minimum'] ) + _('cm') + '.\n' + _( 'Scheduler is now disabled and all Stations turn Off.') ) try: send_email(msg) log.info(NAME, _('Email was sent') + ': ' + msg) send = False except Exception as err: log.error(NAME, _('Email was not sent') + '! ' + str(err)) self._sleep(10) # 2 for tested log.clear(NAME) except Exception: log.error( NAME, _('Water tank and humidity Monitor plug-in') + ':\n' + traceback.format_exc()) self._sleep(60)
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 GET(self): from ospy import server qdict = web.input() stop_all = get_input(qdict, 'stop_all', False, lambda x: True) scheduler_enabled = get_input(qdict, 'scheduler_enabled', None, lambda x: x == '1') manual_mode = get_input(qdict, 'manual_mode', None, lambda x: x == '1') rain_block = get_input(qdict, 'rain_block', None, float) level_adjustment = get_input(qdict, 'level_adjustment', None, float) toggle_temp = get_input(qdict, 'toggle_temp', False, lambda x: True) if stop_all: if not options.manual_mode: options.scheduler_enabled = False programs.run_now_program = None run_once.clear() log.finish_run(None) stations.clear() if scheduler_enabled is not None: options.scheduler_enabled = scheduler_enabled if manual_mode is not None: options.manual_mode = manual_mode if rain_block is not None: options.rain_block = datetime.datetime.now() + datetime.timedelta(hours=rain_block) if level_adjustment is not None: options.level_adjustment = level_adjustment / 100 if toggle_temp: options.temp_unit = "F" if options.temp_unit == "C" else "C" set_to = get_input(qdict, 'set_to', None, int) sid = get_input(qdict, 'sid', 0, int) - 1 set_time = get_input(qdict, 'set_time', 0, int) if set_to is not None and 0 <= sid < stations.count() and options.manual_mode: if set_to: # if status is on start = datetime.datetime.now() new_schedule = { 'active': True, 'program': -1, 'station': sid, 'program_name': "Manual", 'fixed': True, 'cut_off': 0, 'manual': True, 'blocked': False, 'start': start, 'original_start': start, 'end': start + datetime.timedelta(days=3650), 'uid': '%s-%s-%d' % (str(start), "Manual", sid), 'usage': stations.get(sid).usage } if set_time > 0: # if an optional duration time is given new_schedule['end'] = datetime.datetime.now() + datetime.timedelta(seconds=set_time) log.start_run(new_schedule) stations.activate(new_schedule['station']) else: # If status is off stations.deactivate(sid) active = log.active_runs() for interval in active: if interval['station'] == sid: log.finish_run(interval) self._redirect_back()
def run(self): try: import smbus # for PCF 8583 self.bus = smbus.SMBus(1 if get_rpi_revision() >= 2 else 0) except ImportError: log.warning(NAME, _('Could not import smbus.')) if self.bus is not None: self.pcf = set_counter(self.bus) # set pcf8583 as counter log.clear(NAME) send = False # send email disable_text = True val = 0.0 maxval = 0.0 timer_reset = 0 while not self._stop.is_set(): try: if self.bus is not None and wind_options[ 'use_wind_monitor']: # if wind plugin is enabled disable_text = True puls = counter( self.bus) / 10.0 # counter value is value/10sec val = puls / (wind_options['pulses'] * 1.0) val = val * wind_options['metperrot'] if val > maxval: maxval = val if timer_reset >= 86400: # 1 day timer_reset = 0 maxval = 0.0 self.status['meter'] = round(val, 2) self.status['kmeter'] = round(val * 3.6, 2) log.clear(NAME) log.info(NAME, _('Please wait 10 sec...')) log.info( NAME, _('Speed') + ' ' + str(round(val, 2)) + ' ' + _('m/sec')) log.info( NAME, _('Speed Peak 24 hour') + ' ' + str(round(maxval, 2)) + ' ' + _('m/sec')) log.info( NAME, _('Pulses') + ' ' + str(puls) + ' ' + _('pulses/sec')) if val >= 42: log.error(NAME, _('Wind speed > 150 km/h (42 m/sec)')) if get_station_is_on(): # if station is on if val >= int( wind_options['maxspeed'] ): # if wind speed is > options max speed log.clear(NAME) log.finish_run(None) # save log stations.clear() # set all station to off log.clear(NAME) log.info( NAME, _('Stops all stations and sends email if enabled sends email.' )) if wind_options[ 'sendeml']: # if enabled send email send = True else: # text on the web if plugin is disabled if disable_text: log.clear(NAME) log.info(NAME, _('Wind speed monitor plug-in is disabled.')) disable_text = False if send: TEXT = (datetime_string() + ': ' + _( 'System detected error: wind speed monitor. All stations set to OFF. Wind is' ) + ': ' + str(round(val * 3.6, 2)) + ' km/h.') try: from plugins.email_notifications import email email(TEXT) # send email without attachments log.info(NAME, _('Email was sent') + ': ' + TEXT) send = False except Exception: log.clear(NAME) log.error( NAME, _('Email was not sent') + '! ' + traceback.format_exc()) timer_reset += 10 # measure is 10 sec long except Exception: log.clear(NAME) log.error( NAME, _('Wind Speed monitor plug-in') + ':\n' + traceback.format_exc()) self._sleep(60) self.pcf = set_counter(self.bus) # set pcf8583 as counter
def FTP_download(self): try: # read command file and save to ramdisk self.ftp.retrbinary("RETR " + plugin_options['loc'] + "data.txt", open("/home/pi/ramdisk/data.txt", 'wb').write) fs = file("/home/pi/ramdisk/data.txt", 'r') obsahaut = fs.readline() fs.close() log.debug( NAME, _(u'FTP received data from file data.txt') + ': ' + str(obsahaut)) change = False if (obsahaut == "AU"): # scheduller options.manual_mode = False log.info(NAME, _(u'Scheduler mode is activated.')) change = True if (obsahaut == "MA"): # manual options.manual_mode = True log.info(NAME, _(u'Manual mode is activated.')) change = True for num_output in range(0, options.output_count): s = 'Z' + str(num_output) if (obsahaut == s): # stations xx ON options.manual_mode = True log.info(NAME, _(u'Manual mode is activated.')) stations.activate(num_output) log.info(NAME, _(u'Activated stations') + ': ' + str(num_output + 1)) change = True s = 'V' + str(num_output) if (obsahaut == s): # stations xx OFF options.manual_mode = True log.info(NAME, _(u'Manual mode is activated.')) stations.deactivate(num_output) log.info( NAME, _(u'Deactivated stations') + ': ' + str(num_output + 1)) change = True for program in programs.get(): s = 'P' + str(program.index + 1) if (obsahaut == s): # Run-now program xx options.manual_mode = False log.info(NAME, _(u'Scheduler mode is activated.')) programs.run_now(program.index) log.info(NAME, _(u'Activated program') + ': ' + str(program.name)) self._sleep(2) change = True program.index + 1 if (obsahaut == "STOP"): # stop all stations and disable scheduler options.scheduler_enabled = False programs.run_now_program = None run_once.clear() log.finish_run(None) stations.clear() log.info(NAME, _(u'Stop all stations and disable system.')) change = True if (obsahaut == "START"): # enable scheduler options.scheduler_enabled = True log.info(NAME, _(u'Enable system.')) change = True if (change): # delete data.txt command now is OK fs = open('/home/pi/ramdisk/data.txt', 'w') fs.write('OK') fs.close() FTP_upload(self) # send to server actual data # TODO save_to_options except Exception: log.clear(NAME) log.info( NAME, _(u'Remote FTP control settings') + ':\n' + traceback.format_exc()) pass
def run(self): send = False once_text = True two_text = True three_text = True four_text = True five_text = True last_time = int(time.time()) actual_time = int(time.time()) while not self._stop.is_set(): try: if pressure_options[ 'use_press_monitor']: # if pressure plugin is enabled four_text = True if get_master_is_on(): # if master station is on three_text = True if once_text: # text on the web if master is on log.clear(NAME) log.info(NAME, _('Master station is ON.')) once_text = False if get_check_pressure(): # if pressure sensor is on actual_time = int(time.time()) count_val = int(pressure_options['time']) log.clear(NAME) log.info( NAME, _('Time to test pressure sensor') + ': ' + str(count_val - (actual_time - last_time)) + ' ' + _('sec')) if actual_time - last_time > int( pressure_options['time'] ): # wait for activated pressure sensor (time delay) last_time = actual_time if get_check_pressure( ): # if pressure sensor is actual on # options.scheduler_enabled = False # set scheduler to off log.finish_run(None) # save log stations.clear() # set all station to off log.clear(NAME) log.info( NAME, _('Pressure sensor is not activated in time -> stops all stations and send email.' )) if pressure_options[ 'sendeml']: # if enabled send email send = True if not get_check_pressure(): last_time = int(time.time()) if five_text: once_text = True five_text = False else: if stations.master is not None: if two_text: log.clear(NAME) log.info(NAME, _('Master station is OFF.')) two_text = False five_text = True last_time = int(time.time()) else: once_text = True two_text = True if four_text: # text on the web if plugin is disabled log.clear(NAME) log.info(NAME, _('Pressure monitor plug-in is disabled.')) four_text = False if stations.master is None: # text on the web if master station is none if three_text: log.clear(NAME) log.info(NAME, _('Not used master station.')) three_text = False if send: msg = (datetime_string() + ': ' + _('System detected error: pressure sensor.')) try: send_email(msg) log.info(NAME, _('Email was sent') + ': ' + msg) send = False except Exception: log.error( NAME, _('Email was not sent') + '! ' + traceback.format_exc()) if get_check_pressure(): self.status['Pstate%d'] = _('INACTIVE') else: self.status['Pstate%d'] = _('ACTIVE') self._sleep(1) except Exception: log.error( NAME, _('Pressure monitor plug-in') + ':\n' + traceback.format_exc()) self._sleep(60)
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): 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