-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamera.py
More file actions
77 lines (63 loc) · 2.1 KB
/
Camera.py
File metadata and controls
77 lines (63 loc) · 2.1 KB
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
from picamera import PiCamera
import paho.mqtt.publish as publish
import paho.mqtt.client as mqtt
import threading
import MyCamera
"""
on_connect는 subscriber가 브로커에 연결하면서 호출할 함수
rc가 0이면 정상접속이 됐다는 의미
"""
# 전역변수
uuid = ""
class Check():
def __init__(self):
self.data = 0
def getData(self):
return self.data
def setData(self, data):
self.data = data
class MqttCamera():
def __init__(self):
client = mqtt.Client()
client.on_connect = self.on_connect
client.on_message = self.on_message
client.connect("15.164.46.54", 1883, 60)
self.thread = None
self.camera = MyCamera.Camera()
self.check = Check()
client.loop_forever()
def on_connect(self, client, userdata, flags, rc):
print("connect.." + str(rc))
if rc == 0:
client.subscribe("eyeson/ruuid")
else:
print("연결실패")
def mywhileRun(self):
global uuid
while self.check.getData() == 1:
frame = self.camera.getStreaming()
publish.single("eyeson/" + uuid, frame, hostname="15.164.46.54")
# 메시지가 도착됐을때 처리할 일들 - 여러가지 장비 제어하기, Mongodb에 저장
def on_message(self, client, userdata, msg):
global uuid
try:
myval = msg.payload.decode("utf-8")
myval = myval.split("/")
if myval[0] == "android":
if myval[1] == "camera":
if myval[2] == "on":
uuid = myval[3]
print("camera on")
self.check.setData(1)
self.thread = threading.Thread(target=self.mywhileRun)
self.thread.start()
elif myval[2] == "off":
print("camera off")
self.check.setData(0)
except:
pass
if __name__ == "__main__":
try:
mymqtt = MqttCamera()
except KeyboardInterrupt:
print("종료")