-
Notifications
You must be signed in to change notification settings - Fork 0
/
testMQTTClient.py
97 lines (81 loc) · 3.32 KB
/
testMQTTClient.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 20 19:55:22 2018
@author: aangris
"""
import paho.mqtt.client as mqtt #import the client1
import tweepy
from ContractHandler import ContractHandler
consumer_key = 'pTHSW5yxc0lipPvptsExXWkmI'
consumer_secret = 'YTXlvzvg3Qc2dprNIY8JPeg44yUllX4UPsgYk3GWHU4w3GpcmA'
access_token = '969665472043077633-AUMIeR6JrMfLKtg6sPSc97Wq099mFrQ'
access_token_secret = 'RLIpvvC5vupXvT5oGW05l8F4NefbSOHHGuSTJxk1NxI2e'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
class stateWatcher:
""" A simple class, set to watch its variable. """
def __init__(self, value):
self.variable = value
self.ch_variable =value
self.old = ""
self.counter = 0
self.CH = ContractHandler()
self.CH.test()
self.machine1_name = "CNC"
self.machine1_mac = "aa"
self.machine1_time = 4
self.machine1_rate = 1
self.CH.addUser()
self.CH.addMachine(self.machine1_name, self.machine1_mac, self.machine1_time, self.machine1_rate)
print(self.CH.getMachineInfo(self.CH.mining_account, 0))
def set_value(self, new_value):
if self.variable != new_value:
self.old = self.variable
self.variable =new_value
self.ch_variable = self.old
self.on_change()
else:
pass
def on_change(self):
# do stuff when variable changed
if self.variable=="idle":
self.counter+=1
self.CH.updateMachineStatus(self.CH.mining_account, self.machine1_mac, True, self.machine1_time, self.machine1_rate)
#while not self.CH.getMachineInfo(self.machine1_mac, 0):
# pass #this will stall the program until the transaction is recorded in a block
tweet = "Update#"+str(self.counter) + " : I am now available!"
print(tweet)
#api.update_status(status=tweet)
else:
self.counter += 1
self.CH.updateMachineStatus(self.CH.mining_account, self.machine1_mac, False, self.machine1_time, self.machine1_rate)
#while self.CH.getMachineInfo(self.machine1_mac, 0):
# pass #this will stall the program until the transaction is recorded in a block
tweet = "Update#"+str(self.counter) + " : I am now busy"
print(tweet)
#api.update_status(status=tweet)
#tweet = "I am now "+self.variable
Broker="iot.eclipse.org"
sub_topic = "LCNC/data" # send messages to this topic
pub_topic = "LCNC/commands"
state = stateWatcher('idle')
# when connecting to mqtt do this;
prev = "idle"
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe(sub_topic)
# when receiving a mqtt message do this;
def on_message(client, userdata, msg):
message = str(msg.payload)
#print(msg.topic+" "+message)
mach_status=str(message)[2:-1]
#print(mach_status)
state.set_value(mach_status)
def on_publish(mosq, obj, mid):
print("mid: " + str(mid))
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(Broker, 1883, 60)
client.loop_start()