Родитель
9823ddd1f6
Коммит
b5abfbc30d
|
@ -0,0 +1,26 @@
|
|||
{
|
||||
"name": "jacdac-azure-iot-hub-health-server",
|
||||
"version": "1.8.34",
|
||||
"description": "Health and diagnostics information about the Azure Iot Hub connection.",
|
||||
"files": [
|
||||
"server.ts"
|
||||
],
|
||||
"testFiles": [
|
||||
"test.ts"
|
||||
],
|
||||
"supportedTargets": [
|
||||
"maker"
|
||||
],
|
||||
"dependencies": {
|
||||
"core": "*",
|
||||
"settings": "*",
|
||||
"net": "*",
|
||||
"azureiot": "*",
|
||||
"jacdac": "github:microsoft/pxt-jacdac",
|
||||
"jacdac-azure-iot-hub-health": "github:microsoft/pxt-jacdac/jacdac-azure-iot-hub-health"
|
||||
},
|
||||
"testDependencies": {
|
||||
"jacdac-iot-s2": "*"
|
||||
},
|
||||
"preferredEditor": "tsprj"
|
||||
}
|
|
@ -0,0 +1,149 @@
|
|||
namespace servers {
|
||||
export class AzureIotHubHealthServer extends jacdac.Server {
|
||||
private _connectionStatus: jacdac.AzureIotHubHealthConnectionStatus
|
||||
constructor() {
|
||||
super(jacdac.SRV_AZURE_IOT_HUB_HEALTH)
|
||||
this._connectionStatus =
|
||||
jacdac.AzureIotHubHealthConnectionStatus.Disconnected
|
||||
let hasPub = false
|
||||
azureiot.onEvent(AzureIotEvent.Connected, () => {
|
||||
this.setConnectionStatus(
|
||||
jacdac.AzureIotHubHealthConnectionStatus.Connected
|
||||
)
|
||||
if (!hasPub) {
|
||||
hasPub = true
|
||||
azureiot.mqttClient().on("published", () => {
|
||||
this.sendEvent(
|
||||
jacdac.AzureIotHubHealthEvent.MessageSent
|
||||
)
|
||||
})
|
||||
}
|
||||
})
|
||||
azureiot.onEvent(AzureIotEvent.Disconnected, () =>
|
||||
this.setConnectionStatus(
|
||||
jacdac.AzureIotHubHealthConnectionStatus.Disconnected
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
get connectionStatus() {
|
||||
return this._connectionStatus
|
||||
}
|
||||
|
||||
get hubName() {
|
||||
return azureiot.hubName()
|
||||
}
|
||||
|
||||
get hubDeviceId() {
|
||||
return azureiot.hubDeviceId()
|
||||
}
|
||||
|
||||
private setConnectionStatus(
|
||||
status: jacdac.AzureIotHubHealthConnectionStatus
|
||||
) {
|
||||
if (status !== this._connectionStatus) {
|
||||
this._connectionStatus = status
|
||||
this.log(`conn status: ${this._connectionStatus}`)
|
||||
this.sendEvent(
|
||||
jacdac.AzureIotHubHealthEvent.ConnectionStatusChange,
|
||||
jacdac.jdpack<[number]>("u8", [this._connectionStatus])
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private connect() {
|
||||
if (!azureiot.isConnected()) {
|
||||
this.log(`connecting`)
|
||||
this.setConnectionStatus(
|
||||
jacdac.AzureIotHubHealthConnectionStatus.Connecting
|
||||
)
|
||||
try {
|
||||
azureiot.connect()
|
||||
this.setConnectionStatus(
|
||||
jacdac.AzureIotHubHealthConnectionStatus.Connected
|
||||
)
|
||||
} catch {
|
||||
this.setConnectionStatus(
|
||||
jacdac.AzureIotHubHealthConnectionStatus.Disconnected
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private disconnect() {
|
||||
// TODO: disconnect
|
||||
this.log(`disconnecting`)
|
||||
}
|
||||
|
||||
private handleSetConnectionString(pkt: jacdac.JDPacket) {
|
||||
const newConnString = pkt.stringData
|
||||
const connString = settings.programSecrets.readSecret(
|
||||
azureiot.SECRETS_KEY,
|
||||
false
|
||||
)
|
||||
if (connString !== newConnString) {
|
||||
this.log(`updated connection string`)
|
||||
const wasConnected = azureiot.isConnected()
|
||||
azureiot.setConnectionString(newConnString)
|
||||
if (wasConnected) this.connect()
|
||||
}
|
||||
}
|
||||
|
||||
handlePacket(pkt: jacdac.JDPacket) {
|
||||
// hub name, device id
|
||||
if (pkt.isRegGet) {
|
||||
switch (pkt.regCode) {
|
||||
case jacdac.AzureIotHubHealthReg.ConnectionStatus: {
|
||||
this.handleRegValue(
|
||||
pkt,
|
||||
jacdac.AzureIotHubHealthReg.ConnectionStatus,
|
||||
"u16",
|
||||
this.connectionStatus
|
||||
)
|
||||
break
|
||||
}
|
||||
case jacdac.AzureIotHubHealthReg.HubName: {
|
||||
this.handleRegValue(
|
||||
pkt,
|
||||
jacdac.AzureIotHubHealthReg.HubName,
|
||||
"s",
|
||||
this.hubName
|
||||
)
|
||||
break
|
||||
}
|
||||
case jacdac.AzureIotHubHealthReg.HubDeviceId: {
|
||||
this.handleRegValue(
|
||||
pkt,
|
||||
jacdac.AzureIotHubHealthReg.HubDeviceId,
|
||||
"s",
|
||||
this.hubDeviceId
|
||||
)
|
||||
break
|
||||
}
|
||||
default:
|
||||
pkt.possiblyNotImplemented()
|
||||
break
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
switch (pkt.serviceCommand) {
|
||||
case jacdac.AzureIotHubHealthCmd.Connect:
|
||||
this.connect()
|
||||
break
|
||||
case jacdac.AzureIotHubHealthCmd.Disconnect:
|
||||
this.disconnect()
|
||||
break
|
||||
case jacdac.AzureIotHubHealthCmd.SetConnectionString:
|
||||
this.handleSetConnectionString(pkt)
|
||||
break
|
||||
default:
|
||||
pkt.possiblyNotImplemented()
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//% fixedInstance whenUsed weight=1 block="Azure IoT Hub"
|
||||
export const azureIotHubHealth = new AzureIotHubHealthServer()
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
console.log(`azure iot hub health server test`)
|
||||
servers.azureIotHubHealth.start()
|
|
@ -0,0 +1,20 @@
|
|||
{
|
||||
"name": "jacdac-azure-iot-hub-health",
|
||||
"version": "1.8.34",
|
||||
"description": "Health and diagnostics information about the Azure Iot Hub connection.",
|
||||
"files": [
|
||||
"constants.ts"
|
||||
],
|
||||
"supportedTargets": [
|
||||
"arcade",
|
||||
"microbit",
|
||||
"maker"
|
||||
],
|
||||
"dependencies": {
|
||||
"core": "*",
|
||||
"jacdac": "github:microsoft/pxt-jacdac"
|
||||
},
|
||||
"testDependencies": {
|
||||
"nucleo-f411re": "*"
|
||||
}
|
||||
}
|
|
@ -73,7 +73,7 @@ namespace modules {
|
|||
const msg = new CloudMessage()
|
||||
msg.label = label
|
||||
const res = []
|
||||
for (const v of values || []) {
|
||||
for (const v of (values || [])) {
|
||||
res.push(v.shift())
|
||||
}
|
||||
msg.values = res
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
modules.cloudAdapter1.onCloudCommand("test", (value, values) => {
|
||||
console.log(
|
||||
"cloud command received: test " + value + " / " + values.join(",")
|
||||
)
|
||||
console.log("cloud command received: test " + value + " / " + values.join(","))
|
||||
})
|
||||
forever(() => {
|
||||
modules.cloudAdapter1.uploadNumber("rnd", Math.random(), Math.random())
|
||||
|
|
|
@ -45,7 +45,7 @@ namespace jacdac {
|
|||
* `end_time - start_time == window`
|
||||
*
|
||||
* ```
|
||||
* const [numSamples, avg, min, max, startTime, endTime, label] = jdunpack<[number, number, number, number, number, number, string]>(buf, "u32 x[4] f64 f64 f64 u32 u32 s")
|
||||
* const [numSamples, avg, min, max, startTime, endTime, label] = jdunpack<[number, number, number, number, number, number, string]>(buf, "u32 f64 f64 f64 u32 u32 s")
|
||||
* ```
|
||||
*/
|
||||
Stored = 0x90,
|
||||
|
@ -70,7 +70,7 @@ namespace jacdac {
|
|||
/**
|
||||
* Pack format for 'stored' register data.
|
||||
*/
|
||||
export const Stored = "u32 b[4] f64 f64 f64 u32 u32 s"
|
||||
export const Stored = "u32 f64 f64 f64 u32 u32 s"
|
||||
}
|
||||
|
||||
export const enum TimeseriesAggregatorReg {
|
||||
|
|
Загрузка…
Ссылка в новой задаче