Example #1
0
def check_jobs_build_status():
    if automationhat.is_automation_hat():
        automationhat.light.comms.on()
    jobs = cfg.jobs
    success = 0
    unstable = 0
    failed = 0

    for item in jobs:
        try:
            job = get_jenkins_job_by_name(item)
        except jenkinsapi.custom_exceptions.UnknownJob:
            set_error(True)
        else:
            getstatus = job.get_last_build().get_status()
            if getstatus == "SUCCESS":
                success += 1
                continue
            if getstatus == "UNSTABLE":
                unstable += 1
                continue
            if getstatus == "FAILURE":
                failed += 1
                continue

    if success > 0 and unstable == 0 and failed == 0:
        set_status('SUCCESS')
    if unstable > 0 and failed == 0:
        set_status('UNSTABLE')
    if failed > 0:
        set_status('FAILURE')
    if automationhat.is_automation_hat():
        automationhat.light.comms.off()
def on_connect(client, userdata, flags, rc):

    if rc == 0:
        print("connected")
        client.connected_flag = True
        client.subscribe(relay_ctltopic2)
        client.subscribe(relay2_ctltopic2)
        client.subscribe(relay3_ctltopic2)
        client.subscribe(out1_ctltopic2)
        client.subscribe(out2_ctltopic2)
        client.subscribe(out3_ctltopic2)
        # enable lights if using automationhat
        if automationhat.is_automation_hat():
            automationhat.light.comms.on()
        # Schedule ADC data sending
        schedule.every(sleeptime).seconds.do(adcsend)
        # send input statuses on connect
        pulsecallback(26)
        pulsecallback(20)
        pulsecallback(21)

    else:
        print("connection error")
        client.bad_connection_flag = True
        client.connected_flag = False
        if automationhat.is_automation_hat():
            automationhat.light.comms.off()
        # cancel all tasks (currently only adcsend)
        schedule.clear()
def check_jobs_build_status():
    if automationhat.is_automation_hat():
        automationhat.light.comms.on()
    jobs = cfg.jobs
    success = 0
    unstable = 0
    failed = 0

    for item in jobs:
        try:
            job = J.get_job(item)
        except jenkinsapi.custom_exceptions.UnknownJob:
            set_error(True)
        else:
            getstatus = job.get_last_build().get_status()
            if getstatus == "SUCCESS":
                success += 1
                continue
            if getstatus == "UNSTABLE":
                unstable += 1
                continue
            if getstatus == "FAILURE":
                failed += 1
                continue

    if success > 0 and unstable == 0 and failed == 0:
        set_status('SUCCESS')
    if unstable > 0 and failed == 0:
        set_status('UNSTABLE')
    if failed > 0:
        set_status('FAILURE')
    if automationhat.is_automation_hat():
        automationhat.light.comms.off()
def test(dummy):
    automationhat.enable_auto_lights(False)

    automationhat.light.on()

    for analog in automationhat.analog:
        analog.light.on()

    for output in automationhat.output:
        output.light.on()

    for input in automationhat.input:
        input.light.on()

    time.sleep(1)

    automationhat.light.off()

    for analog in automationhat.analog:
        analog.light.off()

    for output in automationhat.output:
        output.light.off()

    for input in automationhat.input:
        input.light.off()

    automationhat.enable_auto_lights(True)

    if automationhat.is_automation_hat():
        automationhat.light.power.write(1)

    for i in range(4):
        if automationhat.is_automation_hat():
            automationhat.light.comms.toggle()
            automationhat.light.warn.toggle()

        automationhat.relay.one.toggle()

        if automationhat.is_automation_hat():
            automationhat.relay.two.toggle()
            automationhat.relay.three.toggle()
        automationhat.output.toggle()

        print(automationhat.analog.read())

        time.sleep(0.5)

    for i in range(4):
        print(automationhat.input.read())
        time.sleep(0.5)

    if automationhat.is_automation_hat():
        automationhat.light.power.write(0)

    return 0
def writeRelay(channel, state):
    if automationhat.is_automation_hat():
        automationhat.enable_auto_lights(True)
    if channel == 1:
        automationhat.relay.one.write(state)
    elif channel == 2:
        if automationhat.is_automation_hat():
            automationhat.relay.two.write(state)
    elif channel == 3:
        if automationhat.is_automation_hat():
            automationhat.relay.three.write(state)
    else:
        print "Specified relay channel is out of range."
    return 0
def set_error(value):
    global error
    if value is True or value is False:
        error = value
        if value is True:
            if automationhat.is_automation_hat():
                automationhat.light.warn.on()
        else:
            if automationhat.is_automation_hat():
                automationhat.light.warn.off()
    else:
        error = True
        if automationhat.is_automation_hat():
            automationhat.light.warn.on()
        print("[ERROR] Only supply True or False to the setError function")
Example #7
0
def set_error(value):
    global error
    if value is True or value is False:
        error = value
        if value is True:
            if automationhat.is_automation_hat():
                automationhat.light.warn.on()
        else:
            if automationhat.is_automation_hat():
                automationhat.light.warn.off()
    else:
        error = True
        if automationhat.is_automation_hat():
            automationhat.light.warn.on()
        print("[ERROR] Only supply True or False to the setError function")
Example #8
0
def turnOffLeds(commsLedTime, warnLedTime=None):
    if automationhat.is_automation_hat():
        t = Timer(commsLedTime, turnOffCommsLed)
        t.start()
        if warnLedTime is not None:
            t2 = Timer(warnLedTime, turnOffWarnLed)
            t2.start()
Example #9
0
 def get(self):
     if automationhat.is_automation_hat():
         automationhat.light.comms.on()
         one = automationhat.relay.one.read()
         two = automationhat.relay.two.read()
         three = automationhat.relay.three.read()
         turnOffLeds(0.5)
         return {'one': one, 'two': two, 'three': three}
     else:
         return {'error': 'Automation Hat not found!'}, 500
def writeAnalogAutoLightMode(channel, state):
    if automationhat.is_automation_hat():
        if channel == 1:
            automationhat.analog.one.auto_light(state)
        elif channel == 2:
            automationhat.analog.two.auto_light(state)
        elif channel == 3:
            automationhat.analog.three.auto_light(state)
        else:
            print "Specified analog light channel is out of range."
    return 0
def writeInputLight(channel, state):
    if automationhat.is_automation_hat():
        automationhat.enable_auto_lights(False)
        if channel == 1:
            automationhat.input.one.light.write(state)
        elif channel == 2:
            automationhat.input.two.light.write(state)
        elif channel == 3:
            automationhat.input.three.light.write(state)
        else:
            print "Specified input light channel is out of range."
    return 0
def writeOutput(channel, state):
    if automationhat.is_automation_hat():
        automationhat.enable_auto_lights(True)
    if channel == 1:
        automationhat.output.one.write(state)
    elif channel == 2:
        automationhat.output.two.write(state)
    elif channel == 3:
        automationhat.output.three.write(state)
    else:
        print "Specified output channel is out of range."
    return 0
Example #13
0
def relay_handler(address, *args, needs_reply_address=False):
    automationhat.light.comms.write(1)
    print("relay " + address.split("/")[2] + " handler called")
    log.info("relay " + address.split("/")[2] + " handler called")
    relay = int(address.split("/")[2])
    relaystate = int(args[0])
    if automationhat.is_automation_hat():
        if relay == 0:
            automationhat.relay[0].write(relaystate)
        if relay == 1:
            automationhat.relay[1].write(relaystate)
        if relay == 2:
            automationhat.relay[2].write(relaystate)
Example #14
0
def led_handler(address, *args, needs_reply_address=False):
    automationhat.light.comms.write(1)
    print("LED " + address.split("/")[2] + " handler called")
    log.info("LED " + address.split("/")[2] + " handler called")
    led = int(address.split("/")[2])
    ledstate = int(args[0])
    if automationhat.is_automation_hat():
        if led == 0:
            automationhat.light.power.write(ledstate)
        if led == 1:
            automationhat.light.comms.write(ledstate)
        if led == 2:
            automationhat.light.warn.write(ledstate)
Example #15
0
def output_handler(address, *args, needs_reply_address=False):
    automationhat.light.comms.write(1)
    print("digitalout " + address.split("/")[2] + " handler called")
    log.info("digitalout " + address.split("/")[2] + " handler called")
    relay = int(address.split("/")[2])
    outputstate = int(args[0])
    if automationhat.is_automation_hat():
        if relay == 0:
            automationhat.output[0].write(outputstate)
        if relay == 1:
            automationhat.output[1].write(outputstate)
        if relay == 2:
            automationhat.output[2].write(outputstate)
Example #16
0
def throw_raw_alarms(tracker, alarm_bounds):
    # Throw alarms for raw values which leave acceptable bounds.
    # Check all sensor tracks:
    do_throw = 0
    do_throw = do_throw or throw_pres1_alarm(tracker,
                                             alarm_bounds.pres1_bounds)
    do_throw = do_throw or throw_pres2_alarm(tracker,
                                             alarm_bounds.pres2_bounds)
    # ...

    # Raise alarms:
    if (automationhat.is_automation_hat() and do_throw == 1):
        automationhat.light.power.write(1)  # Just turning on a light for now.
    return
Example #17
0
def check_jobs_building():
    if automationhat.is_automation_hat():
        automationhat.light.comms.on()
    global building
    jobs = cfg.jobs
    building = 0

    for item in jobs:
        try:
            job = get_jenkins_job_by_name(item)
        except jenkinsapi.custom_exceptions.UnknownJob:
            set_error(True)
        else:
            if job.is_running():
                building += 1

    if building > 0:
        building = True
    else:
        building = False
        check_jobs_build_status()
    if automationhat.is_automation_hat():
        automationhat.light.comms.off()
def check_jobs_building():
    if automationhat.is_automation_hat():
        automationhat.light.comms.on()
    global building
    jobs = cfg.jobs
    building = 0

    for item in jobs:
        try:
            job = J.get_job(item)
        except jenkinsapi.custom_exceptions.UnknownJob:
            set_error(True)
        else:
            if job.is_running():
                building += 1

    if building > 0:
        building = True
    else:
        building = False
        check_jobs_build_status()
    if automationhat.is_automation_hat():
        automationhat.light.comms.off()
def readInput(channel):
    if automationhat.is_automation_hat():
        automationhat.enable_auto_lights(True)
    if channel == 1:
        state = automationhat.input.one.read()
    elif channel == 2:
        state = automationhat.input.two.read()
    elif channel == 3:
        state = automationhat.input.three.read()
    else:
        state = False
        print "Specified input channel is out of range."
    print "Input value = ", state
    return int(state)
Example #20
0
def restart_handler(address, *args, needs_reply_address=False):
    automationhat.light.comms.write(1)
    print("restart " + address.split("/")[2] + " handler called")
    log.info("restart " + address.split("/")[2] + " handler called")
    restart = int(address.split("/")[2])
    restartstate = int(args[0])
    if automationhat.is_automation_hat():
        if restart == 0:
            print('restart 0 restartstate = ' + str(restartstate))
            if restartstate == 1:
                os.system('sudo reboot')
        if restart == 1:
            print('restart 1 restartstate = ' + str(restartstate))
        if restart == 2:
            print('restart 2 restartstate = ' + str(restartstate))
def readADC(channel):
    if automationhat.is_automation_hat():
        automationhat.enable_auto_lights(True)
    if channel == 1:
        voltage = automationhat.analog.one.read()
    elif channel == 2:
        voltage = automationhat.analog.two.read()
    elif channel == 3:
        voltage = automationhat.analog.three.read()
    elif channel == 4:
        voltage = automationhat.analog.four.read()
    else:
        voltage = 0
        print "Specified analog channel is out of range."
    print "voltage value = ", voltage
    return float(voltage)
Example #22
0
def Setup():
    # Turn on the automation hat power light
    if automationhat.is_automation_hat():
        automationhat.light.power.write(1)
    # Automation Hat BCM pins used
    #RELAY_1 = 13 RELAY_2 = 19 RELAY_3 = 16
    #INPUT_1 = 26 INPUT_2 = 20 INPUT_3 = 21
    #OUTPUT_1 = 5 OUTPUT_2 = 12 OUTPUT_3 = 6

    GPIO.setwarnings(True)
    # Use the broadcom pin layout
    GPIO.setmode(GPIO.BCM)
    # Setup some additional pins
    GPIO.setup(PORCH_LIGHT_PIN, GPIO.OUT, initial=GPIO.LOW)
    GPIO.setup(SPARE1_PIN, GPIO.OUT, initial=GPIO.LOW)
    GPIO.setup(SPARE2_PIN, GPIO.OUT, initial=GPIO.LOW)
Example #23
0
    def initHardware(self):
        if automationhat.is_automation_hat():
            # init local automation hat
            automationhat.light.power.write(1)

        mySettings = settingsClass()  # get settings from db
        #mySettings.resetSettings()

        # setup pwm
        self.outpin = mySettings.settings['pwmPin']
        #pwmLow = mySettings.settings['pwmLow'] # Not used as this would reverse the pump
        self.pwmMid = mySettings.settings['pwmMid']
        self.pwmHigh = mySettings.settings['pwmHigh']

        self.hoseOn = self.pwmMid

        # Start pwm
        gpio.setmode(gpio.BCM)
        gpio.setup(self.outpin, gpio.OUT)
        self.myPwm = gpio.PWM(self.outpin, mySettings.settings['pwmFrequency'])
        self.myPwm.start(self.pwmMid)  # pump init
Example #24
0
    def post(self):
        content = request.get_json()
        print('post body: {}'.format(content))
        one = getStateFromAttribute(content, 'one')
        two = getStateFromAttribute(content, 'two')
        three = getStateFromAttribute(content, 'three')

        if automationhat.is_automation_hat():
            automationhat.light.comms.on()
            automationhat.light.warn.on()
            if one is not None:
                automationhat.relay.one.write(one)
            if two is not None:
                automationhat.relay.two.write(two)
            if three is not None:
                automationhat.relay.three.write(three)
            turnOffLeds(0.5, 1.0)

            one = automationhat.relay.one.read()
            two = automationhat.relay.two.read()
            three = automationhat.relay.three.read()
            return {'one': one, 'two': two, 'three': three}
        else:
            return {'error': 'AutomationHat not found!'}, 500
#!/usr/bin/env python

import time

import automationhat

if automationhat.is_automation_hat():
    automationhat.light.power.write(1)

while True:
    print(automationhat.input.read())
    print(automationhat.analog.read())
    time.sleep(0.5)
Example #26
0
from abc import ABC, abstractmethod
from enum import Enum
from log_utils import logger
from RPi import GPIO
import automationhat
import asyncio

if not automationhat.is_automation_hat():
    raise RuntimeError("Automation HAT is not connected")


class Pump:
    def __init__(self, relay=None):
        if not relay:
            relay = automationhat.relay.one
        self._relay = relay

    def on(self):
        automationhat.light.power.on()
        self._relay.on()

    def off(self):
        automationhat.light.power.off()
        self._relay.off()


class SensorEvent(Enum):
    RISING = 1
    FALLING = 2

Example #27
0
    sys.stdout.flush()

def error(message):
    emit("ERROR: " + message)

def fatal(message):
    emit("FATAL: " + message)
    sys.exit(1)


try:
    import automationhat
except ImportError:
    fatal("Unable to import automationhat python library")

if automationhat.is_automation_hat() or automationhat.is_automation_phat():
    automationhat.enable_auto_lights(True)
else:
    fatal("automationHAT/automationPHAT not detected")

running = True

stdin = NonBlockingStreamReader(sys.stdin)

# pin_index = {'one':1, 'two':2, 'three':3, 'four':4}

# def handle_input(pin):
#     emit("input.{}:{}".format(pin_index[pin.name],pin.read()))

# automationhat.input.changed(handle_input)
def writeCommsLight(state):
    if automationhat.is_automation_hat():
        automationhat.enable_auto_lights(False)
        automationhat.light.comms.write(state)
    return 0
def all_hat_off():
    if automationhat.is_automation_hat():
        automationhat.light.power.off()
        automationhat.light.comms.off()
        automationhat.light.warn.off()
    return
def writeWarnLight(state):
    if automationhat.is_automation_hat():
        automationhat.enable_auto_lights(False)
        automationhat.light.warn.write(state)
    return 0
    if building > 0:
        building = True
    else:
        building = False
        check_jobs_build_status()
    if automationhat.is_automation_hat():
        automationhat.light.comms.off()


# ---------------------------------------------------- #
# --            Startup of the script               -- #
# ---------------------------------------------------- #
# Turn all off if any inputs are still high
all_hat_off()
all_tower_off()
if automationhat.is_automation_hat():
    automationhat.light.power.on()

# Toggle everything once
toggle("output", "one", .4)
toggle("output", "two", .4)
toggle("output", "three", .4)

# Configure the Jenkins parameter and get the job
try:
    J = Jenkins(cfg.jenkinsurl, username=cfg.username, password=cfg.password)
except jenkinsapi.custom_exceptions.JenkinsAPIException:
    set_error(True)
    print("[ERROR] Jenkins authentication error!")
except ConnectionError:
    set_error(True)
def writeAutoLightMode(state):
    if automationhat.is_automation_hat():
        automationhat.enable_auto_lights(state)
    return 0
Example #33
0
def all_hat_off():
    if automationhat.is_automation_hat():
        automationhat.light.power.off()
        automationhat.light.comms.off()
        automationhat.light.warn.off()
    return
Example #34
0
def main():

    if automationhat.is_automation_hat():
        # print("Automation HAT detected.") # lib will print this anyway
        automationhat.light.power.write(1)
    else:
        print("Automation HAT not detected. Aborting.")
        return

    print("Allowable key input:")
    print("  1 - Select Relay 1")
    print("  2 - Select Relay 2")
    print("  3 - Select Relay 3")
    print("  4 - Select Digital Out 1")
    print("  5 - Select Digital Out 2")
    print("  6 - Select Digital Out 3")
    print("  + - Power ON the selected output")
    print("  - - Power OFF the selected output")
    print("  Q - Quit")

    keyreader = KeyReader(echo=False, block=False)
    selected = ' '

    while True:
        print json.dumps([
            automationhat.input.read(),
            automationhat.analog.read(), selected
        ])

        key = keyreader.getch()

        if key == 'q':
            keyreader = None
            return
        elif key == '+':
            print("# power ON %s" % selected)

            if selected == '1':
                automationhat.relay.one.write(1)
            elif selected == '2':
                automationhat.relay.two.write(1)
            elif selected == '3':
                automationhat.relay.three.write(1)
            elif selected == '4':
                automationhat.output.one.write(1)
            elif selected == '5':
                automationhat.output.two.write(1)
            elif selected == '6':
                automationhat.output.three.write(1)

        elif key == '-':
            print("# power OFF %s" % selected)

            if selected == '1':
                automationhat.relay.one.write(0)
            elif selected == '2':
                automationhat.relay.two.write(0)
            elif selected == '3':
                automationhat.relay.three.write(0)
            elif selected == '4':
                automationhat.output.one.write(0)
            elif selected == '5':
                automationhat.output.two.write(0)
            elif selected == '6':
                automationhat.output.three.write(0)

        if key:
            # print("-%s-" % key)
            if key in "123456":
                selected = key
        # else:
        # print "None"

        time.sleep(0.1)
Example #35
0
def main():

    if automationhat.is_automation_hat():
        # print("Automation HAT detected.") # lib will print this anyway
        automationhat.light.power.write(1)
    else:
        print("Automation HAT not detected. Aborting.")
        return

    print("Allowable key input:")
    print("  1 - Select Relay 1")
    print("  2 - Select Relay 2")
    print("  3 - Select Relay 3")
    print("  4 - Select Digital Out 1")
    print("  5 - Select Digital Out 2")
    print("  6 - Select Digital Out 3")
    print("  + - Power ON the selected output")
    print("  - - Power OFF the selected output")
    print("  Q - Quit")

    file = False
    if len(sys.argv) > 1:
        file = sys.argv[1]
        print "Monitoring file: ", file
        monitor = True
    else:
        print("Starting KeyReader..")
        keyreader = KeyReader(echo=False, block=False)

    selected = ' '
    key = ' '

    print("Starting loop..")

    while True:
        print json.dumps([
            automationhat.input.read(),
            automationhat.analog.read(), selected
        ])

        if file:
            key = ' '
            if (os.path.exists(file)):
                print "# File (%s) found, reading.." % file
                with open(file, 'r') as input:
                    data = input.read().strip()
                print "# File contents: ", data
                if len(data) > 1:
                    selected = data[0]
                    key = data[1]
                else:
                    print "# WARNING: Input file should be 2 characters."

                os.remove(file)

        else:
            key = keyreader.getch()

        if key == 'q':
            keyreader = None
            return
        elif key == '+':
            print("# Power ON %s" % selected)

            if selected == '1':
                automationhat.relay.one.write(1)
            elif selected == '2':
                automationhat.relay.two.write(1)
            elif selected == '3':
                automationhat.relay.three.write(1)
            elif selected == '4':
                automationhat.output.one.write(1)
            elif selected == '5':
                automationhat.output.two.write(1)
            elif selected == '6':
                automationhat.output.three.write(1)

        elif key == '-':
            print("# Power OFF %s" % selected)

            if selected == '1':
                automationhat.relay.one.write(0)
            elif selected == '2':
                automationhat.relay.two.write(0)
            elif selected == '3':
                automationhat.relay.three.write(0)
            elif selected == '4':
                automationhat.output.one.write(0)
            elif selected == '5':
                automationhat.output.two.write(0)
            elif selected == '6':
                automationhat.output.three.write(0)

        if key:
            # print("-%s-" % key)
            if key in "123456":
                selected = key
        # else:
        # print "None"

        time.sleep(0.1)