def GET(self): statuslist = [] for station in stations.get(): if station.enabled or station.is_master or station.is_master_two: status = { 'station': station.index, 'status': 'on' if station.active else 'off', 'reason': 'master' if station.is_master or station.is_master_two else '', 'master': 1 if station.is_master else 0, 'master_two': 1 if station.is_master_two else 0, 'programName': '', 'remaining': 0 } if not station.is_master or not station.is_master_two: if options.manual_mode: status['programName'] = 'Manual Mode' else: if station.active: active = log.active_runs() for interval in active: if not interval['blocked'] and interval[ 'station'] == station.index: status['programName'] = interval[ 'program_name'] status['reason'] = 'program' status['remaining'] = max( 0, (interval['end'] - datetime.datetime.now() ).total_seconds()) elif not options.scheduler_enabled: status['reason'] = 'system_off' elif not station.ignore_rain and inputs.rain_sensed(): status['reason'] = 'rain_sensed' elif not station.ignore_rain and rain_blocks.seconds_left( ): status['reason'] = 'rain_delay' statuslist.append(status) web.header('Content-Type', 'application/json') return json.dumps(statuslist)
def GET(self): statuslist = [] for station in stations.get(): if station.enabled or station.is_master or station.is_master_two: status = { 'station': station.index, 'status': 'on' if station.active else 'off', 'reason': 'master' if station.is_master or station.is_master_two else '', 'master': 1 if station.is_master else 0, 'master_two': 1 if station.is_master_two else 0, 'programName': '', 'remaining': 0} if not station.is_master or not station.is_master_two: if options.manual_mode: status['programName'] = 'Manual Mode' else: if station.active: active = log.active_runs() for interval in active: if not interval['blocked'] and interval['station'] == station.index: status['programName'] = interval['program_name'] status['reason'] = 'program' status['remaining'] = max(0, (interval['end'] - datetime.datetime.now()).total_seconds()) elif not options.scheduler_enabled: status['reason'] = 'system_off' elif not station.ignore_rain and inputs.rain_sensed(): status['reason'] = 'rain_sensed' elif not station.ignore_rain and rain_blocks.seconds_left(): status['reason'] = 'rain_delay' statuslist.append(status) web.header('Content-Type', 'application/json') return json.dumps(statuslist)
def FTP_upload(self): try: # create a file "stavy.php" cas = time.strftime('%d.%m.%Y (%H:%M:%S)', time.localtime(time.time())) text = "<?php\r\n$cas = \"" # actual time (ex: cas = dd.mm.yyyy (HH:MM:SS) text += cas + "\";\r\n" text += "$rain = \'" # rain state (ex: rain = 0/1) text += str(1 if inputs.rain_sensed() else 0) + "\';\r\n" text += "$output = \'" # use xx outputs count (ex: output = 8) text += str(options.output_count) + "\';\r\n" text += "$program = \'" # use xx programs count (ex: program = 3) text += str(programs.count()) + "\';\r\n" text += "$masterstat = " # master stations index for station in stations.get(): if station.is_master: text += "\'" + str(station.index) + "\';\r\n" if stations.master is None: text += "\'\';\r\n" text += "$raindel = " # rain delay if rain_blocks.seconds_left(): m, s = divmod(int(rain_blocks.seconds_left()), 60) h, m = divmod(m, 60) text += "\'" + "%dh:%02dm:%02ds" % (h, m, s) + "\';\r\n" else: text += "\'\';\r\n" import unicodedata # for only asci text in PHP WEB (stations name, program name...) namestations = [] for num in range(0, options.output_count): # stations name as array namestations.append( unicodedata.normalize('NFKD', stations.get(num).name).encode( 'ascii', 'ignore')) text += "$name" + " = array" text += str(namestations) + ";\r\n" statestations = [] for num in range(0, options.output_count): # stations state as array statestations.append(1 if stations.get(num).active else 0) text += "$state" + " = array" text += str(statestations) + ";\r\n" progrname = [] for program in programs.get(): # program name as array progrname.append( unicodedata.normalize('NFKD', program.name).encode('ascii', 'ignore')) text += "$progname" + " = array" text += str(progrname) + ";\r\n" text = text.replace('[', '(') text = text.replace(']', ')') text += "$schedul = \'" # scheduller state (ex: schedul = 0 manual/ 1 scheduler) text += str(0 if options.manual_mode else 1) + "\';\r\n" text += "$system = \'" # scheduller state (ex: system = 0 stop/ 1 scheduler) text += str(0 if options.scheduler_enabled else 1) + "\';\r\n" text += "$cpu = \'" # cpu temp (ex: cpu = 45.2) text += str(helpers.get_cpu_temp(options.temp_unit)) + "\';\r\n" text += "$unit = \'" # cpu temp unit(ex: unit = C/F) in Celsius or Fahrenheit text += str(options.temp_unit) + "\';\r\n" text += "$ver = \'" # software version date (ex: ver = 2016-07-30) text += str(version.ver_date) + "\';\r\n" text += "$id = \'" # software OSPy ID (ex: id = "opensprinkler") text += str(options.name) + "\';\r\n" text += "$ip = \'" # OSPy IP address (ex: ip = "192.168.1.1") text += str(helpers.get_ip()) + "\';\r\n" text += "$port = \'" # OSPy port (ex: port = "8080") text += str(options.web_port) + "\';\r\n" text += "$ssl = \'" # OSPy use ssl port (ex: ssl = "1" or "0") text += str(1 if options.use_ssl else 0) + "\';\r\n" tank = '' try: from plugins import tank_humi_monitor tank = tank_humi_monitor.get_sonic_tank_cm() if tank < 0: # -1 is error I2C device for ping not found in tank_humi_monitor tank = '' except Exception: tank = '' text = text + "$tank = \'" # from tank humi monitor plugins check water level (ex: tank = "100" in cm or "") text = text + str(tank) + "\';\r\n" press = '' try: from plugins import pressure_monitor press = pressure_monitor.get_check_pressure() except Exception: press = '' text = text + "$press = \'" # from pressure plugins check press (ex: press = "1" or "0") text = text + str(press) + "\';\r\n" ups = '' try: from plugins import ups_adj ups = ups_adj.get_check_power( ) # read state power line from plugin ups adj except Exception: ups = '' text = text + "$ups = \'" text = text + str(ups) + "\';\r\n" result = '' finished = [run for run in log.finished_runs() if not run['blocked']] if finished: result = finished[-1]['start'].strftime( '%d-%m-%Y v %H:%M:%S program: ') + finished[-1]['program_name'] else: result = '' text = text + "$lastrun = \'" # last run program (ex: start d-m-r v h:m:s program: jmemo programu) text = text + str(result) + "\';\r\n" text = text + "$up = \'" # system run uptime text = text + str(helpers.uptime()) + "\';\r\n" text = text + "?>\r\n" #print text """ example php code ----------- <?php $cas = "09.08.2016 (15:28:10)"; $rain = '0'; $output = '3'; $program = '3'; $masterstat = '0'; $raindel = '1:26:22'; // hod:min:sec $name = array('cerpadlo','test','out2'); $state = array('0', '0', '0'); $progname = array('cerpadlo v 5','test','out2'); $schedul = '1'; $system = '1'; $cpu = '40.1'; $unit = 'C'; $ver = '2016-07-30'; $id = 'Zalevac chata'; $ip = '192.168.1.253'; $port = '80'; $ssl = '1'; $tank = '20'; $press = '0'; $ups = '1'; $lastrun = '09.08.2016 v 15:28:10 program: bezi 5 minut)'; $up = '6 days, 0:48:01' ?> ------------------------------- """ try: fs = file("/home/pi/ramdisk/stavy.php", 'w') fs.write(text) fs.close() except: log.error(NAME, _(u'Could not save stavy.php to ramdisk!')) pass self.ftp.storbinary("STOR " + plugin_options['loc'] + 'stavy.php', open("/home/pi/ramdisk/stavy.php", 'rb')) log.info( NAME, _(u'Data file stavy.php has send on to FTP server') + ': ' + str(cas)) self.ftp.storbinary("STOR " + plugin_options['loc'] + 'data.txt', open("/home/pi/ramdisk/data.txt", 'rb')) log.info( NAME, _(u'Data file data.txt has send on to FTP server') + ': ' + str(cas)) self.ftp.close # FTP end except Exception: log.info( NAME, _(u'Remote FTP control settings') + ':\n' + traceback.format_exc())
def run(self): old_statuslist = ' ' log.clear(NAME) log.info(NAME, _('MQTT Zones Plugin started.')) once = True while not self._stop.is_set(): if plugin_options['use_mqtt']: once = True try: statuslist = [] for station in stations.get(): if station.enabled or station.is_master or station.is_master_two: status = { 'station': station.index, 'status': 'on' if station.active else 'off', 'name': station.name, 'reason': 'master' if station.is_master or station.is_master_two else ''} if not station.is_master or not station.is_master_two: if station.active: active = log.active_runs() for interval in active: if not interval['blocked'] and interval['station'] == station.index: status['reason'] = 'program' elif not options.scheduler_enabled: status['reason'] = 'system_off' elif not station.ignore_rain and inputs.rain_sensed(): status['reason'] = 'rain_sensed' elif not station.ignore_rain and rain_blocks.seconds_left(): status['reason'] = 'rain_delay' statuslist.append(status) zone_topic = plugin_options['zone_topic'] if zone_topic: try: from plugins import mqtt except ImportError: log.error(NAME, _('MQTT Zones Plugin requires MQTT plugin.')) if statuslist != old_statuslist: old_statuslist = statuslist client = mqtt.get_client() if client: client.publish(zone_topic, json.dumps(statuslist), qos=1, retain=True) log.clear(NAME) log.info(NAME, _('MQTT Zones Plugin public') + ':\n' + str(statuslist)) else: log.clear(NAME) log.error(NAME, _('Not setup Zone Topic')) self._sleep(10) self._sleep(1) except Exception: log.error(NAME, _('MQTT Zones plug-in') + ':\n' + traceback.format_exc()) self._sleep(60) else: if once: log.info(NAME, _('MQTT Zones Plugin is disabled.')) once = False
def on_message(client, userdata, message): log.clear(NAME) log.info( NAME, datetime_string() + ' ' + _('Message received') + ': ' + str(message.payload.decode("utf-8"))) #print("Message topic=",message.topic) #print("Message qos=",message.qos) #print("Message retain flag=",message.retain) if plugin_options["use_mqtt_secondary"]: if not options.manual_mode: # check operation status log.info( NAME, datetime_string() + ' ' + _('You must this OSPy switch to manual mode!')) return if inputs.rain_sensed(): # check rain sensor log.info( NAME, datetime_string() + ' ' + _('Skipping command, rain sense is detected!')) return if rain_blocks.seconds_left(): # check rain delay log.info( NAME, datetime_string() + ' ' + _('Skipping command, rain delay is activated!')) return try: rec_data = str(message.payload) cmd = json.loads(rec_data) except ValueError as e: log.info(NAME, _('Could not decode command:') + ':\n' + message.payload, e) return first = int( plugin_options["first_station"]) - 1 # first secondary station count = int(plugin_options["station_count"]) # count secondary station try: for i in range( first, first + count): # count of station (example on main OSPy: 5 to 10) zone = cmd[i]["status"] # "off" or "on" state #station = cmd[i]["station"] #name = cmd[i]["name"] #print station, name, zone sid = i - first if sid <= stations.count(): # local station size check if zone == "on": start = datetime.datetime.now() mqn = _('MQTT Manual') new_schedule = { 'active': True, 'program': -1, 'station': sid, 'program_name': mqn, '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), mqn, sid), 'usage': stations.get(sid).usage } # if an optional duration time is given #set_time = xxx second #new_schedule['end'] = datetime.datetime.now() + datetime.timedelta(seconds=set_time) log.start_run(new_schedule) stations.activate(new_schedule['station']) if zone == "off": stations.deactivate(sid) active = log.active_runs() for interval in active: if interval['station'] == i: log.finish_run(interval) else: log.error( NAME, _('MQTT plug-in') + ':\n' + _('Setup stations count is smaler! Set correct first station and station count.' )) except Exception: log.error(NAME, _('MQTT plug-in') + ':\n' + traceback.format_exc()) time.sleep(1)