This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

BIM ioT
#1
Hi, I have to connect LM to Bim ioT ACCA Software.  


Communication takes place via JSON strings such as the following:

READING MESSAGE:

{
"payload":{
"Type":
"Item":{
"Name":
"State":
"Type":
"Editable":
"Link":
},
"TimeStamp":
},
}



WRITING MESSAGE

{
“Action”:
“Controller”:
“ItemName”:
“ItemState”:
}




Hon can arrange that with LM?


Thank you so much


Peppe
Reply
#2
JSON is just a data format but what is used as a transport, HTTP or something else?
Reply
#3
from the notes provided: "The connection to usBIM.IoT takes place via certified Web Sockets. The certificate is provided by
Acca and the flow of incoming and outgoing data is regularized by the formalism shown below "
Reply
#4
(06.05.2021, 15:08)admin Wrote: JSON is just a data format but what is used as a transport, HTTP or something else?

I thought of translating the notes provided by our client to better understand the problem.
I hope you can help me.

Peppe

P.S. This BIM standard, as you certainly know, is expanding a lot and will soon become, at least in Italy, mandatory for any building project. So I think it is important to understand how to interface LM.

Attached Files
.pdf   Bim Iot message protocol.pdf (Size: 59.96 KB / Downloads: 24)
Reply
#5
You can use this library to create a websocket client script on LM: https://forum.logicmachine.net/showthrea...23#pid7823
Reply
#6
(07.05.2021, 07:59)admin Wrote: You can use this library to create a websocket client script on LM: https://forum.logicmachine.net/showthrea...23#pid7823



they send me the attached certificate keys (temporary) for the ws connections.... Can you explain me how I could use them on LM?

the address of the server to connect is 


wss://test-hub.usbim.com

Attached Files
.zip   Komponext CERT.zip (Size: 6.62 KB / Downloads: 7)
Reply
#7
1. Install 2021 RC1 firmware.
2. Create a user library named websocket using the attached source.
3. Upload certificates via FTP using ftp login, rename files with long names to cert.pem and key.pem accordingly.
4. Create a resident script with 0 sleep time.
Code:
if not client then
  require('json')
  ws = require('websocket')
  url = 'wss://test-hub.usbim.com'

  client, err = ws.client('sync', 1)

  opts = {
    protocol = 'tlsv13',
    key = '/home/ftp/key.pem',
    password = [[*q&'wrKQsx+#bj8pbEV=%7Gpr"S+sxV2ngr`N}SjCC`X#rtt!wY6PL`d2'3!^x}u]],
    certificate = '/home/ftp/cert.pem',
    cafile = '/home/ftp/ca-chain.cert.pem',
  }

  res, err = client:connect(url, opts)
  if not res then
    log('connection failed', err)
    client:sock_close()
    client = nil
  end
end

if client then
  frame, opcode, clean, code, reason = client:receive()
  log(frame, opcode, clean, code, reason)
  if frame then
    data = json.pcdecode(frame)
    log(data)
  elseif reason ~= 'timeout' then
    log('connection dropped', reason)
    client:sock_close()
    client = nil
  end
end

This example is only a starting point, it should be extended with additional send/receive logic.

Attached Files
.lua   websocket.lua (Size: 13.95 KB / Downloads: 15)
Reply
#8
(11.05.2021, 07:08)admin Wrote: 1. Install 2021 RC1 firmware.
2. Create a user library named websocket using the attached source.
3. Upload certificates via FTP using ftp login, rename files with long names to cert.pem and key.pem accordingly.
4. Create a resident script with 0 sleep time.
Code:
if not client then
  require('json')
  ws = require('websocket')
  url = 'wss://test-hub.usbim.com'

  client, err = ws.client('sync', 1)

  opts = {
    protocol = 'tlsv13',
    key = '/home/ftp/key.pem',
    password = [[*q&'wrKQsx+#bj8pbEV=%7Gpr"S+sxV2ngr`N}SjCC`X#rtt!wY6PL`d2'3!^x}u]],
    certificate = '/home/ftp/cert.pem',
    cafile = '/home/ftp/ca-chain.cert.pem',
  }

  res, err = client:connect(url, opts)
  if not res then
    log('connection failed', err)
    client:sock_close()
    client = nil
  end
end

if client then
  frame, opcode, clean, code, reason = client:receive()
  log(frame, opcode, clean, code, reason)
  if frame then
    data = json.pcdecode(frame)
    log(data)
  elseif reason ~= 'timeout' then
    log('connection dropped', reason)
    client:sock_close()
    client = nil
  end
end

This example is only a starting point, it should be extended with additional send/receive logic.

Thank you very much.... now I am getting that log.... I guess something is still wrong...

Attached Files Thumbnail(s)
   
Reply
#9
This means that the script is connected but there are no messages to be read from the websocket connection.
Can you describe what data do you want to send/receive via this connection?
Reply
#10
Sure, I already send you a pdf with the notes, by the way, they are JSON strings like the following:

READ MESSAGGE

{
"payload": {
"Type": "NotificationPackage"
"Item": {
"Name": "Temperatura-SudOvest"
"State": “28.5”
"Type": “NUMBER:FLOAT”
"Editable": 0,
"Link": ""
},
"TimeStamp": “2020-09-22T15:27:59.140Z”
}
}


WRITE MESSAGE


{
“Action”:“Post”
“Controller”:“Items”
“ItemName”:“Light-Box”
“ItemState”: “OFF”
}


TO REQUEST THE STATE OF A LAMP

{
“Action”:“GET”
“Controller”:“Items”
“ItemName”: “Light-Box”
}

TO REQUEST THE LIST OF OBJECTS

{
“Action”:“GET”
“Controller”: “Items”
}
Reply
#11
I know about the protocol but the question was about your own use case. What kind of devices are going to control like this? Or do you want to read some sensor values?
Reply
#12
(11.05.2021, 09:42)admin Wrote: I know about the protocol but the question was about your own use case. What kind of devices are going to control like this? Or do you want to read some sensor values?

I have to let them to control the ETS objects in LM... so I have to send sensor values, object states and let them to command i.e. lamps, termostate setpoint, etc.
Reply
#13
1. Create a user library named bim-iot using the attached source.
2. Make sure that the updated websocket library and keys are in place.
3. Create a resident script with sleep time = 0:
Code:
if not wsclient then
  require('json')
  require('user.bim-iot')

  url = 'wss://test-hub.usbim.com'
  wsclient = require('user.websocket').client('sync', 1)

  opts = {
    protocol = 'tlsv13',
    key = '/home/ftp/key.pem',
    password = [[*q&'wrKQsx+#bj8pbEV=%7Gpr"S+sxV2ngr`N}SjCC`X#rtt!wY6PL`d2'3!^x}u]],
    certificate = '/home/ftp/cert.pem',
    cafile = '/home/ftp/ca-chain.cert.pem',
  }

  res, err = wsclient:connect(url, opts)
  if res then
    log('WS connection OK')
  else
    log('WS connection failed', err)
    wsclient:sock_close()
    wsclient = nil
  end

  if not lb then
    lb = require('localbus').new()

    lb:sethandler('groupwrite', eventhandler)
    lb:sethandler('groupresponse', eventhandler)

    lbfd = lb:getfd()
  end

  wsfd = wsclient.sock:getfd()

  init()
  sendall()
end

if wsclient then
  local res, lbstat, wsstat = socket.xselect(10, lbfd, wsfd)

  if res > 0 then
    if lbstat then
      lb:step()
    end

    if wsstat then
      local msg, opcode, clean, code, reason = wsclient:receive()
      log(msg, opcode, clean, code, reason)

      if msg then
        onwsmessage(msg)
      elseif reason ~= 'timeout' then
        log('WS connection dropped', reason)
        wsclient:sock_close()
        wsclient = nil
      end
    end
  end
else
  -- handle incoming object messages before trying to reconnect WS
  lb:loop(10)
end
Since I don't have the tools to test some parts might not work.

Attached Files
.lua   bim-iot.lua (Size: 2.85 KB / Downloads: 14)
Reply
#14
The connection is ok, but they still can't get nothing. I guess you already understood, but just to be clear:
they should receive any object event when it occurs and to be able to write on the objects.
Can I send you the backup of my LM in private? Could be useful?

Thanks

Peppe
Reply
#15
The script is sending the object values when the connection is established. Do you have any tool to read/write data from the IoT gateway side?
Reply
#16
they checked the logs and the data they are receiving but it seems to be not in the correct format due to an error that is present in the documentation we sent you.
We receive the following JSON:

{
"payload": {
"Type": "NotificationPackage",
"Item": {
"Link": "",
"State": "Thu 13-05-2021",
"Type": "STRING",
"Name": "daymonthyear",
"Editable": 2
},
"TimeStamp": "2021-05-13T09: 00: 01.288Z"
}
}

But the correct format is:
{
"Type": "NotificationPackage",
"Item": {
"Link": "",
"State": "Thu 13-05-2021",
"Type": "STRING",
"Name": "daymonthyear",
"Editable": 2
},
"TimeStamp": "2021-05-13T09: 00: 01.288Z"
}

In practice, the payload node is not necessary.
Reply
#17
Modify the sendobjvalue function in bim-iot library and do a full script restart via disable/enable:
Code:
function sendobjvalue(obj)
  local data = json.encode({
    Type = 'NotificationPackage',
    Item = {
      Name = obj.name,
      State = encodevalue(obj.value, obj.dtype),
      Type = obj.dtype,
      Editable = 2, -- allow read/write
      Link = ''
    },
    TimeStamp = timestamp()
  })

  wsclient:send(data)
end
Reply
#18
(13.05.2021, 09:53)admin Wrote: Modify the sendobjvalue function in bim-iot library and do a full script restart via disable/enable:
Code:
function sendobjvalue(obj)
  local data = json.encode({
    Type = 'NotificationPackage',
    Item = {
      Name = obj.name,
      State = encodevalue(obj.value, obj.dtype),
      Type = obj.dtype,
      Editable = 2, -- allow read/write
      Link = ''
    },
    TimeStamp = timestamp()
  })

  wsclient:send(data)
end

I already done it...  Wink
Everything seems to works fine at moment... 
now they said me that they will try to send write messages in the next days...  hopefully everything will be fine.

Thank you so much for your priceless support!

Peppe
Reply
#19
(13.05.2021, 09:53)admin Wrote: Modify the sendobjvalue function in bim-iot library and do a full script restart via disable/enable:
Code:
function sendobjvalue(obj)
  local data = json.encode({
    Type = 'NotificationPackage',
    Item = {
      Name = obj.name,
      State = encodevalue(obj.value, obj.dtype),
      Type = obj.dtype,
      Editable = 2, -- allow read/write
      Link = ''
    },
    TimeStamp = timestamp()
  })

  wsclient:send(data)
end

HI,

They finally  tested the ws connection sending commands from their side: it doesn't work. 
They try to on/off a led or to set temperature on a termostate, but nothing happens.
They json strings they are sending are like: 

    {
  “Action”:“Post”
  “Controller”:“Items”
  “ItemName”:“Led 1”
  “ItemState”:“ON”
}

or
    {
  “Action”:“Post”
  “Controller”:“Items”
  “ItemName”:“Temperatura”
  “ItemState”:“22.0”
}

But no success.

Another problem is that they are receiving the status of ALL objects, ALL in read / write mode.

instead we must be able to choose which objects to transmit and specify whether to read or write or both.

I am grateful for your help.

BR 

Peppe
Reply
#20
Use the attached updated library and see if receiving values works.
You must tag the objects that you want to be accessible via this connection. Use either bim-read, bim-write or bim-read-write tag to define the access level. Only one tag can be used for any object.

Attached Files
.lua   bim-iot.lua (Size: 3.09 KB / Downloads: 17)
Reply


Forum Jump: