forked from switchdoclabs/SDL_Pi_SkyWeather2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testSkyCamRemote.py
177 lines (142 loc) · 4.95 KB
/
testSkyCamRemote.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#
# tests SkyCam Remote and the updates parameters
#
import json
import paho.mqtt.client as mqtt
import os
import socket
import traceback
#commands
MQTTUPDATEPARAM = 10
MQTTCYCLECHANGE = 11
MQTTSTARTDELAY = 12
MQTTTURNOFFBLINK = 13
MQTTBLINKXTIMES = 14
MQTTSETTODEFAULTS = 15
MQTTREBOOT = 16
MQTTRESOLUTION = 17
MQTTERASEMEMORY = 18
from subprocess import check_output
def on_log(client, userdata, level, buf):
print("log: ",buf)
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe("SKYCAM/+/INFO")
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print(msg.topic)
print(msg.payload)
try:
if (cameraID != ""):
SplitTopic = msg.topic
SplitTopic = SplitTopic.split("/")
if (((SplitTopic[1] == cameraID) or (cameraID == "+")) and (SplitTopic[2] == "INFO")):
myMessage = json.loads(msg.payload)
if (myMessage["messagetype"] == "4"):
# last INFO message - SkyCam only listens for a short while
sendMessage(client, cameraID, sendWhatCommand)
except:
traceback.print_exc()
def sendMessage(client, cameraID, messageType):
# send example command to SkyCameraRemote
# send Blynk X Times
# get msg topic
MyTopic = "SKYCAM"+"/"+cameraID+"/"+"COMMANDS"
# set up JSON
myIP = check_output(['hostname', '-I'])
myIP = myIP.decode()
myIP = myIP.replace("\n","")
myIP = myIP.replace(" ","")
print("sending messagetype %s to:%s " %( messageType, MyTopic ))
# Commands:
if (messageType == MQTTBLINKXTIMES):
# send blink message
myMessage = {
"messagetype": MQTTBLINKXTIMES,
"myip": myIP,
"length": 300,
"count": 3
}
myMessage = json.dumps(myMessage)
client.publish(MyTopic, myMessage)
if (messageType == MQTTCYCLECHANGE):
# send time to sleep message
myMessage = {
"messagetype": MQTTCYCLECHANGE,
"myip": myIP,
"timetosleep": 60
}
myMessage = json.dumps(myMessage)
client.publish(MyTopic, myMessage)
if (messageType == MQTTSTARTDELAY):
# send time to wait for contrast adjust
myMessage = {
"messagetype": MQTTSTARTDELAY,
"myip": myIP,
"contrastdelay":1000
}
myMessage = json.dumps(myMessage)
client.publish(MyTopic, myMessage)
if (messageType == MQTTREBOOT):
#reboot SkyCam
myMessage = {
"messagetype": MQTTREBOOT,
"myip": myIP
}
myMessage = json.dumps(myMessage)
client.publish(MyTopic, myMessage)
if (messageType == MQTTRESOLUTION):
# set resolution
myMessage = {
"messagetype": MQTTRESOLUTION,
"myip": myIP,
"framesize":10 # set UXGA
}
myMessage = json.dumps(myMessage)
client.publish(MyTopic, myMessage)
if (messageType == MQTTERASEMEMORY):
# set resolution
myMessage = {
"messagetype": MQTTERASEMEMORY,
"myip": myIP
}
myMessage = json.dumps(myMessage)
client.publish(MyTopic, myMessage)
if (messageType == MQTTUPDATEPARAM):
# update camera parameters
myMessage = {
"messagetype": MQTTUPDATEPARAM,
"myip": myIP,
"sensorparams": "0;0;0;0;1;1;0;1;0;0;300;1;0;0;0;1;1;1;0;0;1;0;"
#"sensorparams": "0;0;0;0;1;1;0;1;0;0;300;1;0;0;0;1;1;1;0;0;1;1;"
}
myMessage = json.dumps(myMessage)
client.publish(MyTopic, myMessage)
print("Messge Sent:",myMessage)
# main program
# what ID to test
#cameraID = "3BAD"
#cameraID = "F329"
#cameraID = "DE45"
cameraID = "26FD"
#cameraID = "+" #sends to all cameras
# this command will be sent after an INFO messagetype 4 from cameraID
#sendWhatCommand = MQTTERASEMEMORY
sendWhatCommand = MQTTCYCLECHANGE
#sendWhatCommand = MQTTRESOLUTION
#sendWhatCommand = MQTTBLINKXTIMES
#sendWhatCommand = MQTTUPDATEPARAM
#sendWhatCommand = MQTTSTARTDELAY
#sendWhatCommand = MQTTREBOOT
#
client = mqtt.Client()
client.on_log=on_log
client.on_connect = on_connect
client.on_message = on_message
client.connect("localhost", port=1883)
# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
client.loop_forever()