/***********************************************************
 * Copyright (c) Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License
 **********************************************************/ import { IpcMainInvokeEvent } from 'electron' ; import { Client } from 'azure-iothub' ; import { Message as CloudToDeviceMessage } from 'azure-iot-common' ; import { SendMessageToDeviceParameters , MessageProperty } from '../interfaces/deviceInterface' ; export const onSendMessageToDevice = async ( event , params ) : Promise < void > => { const { deviceId , messageProperties , messageBody , connectionString } = params ; const hubClient = Client . fromConnectionString ( connectionString ) ; try { const message = new CloudToDeviceMessage ( messageBody ) ; addPropertiesToCloudToDeviceMessage ( message , messageProperties || [ ] ) ; await hubClient . open ( ) ; await hubClient . send ( deviceId , message ) ; } finally { await hubClient . close ( ) ; } } ; export const addPropertiesToCloudToDeviceMessage = ( message , properties ) => { if ( ! properties || properties . length === 0 ) { return ; } for ( const property of properties ) { if ( property . isSystemProperty ) { switch ( property . key ) { case 'ack' : message . ack = property . value ; break ; case 'contentType' : message . contentType = property . value as any ; break ; case 'correlationId' : message . correlationId = property . value ; break ; case 'contentEncoding' : message . contentEncoding = property . value as any ; break ; case 'expiryTimeUtc' : message . expiryTimeUtc = parseInt ( property . value ) ; break ; case 'messageId' : message . messageId = property . value ; break ; case 'lockToken' : message . lockToken = property . value ; break ; default : message . properties . add ( property . key , property . value ) ; break ; } } else { message . properties . add ( property . key , property . value ) ; } } } ;