def main(): mqtt.loop_start() ups = apc.parse(apc.get(host=APCUPSD_HOST)) HOSTNAME = ups.get('HOSTNAME', 'apcupsd-mqtt') MQTT_TOPIC_PREFIX_UPS = MQTT_TOPIC_PREFIX + "/" + UPS_ALIAS + "_" alias = UPS_ALIAS.lower().replace(" ", "_").replace('.', '_') ups_data = apc.parse(apc.get(host=APCUPSD_HOST), strip_units=True) for data_topic in ups_data: data_information = sensor_data(data_topic) send_homeassistant_registration(data_information) while True: ups_data = apc.parse(apc.get(host=APCUPSD_HOST), strip_units=True) for data_topic in ups_data: data_information = sensor_data(data_topic) topic_id = MQTT_TOPIC_PREFIX_UPS + sensor_name(data_information) mqtt_send(topic_id, str(ups_data[data_topic])) time.sleep(INTERVAL)
def statusCheck(): access = apc.get(host="localhost") check = apc.parse(access) if check['STATUS'] == 'ONLINE': return 0 if check['STATUS'] == 'CHARGING': return 0 else: return 1
def run(self): self.state = apc.parse(apc.get(), strip_units=True) if self.state['STATUS'] != "ONLINE": self.log.error("Power status is %s. This is bad" % self.state['STATUS']) self.notifyEmails() sys.exit(0) self.log.info("All is well, nothing to see here.") sys.exit(0)
def main(host="localhost"): # apcupsdよりデータ取得 raw = get(host=host) # rawデータだと文字化け print(raw) print("-" * 10) # OrderedDictへparse parsed = parse(raw) print(parsed) # 読みやすい形でprint print_status(raw) print("-" * 10)
from apcaccess import status as apc from influxdb import InfluxDBClient hostname = os.getenv('HOSTNAME', 'apcupsd-influxdb-exporter') dbname = os.getenv('INFLUXDB_DATABASE', 'apcupsd') user = os.getenv('INFLUXDB_USER') password = os.getenv('INFLUXDB_PASSWORD') port = os.getenv('INFLUXDB_PORT', 8086) host = os.getenv('INFLUXDB_HOST', 'localhost') client = InfluxDBClient(host, port, user, password, dbname) client.create_database(dbname) while True: ups = apc.parse(apc.get(host=os.getenv('APCUPSD_HOST', 'localhost')), strip_units=True) if os.environ['WATTS']: ups['NOMPOWER'] = os.getenv('WATTS') watts = float(ups['NOMPOWER']) * 0.01 * float(ups['LOADPCT']) json_body = [{ 'measurement': 'apcaccess_status', 'fields': { 'WATTS': watts, 'LOADPCT': float(ups['LOADPCT']), 'BCHARGE': float(ups['BCHARGE']), 'TONBATT': float(ups['TONBATT']), 'TIMELEFT': float(ups['TIMELEFT']), 'NOMPOWER': float(ups['NOMPOWER']),
dbname = os.getenv('INFLUXDB_DATABASE', 'upsnew') user = os.getenv('INFLUXDB_USER', '') password = os.getenv('INFLUXDB_PASSWORD', '') port = os.getenv('INFLUXDB_PORT', 8086) host = os.getenv('INFLUXDB_HOST', '10.0.1.11') apc_host = os.getenv('APC_HOST', 'localhost') client = InfluxDBClient(host, port, user, password, dbname) print "Hostname: ", HOSTNAME print "database name: ", dbname print "db host:", host #print ups while True: ups = apc.parse(apc.get(host=apc_host), strip_units=True) if os.environ['WATTS']: ups['NOMPOWER'] = os.environ['WATTS'] json_body = [ { 'measurement': 'APC-NEW', 'fields': { 'BCHARGE' : float(ups['BCHARGE']), 'TONBATT' : float(ups['TONBATT']), 'TIMELEFT' : float(ups['TIMELEFT']), 'NOMPOWER' : float(ups['NOMPOWER']) }, 'tags': { 'host': HOSTNAME } }
# -*- coding: utf-8 -*- # import os from apcaccess import status as apc def str_escape(string): if string is None: return "" ret = string.replace(" ", "\\ ") ret = ret.replace(",", "\\,") ret = '"{}"'.format(ret) return ret ups = apc.parse(apc.get(), strip_units=True) watts = float(os.getenv('WATTS', ups.get('NOMPOWER', 0.0))) * \ 0.01 * float(ups.get('LOADPCT', 0.0)) results = { 'measurement': 'apcaccess_status', 'fields': { 'WATTS': watts, 'STATUS': str_escape(ups.get('STATUS')), 'LOADPCT': float(ups.get('LOADPCT', 0.0)), 'BCHARGE': float(ups.get('BCHARGE', 0.0)), 'TONBATT': float(ups.get('TONBATT', 0.0)), 'TIMELEFT': float(ups.get('TIMELEFT', 0.0)), 'NOMPOWER': float(ups.get('NOMPOWER', 0.0)), 'CUMONBATT': float(ups.get('CUMONBATT', 0.0)), 'BATTV': float(ups.get('BATTV', 0.0)), 'OUTPUTV': float(ups.get('OUTPUTV', 0.0)),
user = "" password ="" port = os.getenv('INFLUXDB_PORT', 8086) host = os.getenv('INFLUXDB_HOST', '10.0.1.11') client = InfluxDBClient(host, port, user, password, dbname) client.create_database(dbname) print "Hostname: ", HOSTNAME print "database name: ", dbname print "db host:", host #print ups while True: ups = apc.parse(apc.get(host="localhost"), strip_units=True) if os.environ['WATTS']: ups['NOMPOWER'] = os.environ['WATTS'] watts = float(float(ups['NOMPOWER']) * float(0.01 *float(ups['LOADPCT']))) json_body = [ { 'measurement': 'APC-NEW', 'fields': { 'WATTS' : float(watts), 'LOADPCT' : float(ups['LOADPCT']), 'BCHARGE' : float(ups['BCHARGE']), 'TONBATT' : float(ups['TONBATT']), 'TIMELEFT' : float(ups['TIMELEFT']), 'NOMPOWER' : float(ups['NOMPOWER']) }, 'tags': {
""" apcaccess.__main__ Provides the command-line functionalty similar to the original apcaccess cmd. """ import argparse from apcaccess import status if __name__ == "__main__": # No need to use "proper" names on such simple code. # pylint: disable=invalid-name p = argparse.ArgumentParser() p.add_argument("--host", default="localhost") p.add_argument("--port", type=int, default=3551) p.add_argument("--strip-units", action="store_true", default=False) args = p.parse_args() status.print_status(status.get(args.host, args.port), strip_units=args.strip_units)
# #Author: Stinocon #Date: 23 Sep 2020 # import sys import os import time import logging sys.path.insert(1, "./lib") sys.path.insert(1, "./pic") from lib import epd2in13_V2 from PIL import Image, ImageDraw, ImageFont from apcaccess import status as apc #logging.basicConfig(level=logging.DEBUG) while True: apcdata = apc.parse(apc.get(host="YOUR_RASPI_IP"), strip_units=True) status = apcdata["STATUS"] load = apcdata["LOADPCT"] charge = apcdata["BCHARGE"] timeleft = apcdata["TIMELEFT"] voltage = apcdata["LINEV"] firmware = apcdata["FIRMWARE"] serial = apcdata["SERIALNO"] model = apcdata["MODEL"] #print (apcdata) #uncomment to print info about your UPS via cli try: epd = epd2in13_V2.EPD() epd.init(epd.FULL_UPDATE) font15 = ImageFont.truetype('/opt/ups-display/pic/Font.ttc', 15) font24 = ImageFont.truetype('/opt/ups-display/pic/Font.ttc', 24) image = Image.new('1', (epd.height, epd.width), 255)
import time #import subprocess from apcaccess import status as apc #from subprocess import check_output #out = subprocess.check_output(["apcaccess"]) ThingSpeakURL = "https://api.thingspeak.com/update?api_key=HN8U6AUCC50NL75P" ThingSpeakResponse = -1 #TestMode = 0 TestMode = int(sys.argv[1]) ProgramLogFileName = "/home/pi/apc/apc.log" #spl = [ele.split(":",1)for ele in out.splitlines()] #ups = {k.strip():v.strip() for k,v in spl} ups = apc.parse(apc.get(), True) #ups['LINEV'] = ups['LINEV'].strip(' Volts') #ups['LOADPCT'] = ups['LOADPCT'].strip(' Percent') #ups['BCHARGE'] = ups['BCHARGE'].strip(' Percent') #ups['TIMELEFT'] = ups['TIMELEFT'].strip(' Minutes') #ups['BATTV'] = ups['BATTV'].strip(' Volts') if TestMode == 1: print "Status : ", ups['STATUS'] print "Input Voltage : ", ups['LINEV'] print "Load % : ", ups['LOADPCT'] print "Battery Charge : ", ups['BCHARGE'] print "Runtime Left : ", ups['TIMELEFT'] print "Battery Voltage : ", ups['BATTV']