diff options
| -rw-r--r-- | lamp.ts | 25 | ||||
| -rw-r--r-- | main.py | 31 | ||||
| -rw-r--r-- | package.json | 1 | ||||
| -rw-r--r-- | tsconfig.json | 6 | ||||
| -rw-r--r-- | yarn.lock | 19 | 
5 files changed, 67 insertions, 15 deletions
| @@ -0,0 +1,25 @@ +import { ChildProcess, spawn } from 'child_process'; + +export type LampColor = [number, number, number, number]; + +export default class Lamp { +	#color: LampColor; +	private subpr: ChildProcess; + +	constructor(public addr: string) { +		this.subpr = spawn('python3', ['./main.py', addr]); +	} + +	set color(newColor: LampColor) { +		this.#color = newColor.map(c => Math.floor(c)) as LampColor; +		this.subpr.stdin.write(this.colorToString() + '\n'); +	} + +	get color() { +		return this.#color; +	} + +	private colorToString() { +		return this.color.map(i => i.toString(16).padStart(2, '0')).join(''); +	} +} @@ -7,6 +7,7 @@ import sys  mac = sys.argv[1]  dev = None +messages = []  def verify_connection():    global dev @@ -16,9 +17,6 @@ def verify_connection():      except BTLEDisconnectError as e:        continue -verify_connection() -print("connected") -  def makemsg(r, g, b, l=0):    return bytes([      int(g > 0), g, @@ -28,17 +26,24 @@ def makemsg(r, g, b, l=0):      int(l > 0), l,    ]) -messages = [] -def thread_func(): +def keep_alive():    while True: -    if len(messages) < 1: continue -    message = messages.pop(0) -    verify_connection() -    dev.writeCharacteristic(0x002A, message) +    global messages +    messages.append((0x0001, bytes(10))) +    time.sleep(10) + +def user_input(): +  for line in sys.stdin: +    r, g, b, l = [ int(x, 16) for x in [ line.strip()[i:i+2] for i in range(0, 8, 2) ] ] +    messages.append((0x002a, makemsg(r, g, b, l))) -threading.Thread(target=thread_func).start() +threading.Thread(target=keep_alive).start() +threading.Thread(target=user_input).start() -for line in sys.stdin: -  r, g, b, l = [ int(x, 16) for x in [ line.strip()[i:i+2] for i in range(0, 8, 2) ] ] -  messages.append(makemsg(r, g, b, l)) +verify_connection() +while True: +  if len(messages) < 1: continue +  message = messages.pop(0) +  verify_connection() +  dev.writeCharacteristic(message[0], bytearray(message[1])) diff --git a/package.json b/package.json index b641603..711f8f0 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@  		"homebridge": "^1.3.4"  	},  	"devDependencies": { +		"@types/color": "^3.0.2",  		"@types/node": "^16.4.10",  		"typescript": "^4.3.5"  	} diff --git a/tsconfig.json b/tsconfig.json index a245282..8e22c55 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,11 +5,13 @@  		"target": "ESNext",  		"module": "ESNext",  		"inlineSources": true, -		"inlineSourceMap": true +		"inlineSourceMap": true, +		"allowSyntheticDefaultImports": true  	},  	"exclude": [ "node_modules" ],  	"files": [  		"./index.ts", -		"./plugin.ts" +		"./plugin.ts", +    "./lamp.ts"  	]  } @@ -39,6 +39,25 @@    resolved "https://registry.yarnpkg.com/@leichtgewicht/ip-codec/-/ip-codec-2.0.3.tgz#0300943770e04231041a51bd39f0439b5c7ab4f0"    integrity sha512-nkalE/f1RvRGChwBnEIoBfSEYOXnCRdleKuv6+lePbMDrMZXeDQnqak5XDOeBgrPPyPfAdcCu/B5z+v3VhplGg== +"@types/color-convert@*": +  version "2.0.0" +  resolved "https://registry.yarnpkg.com/@types/color-convert/-/color-convert-2.0.0.tgz#8f5ee6b9e863dcbee5703f5a517ffb13d3ea4e22" +  integrity sha512-m7GG7IKKGuJUXvkZ1qqG3ChccdIM/qBBo913z+Xft0nKCX4hAU/IxKwZBU4cpRZ7GS5kV4vOblUkILtSShCPXQ== +  dependencies: +    "@types/color-name" "*" + +"@types/color-name@*": +  version "1.1.1" +  resolved "https://registry.yarnpkg.com/@types/color-name/-/color-name-1.1.1.tgz#1c1261bbeaa10a8055bbc5d8ab84b7b2afc846a0" +  integrity sha512-rr+OQyAjxze7GgWrSaJwydHStIhHq2lvY3BOC2Mj7KnzI7XK0Uw1TOOdI9lDoajEbSWLiYgoo4f1R51erQfhPQ== + +"@types/color@^3.0.2": +  version "3.0.2" +  resolved "https://registry.yarnpkg.com/@types/color/-/color-3.0.2.tgz#3779043e782f562aa9157b5fc6bd07e14fd8e7f3" +  integrity sha512-INiJl6sfNn8iyC5paxVzqiVUEj2boIlFki02uRTAkKwAj++7aAF+ZfEv/XrIeBa0XI/fTZuDHW8rEEcEVnON+Q== +  dependencies: +    "@types/color-convert" "*" +  "@types/node@^16.4.10":    version "16.4.10"    resolved "https://registry.yarnpkg.com/@types/node/-/node-16.4.10.tgz#e57e2a54fc6da58da94b3571b1cb456d39f88597" |