-
Notifications
You must be signed in to change notification settings - Fork 0
/
p3_tools.py
executable file
·198 lines (174 loc) · 5.12 KB
/
p3_tools.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
'''
Created on 29.12.2018
@author: andrek
'''
import os
import sys
from argparse import Namespace
import logging
from datetime import datetime,timedelta
from .device import AlexaDevices, AlexaDevice
import json
def CreateStreamSettings(myItemConf):
myRetVal = []
for k,v in myItemConf.camera_setting.items():
myRetVal.append(v)
return myRetVal
def CreateStreamPayLoad(myItemConf):
now = datetime.now()
offset = timedelta(seconds=86400) # Experition time 24h
now = now + offset
now = now.isoformat()
expirationDate = now[0:22]+'Z'
cameraStream = []
cameraUri = []
imageuri = myItemConf.camera_imageUri
if myItemConf.alexa_auth_cred != '':
imageuri = imageuri.replace("//","//"+myItemConf.alexa_auth_cred+"@")
if len(myItemConf.proxied_Urls) == 0:
for k,v in myItemConf.camera_uri.items():
cameraUri.append(v)
else:
for k,v in myItemConf.proxied_Urls.items():
cameraUri.append(v)
i=0
for k,v in myItemConf.camera_setting.items():
if myItemConf.alexa_auth_cred != '' and myItemConf.alexa_proxy_credentials == '':
uri = v['protocols'][0].lower()+"://"+myItemConf.alexa_auth_cred+'@'+cameraUri[i]
elif myItemConf.alexa_proxy_credentials != '':
uri = v['protocols'][0].lower()+"://"+myItemConf.alexa_proxy_credentials+'@'+cameraUri[i]
else:
uri = v['protocols'][0].lower()+"://"+cameraUri[i]
# Find highest resolution
streamResolution = {}
highestRes = 0
for res in v['resolutions']:
test = res['width']
if res['width'] > highestRes:
streamResolution = res
highestRes = res['width']
myStream= {
"uri":uri,
"expirationTime": expirationDate,
"idleTimeoutSeconds": 30,
"protocol": v['protocols'][0].upper(),
"resolution":streamResolution,
"authorizationType": v['authorizationTypes'][0].upper(),
"videoCodec": v['videoCodecs'][0].upper(),
"audioCodec": v['audioCodecs'][0].upper()
}
cameraStream.append(myStream)
i +=1
response = {"cameraStreams": cameraStream}
response.update({ "imageUri":imageuri})
# only interesting for debugging
#DumpStreamInfo(response)
return response
def DumpStreamInfo(directive):
myFile = open("streamdump.txt","a+")
myString=json.dumps(directive)
myFile.write(myString+"\r\n")
myFile.write("=====================\r\n")
myFile.close()
# Calculating HSV to RGB based on
# https://www.rapidtables.com/convert/color/hsv-to-rgb.html
def hsv_to_rgb(h, s, v):
if( h=="" ):
h=0
if( s=="" ):
s=0
if( v=="" ):
v=0
if( h<0 ):
h=0
if( s<0 ):
s=0
if( v<0 ):
v=0
if( h>=360 ):
h=359
if( s>100 ):
s=100
if( v>100 ):
v=100
C = v*s
hh = h/60
X = C*(1-abs(hh%2-1))
r = g = b = 0
if( hh>=0 and hh<1 ):
r = C
g = X
elif( hh>=1 and hh<2 ):
r = X
g = C
elif( hh>=2 and hh<3 ):
g = C
b = X
elif( hh>=3 and hh<4 ):
g = X
b = C
elif( hh>=4 and hh<5 ):
r = X
b = C
else:
r = C
b = X
m = v-C
r += m
g += m
b += m
r *= 255.0
g *= 255.0
b *= 255.0
r = round(r)
g = round(g)
b = round(b)
return r,g,b
def rgb_to_hsv(r, g, b):
if( r=="" ):
r=0
if( g=="" ):
g=0
if( b=="" ):
b=0
if( r<0 ):
r=0
if( g<0 ):
g=0
if( b<0 ):
b=0
if( r>255 ):
r=255
if( g>255 ):
g=255
if( b>255 ):
b=255
r/=255.0
g/=255.0
b/=255.0
M = max(r,g,b)
m = min(r,g,b)
C = M-m
if( C==0 ):
h=0
elif( M==r ):
h=((g-b)/C)
h=h%6
elif( M==g ):
h=(b-r)/C+2
else:
h=(r-g)/C+4
h*=60;
if( h<0 ):
h+=360
else:
h*=60
v = M
if( v==0 ):
s = 0
else:
s = C/v
h = round(h,0)
s = round(s,4)
v = round(v,4)
return h,s,v