def main(): sp_interface = SpInterface() Settings = namedtuple('Settings', 'periodicity sp_interface pin_no number_of_reads time_between_reads') temp_rule = TemperatureRule(Settings(10*60, sp_interface, 0, 5, 2), [MqttClient('temperature')]) humidity_rule = HumidityRule(Settings(15*60, sp_interface, 2, 5, 2), [MqttClient('humidity')]) luminosity_rule = LuminosityRule(Settings(7*60, sp_interface, 4, 5, 2), [MqttClient('luminosity')]) moisture_rule = MoistureRule(Settings(5*60, sp_interface, 6, 3, 2), [MqttClient('moisture')]) # motion_rule = MotionRule(Settings(1*60, sp_interface, 3, 5, 2), [Display('presence')]) # mq135_rule = MQ135Rule(Settings(2*60, sp_interface, 4, 3, 2), [Display('mq135')]) # camera_rule = CameraRule(1*60, [Display('image')]) # psutil_rule = PsutilRule(4*60, [Display('cpu'), Display('memory'), Display('hdd')]) # led_displays = [Display('blue_led'), Display('red_led'), Display('green_led'), Display('yellow_led')] # led_apis = [LedApi(color=2), LedApi(color=1), LedApi(color=4), LedApi(color=3)] # button_rule = ButtonRule(Settings(1*60, sp_interface, 3, 0, 0), led_displays) # add the rules rule_engine = RuleEngine() rule_engine.add_many([temp_rule, humidity_rule, luminosity_rule, moisture_rule]) # fire up the engine rule_engine.execute()
def __init__(self, root): self.mqttHandler = MqttClient() self.mqttHandler.setup_client() self.name = "component" self.sampInterval = 1 self.loopCycles = 1 self.loopRate = 1 self.rootTopic = root self.config_topic = self.rootTopic + "/components/" + self.name self.my_topic = self.rootTopic + "/" + self.name
def start_mqtt(): try: with open('env.json') as config_file: all_configs = json.load(config_file) main_env = all_configs["main"] mqtt_username = main_env["MQTT_USERNAME"] mqtt_password = main_env["MQTT_PASSWORD"] mqtt_host = main_env["MQTT_HOST"] mqtt_port = main_env["MQTT_PORT"] mqtt_topics = main_env["MQTT_TOPICS"] mqtt_client = MqttClient("pi_connect", "Random", [(i, 0) for i in mqtt_topics], notify_new_message) except IOError as error: # if not config file found exit print("ENV Json file not found, Exiting") print(error) exit(1) except: print("Error while loading config file from json") exit(1) # print(main_env) mqtt_client.create_client() mqtt_client.client.username_pw_set(mqtt_username, mqtt_password) mqtt_client.client.on_connect = mqtt_client.on_connect_handler mqtt_client.client.on_disconnect = mqtt_client.on_disconnect_handler mqtt_client.client.on_subscribe = mqtt_client.on_subscribe_handler mqtt_client.client.on_message = mqtt_client.on_message_handler mqtt_client.client.connect(mqtt_host, mqtt_port, keepalive=60) mqtt_client.start()
def start_mqtt(): global mqtt_client try: with open('env.json') as config_file: all_configs = json.load(config_file) main_env = all_configs["mqtt"] bugsnag.configure(api_key=main_env["BUGSNAG_KEY"]) mqtt_username = main_env["MQTT_USERNAME"] mqtt_password = main_env["MQTT_PASSWORD"] mqtt_host = main_env["MQTT_HOST"] mqtt_port = main_env["MQTT_PORT"] mqtt_topics = main_env["MQTT_TOPICS"] mqtt_publish_topic = main_env["PUBLISH_TOPIC"] # module_to_use = all_configs["MODULE"] mqtt_client = MqttClient("pi_connect", "Random", [(i, 0) for i in mqtt_topics], mqtt_publish_topic, shared_queue) except IOError as error: # if not config file found exit print("ENV Json file not found, Exiting") print(error) exit(1) except Exception as e: tb = traceback.format_exc() print("Error while loading config file from json") print(e) print(tb) exit(1) # print(main_env) mqtt_client.create_client() mqtt_client.client.username_pw_set(mqtt_username, mqtt_password) mqtt_client.client.on_connect = mqtt_client.on_connect_handler mqtt_client.client.on_disconnect = mqtt_client.on_disconnect_handler mqtt_client.client.on_subscribe = mqtt_client.on_subscribe_handler mqtt_client.client.on_message = mqtt_client.on_message_handler mqtt_client.client.connect(mqtt_host, mqtt_port, keepalive=60) mqtt_client.start()
help='Generate default config') args = parser.parse_args() # global isDebug # isDebug = args.isDebug if args.gen: print_conf() exit(0) # not good if args.config: global config with open(args.config) as json_file: config = json.load(json_file) pass if __name__ == '__main__': parse_args() mqtt = MqttClient(config['mqtt']) uart = UartClient(config['uart']) mqtt.uart = uart uart.mqtt = mqtt mqtt.start() uart.start() while True: pass
bleHnadler.BleConnect() def connect(devAddr): t = ConnectionHandlerThread(len(connections), devAddr) t.start() config = configparser.ConfigParser() config.read('./config/config.ini') connections = [] token = config.get('default', 'AesToken') broker = config.get('default', 'broker') mqttClient = MqttClient(broker) mqttClient.init() lineClient = LineClient(token) lineClient.NotifyMessage("spo2 gateway start!!") lineClient.NotifyMessage("mqtt broker address : " + broker) scanner = Scanner(0) devices = scanner.scan(10.0) print("scaning") addresses = [] for dev in devices: print("Device %s (%s), RSSI=%d dB" % (dev.addr, dev.addrType, dev.rssi)) for (adtype, desc, value) in dev.getScanData(): # print (" %s = %s" % (desc, value))
from mqttClient import MqttClient import controllerMessages as messages import tkinter as tk import json print("UpDown.py launched") drive_topic = "sbrick/01/sp/drive" my_client = MqttClient() my_client.setup_client() def UpCallBack(): my_client.publish(drive_topic, json.dumps(messages.getMsgUp(), sort_keys=True)) def DownCallBack(): my_client.publish(drive_topic, json.dumps(messages.getMsgDown(), sort_keys=True)) top = tk.Tk() top.title("TrailerControl") frame = tk.Frame(top) frame.pack() top.geometry("200x135") BtnUp = tk.Button(top, text=" Up ", font="Helvetica 50 bold",
class Component: __metaclass__ = ABCMeta def __init__(self, root): self.mqttHandler = MqttClient() self.mqttHandler.setup_client() self.name = "component" self.sampInterval = 1 self.loopCycles = 1 self.loopRate = 1 self.rootTopic = root self.config_topic = self.rootTopic + "/components/" + self.name self.my_topic = self.rootTopic + "/" + self.name # Component specific setup # Needs to contain all the code to be executed once # before the loop starts # Needs to be implemented in order for the main to run correctly @abstractmethod def setup(self, samplingInterval): pass # Component specific data handling and MQTT publishing # It should receive the main timestamp to stamp the package # in order to ensure coherence between components. # # Needs to be implemented in order for the main to run correctly def handleData(self, timestamp): pass # Run code for components which are supposed to loop in parallel # and do not require data acquisition, such as the camera def run(self): pass # Component specific setup JSON payload generation # (Good practice to implement it, so it's well organized) def gen_payload_message(self, data, timestamp): pass # Calculate the number of loop cycles before sampling the # sensor based on the rate the loop is run and publish # the new configuration parameters of the component def calculate_loop_cycles(self, loop_rate, timestamp): self.loopRate = loop_rate self.loopCycles = int(math.ceil(self.sampInterval / self.loopRate)) self.publishConfiguration(int(timestamp)) # Publish component configuration values to the # root/component/config topic def publishConfiguration(self, timestamp): self.mqttHandler.publish( self.config_topic, json.dumps(self.gen_curr_configuration_message(timestamp)), retain=True) # Generate current configuration message def gen_curr_configuration_message(self, timestamp): return { 'name': self.name, 'topic': self.my_topic, 'pollRate': float(self.sampInterval), 'loopRate': self.loopRate, 'cycles': self.loopCycles, 'timestamp': timestamp } # Set component topic def set_topic(self, component_name): self.name = component_name self.config_topic = self.rootTopic + "/components/" + self.name self.my_topic = self.rootTopic + "/" + self.name