httpServer.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { PlatformConfig } from 'homebridge'
  2. import { domoticaPlatform } from './platform'
  3. const request = require("request")
  4. const http = require('http')
  5. const url = require('url')
  6. export class httpServer {
  7. private address
  8. private port
  9. constructor(
  10. private readonly platform: domoticaPlatform,
  11. private readonly config: PlatformConfig
  12. ) {
  13. this.address = "0.0.0.0"
  14. this.port = 8080
  15. }
  16. serverCallback (request, response) {
  17. var urlObject = url.parse(request.url, true)
  18. var urlQuery = urlObject.query
  19. var body: Uint8Array[] = []
  20. var log = this.platform.log
  21. request.on('error', (err) => {
  22. log.debug("[ERROR Homebridge Domotica HttpServer] Reason: %s.", err)
  23. }).on('data', (chunk) => {
  24. body.push(chunk)
  25. }).on('end', () => {
  26. var bodyString = Buffer.concat(body).toString()
  27. response.on('error', function(err) {
  28. log.debug("[ERROR Homebridge Domotica HttpServer] Reason: %s.", err)
  29. })
  30. response.statusCode = 200
  31. response.setHeader('Content-Type', 'application/json')
  32. if (!urlQuery.type || !urlQuery.typeId) {
  33. response.statusCode = 404
  34. response.setHeader("Content-Type", "text/plain")
  35. var errorText = "[ERROR Homebridge Domotica HttpServer] Type or TypeId missing in request."
  36. response.write(errorText)
  37. response.end()
  38. } else {
  39. var type = urlQuery.type
  40. var typeId = urlQuery.typeId
  41. if (type != "PowerSocket" && type != "PowerSwitch" && type != "Xiaomi" && type != "Camera" && type != "Activity") {
  42. log.debug(request.url)
  43. }
  44. if (type == "Xiaomi") {
  45. type = urlQuery.model
  46. if (type == "TemperatureHumiditySensor") {
  47. type = "EnvironmentSensor"
  48. }
  49. }
  50. if (type == "Toon") {
  51. type = "Thermostat"
  52. }
  53. var foundAccessory
  54. for (const accessory of this.platform.accessories) {
  55. if (accessory.type == type && accessory.typeId == typeId) {
  56. foundAccessory = accessory
  57. break
  58. }
  59. }
  60. if (foundAccessory) {
  61. for (const key of Object.keys(urlQuery)) {
  62. if (key != "type" && key != "typeId") {
  63. foundAccessory.setValue(key, urlQuery[key])
  64. }
  65. }
  66. var responseBody = { "success" : true };
  67. response.write(JSON.stringify(responseBody))
  68. response.end()
  69. } else {
  70. response.setHeader("Content-Type", "text/plain")
  71. errorText = "[ERROR Homebridge Domotica HttpServer] " + type + " " + typeId + " not found."
  72. if (type != "PowerSocket") {
  73. log.debug(errorText + '(' + request.url + ')')
  74. }
  75. response.write(errorText)
  76. response.end()
  77. }
  78. }
  79. })
  80. }
  81. start() {
  82. http.createServer(this.serverCallback.bind(this)).listen(this.port, this.address)
  83. }
  84. }