<commit_before>package homekit

import (
	"fmt"
	"github.com/brutella/hc/accessory"
	"github.com/brutella/hc/characteristic"
	"github.com/brutella/hc/service"
	"github.com/gosexy/to"
	"github.com/hemtjanst/hemtjanst/device"
	"github.com/hemtjanst/hemtjanst/homekit/util"
	"github.com/hemtjanst/hemtjanst/messaging"
	"log"
	"net"
	"reflect"
)

type deviceHolder struct {
	device          *device.Device
	accessory       *accessory.Accessory
	mainService     *service.Service
	characteristics map[string]*characteristic.Characteristic
}

func newDeviceHolder(d *device.Device) (*deviceHolder, error) {
	newDev := &deviceHolder{
		device:          d,
		accessory:       nil,
		mainService:     nil,
		characteristics: map[string]*characteristic.Characteristic{},
	}
	err := newDev.createAccessory()
	if err != nil {
		return nil, err
	}
	return newDev, nil
}

func (h *deviceHolder) onHomekitUpdate(c string, value interface{}) {
	log.Printf("onHomeKitUpdate(%s, %v) on device %s\n", c, value, h.device.Topic)
	log.Print(h.device)
	if feature, ok := h.device.Features[c]; ok {
		log.Print(feature)
		var out string

		if converted, err := to.Convert(value, reflect.TypeOf(out).Kind()); err == nil {
			out = converted.(string)
		}
		if b, ok := value.(bool); ok {
			if b {
				out = "1"
			} else {
				out = "0"
			}
		}

		if out != "" {
			feature.Set(out)
		}
		return
	}
}

func (h *deviceHolder) onUpdate(c, value string) {
	log.Printf("onUpdate(%s, %s) on device %s\n", c, value, h.device.Topic)
	if ch, ok := h.characteristics[c]; ok {
		log.Print("Found characteristic: ", c)
		ch.UpdateValue(value)
	}
}

func (h *deviceHolder) deviceUpdate(d *device.Device) {
	h.device = d
	util.SetReachability(h.accessory, d.Reachable)

	// TODO
	// h.updateAccessory()
}<commit_msg>:ambulance: :pencil: Resubscribe on device update<commit_after>package homekit

import (
	"fmt"
	"github.com/brutella/hc/accessory"
	"github.com/brutella/hc/characteristic"
	"github.com/brutella/hc/service"
	"github.com/gosexy/to"
	"github.com/hemtjanst/hemtjanst/device"
	"github.com/hemtjanst/hemtjanst/homekit/util"
	"github.com/hemtjanst/hemtjanst/messaging"
	"log"
	"net"
	"reflect"
)

type deviceHolder struct {
	device          *device.Device
	accessory       *accessory.Accessory
	mainService     *service.Service
	characteristics map[string]*characteristic.Characteristic
}

func newDeviceHolder(d *device.Device) (*deviceHolder, error) {
	newDev := &deviceHolder{
		device:          d,
		accessory:       nil,
		mainService:     nil,
		characteristics: map[string]*characteristic.Characteristic{},
	}
	err := newDev.createAccessory()
	if err != nil {
		return nil, err
	}
	return newDev, nil
}

func (h *deviceHolder) onHomekitUpdate(c string, value interface{}) {
	log.Printf("onHomeKitUpdate(%s, %v) on device %s\n", c, value, h.device.Topic)
	log.Print(h.device)
	if feature, ok := h.device.Features[c]; ok {
		log.Print(feature)
		var out string

		if converted, err := to.Convert(value, reflect.TypeOf(out).Kind()); err == nil {
			out = converted.(string)
		}
		if b, ok := value.(bool); ok {
			if b {
				out = "1"
			} else {
				out = "0"
			}
		}

		if out != "" {
			feature.Set(out)
		}
		return
	}
}

func (h *deviceHolder) onUpdate(c, value string) {
	log.Printf("onUpdate(%s, %s) on device %s\n", c, value, h.device.Topic)
	if ch, ok := h.characteristics[c]; ok {
		log.Print("Found characteristic: ", c)
		ch.UpdateValue(value)
	}
}

func (h *deviceHolder) deviceUpdate(d *device.Device) {
	h.device = d
	util.SetReachability(h.accessory, d.Reachable)

	for name, feature := range h.device.Features {
		chName := name
		feature.OnUpdate(func(msg messaging.Message) {
			h.onUpdate(chName, string(msg.Payload()))
		})
	}
}
{'fraction_non_alphanumeric': 0.15056360708534622, 'fraction_numerical': 0.0010735373054213634, 'mean_word_length': 3.711757269279393, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7426860', 'n_tokens_mistral': 1468, 'n_tokens_neox': 1391, 'n_words': 284}