Posts: 5
Threads: 1
Joined: Feb 2023
Reputation:
0
07.02.2023, 23:57
(This post was last modified: 08.02.2023, 09:13 by coolaew .)
Hello!
Using a SE spaceLYnk I'm trying to send an image to a 2N IP Verso intercom when the firealarm is activated and remove the image when the fire alarm resets.
The intercom uses HTTPS and Digest authentication.
I've got it working in postman but not from the SpaceLYnk.
I only got the DELETE part working with basic authentication.
Code:
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
fire =
grp.getvalue (
'32/1/1' )
if fire ==
true then
log (
'FIREALARM ACTIVE' )
elseif fire ==
false then
log (
'No fire' )
local https =
require (
"ssl.https" )
local ltn12 =
require (
"ltn12" )
local response_body = {}
local request_body =
""
local res ,
code ,
response_headers ,
status =
https.request {
url =
"https://test:test@10.0.0.230/api/display/image?display=ext1" ,
method =
"DELETE" ,
source =
ltn12.source.string (
request_body ),
sink =
ltn12.sink.table (
response_body ),
protocol =
"tlsv1_2"
}.
if code ==
200 then
log (
"Image deleted successfully!" )
else
log (
"Error deleting image: " ..
code )
end
else
log (
'ERROR' )
end
I found some threads here about digest authentication and image handling, but I'm not experienced with coding so I was not able to put the code together from what I found to do what I need.
I have saved the image to /img/ which i created in the root of the ftp server logged in as ftp.
Here's a link to the 2N API documentation, part 5.9.2 is about sending images and clearing the display.
https://wiki.2n.com/hip/hapi/latest/en
Posts: 8097
Threads: 43
Joined: Jun 2015
Reputation:
471
Try this.
filedata variable must contain your image in JPEG format. Use io.readfile() to read a local file.
Code:
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
require (
'ltn12' )
require (
'ssl.https' )
boundary =
os.date (
'%d%m%Y%H%M%S' )
filedata =
'...'
body =
table.concat ({
'--' ..
boundary ,
'Content-Disposition: form-data; name="blob-image"; filename="picture.jpeg"' ,
'Content-Type: image/jpeg' ,
'' ,
filedata ,
'--' ..
boundary ..
'--' ,
''
},
'\r\n' )
resp = {}
res ,
code ,
response_headers ,
status =
ssl.https.request ({
url =
'https://test:test@10.0.0.230/api/display/image?display=ext1' ,
sink =
ltn12.sink.table (
resp ),
method =
'PUT' ,
source =
ltn12.source.string (
body ),
headers = {
[
'content-length' ] = #
body ,
[
'content-type' ] =
'multipart/form-data; boundary=' ..
boundary
}
})
log (
res ,
code ,
table.concat (
resp ))
For digest auth you can use this example:
https://forum.logicmachine.net/showthrea...64#pid9364
Replace
ssl.https.request in the above code with
request from the digest code.
Posts: 5
Threads: 1
Joined: Feb 2023
Reputation:
0
I updated the filedata line
Code:
1
filedata =
'io.readfile(/img/STOP_DNE_241x320.jpeg)' But I get this error now
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
08.02.2023 22 :
19 :
12
*
arg :
1
*
number :
1
*
arg :
2
*
number :
200
*
arg :
3
*
string : {
"success" :
false ,
"error" : {
"code" :
12 ,
"param" :
"blob-image" ,
"description" :
"invalid parameter value"
}
}
Posts: 1794
Threads: 6
Joined: Jul 2015
Reputation:
120
You converted the whole command into a string by placing the '' at the wrong position ..
Try: filedata = io.readfile(‘/img/STOP_DNE_241x320.jpeg')
Posts: 5
Threads: 1
Joined: Feb 2023
Reputation:
0
09.02.2023, 05:26
(This post was last modified: 09.02.2023, 05:43 by coolaew .)
Ok, tried that now, new error.
Code:
1 2 3 4
09.02.2023 06 :
25 :
54
User script :
19 :
invalid value (
nil )
at index 5 in table for 'concat'
stack traceback :
[
C ]:
in function 'concat' I'm suspecting that either the path to the file is wrong or there's a permission missing somewhere.
I've tried using /img/FILE and /data/ftp/img/FILE
Posts: 8097
Threads: 43
Joined: Jun 2015
Reputation:
471
Try this (assuming that you've uploaded the image to Vis. graphics > Image / backgrounds):
Code:
1
filedata =
io.readfile (‘/
www /
scada /
resources /
img /
STOP_DNE_241x320.jpeg ')
Posts: 5
Threads: 1
Joined: Feb 2023
Reputation:
0
Thanks! Finally got it working.
It might have been your suggestion, or it might have been my typo in the filename that fixed it... 241/214...
Now I just have to get digest auth working and I'm all set
Code:
1 2 3 4 5 6 7 8 9
09.02.2023 15 :
38 :
52
*
arg :
1
*
number :
1
*
arg :
2
*
number :
200
*
arg :
3
*
string : {
"success" :
true
}
Posts: 5
Threads: 1
Joined: Feb 2023
Reputation:
0
Thank you so much!
Finally got it working with digest, it could problably be done prettier, but this works for me
Code:
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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
fire =
grp.getvalue (
"32/1/1" )
if fire ==
true then
log (
"FIREALARM ACTIVE" )
local skthttp =
require (
"socket.http" )
local skturl =
require (
"socket.url" )
local ltn12 =
require (
"ltn12" )
local md5sum =
require (
"encdec" ).
md5
boundary =
os.date (
"%d%m%Y%H%M%S" )
filedata =
io.readfile (
"/www/scada/resources/img/STOP_DNE_214x320.jpeg" )
body =
table.concat (
{
"--" ..
boundary ,
'Content-Disposition: form-data; name="blob-image"; filename="picture.jpeg"' ,
"Content-Type: image/jpeg" ,
"" ,
filedata ,
"--" ..
boundary ..
"--" ,
""
},
"\r\n"
)
resp = {}
local hash =
function (...)
return md5sum (
table.concat ({...},
":" ))
end
local parse_header =
function (
header )
local result = {}
for key ,
value in (
header ..
"," ):
gmatch (
"(%w+)=(.-)," )
do
if value :
sub (
1 ,
1 ) ==
'"' then
result [
key :
lower ()] =
value :
sub (
2 , -
2 )
else
result [
key :
lower ()] =
value
end
end
return result
end
local make_digest_header =
function (
headers )
local digest = {}
for _ ,
header in ipairs (
headers )
do
if not header.unquote then
header [
2 ] =
'"' ..
header [
2 ] ..
'"'
end
digest [#
digest +
1 ] =
header [
1 ] ..
"=" ..
header [
2 ]
end
return "Digest " ..
table.concat (
digest ,
", " )
end
local _request =
function (
req )
if not req.url then
return nil ,
"missing url"
end
local url =
skturl.parse (
req.url )
local user ,
password =
url.user ,
url.password
local sink =
req.sink
if not user or not password then
return nil ,
"missing credentials in url"
end
url.user ,
url.password ,
url.authority ,
url.userinfo =
nil ,
nil ,
nil ,
nil
req.url =
skturl.build (
url )
local source
if req.source then
local chunks = {}
local capture =
function (
chunk )
if chunk then
chunks [#
chunks +
1 ] =
chunk
end
return chunk
end
local chunk_id =
0
source =
function ()
chunk_id =
chunk_id +
1
return chunks [
chunk_id ]
end
req.source =
ltn12.source.chain (
req.source ,
capture )
end
req.sink =
nil
local body ,
code ,
hdrs =
skthttp.request (
req )
if code ==
401 and hdrs [
"www-authenticate" ]
then
local ht =
parse_header (
hdrs [
"www-authenticate" ])
if not ht.realm or not ht.nonce then
return nil ,
"missing realm/nonce from response"
end
local qop =
ht.qop
if qop and qop ~=
"auth" then
return nil ,
"unsupported qop " ..
tostring (
qop )
end
if ht.algorithm and ht.algorithm :
lower () ~=
"md5" then
return nil ,
"unsupported algo " ..
tostring (
ht.algorithm )
end
local nc =
"00000001"
local cnonce =
string.format (
"%08x" ,
os.time ())
local uri =
skturl.build ({
path =
url.path ,
query =
url.query })
local method =
req.method or "GET"
local response =
hash (
hash (
user ,
ht.realm ,
password ),
ht.nonce ,
nc ,
cnonce ,
"auth" ,
hash (
method ,
uri ))
req.headers =
req.headers or {}
local auth = {
{
"username" ,
user },
{
"realm" ,
ht.realm },
{
"nonce" ,
ht.nonce },
{
"uri" ,
uri },
{
"cnonce" ,
cnonce },
{
"nc" ,
nc ,
unquote =
true },
{
"qop" ,
"auth" },
{
"algorithm" ,
"MD5" },
{
"response" ,
response }
}
if ht.opaque then
table.insert (
auth , {
"opaque" ,
ht.opaque })
end
req.headers.authorization =
make_digest_header (
auth )
if not req.headers.cookie and hdrs [
"set-cookie" ]
then
local cookie = (
hdrs [
"set-cookie" ] ..
";" ):
match (
"(.-=.-)[;,]" )
if cookie then
req.headers.cookie =
"$Version: 0; " ..
cookie ..
";"
end
end
if req.source then
req.source =
source
end
req.sink =
sink
body ,
code ,
hdrs =
skthttp.request (
req )
end
return body ,
code ,
hdrs
end
local request =
function (
url )
local t =
type (
url )
if t ==
"table" then
return _request (
table.clone (
url ))
elseif t ==
"string" then
local req = {}
local _ ,
code ,
headers =
_request ({
url =
url ,
sink =
ltn12.sink.table (
req )})
return table.concat (
req ),
code ,
headers
end
end
res ,
code ,
response_headers ,
status =
request (
{
url =
"https://test:test@10.0.0.230/api/display/image?display=ext1" ,
sink =
ltn12.sink.table (
resp ),
method =
"PUT" ,
source =
ltn12.source.string (
body ),
headers = {
[
"content-length" ] = #
body ,
[
"content-type" ] =
"multipart/form-data; boundary=" ..
boundary
}
}
)
log (
res ,
code ,
table.concat (
resp ))
elseif fire ==
false then
log (
"Firealarm reset" )
local skthttp =
require (
"socket.http" )
local skturl =
require (
"socket.url" )
local ltn12 =
require (
"ltn12" )
local md5sum =
require (
"encdec" ).
md5
local hash =
function (...)
return md5sum (
table.concat ({...},
":" ))
end
local parse_header =
function (
header )
local result = {}
for key ,
value in (
header ..
"," ):
gmatch (
"(%w+)=(.-)," )
do
if value :
sub (
1 ,
1 ) ==
'"' then
result [
key :
lower ()] =
value :
sub (
2 , -
2 )
else
result [
key :
lower ()] =
value
end
end
return result
end
local make_digest_header =
function (
headers )
local digest = {}
for _ ,
header in ipairs (
headers )
do
if not header.unquote then
header [
2 ] =
'"' ..
header [
2 ] ..
'"'
end
digest [#
digest +
1 ] =
header [
1 ] ..
"=" ..
header [
2 ]
end
return "Digest " ..
table.concat (
digest ,
", " )
end
local _request =
function (
req )
if not req.url then
return nil ,
"missing url"
end
local url =
skturl.parse (
req.url )
local user ,
password =
url.user ,
url.password
local sink =
req.sink
if not user or not password then
return nil ,
"missing credentials in url"
end
url.user ,
url.password ,
url.authority ,
url.userinfo =
nil ,
nil ,
nil ,
nil
req.url =
skturl.build (
url )
local source
if req.source then
local chunks = {}
local capture =
function (
chunk )
if chunk then
chunks [#
chunks +
1 ] =
chunk
end
return chunk
end
local chunk_id =
0
source =
function ()
chunk_id =
chunk_id +
1
return chunks [
chunk_id ]
end
req.source =
ltn12.source.chain (
req.source ,
capture )
end
req.sink =
nil
local body ,
code ,
hdrs =
skthttp.request (
req )
if code ==
401 and hdrs [
"www-authenticate" ]
then
local ht =
parse_header (
hdrs [
"www-authenticate" ])
if not ht.realm or not ht.nonce then
return nil ,
"missing realm/nonce from response"
end
local qop =
ht.qop
if qop and qop ~=
"auth" then
return nil ,
"unsupported qop " ..
tostring (
qop )
end
if ht.algorithm and ht.algorithm :
lower () ~=
"md5" then
return nil ,
"unsupported algo " ..
tostring (
ht.algorithm )
end
local nc =
"00000001"
local cnonce =
string.format (
"%08x" ,
os.time ())
local uri =
skturl.build ({
path =
url.path ,
query =
url.query })
local method =
req.method or "GET"
local response =
hash (
hash (
user ,
ht.realm ,
password ),
ht.nonce ,
nc ,
cnonce ,
"auth" ,
hash (
method ,
uri ))
req.headers =
req.headers or {}
local auth = {
{
"username" ,
user },
{
"realm" ,
ht.realm },
{
"nonce" ,
ht.nonce },
{
"uri" ,
uri },
{
"cnonce" ,
cnonce },
{
"nc" ,
nc ,
unquote =
true },
{
"qop" ,
"auth" },
{
"algorithm" ,
"MD5" },
{
"response" ,
response }
}
if ht.opaque then
table.insert (
auth , {
"opaque" ,
ht.opaque })
end
req.headers.authorization =
make_digest_header (
auth )
if not req.headers.cookie and hdrs [
"set-cookie" ]
then
local cookie = (
hdrs [
"set-cookie" ] ..
";" ):
match (
"(.-=.-)[;,]" )
if cookie then
req.headers.cookie =
"$Version: 0; " ..
cookie ..
";"
end
end
if req.source then
req.source =
source
end
req.sink =
sink
body ,
code ,
hdrs =
skthttp.request (
req )
end
return body ,
code ,
hdrs
end
local request =
function (
url )
local t =
type (
url )
if t ==
"table" then
return _request (
table.clone (
url ))
elseif t ==
"string" then
local req = {}
local _ ,
code ,
headers =
_request ({
url =
url ,
sink =
ltn12.sink.table (
req )})
return table.concat (
req ),
code ,
headers
end
end
local response_body = {}
local request_body =
""
local res ,
code ,
response_headers ,
status =
request {
url =
"https://test:test@10.0.0.230/api/display/image?display=ext1" ,
method =
"DELETE" ,
source =
ltn12.source.string (
request_body ),
sink =
ltn12.sink.table (
response_body ),
protocol =
"tlsv1_2"
}
if code ==
200 then
log (
"Image deleted successfully!" )
else
log (
"Error deleting image: " ..
code )
end
else
log (
"ERROR" )
end