host -> server
This commit is contained in:
Родитель
d426be4ab5
Коммит
f368a530b6
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"name": "jacdac-accelerometer-host",
|
||||
"version": "0.4.31",
|
||||
"description": "MakeCode support for Jacdac Accelerometer service",
|
||||
"description": "MakeCode support for Jacdac Accelerometer server",
|
||||
"dependencies": {
|
||||
"core": "*",
|
||||
"accelerometer": "*",
|
||||
|
@ -9,7 +9,7 @@
|
|||
"jacdac-accelerometer": "github:microsoft/pxt-jacdac/accelerometer"
|
||||
},
|
||||
"files": [
|
||||
"host.ts"
|
||||
"server.ts"
|
||||
],
|
||||
"supportedTargets": [
|
||||
"arcade",
|
|
@ -1,5 +1,5 @@
|
|||
namespace jacdac {
|
||||
export class AccelerometerHost extends jacdac.SensorHost {
|
||||
export class AccelerometerServer extends jacdac.SensorServer {
|
||||
constructor(dev: string) {
|
||||
super(dev, jacdac.SRV_ACCELEROMETER);
|
||||
|
|
@ -1,6 +0,0 @@
|
|||
console.log(`button host test`)
|
||||
const buttonA = new jacdac.ButtonHost("A", input.buttonA)
|
||||
const buttonB = new jacdac.ButtonHost("B", input.buttonB)
|
||||
buttonA.start()
|
||||
buttonB.start()
|
||||
console.log(`jacdac started`)
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"name": "jacdac-button-host",
|
||||
"version": "0.4.31",
|
||||
"description": "MakeCode support for Jacdac Button host service",
|
||||
"description": "MakeCode support for Jacdac Button server",
|
||||
"dependencies": {
|
||||
"core": "*",
|
||||
"buttons": "*",
|
||||
|
@ -9,7 +9,7 @@
|
|||
"jacdac-button": "github:microsoft/pxt-jacdac/button"
|
||||
},
|
||||
"files": [
|
||||
"host.ts"
|
||||
"server.ts"
|
||||
],
|
||||
"testFiles": [
|
||||
"test.ts"
|
|
@ -2,12 +2,11 @@ import JDButtonEvent = jacdac.ButtonEvent
|
|||
import MkcdButtonEvent = ButtonEvent
|
||||
|
||||
namespace jacdac {
|
||||
export class ButtonHost extends jacdac.SensorHost {
|
||||
export class ButtonServer extends jacdac.SensorServer {
|
||||
constructor(dev: string, public readonly button: Button) {
|
||||
super(dev, jacdac.SRV_BUTTON);
|
||||
this.button.onEvent(MkcdButtonEvent.Down, () => this.sendEvent(JDButtonEvent.Down));
|
||||
this.button.onEvent(MkcdButtonEvent.Up, () => this.sendEvent(JDButtonEvent.Up));
|
||||
this.button.onEvent(MkcdButtonEvent.Click, () => this.sendEvent(JDButtonEvent.Click));
|
||||
this.button.onEvent(<number>DAL.DEVICE_BUTTON_EVT_HOLD, () => this.sendEvent(JDButtonEvent.Hold));
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
console.log(`button host test`)
|
||||
const buttonA = new jacdac.ButtonServer("A", input.buttonA)
|
||||
const buttonB = new jacdac.ButtonServer("B", input.buttonB)
|
||||
buttonA.start()
|
||||
buttonB.start()
|
||||
console.log(`jacdac started`)
|
|
@ -1,7 +1,7 @@
|
|||
namespace microbit {
|
||||
|
||||
export const SRV_ACCELEROMETER = 0x1f140409
|
||||
export class Accelerometer extends jacdac.SensorHost {
|
||||
export class Accelerometer extends jacdac.SensorServer {
|
||||
private lastEvent: number = -1;
|
||||
constructor() {
|
||||
super("microbitAccel", SRV_ACCELEROMETER);
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
namespace microbit {
|
||||
export const SRV_BUTTON = 0x1473a263
|
||||
export class MButton extends jacdac.SensorHost {
|
||||
export class MButton extends jacdac.SensorServer {
|
||||
constructor(dev: string, private button: Button) {
|
||||
super(dev, SRV_BUTTON);
|
||||
control.onEvent(button, EventBusValue.MICROBIT_EVT_ANY,
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
namespace microbit {
|
||||
export const SRV_LIGHT_LEVEL = 0x17dc9a1c
|
||||
export class LightLevel extends jacdac.SensorHost {
|
||||
export class LightLevel extends jacdac.SensorServer {
|
||||
constructor() {
|
||||
super("microbitLight", SRV_LIGHT_LEVEL)
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
namespace microbit {
|
||||
export class Screen extends jacdac.Host {
|
||||
export class Screen extends jacdac.Server {
|
||||
constructor() {
|
||||
super("microbitScreen", 0x110d154b)
|
||||
}
|
||||
|
|
|
@ -80,7 +80,7 @@ namespace jacdac {
|
|||
}
|
||||
|
||||
namespace microbit {
|
||||
export class SoundLevel extends jacdac.SensorHost {
|
||||
export class SoundLevel extends jacdac.SensorServer {
|
||||
enabled: boolean = true;
|
||||
// Sensitivity -38dB ±3dB @ 94dB SPL
|
||||
minDecibels: number = 56
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
namespace jacdac {
|
||||
export class LoggerHost extends Host {
|
||||
export class LoggerServer extends Server {
|
||||
private _lastListenerTime = 0;
|
||||
minPriority = jacdac.LoggerPriority.Silent;
|
||||
|
||||
|
@ -60,5 +60,5 @@ namespace jacdac {
|
|||
}
|
||||
|
||||
//% whenUsed
|
||||
export const loggerHost = new LoggerHost()
|
||||
export const logger = new LoggerServer()
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
namespace jacdac {
|
||||
export class ProtoTestHost extends Host {
|
||||
export class ProtoTestServer extends Server {
|
||||
constructor() {
|
||||
super("prototest", SRV_PROTO_TEST);
|
||||
}
|
||||
|
@ -88,5 +88,5 @@ namespace jacdac {
|
|||
}
|
||||
|
||||
//% whenUsed
|
||||
export const protoTestHost = new ProtoTestHost();
|
||||
export const protoTestServer = new ProtoTestServer();
|
||||
}
|
6
pxt.json
6
pxt.json
|
@ -36,14 +36,14 @@
|
|||
"control/constants.ts",
|
||||
"logger/constants.ts",
|
||||
"logger/client.ts",
|
||||
"logger-host/host.ts",
|
||||
"logger-server/server.ts",
|
||||
"sensor/constants.ts",
|
||||
"sensor/client.ts",
|
||||
"sensor/host.ts",
|
||||
"sensor/server.ts",
|
||||
"role-manager/constants.ts",
|
||||
"rolemgr.ts",
|
||||
"proto-test/constants.ts",
|
||||
"proto-test/host.ts",
|
||||
"proto-test/server.ts",
|
||||
"routing.ts"
|
||||
],
|
||||
"testFiles": [
|
||||
|
|
24
rolemgr.ts
24
rolemgr.ts
|
@ -54,7 +54,7 @@ namespace jacdac._rolemgr {
|
|||
}
|
||||
}
|
||||
|
||||
class HostBindings {
|
||||
class ServerBindings {
|
||||
bindings: RoleBinding[] = []
|
||||
constructor(
|
||||
public host: string
|
||||
|
@ -125,7 +125,7 @@ namespace jacdac._rolemgr {
|
|||
return maxElt
|
||||
}
|
||||
|
||||
export class RoleManagerHost extends Host {
|
||||
export class RoleManagerServer extends Server {
|
||||
private _oldBindingsHash: number;
|
||||
|
||||
constructor() {
|
||||
|
@ -237,26 +237,26 @@ namespace jacdac._rolemgr {
|
|||
}
|
||||
}
|
||||
|
||||
let hosts: HostBindings[] = []
|
||||
let servers: ServerBindings[] = []
|
||||
|
||||
// Group all clients by host
|
||||
for (const b of bindings) {
|
||||
const hn = b.host()
|
||||
let h = hosts.find(h => h.host == hn)
|
||||
let h = servers.find(h => h.host == hn)
|
||||
if (!h) {
|
||||
h = new HostBindings(hn)
|
||||
hosts.push(h)
|
||||
h = new ServerBindings(hn)
|
||||
servers.push(h)
|
||||
}
|
||||
h.bindings.push(b)
|
||||
}
|
||||
|
||||
// exclude hosts that have already everything bound
|
||||
hosts = hosts.filter(h => !h.fullyBound)
|
||||
servers = servers.filter(h => !h.fullyBound)
|
||||
|
||||
while (hosts.length > 0) {
|
||||
while (servers.length > 0) {
|
||||
// Get host with maximum number of clients (resolve ties by name)
|
||||
// This gives priority to assignment of "more complicated" hosts, which are generally more difficult to assign
|
||||
const h = maxIn(hosts, (a, b) => a.bindings.length - b.bindings.length || b.host.compare(a.host))
|
||||
const h = maxIn(servers, (a, b) => a.bindings.length - b.bindings.length || b.host.compare(a.host))
|
||||
|
||||
for (const d of wraps)
|
||||
d.score = h.scoreFor(d)
|
||||
|
@ -265,7 +265,7 @@ namespace jacdac._rolemgr {
|
|||
|
||||
if (dev.score == 0) {
|
||||
// nothing can be assigned, on any device
|
||||
hosts.removeElement(h)
|
||||
servers.removeElement(h)
|
||||
continue
|
||||
}
|
||||
|
||||
|
@ -278,7 +278,7 @@ namespace jacdac._rolemgr {
|
|||
|
||||
// if everything bound on this host, remove it from further consideration
|
||||
if (h.fullyBound)
|
||||
hosts.removeElement(h)
|
||||
servers.removeElement(h)
|
||||
else {
|
||||
// otherwise, remove bindings on the current device, to update sort order
|
||||
// it's unclear we need this
|
||||
|
@ -305,7 +305,7 @@ namespace jacdac._rolemgr {
|
|||
namespace jacdac {
|
||||
|
||||
//% fixedInstance whenUsed block="role manager"
|
||||
export const roleManagerHost = new _rolemgr.RoleManagerHost()
|
||||
export const roleManager = new _rolemgr.RoleManagerServer()
|
||||
|
||||
/*
|
||||
|
||||
|
|
16
routing.ts
16
routing.ts
|
@ -9,7 +9,7 @@ namespace jacdac {
|
|||
// common logging level for jacdac services
|
||||
export let consolePriority = ConsolePriority.Debug;
|
||||
|
||||
let _hostServices: Host[]
|
||||
let _hostServices: Server[]
|
||||
export let _unattachedClients: Client[]
|
||||
export let _allClients: Client[]
|
||||
let _myDevice: Device;
|
||||
|
@ -39,7 +39,7 @@ namespace jacdac {
|
|||
}
|
||||
|
||||
//% fixedInstances
|
||||
export class Host {
|
||||
export class Server {
|
||||
protected supressLog: boolean;
|
||||
running: boolean
|
||||
serviceIndex: number
|
||||
|
@ -691,7 +691,7 @@ namespace jacdac {
|
|||
|
||||
function doNothing() { }
|
||||
|
||||
class ControlService extends Host {
|
||||
class ControlService extends Server {
|
||||
constructor() {
|
||||
super("ctrl", 0)
|
||||
}
|
||||
|
@ -812,14 +812,14 @@ namespace jacdac {
|
|||
// only try autoBind, proxy we see some devices online
|
||||
if (_devices.length > 1) {
|
||||
// check for proxy mode
|
||||
jacdac.roleManagerHost.checkProxy()
|
||||
jacdac.roleManager.checkProxy()
|
||||
// auto bind
|
||||
if (autoBind) {
|
||||
autoBindCnt++
|
||||
// also, only do it every two announces (TBD)
|
||||
if (autoBindCnt >= 2) {
|
||||
autoBindCnt = 0
|
||||
jacdac.roleManagerHost.autoBind();
|
||||
jacdac.roleManager.autoBind();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1132,12 +1132,12 @@ namespace jacdac {
|
|||
if (!options.disableLogger) {
|
||||
console.addListener(function (pri, msg) {
|
||||
if (msg[0] != ":")
|
||||
loggerHost.add(pri as number, msg);
|
||||
jacdac.logger.add(pri as number, msg);
|
||||
});
|
||||
loggerHost.start()
|
||||
jacdac.logger.start()
|
||||
}
|
||||
if (!options.disableRoleManager) {
|
||||
roleManagerHost.start();
|
||||
roleManager.start();
|
||||
controlService.sendUptime();
|
||||
}
|
||||
// and we're done
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
},
|
||||
"files": [
|
||||
"constants.ts",
|
||||
"host.ts"
|
||||
"server.ts"
|
||||
],
|
||||
"supportedTargets": [
|
||||
"arcade",
|
||||
|
|
|
@ -15,7 +15,7 @@ namespace jacdac {
|
|||
class Collector extends Client {
|
||||
private requiredServiceNum: number
|
||||
lastSample: Buffer
|
||||
private parent: SensorAggregatorHost
|
||||
private parent: SensorAggregatorServer
|
||||
private numElts: number
|
||||
private sampleType: jacdac.SensorAggregatorSampleType
|
||||
private sampleMult: number
|
||||
|
@ -40,7 +40,7 @@ namespace jacdac {
|
|||
this.setReg(jacdac.SensorReg.StreamingSamples, "u8", [255])
|
||||
}
|
||||
|
||||
constructor(parent: SensorAggregatorHost, config: Buffer) {
|
||||
constructor(parent: SensorAggregatorServer, config: Buffer) {
|
||||
const [devIdBuf, serviceClass, serviceNum, sampleSize, sampleType, sampleShift] = jdunpack(config, "b[8] u32 u8 u8 u8 i8")
|
||||
const devId = devIdBuf.getNumber(NumberFormat.Int32LE, 0) == 0 ? null : devIdBuf.toHex()
|
||||
super(serviceClass, devId + ":" + serviceNum)
|
||||
|
@ -65,7 +65,7 @@ namespace jacdac {
|
|||
}
|
||||
}
|
||||
|
||||
export class SensorAggregatorHost extends Host {
|
||||
export class SensorAggregatorServer extends Server {
|
||||
private collectors: Collector[]
|
||||
private lastSample: number
|
||||
private samplingInterval: number
|
||||
|
@ -208,5 +208,5 @@ namespace jacdac {
|
|||
}
|
||||
|
||||
//% whenUsed
|
||||
export const sensorAggregatorHost = new SensorAggregatorHost()
|
||||
export const sensorAggregatorServer = new SensorAggregatorServer()
|
||||
}
|
|
@ -2,7 +2,7 @@ namespace jacdac {
|
|||
/**
|
||||
* JacDac service running on sensor and streaming data out
|
||||
*/
|
||||
export class SensorHost extends Host {
|
||||
export class SensorServer extends Server {
|
||||
public streamingInterval: number; // millis
|
||||
public streamingSamples: number;
|
||||
|
||||
|
@ -42,7 +42,7 @@ namespace jacdac {
|
|||
protected handleCustomCommand(pkt: JDPacket) {
|
||||
}
|
||||
|
||||
protected raiseHostEvent(value: number) {
|
||||
protected raiseServerEvent(value: number) {
|
||||
this.sendEvent(value)
|
||||
}
|
||||
|
|
@ -11,7 +11,7 @@ namespace jacdac {
|
|||
}
|
||||
}
|
||||
|
||||
export class Broadcast extends Host {
|
||||
export class Broadcast extends Server {
|
||||
readonly client: BroadcastClient
|
||||
|
||||
constructor(name: string, serviceClass: number) {
|
||||
|
|
|
@ -1,2 +0,0 @@
|
|||
jacdac.settingsHost.start()
|
||||
jacdac.start()
|
|
@ -1,9 +1,9 @@
|
|||
{
|
||||
"name": "jacdac-settings-host",
|
||||
"name": "jacdac-settings-server",
|
||||
"version": "0.4.31",
|
||||
"description": "MakeCode support for Jacdac Settings host service",
|
||||
"description": "MakeCode support for Jacdac Settings server",
|
||||
"files": [
|
||||
"host.ts"
|
||||
"server.ts"
|
||||
],
|
||||
"testFiles": [
|
||||
"test.ts"
|
|
@ -1,6 +1,6 @@
|
|||
namespace jacdac {
|
||||
export const SETTINGS_PREFIX = "jdsts:"
|
||||
export class SettingsHost extends Host {
|
||||
export class SettingsServer extends Server {
|
||||
constructor(name: string) {
|
||||
super(name, jacdac.SRV_SETTINGS);
|
||||
}
|
||||
|
@ -74,5 +74,5 @@ namespace jacdac {
|
|||
}
|
||||
|
||||
//% fixedInstance whenUsed
|
||||
export const settingsHost = new SettingsHost("settings");
|
||||
export const settingsServer = new SettingsServer("settings");
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
jacdac.settingsServer.start()
|
||||
jacdac.start()
|
6
test.ts
6
test.ts
|
@ -39,10 +39,10 @@ function jdpackTest() {
|
|||
// pins.A9.digitalWrite(false)
|
||||
|
||||
jacdac.consolePriority = ConsolePriority.Log;
|
||||
jacdac.roleManagerHost.start()
|
||||
jacdac.protoTestHost.start()
|
||||
jacdac.roleManager.start()
|
||||
jacdac.protoTestServer.start()
|
||||
jacdac.start()
|
||||
jacdac.loggerHost.log("test started")
|
||||
jacdac.logger.log("test started")
|
||||
modules.identify();
|
||||
|
||||
jdpackTest()
|
||||
|
|
Загрузка…
Ссылка в новой задаче