def run(self): topic = self.create_topic(self.mqtt_data) # producer_data.append(dic_data["payload"]) mqtt.publish(topic, self.mqtt_data)
def open(box_name): mqtt.publish('open_box', box_name) box = get_box_from_firebase(box_name) owner = box['owner'] message = box['message'] message.append({ 'sender':owner, 'time':my_date_time(), 'contact':1 }) reserved_date = box['reserved_date'] end_date = box['end_date'] username = box['username'] password = box['password'] push = firebase.put('/boxs', box_name, { 'owner': owner, 'username': username, 'status' : 'reserved', 'reserved_date': reserved_date, 'end_date' : end_date, 'password': password, 'message': message }) #print(box_name) return redirect(url_for('box', box_name=box_name))
def reserve(box_name, number): reserve_date = my_date_time() if number == 1: end_date = add_date(day=1,month=0,year=0) elif number == 2: end_date = add_date(day=0,month=1,year=0) elif number == 3: end_date = add_date(day=0,month=0,year=1) box = get_box_from_firebase(box_name) if box['status'] == 'no': # mqtt.publish('is_reserved', box_name) mqtt.publish('status_box', "reserved") push = firebase.put('/boxs', box_name, { 'owner': current_user.first_name+" "+current_user.last_name, 'username': current_user.username, 'status' : 'reserved', 'reserved_date': reserve_date, 'end_date' : end_date, 'password': [1], 'message': [1] }) historys = get_history_from_firebase(current_user.username) box_names = historys['box_name'] box_names.append(box_name) history = historys['history'] push2 = firebase.put('/historys', current_user.username, { 'box_name':box_names, 'history':history }) return redirect(url_for('box', box_name=box_name)) else: return redirect(url_for('box', box_name=box_name))
def change_state(toggle, device): txt = 'cmnd/' + device + '/power' if toggle == 'on': objects.data[device].on() mqtt.publish(txt, 'on') if toggle == 'off': objects.data[device].off() mqtt.publish(txt, 'off')
def command(): command=request.args.get('command') print(command) print(current_user.id) tmp=Tmp.query.filter_by(user_id=current_user.id).first() mqtt.publish(str(current_user.id)+"/"+tmp.controller, command, 1) return "OK"
def dashboard(): check_devices() devices = [] for device in RegisteredDevice.query.all(): device_payload = json.loads(device.payload) devices.append({ 'ip_address': device.ip_address, 'name': device.name, 'payload': device_payload, 'time_registered': device.time_registered }) mqtt.publish( os.path.join('homeConnect/device/', device_payload['topic']), json.dumps({'action': 'get_current_state'})) return render_template('dashboard.html', devices=devices)
def run_injection_handler(data): condition = data['condition'] slot = data['slot'] items = data['items'] s = '\"{}\" : [ {} ] }} '.format(slot, items[0:-1]) c = "[ \"{}\",{{".format(condition) d = "{\"operations\": [" e = " ] ] }" f = ''.join([d, c, s, e]) mqtt.publish('hermes/asr/inject', payload=f) socketio.emit('injectionsComplete', "Done", namespace='/device')
def device_change_state(): check_devices() response = {'status': 'error'} if request.method == 'POST': payload = request.get_json() print('reached 7', payload['name']) device = RegisteredDevice.query.filter_by(name=payload['name']).first() if device: device_payload = json.loads(device.payload) message = { 'action': 'change_state', 'change_to': payload['change_to'] } mqtt.publish( os.path.join('homeConnect/device/', device_payload['topic']), json.dumps(message)) response = {'status': 'success'} return json.dumps(response)
def sending(box_name): box = get_box_from_firebase(box_name) form = BoxPasswordForm() if form.validate_on_submit(): pw = form.password.data password = box['password'] if pw in password: sender = current_user.first_name+" "+current_user.last_name # print(sender) # print(pw) password.remove(pw) # print(password) message = box['message'] message.append({ 'sender':sender, 'time':my_date_time(), 'contact':current_user.cellular }) reserved_date = box['reserved_date'] end_date = box['end_date'] owner = box['owner'] username = box['username'] push = firebase.put('/boxs', box_name, { 'owner': owner, 'username': username, 'status' : 'reserved', 'reserved_date': reserved_date, 'end_date' : end_date, 'password': password, 'message': message }) flash(f'success {box_name} can open!', 'green lighten-5') #function เปิดกล่อง mqtt.publish('open_box', box_name) path = '/Users/'+username data = firebase.get(path, None) thumnail = data['link'] #print(thumnail) owner = box['owner'] end_date = box['end_date'] return render_template('box.html', title='Box '+box_name, box_name=box_name, owner=owner, end_date=end_date, form=form, thumnail=thumnail) else: flash(f'invalid ! please contact your customer', 'red lighten-5') return redirect(url_for('box', box_name=box_name))
def hello(): # mqtt.publish('home/testtopic', 'hello world') value = random.choice([10, 20, 30, 49, 23, 34, 12, 5]) stype = random.choice(['h', 't', 'g']) message = '{"v": ' + str( value ) + ', "t": "' + stype + '", "id": "sensor1", "ts": "04-03-12 12:00"}' mqtt.publish('home/testtopic', message) data = SensorData.query.order_by(desc( SensorData.timestamp)).limit(20).all() data_humidity = SensorData.query.order_by(desc( SensorData.timestamp)).filter_by(type='h').limit(20).all() data_temperature = SensorData.query.order_by(desc( SensorData.timestamp)).filter_by(type='t').limit(20).all() data_gas = SensorData.query.order_by(desc( SensorData.timestamp)).filter_by(type='g').limit(20).all() if len(data_temperature) > 0: health_predicted = int( predict_health((data_temperature[-1]).value, (data_humidity[-1]).value)[0]) else: health_predicted = "" data_humidity = transform_to_chart_data(data_humidity, "humidity") data_temperature = transform_to_chart_data(data_temperature, "temperature") data_gas = transform_to_chart_data(data_gas, "gas") # humidity_data = [['-', "Humidity"], ['%', 20] , ['%', 40], ['%', 50], ['%', 20], ['%', 80] ] return render_template("index.html", data=data, humidity_data=data_humidity, temperature_data=data_temperature, gas_data=data_gas, health_predicted=health_predicted)
def handle_publish(json_str): data = json.loads(json_str) mqtt.publish(data['topic'], data['message'], data['qos'])
def send(msg): payload = form_payload('{}.{}'.format(msg, round(time()))) mqtt.publish(app.config['MQTT_OUT_TOPIC'], payload)